相对Xpath在Maven TestNg中不起作用,但绝对路径有效

时间:2019-05-16 19:58:08

标签: java maven selenium selenium-webdriver automated-tests

在安装/使用插件之前,所有绝对路径和相对路径都可以在testNG中使用。但是使用之后,Relative Xpath无法正常工作。 即使是我自己的Relative Xpath尝试,也没有用。 我将Eclipse IDE用于JAVA Selenium 3.141.59。在这里,我分别附上了我的两个代码 src / main / java src / test / java

phpTravelsLoginPage.java

package sqa;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class phpTravelsLoginPage {
    WebDriver driver;

/* Here This Below XPath works  */ 
    By MyAccount = By.xpath("/html/body/nav/div/div[2]/ul[2]/ul/li[1]/a");
    By loginBtn = By.xpath("/html/body/nav/div/div[2]/ul[2]/ul/li[1]/ul/li[1]/a");

/* But Here This Below XPath works  */ 
//  By MyAccount = By.xpath("//*[@id=\"li_myaccount\"]/a");
//  By loginBtn = By.xpath("//*[@id=\"li_myaccount\"]/ul/li[]/a");


    public phpTravelsLoginPage(WebDriver driver)
    {
        this.driver = driver;
    }

    // CLick on MyAccount Dropdown
    public void clickOnMyAccount()
    {
        driver.findElement(MyAccount).click();
    }

    // CLick on Login Dropdown button
    public void clickOnLoginButton()
    {
        driver.findElement(loginBtn).click();
    }


    /* This loginToPhptravels method will be exposed to the Test Case  */ 

    public void loginToPhptravels() throws InterruptedException

    {
        // Click on the MyAccount Dropdown
        this.clickOnMyAccount();

        // Click on the Login dropdown menu
        this.clickOnLoginButton();
    }
}

loginTest.java

package sqa;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

import sqa.phpTravelsLoginPage;

public class loginTest {
    WebDriver driver;
    phpTravelsLoginPage php;
  @Test
  public void f() throws InterruptedException {
      driver.get("https://phptravels.net");
      php = new phpTravelsLoginPage(driver);
      php.loginToPhptravels();
  }
  @BeforeTest
  public void beforeTest() {
      System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
      System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

      driver = new FirefoxDriver();
  }

  @AfterTest
  public void afterTest() {
  }
}

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>sqa</groupId>
  <artifactId>sqa</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
  </dependencies>

</project>

注意:TestNG是通过“安装新软件”安装的,而不是通过maven安装的。

Project Maven

1 个答案:

答案 0 :(得分:1)

具有绝对xpath的测试用例在执行中不太一致,建议不要使用绝对Xpath,这与安装maven无关。在搜索了下面的xpaths的唯一匹配项后,希望它们对您有用!!!

By myaccount = By.xpath(".//a[@href='https://www.phptravels.net/']/ancestor::div[2]//a[text()=' My Account ']");
By Login = By.xpath(".//a[text()=' My Account ' and @aria-expanded]/following-sibling::ul//a[text()=' Login']");