Articles

AutomationSeleniumTools

Synchronisation in Selenium

When two or more components work together to perform any action at the same pace, the process is called synchronisation. In Selenium, there should be synchronisation between the Selenium script, web app, and the script execution speed.

When two or more components work together to perform any action at the same pace, the process is called synchronisation. In Selenium, there should be synchronisation between the Selenium script, web app, and the script execution speed.

You might have noticed that sometimes pages load successfully without any time delay, while in other cases it takes a few seconds to load. I.e., the web elements on a webpage are taking time to load.

How do you resolve such an issue? This is where “Waits” enter the picture from the perspective of the Selenium automated testing suite.

Selenium provides synchronisation in the form of Implicit Wait and Explicit Wait. These synchronisation points are inserted in the script using WebDriverWait class.

Implicit Wait

Implicit Wait is a global wait. Implicit Wait time is applied to all the web elements in a test script with which the driver is interacting.

Implicit Wait is given immediately after driver is initialised. It tells driver to poll the DOM for a certain amount of time when trying to find an element if the element is not immediately available.

The syntax for implicit wait is simply and just one line:

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

In this example, ten seconds is the maximum time the driver will wait for an element to be found. If an element is not located on the web page within that time frame, it will throw an exception.

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

The subsequent step in your script will only execute when ten seconds have elapsed.

Implicit wait is useful when certain elements on the webpage are not immediately available and need some time to load. The default setting for implicit wait is zero meaning disabled. Once set, it will be globally applicable to the script.

Code snippet:

public static void main(String[] args) {
// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver",
"E://Selenium Tutorial//chromedriver.exe")
WebDriver driver = new ChromeDriver();

driver.get("https://www.fliplkart.com");
driver.manage().timeouts()
.implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.name("Signup")).click();
//driver.findElement(By.
}

Explicit Wait

Explicit Wait is applied to a specific element on a web page. It allows your code to halt program execution until the condition is resolved or the expected condition is met.

Once you declare an Explicit Wait, “ExpectedConditions” needs to be used on the element to be located. When elements take a long time to load and you want to verify the property of the element, there is (VisitbilityOfElementLocated, elementToBeClickable, elementToBeSelected)

Syntax for Explicit Wait:

New WebDriverWait(driver,timeout)
.until(ExpectedConditions
.elementToBeClickable(element));

Code snippet:

// By default it will accepts in Seconds
WebDriverWait wait = new WebDriverWait(driver, 20);
// Here we will wait until element is not visible, if element is visible then it will return web element
// or else it will throw exception
WebElement element = wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath("xpathvalue")));

Note that Thread.sleep() always pauses the current thread execution. It interacts with the thread scheduler to put the current thread in Wait state for a specified period of time.

Once the wait time is over, the thread state is changed to runnable state and waits for the CPU for further execution. Thread.sleep() is a static wait and it is not recommended for use it in our automation script.

This is just an introduction to the differences between these Waits. When you feel confident in using these, there are newer and smarter Fluent Waits commands that you can go on to use.

AUTHOR:

Mukta Sharma

Consultant

Get the Most Out of Your Tools

No matter where you are on your journey, Planit can help you make the appropriate choices to ultimately deliver your applications faster, with better quality, and at lower cost. With expertise across the spectrum of tools, our consultants can be leveraged from ideation to operations and everywhere in between.

Find out how our certified experts across the spectrum of testing toolsets can help you find the right tools for your needs, get the most out of your tools, and achieve a CI/CD toolchain.

Get Updates

Get the latest articles, reports, and job alerts.

Security Center

We use cookies to optimise our site and deliver the best experience. By continuing to use this site, you agree to our use of cookies. Please read our Cookie Policy  for more information or to update your cookie settings.