开发者

How to use explicit wait in pageobject fields in selenium webdriver?

开发者 https://www.devze.com 2023-04-05 00:12 出处:网络
How to use explicit wait in pageobject fields? I have a pageobject class in which i declare fields and use the FindBy tag to instantiate them. How ca开发者_运维知识库n i add a explicit wait for some o

How to use explicit wait in pageobject fields? I have a pageobject class in which i declare fields and use the FindBy tag to instantiate them. How ca开发者_运维知识库n i add a explicit wait for some of or all of those fields declared in the


My solution is to not use @FindBy.

In your page object:

   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);
  }

Maybe it's an architectural issue. I can't seem to find any resources confirming that @FindBy support waits so maybe its usage depends on a test design/architecture.


I am completely changing my answer here. Its possible:

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

and now simply call the submitLoginBtn() method :

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号