1

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 can i add a explicit wait for some of or all of those fields declared in the

jerald
  • 71
  • 1
  • 2
  • 5

2 Answers2

0

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.

0

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()
}
imp
  • 435
  • 6
  • 20
  • I do realise this doesnt answer the question to your liking, but look into synchronization with Selenium (explicit and implicit waits together). From what i'm understanding its meant to be a better approach – imp Oct 02 '19 at 18:56