Categories
Uncategorized

Various Synchronization techniques in Selenium

Why Synchronization?

It is required so that our AUT and testing tool are running in sync with each other.For example if our application is taking time to load but our automation tool commands are running fast,than this would result in error. To make sure selenium/UFT waits for the application controls to load, so that tool can take an action on them we need synchronization. In simple words using wait, we are telling tool engine to wait for a certain amount of time before going to the next line of code or step.It plays a significant role in dynamic websites using technologies like Angular JS and Ajax

How to achieve it?

Implicit wait

Support to work with wait commands is available under the package,
 org.openqa.selenium.support.ui (Thus we need to import this package in our code)

Note:- We can check the classes under the package from “Package Explorer” in Eclipse.

Path:-
Package Explorer – Referenced Libraries – Selenium-java-version no .Jar -org.openqa.selenium.support.ui (Expand this and click on the available classes to view the structure)

Open Package Explorer(Window – Show View)

org.openqa.selenium.support.ui
Package for Wait commands


Selenium WebDriver has borrowed the idea of implicit waits from Watir(web application testing in Ruby). It means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception (when it cannot find the element on the page). We should keep in mind, that
Implicit waits will be in place for the entire time the browser is open. This means selenium will wait maximum of implicit wait time before throwing an exception error, but incase that element exists before that maximum time also, still it will be waiting for that much time. 

Logic:-Selenium checks the control in first go, incase it is not able to find the control it will wait for the given implicit time, and then try again, after the end of the wait time to look for the control before throwing an exception. By default its value is 0 .

Limitation: – implicit wait will be applied to all the elements of the test case by default.

Syntax:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

It accepts 2 params, time to wait = 20 and TimeUnit which is seconds in our case.Thus here,It asks selenium to wait for 20 seconds for all the controls before throwing an error.

Code:-

 Implicit Wait Selenium
Applying Implicit Wait 
Timeunit for Implicit wait
Setting Time in Seconds for Implicit wait


Explicit Wait
We are explicitly making the execution of selenium code to wait, for a certain condition to occur before, proceeding further in the code.

Why Explicit Wait?
We know implicit wait applies to all the controls of a browser ,but lets take a example where in one control takes bit longer to load compared to others.Does this means we should increase the time for implicit wait to accommodate it ?

Answer is strict no, and we would only apply explicit wait on this(slower) control with an expected condition.

Logic:
It is applicable to the given control and waits to the maximum amount of time or move forward incase the given condition is met. 

Syntax:-
WebDriverWait.until(condition-that-finds-the-element(ExpectedCondition))

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully

Note:- we need to import the webdriverwait class and expectedCondition Packages
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriverWait in selenium
WebDriverWait Package
Expected condition can be in the form of like:-
button is enabled:elementToBeClickable
textbox is visible:visibilityOf
alertispresent.

ExpectedConditions in Selenium
ExpectedConditions in Selenium


Code:

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

public class SyncInSelenium {

public static void main(String[] args) {

// Creating the Driver object
WebDriver selenium ;
selenium = new FirefoxDriver();
//Using Implicit wait for 20 seconds,which is applicable to all the controls in application
//selenium.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Opening the application page
selenium.navigate().to("http://www.uftHelp.com");
//Using Explicit wait , Expected Condition is search box to be visible
WebDriverWait wait = new WebDriverWait(selenium, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gsc-i-id1")));
//Fill the value into the search
selenium.findElement(By.id("gsc-i-id1")).sendKeys("Explicit Wait");
//Destroying the Application instance
//selenium.quit();

}

}


Note:- We can achieve synchronization using following approach also

Fluent Wait

Fluent wait = Explicit Wait + frequency with which control need to be checked on application +  exceptions to be ignored like 
NoSuchElementExceptions.

It defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Package :- org.openqa.selenium.support.ui.FluentWait

It is mostly used, while working with AJAX applications as well as in scenarios when element load time fluctuating.

FluentWait Selenium
Fluentwait Class

PageLoadTimeout 
It is again an extension of Implicit wait but it used in page loading.It sets the amount of time Selenium to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

PageLoadTimeout method in selenium
PageLoadTimeout method


Syntax:-
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
waits for 20 seconds for page to load

Note: – we can achieve synchronization using Thread.Sleep, method of Java.However this is not recommended methodology. As it is same like asking selenium to stop executing for given X milliseconds so that our control is loaded and  it is not guaranteed that within that hard coded X value, control is fully loaded and our test code can execute safely.

Ex: Thread. Sleep (20000);

Here the execution is halted for 20 Sec., even if the object we  are looking exists at that instant, so it unnecessary waits of 20 sec.
or
Control exists  after 10 sec. So here also selenium unnecessarily waits for more 10 sec.


Synchronization in UFT


2 replies on “Various Synchronization techniques in Selenium”

The wait time for elements for loading works but I have seen they just don't work consistently well enough ..I am just thinking If the selenium jar files and engine sync lookup is necessary.. Presently most of the cases they work but not all the cases..I am seeing 1) stale elements issues 2) javascript errors 3) timeout errors all cascading due to the synchronization errors.. In short there could be a basic problem with the selenium and test ng root methods … The users are just masking the root engine errors with multiple handles. There needs to be a complete review for selenium sync problems..

Leave a Reply to Sunil Bhatta Cancel reply

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