IllegalArgumentException - 具有FindBy注释的变量无法设置为代理

时间:2017-09-30 14:28:40

标签: java selenium cucumber

尝试使用Gradle,Java和WebDriver实现Cucumber自动测试,我遇到了一些我以前从未见过的问题。当我尝试在代码

中创建一个类的实例时
BoardGameGeekHomePage currentPage = new BoardGameGeekHomePage(driver);

我得到一个例外:

java.lang.IllegalArgumentException: Can not set java.util.ArrayList field pages.BoardGameGeekHomePage.usersOfMainPage to com.sun.proxy.$Proxy15

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:117)
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)
at pages.AbstractPage.<init>(AbstractPage.java:13)
at pages.BoardGameGeekHomePage.<init>(BoardGameGeekHomePage.java:15)
at steps.CucumberSteps.i_open_the_game_collection_of_a_user(CucumberSteps.java:25)
at ✽.When I open the game collection of a user(Cucumber.feature:5)

以下是BoardGameGeekHomePage类的代码:

    package pages;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;

    import java.util.ArrayList;

    import static utils.DriverFactory.driver;

    public class BoardGameGeekHomePage extends AbstractPage{

    public BoardGameGeekHomePage(WebDriver driver) {
        super(driver);
    }

    @FindBy(xpath = "//td/span/a[contains(@ng-href, '/user/')]")
    private ArrayList<WebElement> usersOfMainPage;

    public UserPage selectUserByIndex(int i) {
        usersOfMainPage.get(i).click();
        return PageFactory.initElements(driver, UserPage.class);
    }

    public BoardGameGeekHomePage goTo(String url) {
        driver.get(url);
        return PageFactory.initElements(driver, BoardGameGeekHomePage.class);
    }
}

和AbstractPage类:

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import static utils.DriverFactory.driver;

public class AbstractPage {
//    protected WebDriver driver;
    public AbstractPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
}

如果我在BoardGameGeekHomePage类中注释@FindBy注释,那么代码将被正确执行(当然我会得到NullPointerException,因为ArrayList将为空)。 希望听到一些想法。

0 个答案:

没有答案