如何在selenium webdriver中的pageobject字段中使用显式等待?

时间:2011-09-14 11:50:46

标签: webdriver

如何在pageobject字段中使用显式等待? 我有一个pageobject类,我在其中声明字段并使用FindBy标记来实例化它们。如何为

中声明的某些或所有字段添加显式等待

2 个答案:

答案 0 :(得分:0)

我的解决方案是不使用@FindBy。

在您的页面对象中:

   By someElementLocator = By.cssSelector(".locator");

   public void waitForElementPresent(final By locator, int timeout) {
    ExpectedCondition e = new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {

            return driver.findElements(locator).size() > 0;
        }
    };

    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(e);

}

  public WebElement getSomeElement() {
    waitForElementPresent(someElementLocator);
    return driver.findElement(locator);
  }

也许这是一个架构问题。我似乎无法找到确认@FindBy支持等待的任何资源,所以它的用法可能取决于测试设计/架构。

答案 1 :(得分:0)

我在这里完全改变我的答案。可能的:

@FindBy(css="#loginBtn")
WebElement submitLoginBtn
WebElement submitLoginBtn(){
    WebDriverWait wait = new WebDriverWait(driver,3)
    wait.until(ExpectedConditions.elementToBeClickable(submitLoginBtn))
    return  submitLoginBtn
}

现在只需调用submitLoginBtn()方法:

@Test
void login(){
    LoginPage login = new LoginPage(driver)
    login.userName().sendKeys("0000")
    login.pin().sendKeys("0000")
    login.submitLoginBtn().click()
}