Sunday, 7 February 2016

What is synchronization in selenium webdriver

synchronization in selenium webdriver:
==============================
Synchronization is the process of making our application and tool wait each other to get appropriate results is called synchronization.

When our automation execution starts, then there should be fair communication between tool and application.
What i mean here is if the tool is too fast of execution and the application/objects are not fully loaded/not ready by that time, then our automation test cases will fail. So the tool should wait(appropriate) till the objects are present/ready in the application, so that communication/synchronization happens between the tool and application so the chances of our test cases will pass.


Synchronization can be done in three ways.
================================
1.Implicit Wait
2.Explicit wait
3.Thread.sleep()

Implicit Wait:
============
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
It is a mechanism which will be written once and applied for entire session automatically. It should be applied immediately once we initiate the Webdriver.
Implicit wait will not work all the commands/statements in the application. It will work only for "FindElement" and "FindElements" statements.

Syntax: 
 ========
driver.manage.TimeOuts.implicitwait(6,Timeunit.SECONDS);
 
 
example:
==========
                WebDriver driver = new FirefoxDriver();
 
                driver.manage().window().maximize();
  
  driver.manage().deleteAllCookies();
  
  driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
  
  driver.get("https://www.gmail.com"); 
 
 
Thread.sleep():
================== 
Thread.sleep waits the specified time irrespective of the object state.
Ex: Thread.sleep(30000);
Here
 the execution is halted for 30 Sec., even if the object you are looking
 exists in 10 sec. So here tool unnecessarily waits for 20 sec.
Execution wont wait after 30 sec.s even if the object does not available, so the chances of your Test fails.
  
 
 

Explict Wait:
===============
If we want to handle ajax kind of calls we can use explicit wait mechanism.

There are few methods supported in ExpectedConditions class to support synchronisation.
Here is the example:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement o_element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Id")));

Here
 the tool waits a maximum time of 30 Sec., if the object you are looking
 is displayed in 10 sec. then the execution proceeds with the next step 
afte 10 secs. rather than waiting for 30 secs.
 
Explicit wait is mostly used when we need to Wait for a specific content/attribute change after performing any action, like when application gives AJAX call to system and get dynamic data and render on UI.

example:
========
/*Explicit wait for state dropdown field*/
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("statedropdown")));
 
 
 
  
 
 

No comments:

Post a Comment