Waits in Selenium | ImplicitWait | ExplicitWait | FluentWait | Sleep

Waits in selenium :

In UI automation, waits are required because few elements get loaded on the page asynchronously,  so after triggering an event a page may get loaded successfully but some of its element may still not get loaded.

Sometime Due to network issue an webpage elements load after sometime or with a delay.

There is 3 different way to handle this using waits in Selenium:

  • Implicit Waits
  • Explicit Waits
  • Fluent waits

 

Implicit Waits

Implicit wait is set for the entire duration of your webdriver and is set at the start of your program.

What it does is, in case while executing, if your webdriver doesn’t finds any element then instead of throwing an exception, implicit wait makes your driver to wait for the specified wait time and then try to find the element once again.

If the driver still does not finds the element then it throws exception. Implicit wait does the same for all the elements in your program, so you just have to set it once.

WebDriver driver = new FirefoxDriver();
// you can set the time you wish and the units
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 

Explicit Wait :

Explicit wait works similar to Implicit Wait, but unlike Implicit wait, it works only on the particular element, on which it is set, rather than on all elements in your code.

Also explicit wait tells the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an exception.

// Create object for WebDriverWait class
WebDriverWait wait=new WebDriverWait(driver, 30 /*30 seconds*/);
// Wait till the element is not visible
WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated("Locator of element"));

 

Fluent Wait:

When using the FluentWait instance, we can specify:

  • The frequency with which FluentWait has to check the conditions defined.
  • Ignore specific types of exception waiting such as NoSuchElementExceptions while searching for an element on the page.
  • The maximum amount of time to wait for a condition
  • This is parent of WebdriverWait in selenium
// wait for 45 secs max for an element to be present on the page, checking
// for its presence once every 3 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(45, SECONDS)
    .pollingEvery(3, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement ele = wait.until(new Function<WebDriver, WebElement>() 
{
	public WebElement apply(WebDriver driver) {
	return driver.findElement(By.id("some id"));
}
});

ExpectedConditions:

attributeContains(By locator, java.lang.String attribute, java.lang.String value)
An expectation for checking WebElement with given locator has attribute which contains specific value

attributeContains(WebElement element, java.lang.String attribute, java.lang.String value)
An expectation for checking WebElement with given locator has attribute which contains specific value

attributeToBe(By locator, java.lang.String attribute, java.lang.String value)
An expectation for checking WebElement with given locator has attribute with a specific value

attributeToBe(WebElement element, java.lang.String attribute, java.lang.String value)
An expectation for checking given WebElement has attribute with a specific value

attributeToBeNotEmpty(WebElement element, java.lang.String attribute)
An expectation for checking WebElement any non empty value for given attribute

elementSelectionStateToBe(By locator, boolean selected)
elementSelectionStateToBe(WebElement element, boolean selected)
An expectation for checking if the given element is selected.

elementToBeClickable(By locator)
An expectation for checking an element is visible and enabled such that you can click it.

elementToBeClickable(WebElement element)
An expectation for checking an element is visible and enabled such that you can click it.

elementToBeSelected(By locator)
elementToBeSelected(WebElement element)
An expectation for checking if the given element is selected.

frameToBeAvailableAndSwitchToIt(By locator)
An expectation for checking whether the given frame is available to switch to.

frameToBeAvailableAndSwitchToIt(int frameLocator)
An expectation for checking whether the given frame is available to switch to.

frameToBeAvailableAndSwitchToIt(java.lang.String frameLocator)
An expectation for checking whether the given frame is available to switch to.

frameToBeAvailableAndSwitchToIt(WebElement frameLocator)
An expectation for checking whether the given frame is available to switch to.

invisibilityOf(WebElement element)
An expectation for checking the element to be invisible

invisibilityOfAllElements(java.util.List<WebElement> elements)
An expectation for checking all elements from given list to be invisible

 

invisibilityOfAllElements(WebElement… elements)
An expectation for checking all elements from given list to be invisible

invisibilityOfElementLocated(By locator)
An expectation for checking that an element is either invisible or not present on the DOM.

invisibilityOfElementWithText(By locator, java.lang.String text)
An expectation for checking that an element with text is either invisible or not present on the DOM.

javaScriptThrowsNoExceptions(java.lang.String javaScript)
An expectation to check if js executable.

 jsReturnsValue(java.lang.String javaScript)
An expectation for String value from javascript

not(ExpectedCondition<?> condition)
An expectation with the logical opposite condition of the given condition.

numberOfElementsToBe(By locator, java.lang.Integer number)
An expectation for checking number of WebElements with given locator

numberOfElementsToBeLessThan(By locator, java.lang.Integer number)
An expectation for checking number of WebElements with given locator being less than defined number

numberOfElementsToBeMoreThan(By locator, java.lang.Integer number)
An expectation for checking number of WebElements with given locator

or(ExpectedCondition<?>… conditions)
An expectation with the logical or condition of the given list of conditions.

presenceOfAllElementsLocatedBy(By locator)
An expectation for checking that there is at least one element present on a web page.

presenceOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page.

presenceOfNestedElementLocatedBy(By locator, By childLocator)
An expectation for checking child WebElement as a part of parent element to present

presenceOfNestedElementLocatedBy(WebElement element, By childLocator)
An expectation for checking child WebElement as a part of parent element to be present

presenceOfNestedElementsLocatedBy(By parent, By childLocator)
An expectation for checking child WebElement as a part of parent element to present

refreshed(ExpectedCondition<T> condition)
Wrapper for a condition, which allows for elements to update by redrawing.

stalenessOf(WebElement element)
Wait until an element is no longer attached to the DOM.

textMatches(By locator, java.util.regex.Pattern pattern)
An expectation for checking WebElement with given locator has text with a value as a part of it

textToBe(By locator, java.lang.String value)
An expectation for checking WebElement with given locator has specific text

textToBePresentInElement(WebElement element, java.lang.String text)
An expectation for checking if the given text is present in the specified element.

textToBePresentInElementLocated(By locator, java.lang.String text)
An expectation for checking if the given text is present in the element that matches the given locator.

textToBePresentInElementValue(By locator, java.lang.String text)
An expectation for checking if the given text is present in the specified elements value attribute.

textToBePresentInElementValue(WebElement element, java.lang.String text)
An expectation for checking if the given text is present in the specified elements value attribute.

titleContains(java.lang.String title)
An expectation for checking that the title contains a case-sensitive substring

titleIs(java.lang.String title)
An expectation for checking the title of a page.

urlContains(java.lang.String fraction)
An expectation for the URL of the current page to contain specific text.

urlMatches(java.lang.String regex)
Expectation for the URL to match a specific regular expression

urlToBe(java.lang.String url)
An expectation for the URL of the current page to be a specific url.

visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible.

visibilityOfAllElements(java.util.List<WebElement> elements)
An expectation for checking that all elements present on the web page that match the locator are visible.

visibilityOfAllElements(WebElement… elements)
An expectation for checking that all elements present on the web page that match the locator are visible.

visibilityOfAllElementsLocatedBy(By locator)
An expectation for checking that all elements present on the web page that match the locator are visible.

visibilityOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page and visible.

visibilityOfNestedElementsLocatedBy(By parent, By childLocator)
An expectation for checking child WebElement as a part of parent element to be visible

visibilityOfNestedElementsLocatedBy(WebElement element, By childLocator)
An expectation for checking child WebElement as a part of parent element to be visible

 

Leave a Reply

Your email address will not be published. Required fields are marked *