Sunday 23 June 2019

Selenium waits - FluentWait, PageLoadTimeOut, ImplicitWait and ExplicitWait,

Fluent Wait: Fluent Wait. The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.

Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(1, TimeUnit.SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement content = fluentWait.until(new Function<WebDriver, WebElement>() {
   public WebElement apply(WebDriver driver) {
    return driver.findElement(By.xpath("//h4[text()='Hello World!']"));
   }
});

pageLoadTimeout: if any page is taking more than x amount of time, then we can stop executing that script by seetting up pageLoadTimeout
Throws -  TimeoutException.

driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.MILLISECONDS);

Implicit Wait: If we setup implicit wait, wait for x amount of time for elements avaialbility
Throws : NoSuchElementException

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Explicit Wait: This wait is applicable for specific element based on condition provided
WebDriverWait explicitWait = new WebDriverWait(driver, 10);
explicitWait.until(ExpectedConditions.visibilityOf(content));