【问题标题】:Relative Xpath Not Work In Maven TestNg But Absolute Path Works相对 Xpath 在 Maven TestNg 中不起作用,但绝对路径有效
【发布时间】:2019-05-16 19:58:07
【问题描述】:

在安装/使用 Maven 插件之前,所有绝对和相对路径都可以在 testNG 中使用。但是使用 maven 后,Relative Xpath 不起作用。 即使我尝试了我自己的相对 Xpath 但没有奏效。 我使用 Eclipse IDE for JAVA,Selenium 3.141.59。在这里,我分别附上了我的 src/main/javasrc/test/java 的 2 个代码。

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。

【问题讨论】:

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


    【解决方案1】:

    具有绝对xpath的测试用例在执行上不是很一致,建议不要使用绝对xpath,与安装maven无关。在搜索了下面的工作和 xpath 的独特匹配之后,希望这些对你有用!!!

    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']");

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2018-06-04
      • 2012-05-20
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 2013-07-06
      • 2012-07-27
      相关资源
      最近更新 更多