如何在Wait.until()中使用selenium 2 PageFactory init元素?

Bil*_*ill 14 selenium webdriver

下面的代码片段工作正常,但我在使用该wait.until()行时有点麻烦:

wait.until(new ElementPresent(By.xpath("//a[@title='Go to Google Home']")));
Run Code Online (Sandbox Code Playgroud)

它有效,但我想发送我的PageFactory WebElement homePageLink代替:

wait.until(new ElementPresent(homePageLink));
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

这些新奇的Selenium 2功能让我的头脑有点旋转,我找不到太多的文档.

谢谢.

public class GoogleResultsPage extends TestBase {

    @FindBy(xpath = "//a[@title='Go to Google Home']")
    @CacheLookup
    private WebElement homePageLink;

    public GoogleResultsPage() {  
        wait.until(new ElementPresent(By.xpath("//a[@title='Go to Google Home']")));
        assertThat(driver.getTitle(), containsString("Google Search"));
    }  
}

public class ElementPresent implements ExpectedCondition<WebElement> {

    private final By locator;

    public ElementPresent(By locator) {
        this.locator = locator;
    }

    public WebElement apply(WebDriver driver) {
        return driver.findElement(locator);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*rov 21

我将PageFactoryAjaxElementLocatorFactory 一起使用- PageFactory是您正在使用的Selenium 2 Page Objects模式的支持类,而AjaxElementLocatorFactory是元素定位器的工厂.在您的情况下,构造函数将如下所示:

public GoogleResultsPage() { 
    PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
}
Run Code Online (Sandbox Code Playgroud)

此代码将等待最多15秒,直到注释指定的元素出现在页面上,在您的情况下,将由xpath定位homePageLink.您不需要使用ElementPresent类.