尝试在Java中使用硒中的PageFactory时出现异常

时间:2018-12-15 02:11:19

标签: java selenium selenium-webdriver selenium-chromedriver

我正在尝试自动化简单的Web应用程序,为此,我正在使用带有Selenium和Java的Page Object Model Pattern, 我的Maven Pom依赖项如下所示:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.12.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.14.0</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.13.5</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-ie-driver</artifactId>
        <version>3.0.0-beta4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.12.0</version>
    </dependency>

</dependencies>

我的页面对象类如下:

public class Page {
    public Page(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
    @FindBy(linkText = "Players")
    private WebElement playersLink;

    public boolean isPlayersLinkVisible(){
        return playersLink.isDisplayed();
    }
}

我的测试如下:

public class Test{
  @Test
  public void test(){
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(URL);
        Page page = new Page(driver);
        System.out.println(page.isPlayersLinkVisible());
  }
}

但是我遇到了这个异常,无法弄清楚为什么?

java.lang.NoClassDefFoundError: org/openqa/selenium/WrapsElement

at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.proxyForLocator(DefaultFieldDecorator.java:101)
at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:62)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:113)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:92)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:79)

2 个答案:

答案 0 :(得分:0)

我发现,显然存在不兼容的依赖关系,我删除了不必要的依赖关系,只留下了我需要的依赖关系:

<dependencies>

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

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.13.5</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

</dependencies>

答案 1 :(得分:0)

我遇到了这个问题,因为硒支持版本与我使用的其他硒库不同。当所有硒库(包括pom.xml中的硒支持库)使用相同版本时,此问题已解决。