Synchronization or Waits in Selenium WebDriver

Synchronization is the key when it is about automating a web based application using any tool.

When we perform a set of action on a web page, many processes are invoked in the back-end systems. So, the response time of the application might not be the same every time.

Let us assume the web server or the application server or the database server is slow a bit. And because of that the response time is increased by a few seconds.

Because of the delay in response time, the Selenium will not be able to find the required elements on time, and it will result in failure of the test script.

So, we need to inculcate a mechanism in our automation script that deals with such type of delays, this is nothing but Synchronization between our tool and the application under test.

Synchronization/ Waits can be achieved in many different ways :

  1. Sleep method of Thread class
  2. Page Load timeout
  3. Script timeout
  4. Implicit Wait
  5. Explicit Wait

 

sleep() command :

The command halts the execution of the script for specified amount of milliseconds.

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement homeLink = driver.findElement(By.linkText(“Home”));

homeLink.click();

try{

           Thread.sleep(5000);

} catch(Exception e){

            System.out.print(e);

}

This method is discouraged as even if the page/ web element is loaded, it instructs the browser to wait unnecessarily for the fixed amount of time.


Page Load timeout :

We can set the amount of time to wait for a page load to complete before throwing an error.

Example :

driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

Once added in the script, the WebDriver instance waits for 20 seconds for every page to get loaded before throwing an exception. If the page is not loaded in 20 seconds of time, then it throws TimedOutException at run time.


Script timeout :

We can set the amount of time to wait for an asynchronous script to finish execution before throwing any error.

Example :

driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);

Once added in the script, the WebDriver instance waits for 20 seconds for every asynchronous script to get executed on the web page before throwing an exception.


 Implicit Wait :

Implicit wait is a mechanism, which is used to notify WebDriver instance to wait for specific time, if any element is readily not available on the webpage.

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

The above line once added at the beginning of the automation script, the WebDriver will wait for 10 SECONDS for any element on the web page to be loaded before throwing any exception.

Once set, the timing will be set for the life of the WebDriver instance until and unless it is changed again. Meaning, using implicit wait command, we can say the WebDriver instance to wait for specified amount time before throwing NoSuchElementException, if it does not find any element on the web page immediately.

The synchronization mechanism is not associated with any particular web element, so it will be applicable for all the elements that our script interacts with, it is not highly appreciated.

Explicit Wait :

Explicit wait is a technique used to advise the WebDriver instance to stand by the execution till the time the certain condition is met or the maximum wait time is elapsed.

WebElement element = driver.findElement(By.linkText(“Home”));
WebDriverWait wait = new WebDriverWait(driver, 20);

wait.until(ExpectedConditions.visibilityOf(element));

WebDriverWait wait = new WebDriverWait(driver, 20);

The above line sets maximum time the WebDriver instance will wait for the condition to become true.

wait.until(ExpectedConditions.visibilityOf(element));

The above line asks WebDriver instance to wait until the element is visible on the web page.

Explicit wait conditions supported by WebDriver API : 

We can make the WebDriver instance wait until –

  • an element is visible on the web page
  • an alert is visible on the web page
  • a frame is displayed on the web page
  • multiple elements are visible on the web page
  • and many more…..

ExpectedConditions class provides below conditions for which we can ask WebDriver to wait for :

ExplicitWait1

ExplicitWait2

ExplicitWait3

Let us have a look at the simplest usage of Explicit wait.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWaitDemo {

             public static void main(String[] args) {

                          WebDriver driver = new FirefoxDriver();
                          driver.get(“https://seleniumatfingertips.wordpress.com/”);
                          driver.manage().window().maximize();

                          WebElement element = driver.findElement(By.linkText(“Home”));

                          // waits until Home link is visible on the web page

                          WebDriverWait wait = new WebDriverWait(driver, 20);
                          wait.until(ExpectedConditions.visibilityOf(element));

                         element.click();

                         // waits until About link is visible
                        wait.until(ExpectedConditions.visibilityOfElementLocated

                                           (By.linkText(“About”)));

                         driver.quit();
            }
}

If our script and application under test (AUT) demands a custom wait condition, which is not defined by ExpectedConditions class, then we can define it by overriding apply() method.

Visit the link to  know more about custom wait conditions.

Hope it helps you!!

Cheers 🙂

Advertisements

#explicitwait, #implicitwait, #pageloadtimeout, #setscripttimeout, #synchronizationinseleniumwebdriver, #waitinseleniumwebdriver