Explicit wait - Loop a condition

601 views
Skip to first unread message

phanindra chitta

unread,
Jun 22, 2016, 7:52:38 AM6/22/16
to webdriver
Hi All,

I have a scenario where I need to wait for "Accept" button to load. But the button will appear when all the records were loaded successfully, meanwhile I have to click on "Refresh" button. So the click of "Refresh" button is not defined. I need to click, until I found the "Accept" button.

Can you tell me how to resolve this scenario. I tried explicit wait, but not sure which expected condition, I need to select.

I have captured both the refresh button and accept button.

Thanks in advance.

darrell grainger

unread,
Jun 23, 2016, 7:34:23 AM6/23/16
to webdriver
It is not clear what you mean by "Refresh button". If there is a button on the page which refreshes the page that is VERY different from the browser refreshing the page. Anything outside the web page, i.e. part of the web browser, cannot be 'clicked'. Regardless, what I would do is:

  • By acceptButtonLocator = "locator for the Accept button"
  • do {
    • // refresh page
    • Thread.sleep(200);
    • int found = driver.findElements(acceptButtonLocator).size();
  • } while(found == 0);
This will get a List<WebElement> of elements which match the Accept button. If the list is empty then the Accept button was not found. This does exactly what I would do if manually testing the page.

Andy Doan

unread,
Jun 23, 2016, 7:29:48 PM6/23/16
to webdriver
Assuming you are using java, you can create your own expected condition.

Assuming the refresh button is an actual button on your website, this expected condition will loop until true is returned or the timeout is reached. Clicking one thing, and waiting for another thing to appear.

private static ExpectedCondition<Boolean> clickUntilFound(final By locatorToClick, final By locatorToFind) {
  return new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver driver) {
          driver.findElement(locatorToClick).click();
          Thread.sleep(xxx); //some small delay here to prevent rapid refresh DOS'ing your site
          if (driver.findElements(locatorToFind).size() > 0)
              return driver.findElement(locatorToFind).isDisplayed();
          else
              return false;
      }
      public String toString() {
          return String.format("element found by %s after clicking %s", locatorToFind, locatorToClick);
      }
  };
}


Usage:
WebDriverWait wait = new WebDriverWait(driver, waitTimeout);
wait.until(clickUntilFound(refreshButton, acceptButton));


And if you wanted to do a browser "Refresh", use driver().navigate().refresh();

private static ExpectedCondition<Boolean> refreshUntilFound(final By locatorToFind) {
   return new ExpectedCondition<Boolean>() {
       public Boolean apply(WebDriver driver) {
           driver().navigate().refresh();
           Tread.sleep(xxx) //some small delay to prevent DOS
           if (driver.findElements(locatorToFind).size() > 0)
               return driver.findElement(locatorToFind).isDisplayed();
           else
               return false;
       }
       public String toString() {
           return String.format("element found by %s after refresh", locatorToFind);
       }
   };
}

WebDriverWait wait = new WebDriverWait(driver, waitTimeout);
wait.until(refreshUntilFound(acceptButton));



phanindra chitta

unread,
Jul 14, 2016, 9:37:54 AM7/14/16
to webdriver
Thanks Andy. it worked out :)

phanindra chitta

unread,
Jul 14, 2016, 9:38:29 AM7/14/16
to webdriver
Thanks for the reply Darrell :)
Reply all
Reply to author
Forward
0 new messages