The concept of Synchronization and Selenium

The concept of Synchronization and Selenium

Many a times when we design tests with selenium, we find that during execution sometimes test shows a behavior which is unpredictable. For some runs it will pass, where as for others it will fail. Generally whenever that happens, it is an issue with the synchronization. The problem is the speed at which the tests are being executed by the tool has to match the speed at which the application is able to process them, and whenever we will have a mismatch, the error will come and test will fail.

 

 

Sync

In Selenium, we can overcome these issues by using waits – Implicit wait, explicit wait and fluent wait. Let us talk about them and understand why and where we use them.

 

Implicit Wait

Selenium derived the concept of implicit wait from watir, another test automation tool. We generally declare this wait at the top of our scripts where we define driver, and this time is set at global level for every element. It is generally described like this –

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

This means that we want to wait for 30 seconds for every command which requires the element to be found first, and then act on it. The maximum wait it will do is 30 secsm after that we will get an exception “No such element found” for the given locator value.  We can set the implicit wait to 0, which we generally do when we have to set some explicit wait, so that it doesn’t interfere. The polling time is around 250 milliseconds.

Explicit Wait

Explicit wait are generally used in situations where a local scenario may require more time than the global time out. And we don’t want to increase the global timeout because of one or two scenarios in the tests. So for those situations we set implicit wait to 0, and define explicit wait. The explicit wait can be defined as – Static wait and Dynamic wait.

Static wait

Static wait is when we introduce forced waits as in where we need to wait exactly for that unit of time. This is introduced by, Thread.sleep(time in milliseconds).  

In this the script when encounters static wait will halt the execution for that number of seconds so that the application can catch up with the automation tool and then after the time is over go on and execute the next command. The drawback is we will never know how much wait is enough. It can be too much or too little. And since we have to be smart test automation engineers, these waits are best avoided.

Dynamic wait

We introduce dynamic wait by using WebDriverWait class in Selenium. In this we have a method called as wait.until(Expected Condition). This basically says that either an event will occur or it will time out. So if the event occurs in the first few milliseconds it won’t wait the entire time, else it will keep on waiting until timeout occurs. So the kind of exception we always see with this wait is – “Timeout Exception”.

This wait is generally defined as follows –

WebDriverWait wait = new WebDriverWait(driver, 60);

 WebElement element = wait.until(ExpectedConditions.presenceofElementLocatoredBy(By.linkText(“Log Off”)));

 

So in the above code snippet we will either wait for 60 seconds or the link with the text Log Off will appear. Whichever event will occur first will allow us to move to the next statement in the code.

To read more about it visit- https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html


Fluent Wait

Fluent wait allows us to define maximum wait time, in how much time should we poll for the event, also which all errors to ignore, So it is more advance than the above waits defined. We define fluent wait as follows –

  Wait wait = new FluentWait(driver)

 

    .withTimeout(120, SECONDS)

 

    .pollingEvery(1, SECONDS)

 

    .ignoring(NoSuchElementException.class);

 

  WebElement foo = wait.until(new Function() {

 

    public WebElement apply(WebDriver driver) {

 

    return driver.findElement(By.linkText("My Account"));

 

    }

 

   });

 

In the above example we will be waiting at the max for 120 seconds, polling after every 1 second for element with the link text My Account to appear. And ignoring the no such element found exception if it occurs within the time provided.

So this was all about waits and the concept of synchronization in Selenium. Hope you found the article helpful.

 

No comments

Leave a reply

Your email is never published nor shared. Required fields are marked *