Wednesday 6 May 2015

Selenium WebDriver API Synchronization with web page



This is a technique (mechanism) which involves more than one components to work parallel (smoothly) with each other without interruption.
Commonly in Software Test Automation, It has two components
1. Application under Test
2. Test Automation Tool
Above components have their own speed? We should write an automation scripts in such a way that both the components should move with same and desired speed, so that we should not get "Element Not Found" exception which will consume time again in debugging.
Synchronization can be divided into two important categories:
1. Unconditional Synchronization
2. Conditional Synchronization
Unconditional Synchronization:
In this condition,  just specify the timeout value. It causes to wait until certain amount of time and then proceed further.
i.e.: Wait () and Thread.Sleep (“5000”);
Disadvantage for the statements are, there is a high chance of unnecessary waiting time even though the application is ready for execution.
Advantages are-In a situation where we connect for third party systems like interfaces, it isn’t possible to write a condition or check for a condition. Here in this situations, we have to use the application to wait for certain amount of time by specifying the timeout value.
Conditional Synchronization:
We specify a condition along with timeout value, so that tool waits to check for the condition and then come out if nothing happens.
It is very important to set the timeout value in conditional synchronization, because the tool should proceed further instead of making the tool to wait for a particular condition to satisfy.
In Selenium we have implicit Wait and Explicit Wait conditional statements.
1. Implicit Wait.
An implicit wait is to tell WebDriver API to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediate available.
The default setting is zero (0). Once when we define the implicit wait, it will set for the life of the WebDriver API object instance.
It is a mechanism which shall  be written once and applied for entire session automatically. It should be applied immediately once we initiate the WebDriver API.
Implicit wait will not work all the commands and statements in the application. It will workable only for "FindElement" and "FindElements" statements.
If we set implicit wait, find element will not throw an exception if the element is not found in first instance, instead it will poll for the element until the timeout and then proceeds further. We should always remember to add the below syntax immediately below the WebDriver API statement.
Syntax: driver.manage.TimeOuts.implicitwait(5,Timeunit.SECONDS);
Ie.:
WebDriver API driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("
https://silenumnmisc.blogspot.com/ ");
Explicit Wait:
We need to define a wait statement for certain condition to be satisfied until the specified timeout period. If the WebDriver API finds the element within the timeout period the automation script will get executed.
Explicit wait is mostly used when we need to Wait for a specific content and attribute change after performing any action, like when application gives AJAX call to system and get dynamic data and render on to the UI.
ie: Like there are drop-downs Country and State, based on the country value selected, the values in the state drop-down will change, It will take few seconds of time to get the data based on user selection.
Example:
//Explicit wait for state dropdown field//
WebDriver APIWait wait = new WebDriver APIWait(driver, 10);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("statedropdown")));
 Fluent Wait:
Using Fluent Wait we can define the maximum amount of time to wait for a specific condition, as well as the frequency with which to check for the condition.
And also the user may configure to ignore particular types of exceptions such as "No Such Element Exceptions" when searching for an element in dom.
Syntax:
Wait<WebDriver API> wait = new FluentWait<WebDriver API>(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 NoSuchElementExcepti
.ignoring(NoSuchElementException.class);

No comments:

Post a Comment