Various Synchronization techniques in Selenium – User friendly Tech help
Why Synchronization?
n
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
n
How to achieve it?
n
Implicit wait
n
nSupport to work with wait commands is available under the package,
n org.openqa.selenium.support.ui (Thus we need to import this package in our code)
n
Note:- We can check the classes under the package from “Package Explorer” in Eclipse.
n
nPath:-
nPackage 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)
n
nOpen Package Explorer(Window – Show View)
n
Package for Wait commands |
n
nSelenium 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
nImplicit 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.
n
nLogic:-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 .
n
nLimitation: – implicit wait will be applied to all the elements of the test case by default.
n
nSyntax:–
ndriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
n
nIt 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.
n
nCode:-
n
n
Applying Implicit Wait |
n
Setting Time in Seconds for Implicit wait |
n
nExplicit Wait
nWe are explicitly making the execution of selenium code to wait, for a certain condition to occur before, proceeding further in the code.
n
nWhy Explicit Wait?
nWe 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 ?
n
nAnswer is strict no, and we would only apply explicit wait on this(slower) control with an expected condition.
n
nLogic:–
nIt is applicable to the given control and waits to the maximum amount of time or move forward incase the given condition is met.
n
nSyntax:-
nWebDriverWait.until(condition-that-finds-the-element(ExpectedCondition))
n
nWebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully
n
nNote:- we need to import the webdriverwait class and expectedCondition Packages
nimport org.openqa.selenium.support.ui.WebDriverWait;
nimport org.openqa.selenium.support.ui.ExpectedConditions;
n
WebDriverWait Package |
nExpected condition can be in the form of like:-
nbutton is enabled:elementToBeClickable
ntextbox is visible:visibilityOf
nalertispresent.n
ExpectedConditions in Selenium |
n
Code:–
n
n
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();
}
}
n
nNote:- We can achieve synchronization using following approach also
n
Fluent Wait
n
Fluent wait = Explicit Wait + frequency with which control need to be checked on application + exceptions to be ignored like
nNoSuchElementExceptions.
n
nIt 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.
n
nPackage :- org.openqa.selenium.support.ui.FluentWait
n
nIt is mostly used, while working with AJAX applications as well as in scenarios when element load time fluctuating.
n
n
Fluentwait Class |
n
PageLoadTimeout
nIt 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.
n
PageLoadTimeout method |
n
nSyntax:-
ndriver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
nwaits for 20 seconds for page to load
n
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.
n
nEx: Thread. Sleep (20000);
n
nHere 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.
nor
nControl exists after 10 sec. So here also selenium unnecessarily waits for more 10 sec.
n