Synchronization in Selenium Webdriver

When testing a software there are instances where two or more components will have to work concurrently and parallel with each other. When you automate the test, there are two components such as the software application that is to be tested and the test automation tool that is used for executing the test.

Both these components will have their own speed and the test scripts should be written facilitating both these components to work with the same speed. If they have not worked with the same speed the chances for “Element Not Found” errors are more. In such cases synchronization will help both the components to work with the same speed.

When you consider the synchronization in Selenium Webdriver, there are two different types of synchronizations.

Unconditional Synchronization

In unconditional synchronization, only timeout value will be specified. Selenium webdriver will wait till the specified time and then start executing the test automatically. Below given are the two examples.

wait(), time.sleep()

Since only timeout is mentioned, the tool will wait unnecessarily till the time is out even when the conditions are met. However, this type of synchronization will be helpful while interacting with third party system such as an interface. While interacting with third party system writing conditions or checking conditions is not possible. Here the tool can be made to wait for a specified period.

Conditional Synchronization

In Conditional Synchronization one or more conditions will be set along with a timeout limit. Here the tool will start working as soon as the condition or the conditions are met. When the condition or the conditions are not met, the tool will wait till the specified time and then proceed to execute the test.

There are two types of conditional statements is selenium webdriver. Explicit wait and implicit wait.

Implicit wait

Usually, implicit wait is being used when the element or the elements are not available readily. This wait will work only for commands such as “Find Element” and “Find Elements” and not for any other statements.

Syntax

driver.manage.TimeOuts.implicitwait(6,Timeunit.SECONDS);

Example for Implicit wait

WebDriver driver = new FirefoxDiriver();

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

driver.get(“http://www.google.com”);

Explicit wait

Here a condition will be specified for a wait statement along with a specified time limit. The condition should be met within the specified time limit. The testing will start proceeding when the condition is not met within the specified period of time.

Example for Explicit wait

/*Explicit wait for state dropdown field*/

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“statedropdown”)));

Fluent wait

This is used when a maximum amount of time to be allowed for a condition to met and also when the frequency of checking conditions is more.

Syntax

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

//Wait for the condition

.withTimeout(30, TimeUnit.SECONDS)

// which to check for the condition with interval of 5 seconds.

.pollingEvery(5, TimeUnit.SECONDS)

//Which will ignore the NoSuchElementException

.ignoring(NoSuchElementException.class);

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>