Synchronization in Selenium

Parth Kandpal
Nov 4 · 3 min read

In Automation Testing Synchronization is used to make application under test and the Test Automation Tool work in parallel. Sometimes webpage takes more time than the speed of webdriver and in such cases we face Exceptions such as TimeoutException, NoSuchElementFoundException etc.

Image Source: Internet

Synchronization in selenium in achieved by using waits. Basically there are two types of waits which are divided further with different kinds. In this blog we’ll look into all types and its kinds.

Static Wait

Static Wait is used to pause execution for a given time. In static wait we pause our script to synchronize with Web Browser. Our Script waits for given time even if synchronization is completed before given time. And that is the Disadvantage of using Static Wait.

In Java with Selenium we use Thread.sleep() method to achieve static wait.

Eg-:

Webdriver driver = new ChromeDriver();driver.get(“https://www.amazon.in”);Thread.sleep(10000); // it takes arguments in milliseconds 10000ms = 10 seconddriver.findElement(By.id(“twotabsearchtextbox”)).click();

In above example execution will be paused for 10 seconds even if search box is loaded before 10 seconds. Hence Static Wait in NOT recommended as it can slow our execution.

Dynamic Wait

Static Wait is used to wait for a given time although if our desired synchronization is done before given time we don’t need to wait anymore and that is the advantage of using Dynamic Wait. But if desired synchronization is not completed after time taken by Dynamic wait, Exceptions are thrown by WebDriver.

In Selenium we’ve 4 Dynamic waits which have their own use and we can use them for specific conditions.

  1. PageLoadTimeout
  2. ImplicitWait
  3. WebdriverWait (Explicit Wait)
  4. Fluent Wait

PageLoadTimeout

is used for Web browser page load synchronization. If page load takes more time than webdriver speed TimeOutException is thrown by Webdriver. To overcome this situation we use PageLoadTimeout.

Syntax-:

driver.manage().timeouts().pageLoadTimeout(<waittime>, TimeUnit.<timeunit>);

Eg-:

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

In above example if pageload is completed in 5 seconds, driver will not wait for another fifteen seconds. This is the advantage of Dynamic Wait.

ImplicitWait

is used to synchronize all the elements of a webpage. The implicit wait will tell the webdriver to poll the DOM for a given time. If element is found within the time period, webdriver will come out of wait whereas if element is NOT found within the time period, webdriver will throw NoSuchElementException.

Syntax-:

driver.manage().timeouts().implicitlyWait(<waitTime>, TimeUnit.<timeunit>);

Eg-:

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

In above example if elements in the page are loaded in 5 seconds, driver will not wait for another fifteen seconds.

WebDriverWait (Explicit Wait)

WebDriver Wait is used to achieve synchronization for a specific condition such as ElementIsClickable etc. If a specific element is taking more time to load we use WebDriver Wait. It polls DOM for the specific element with Dynamic wait.

Syntax-:

WebDriverWait wait = new WebDriverWait(<driverinstance>,<waitTimeInSeconds);

Eg-:

WebDriverWait wait = new WebDriverWait(driver,10);wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“results”)));

In above example if Webdriver will look for visibility of an element with id “results”. Once element with id “results” is visible on webpage wait will stop.

FluentWait

provides polling mechanism which is used for how frequently we want to poll for a specific element to appear or load. It tries to find the web element repeatedly at regular intervals(as specified in polling period) of time until the timeout or till the object is found. It can define the maximum amount of time to wait for a specific condition and frequency with which to check the condition before throwing an ElementNotVisibleException exception.

Syntax-:

Wait<WebDriver> wait = new FluentWait<WebDriver>(<driverinstance>).withTimeout(<waitTime>, TimeUnit.<timeunit>).pollingEvery(<waitTime>, TimeUnit.<timeunit>).ignoring(NoSuchElementException.class);

Eg -:

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

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

That is all about Synchronization and Waits in Selenium.

Cheers!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade