1.

How do you achieve synchronization in WebDriver?

Answer»

When two or more components work together to perform any action, we expect these components to work together at the same pace, and the coordination between these components to run parallel is called Synchronization. We require synchronization so that the Application Under Test(AUT) and testing TOOL are running in sync with each other.

Most of the web applications are developed using JavaScript and Ajax and it might happen that some of the elements may load at distant time intervals, because of which we may see “ElementNotVisibleException” or “'NoSuchElementException” exceptions. To handle these scenarios, we use Synchronization/wait to overcome these exceptions.

Two components are involved in Automation Testing is Application under test and Test Automation Tool.

While writing scripts we must ensure these two components move with the same desired pace, because both have their own agility and therefore, avoid Errors which will exhaust time in debugging.

There are two types of Conditional Synchronization:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait

Implicit Wait tells the WebDriver before throwing exception NoSuchElement/ElementNotVisible, to wait until the stated time. Waiting time between each consecutive step is taken by DEFAULT across the test script. So, the next test Step will be executed only when the SPECIFIED time is elapsed.

Syntax:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

The Implicit wait takes two arguments: TimeSpan (timeToWait) and time measure.

  • FromMilliSeconds
  • FromSeconds
  • FromMinutes
  • FromHours
  • FromDays

Explicit Wait:

When page load dynamically explicit WAITS are good to use. Before throwing NoSuchElement (or) ElementNotVisible Exceptions, explicit Wait tells the WebDriver to Wait till the specified condition is met or maximum time elapses. Explicit waits are APPLIED for the specified test Step in the test script.

We must first create an instance for the “WebDriverWait” class to use Explicit wait.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(element_ID)));

The reference variable “wait” informs the WebDriver to wait until the Expected condition to occur (or) Wait for the specified time of 30seconds, whichever shows in the first place before throwing an exception.

Below are the ExpectedConditions:

  • AlertIsPresent()
  • ElementSelectionStateToBe()
  • VisibilityOfAllElementsLocatedBy()
  • ElementToBeSelected()
  • FrameToBeAvaliableAndSwitchToIt()
  • InvisibilityOfTheElementLocated()
  • TextToBePresentInElementLocated()
  • TextToBePresentInElementValue()
  • TitleIs()
  • TitleContains()
  • VisibilityOf()
  • VisibilityOfAllElements()
  • VisibilityOfElementLocated()
  • InvisibilityOfElementWithText()
  • ElementToBeClickable()
  • PresenceOfAllElementsLocatedBy()
  • PresenceOfElementLocated()
  • TextToBePresentInElement()

The Implicit wait is simple as compared to Explicit wait and can be declared in setup method of the test script in a single line of code. But, despite being easy and simple to apply and hence rising scope of Explicit Waits as these waits can be applied for a specified test Step in test script.



Discussion

No Comment Found