Selenium Webdriver Wait is a critical synchronization topic for selenium test automation. We have to know explicit wait, implicit wait, fluent wait to do wait operations properly. In this post, we will cover Selenium Synchronization methods. Synchronization is one of the most critical issues while we are doing test automation. Selenium Webdriver Wait problems generally lead to non-reliable, intermittent, slow and non-stable tests. Thus, we have to do proper synchronization to make our tests fast, robust, and reliable. When we automate a web page, we should not wait for too much and should not wait at the wrong point. In this post, I will try to explain Selenium Webdriver’s wait methods and how to work with expected conditions to synchronize your tests properly.

Audience

Selenium Webdriver Synchronization post is designed for SW test professionals who have some information about selenium web automation. At the end of this tutorial, you will know how to synchronize your tests correctly.

Prerequisites for Selenium Webdriver Wait

 Nice to have: Before starting to read Selenium Webdriver Synchronization post it is better to read previous selenium tutorials.

  1. Quick Start to Selenium with JAVA and JUnit
  2. Selenium Webdriver API Architecture
  3. Selenium Navigation Commands
  4. Selenium Find Elements
  5. Selenium Actions
  6. Selenium Alerts
  7. Selenium Frames
  8. Selenium Windows

Selenium WebDriverWait and Selenium Expected Conditions

If we need some synchronization points, we should use Selenium WebDriverWait methods. These methods help us to control our tests. We can wait at any specific point until an expected condition occurs. When that expected condition occurred, our test script goes on running from that point.

Before doing an example, it is better to talk about AJAX pages. AJAX expansion is Asynchronous JavaScript and AJAX allows the web page to retrieve small amounts of data from the server without refreshing the entire page and retrieving that data takes time. Thus, at that point, our test code should also wait. As I told you above, with Selenium WebdriverWait and ExpectedCondition methods, we can wait at any point and then continue the test execution when the element is found/visible. There are many wait.until(ExpectedConditions.anyCondition) methods, but I want to explain the most common ones below for selenium webdriver synchronization.

WebDriverWait Syntax

■ presenceOfElementLocated(locator):

It checks the element presence on the DOM of a page. This does not necessarily mean that the element is visible.

■ visibilityOfElementLocated(locator):

 It is for the element present in the DOM of a page is visible.

■ invisibilityOfElementLocated(locator):

It is for the element present in the DOM of a page is invisible.

■ elementToBeClickable(locator):

It is for the element to be clickable.

 Note: All Expected Conditions are listed on below page.

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

► Now, it is time to do an example for Selenium Webdriver Wait.

Test site: http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx

I want to explain webpage and date form functionality. When you select a date, then a loader will occur immediately and after a period of time selected date will be seen in Selected Dates pane. When you deselect the selected date, again a loader will occur and after a period of time selected date will be wiped away from the Selected Dates pane.

synchronization

synchronization

Test Scenario without Synchronization

  1. Go to above URL.
  2. Maximize the window.
  3. Get the selected date text before selecting the date. (Before AJAX call)
  4. Print selected date text to the console. (Before AJAX call)
  5. Click 3rd January.
  6. Get the selected date. (After AJAX callThis will be the failing point)
  7. Print selected date text to the console. (After AJAX call)
  8. Check the expected text and actual text.

Selected Text Area CSS Path: #ctl00_ContentPlaceholder1_Label1

3rd January Xpath: .//*[contains(@class, ‘rcWeekend’)]/a[.=’3′]

Test Code(it will fail):

Console Output:

synchronization

Test Scenario with Synchronization

  1. Go to above URL.
  2. Maximize the window
  3. Declare WebDriverWait for 10 seconds.
  4. Wait until the presence of date form container in DOM. (Synchronization Point)
  5. Get the selected date text before selecting the date. (Before AJAX call)
  6. Print selected date text to the console. (Before AJAX call)
  7. Click 3rd January.
  8. Wait until invisibility of loader. (Synchronization Point)
  9. Wait until visibility of selected date text. (Synchronization Point – This is not necessary but I added this to show visibilityOfElementLocated)
  10. Get the selected date. (After AJAX call – This time it will not fail. J )
  11. Print selected date text to the console. (After AJAX call)
  12. Check the expected text and actual text.

Date Container CSS Path: .demo-container.size-narrow

Loader CSS Path: .raDiv

Selected Text Area CSS Path: #ctl00_ContentPlaceholder1_Label1

3rd January Xpath: .//*[contains(@class, ‘rcWeekend’)]/a[.=’3′]

Test Code (it will pass):

Console Output:

synchronization

Selenium Custom Expected Conditions

Sometimes we need, or we want to use expected conditions rather than built-in expected conditions. These custom expected conditions can make our tests more readable, tidy, and short.

I want to show a sample custom ExpectedCondition class below. It checks “Does an element contain given text until defined WebdriverWait time?”

Method-1: Custom ExpectedCondition by Using Named Class

In the above class (named class) you can see that I did below stuff:
–    Implemented ExpectedContion<Boolean> interface.
–    Set FindBy and testToFind values in constructor.
–    Override the apply method to implement “Does the given element contains the given text?” functionality. (We should put our logic in here.)
–    Override the String method because when the test fails, I want to see a more meaningful message. It is shown below.

synchronization

Example: Let’s do the same example that I showed and coded “Working with WebDriverWait and ExpectedConditions” section but now we will use our custom “ElementContainsText” class.

Test website is: http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx

Selected Text Area CSS Path: #ctl00_ContentPlaceholder1_Label1

3rd January Xpath: .//*[contains(@class, ‘rcWeekend’)]/a[.=’3′]

Test Scenario with Custom ExpectedCondition by Using Named Class:

  1. Go to above URL.
  2. Maximize the window
  3. Declare WebDriverWait for 10 seconds.
  4. Wait until the presence of date form container in DOM. (Synchronization)
  5. Get the selected date text before selecting the date. (Before AJAX call)
  6. Print selected date text to the console. (Before AJAX call)
  7. Click 3rd January.
  8. (-) Removed: Wait until invisibility of loader. (We will not use built-in ExpectedCondition)
  9. (+) Added: Use custom named ExpectedCondition (Synchronization)
  10. Get the selected date. (After AJAX call)
  11. Print selected date text to the console. (After AJAX call)
  12. Check the expected text and actual text.
Method-2: Custom ExpectedCondition by Using Anonymous Class

Sometimes you may want to synchronize your test with Adhoc(Inline) ExpectedCondition for selenium webdriver synchronization. This can be done with an anonymous class. If you do synchronization in this way, you can still override the apply method, but you cannot use a constructor. The anonymous class is located in until () block, and we can write our synchronization logic in the apply method.

Example: Now, let’s do the same example that is shown below sections by using an anonymous class. Test scenario, URL, and CSS, XPath paths of the elements are same.

Test Scenario with Custom ExpectedCondition by Using Anonymous Class:

  1. Go to test URL.
  2. Maximize the window
  3. Declare WebDriverWait for 10 seconds.
  4. Wait until presence of date form container in DOM. (Synchronization)
  5. Get the selected date text before selecting date. (Before AJAX call)
  6. Print selected date text to the console. (Before AJAX call)
  7. Click 3rd January.
  8. (-) Removed: Wait until invisibility of loader. (We will not use built-in ExpectedCondition!)
  9. (-) Removed: Use custom named ExpectedCondition class. (We will not use!)
  10. (+) Added: Use custom anonymous ExpectedCondition
  11. Get the selected date. (After AJAX call)
  12. Print selected date text to the console. (After AJAX call)
  13. Check the expected text and actual text.
Method-3: Custom ExpectedCondition by Wrapping Anonymous Class in a Method

Actually, using an anonymous class for synchronization is a kind of hard-coded coding style and it is not so flexible. However, when you encapsulate an anonymous class in a method, your selenium webdriver synchronization code will be more flexible and reusable. The sample code is shown below.

Implementation:

Extra Note: Below code is lambda expression of above method. It works only on JAVA 8 JVM installed machines.

How to call wrapped anonymous class:

Example: The Same example is done with wrapped anonymous class below. (I do not want to write the SAME scenario again. It is as same as former ones.)