What is Synchronization? Handling Synchronization in Selenium WebDriver using C#: - Blogs
X
23Nov

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these components to work together with the same pace. The co-ordination between these components to run paralelly is called Synchronization.

Synchronization (Wait) in Selenium has a great significant value.

Now a day, we see most of the web applications are developed by using Javacript and Ajax where it might happen that some of the elements may load at distant time intervals, due to which we may encounter “ElementNotVisibleException” or “NoSuchElementException” exceptions. In such scenarios, we use Synchronization/wait to overcome these kinds of exceptions.

Automation Testing is commonly comprising of following two segments:

  1. Application Under Test
  2. Test Automation Tool.

We must write our test scripts in such a way that these two segments move with same desired pace, because both have their own agility. Hence, to avoid Errors which will exhaust time in debugging.

Synchronization can be classified into two categories:

  1. Unconditional Synchronization 
  2. Conditional Synchronization

Unconditional synchronization indicates timeout value alone and makes tool to continue further only when specified time is elapsed. One such functions is:

 System.Threading.Thread.Sleep(); 

This statement is helpful only when we are interacting with third party systems like interfaces because it is not possible to write conditions to check the status. Now, we want tool to wait for certain amount of time and then perform the next actions sequence.

This statement makes the tool to wait for certain time even though the application is ready, thus increasing the chance of unnecessary wait and hence giving the scope to Conditional Synchronization.

Conditional Synchronization: By using conditional synchronization we can specify timeout duration along with some desired conditions to check and then throw an error if nothing happens.

Types of Conditional Synchronization:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: Implicit Wait tells the WebDriver to Wait until the stated time before throwing the NoSuchElement/ElementNotVisible exception. Waiting time across the test script between each consecutive steps are taken by default. Hence, next testStep will execute only when the specified time is elapsed post executing the previous testStep.

Syntax:

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

The Implicit wait takes one argument: TimeSpan (timeToWait)

  • FromMilliSeconds
  • FromSeconds
  • FromMinutes
  • FromHours
  • FromDays
     

Eg: The wait time specified in Implicit Wait command is say 30seconds. The specified time (30seconds) is default for all the succeeding testSteps thus giving rise to execution time. To overcome this issue, Explicit waits are introduced to apply waits whenever it is required while executing the testSteps.

Explicit Wait: Explicit waits are very good to use when page loads dynamically. Explicit Wait tells the WebDriver to Wait until the specified condition is met or maximum time elapses before throwing NoSuchElement (or) ElementNotVisible Exceptions. Explicit waits are applied for the specified testStep in test script.

To use Explicit waits, we must first create instance for “WebDriverWait” class.

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 first place before throwing an exception.

Following are the ExpectedConditions:

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

The Implicit wait is simple when compared to Explicit wait and can be declared in setup method of the test script in single line of code. However, the Implicit wait has drawbacks despite being easy and simple to apply. Hence rising scope to use Explicit Waits because these waits can be applied for a specified testStep in test script.

 

Happy Testing smiley

Related

What is Defect? Defect Life Cycle in Software Testing.

Defect is an unexpected behavior of the software application flow against the requirement specificat...

Read More >

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Assertions in Coded UI

Let us discuss how to add assertions in coded UI. Assertions are checkpoints/benchmarks to UI c...

Read More >

Analysis of Load Test Results

Analysis of Performance DataAfter you capture and consolidate your results, analyze the captured dat...

Read More >

Parallel Execution using Selenium Webdriver and TestNG

While testing a web application it is very important to test the application on different browsers. ...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *