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.
- Quick Start to Selenium with JAVA and JUnit
- Selenium Webdriver API Architecture
- Selenium Navigation Commands
- Selenium Find Elements
- Selenium Actions
- Selenium Alerts
- Selenium Frames
- 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
1 | WebDriverWait wait = new WebDriverWait(driver, waitTime); |
■ presenceOfElementLocated(locator):
1 | wait.until(ExpectedConditions.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):
1 | wait.until(ExpectedConditions.visibilityOfElementLocated(locator)); |
It is for the element present in the DOM of a page is visible.
■ invisibilityOfElementLocated(locator):
1 | wait.until(ExpectedConditions.invisibilityOfElementLocated(locator)); |
It is for the element present in the DOM of a page is invisible.
■ elementToBeClickable(locator):
1 | wait.until(ExpectedConditions.elementToBeClickable(locator)); |
It is for the element to be clickable.
Note: All Expected Conditions are listed on below page.
► Now, it is time to do an example for Selenium Webdriver Wait.
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.
Test Scenario without Synchronization
- Go to above URL.
- Maximize the window.
- Get the selected date text before selecting the date. (Before AJAX call)
- Print selected date text to the console. (Before AJAX call)
- Click 3rd January.
- Get the selected date. (After AJAX call – This will be the failing point)
- Print selected date text to the console. (After AJAX call)
- 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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package Synchronization; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; public class FirstFailedSyncTest { static WebDriver driver; private static String url = "http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx"; //Setup Driver @BeforeClass public static void setupTest() { driver = new FirefoxDriver(); driver.navigate().to(url); driver.manage().window().maximize(); } @Test public void FirstFailedSyncTest() { //Get the selected date text before AJAX call String selectedDateTextBeforeAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextBeforeAjaxCall to the console System.out.println("selectedDateTextBeforeAjaxCall: " + selectedDateTextBeforeAjaxCall +"\n" ); //Find 3rd January on the calendar WebElement thirdJanuary = driver.findElement(By.xpath(".//*[contains(@class, 'rcWeekend')]/a[.='3']")); //Click 3rd January thirdJanuary.click(); //Get the selected date text after AJAX call String selectedDateTextAfterAjaxCall = driver.findElement( By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextAfterAjaxCall to the console System.out.println("selectedDateTextAfterAjaxCall: " + selectedDateTextAfterAjaxCall +"\n" ); //Check the Actual and Expected Text assertThat(selectedDateTextAfterAjaxCall, is("Sunday, January 03, 2016")); } //Close Driver @AfterClass public static void quitDriver() { driver.quit(); } } |
Console Output:
Test Scenario with Synchronization
- Go to above URL.
- Maximize the window
- Declare WebDriverWait for 10 seconds.
- Wait until the presence of date form container in DOM. (Synchronization Point)
- Get the selected date text before selecting the date. (Before AJAX call)
- Print selected date text to the console. (Before AJAX call)
- Click 3rd January.
- Wait until invisibility of loader. (Synchronization Point)
- Wait until visibility of selected date text. (Synchronization Point – This is not necessary but I added this to show visibilityOfElementLocated)
- Get the selected date. (After AJAX call – This time it will not fail. J )
- Print selected date text to the console. (After AJAX call)
- 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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package Synchronization; import org.hamcrest.CoreMatchers; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import static org.junit.Assert.assertThat; public class FirstSyncTest { static WebDriver driver; private static String url = "http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx"; //Setup Driver @BeforeClass public static void setupTest() { driver = new FirefoxDriver(); driver.navigate().to(url); driver.manage().window().maximize(); } @Test public void FirstSyncTest() { //Declare a Webdriver Wait WebDriverWait wait = new WebDriverWait(driver,10); //Wait until presence of container wait.until(ExpectedConditions.presenceOfElementLocated (By.cssSelector(".demo-container.size-narrow"))); //Get the selected date text before AJAX call String selectedDateTextBeforeAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextBeforeAjaxCall to the console System.out.println("selectedDateTextBeforeAjaxCall: " + selectedDateTextBeforeAjaxCall +"\n" ); //Find 3rd January on the calendar WebElement thirdOfJanuary = driver.findElement(By.xpath(".//*[contains(@class, 'rcWeekend')]/a[.='3']")); //Click 3rd January thirdOfJanuary.click(); //Wait until invisibility of loader new WebDriverWait(driver,10).until( ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".raDiv"))); //Wait until visibility of selected date text //Actually it is not necessary, I added this control to see an example of visibilityOfElementLocated usage. new WebDriverWait(driver,10).until( ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#ctl00_ContentPlaceholder1_Label1"))); //Find Selected Dates Text String selectedDateTextAfterAjaxCall = driver.findElement( By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextAfterAjaxCall to the console System.out.println("selectedDateTextAfterAjaxCall: " + selectedDateTextAfterAjaxCall +"\n" ); //Check the Actual and Expected Text assertThat(selectedDateTextAfterAjaxCall, CoreMatchers.is("Sunday, January 03, 2016")); } //Close Driver @AfterClass public static void quitDriver() { driver.quit(); } } |
Console Output:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //Custom Expected Condition //Returns boolean value (True or False) private class ElementContainsText implements ExpectedCondition<Boolean> { private String textToFind; private By findBy; //Constructor (Set the given values) public ElementContainsText (final By elementFindBy, final String textToFind) { this.findBy = elementFindBy; this.textToFind = textToFind; } //Override the apply method with your own functionality @Override public Boolean apply(WebDriver webDriver) { //Find the element with given By method (By CSS, XPaht, Name, etc.) WebElement element = webDriver.findElement(this.findBy); //Check that the element contains given text? if(element.getText().contains(this.textToFind)) { return true; } else { return false; } } //This is for log message. I override it because when test fails, it will give us a meaningful message. @Override public String toString() { return ": \"Does " + this.findBy + " contain " + this.textToFind + "?\""; } } |
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.
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:
- Go to above URL.
- Maximize the window
- Declare WebDriverWait for 10 seconds.
- Wait until the presence of date form container in DOM. (Synchronization)
- Get the selected date text before selecting the date. (Before AJAX call)
- Print selected date text to the console. (Before AJAX call)
- Click 3rd January.
- (-) Removed: Wait until invisibility of loader. (We will not use built-in ExpectedCondition)
- (+) Added: Use custom named ExpectedCondition (Synchronization)
- Get the selected date. (After AJAX call)
- Print selected date text to the console. (After AJAX call)
- Check the expected text and actual text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | package Synchronization; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; /** * Created by ONUR on 03.01.2016. */ public class CustomExpConWithNamedClass { static WebDriver driver; private static String url = "http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx"; //Setup Driver @BeforeClass public static void setupTest() { driver = new FirefoxDriver(); driver.navigate().to(url); driver.manage().window().maximize(); } @Test public void CustomExpectedConditionWithNamedClassTest() { //Declare a Webdriver Wait WebDriverWait wait = new WebDriverWait(driver, 10); //Wait until presence of container wait.until(ExpectedConditions.presenceOfElementLocated (By.cssSelector(".demo-container.size-narrow"))); //Get the selected date text before AJAX call String selectedDateTextBeforeAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextBeforeAjaxCall to the console System.out.println("selectedDateTextBeforeAjaxCall: " + selectedDateTextBeforeAjaxCall +"\n" ); //Find 3rd January on the calendar WebElement thirdOfJanuary = driver.findElement(By.xpath(".//*[contains(@class, 'rcWeekend')]/a[.='3']")); //Click 3rd January thirdOfJanuary.click(); //Instead of using below ExpectedConditions, we will use our custom ExpectedCondition /*//Wait until invisibility of loader new WebDriverWait(driver,10).until( ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".raDiv"))); //Wait until visibility of selected date text new WebDriverWait(driver,10).until( ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#ctl00_ContentPlaceholder1_Label1")));*/ //Use custom ExpectedCondition wait.until(new ElementContainsText(By.cssSelector("#ctl00_ContentPlaceholder1_Label1"),"2016")); //Find Selected Dates Text String selectedDateTextAfterAjaxCall = driver.findElement( By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextAfterAjaxCall to the console System.out.println("selectedDateTextAfterAjaxCall: " + selectedDateTextAfterAjaxCall +"\n" ); //Check the Actual and Expected Text assertThat(selectedDateTextAfterAjaxCall, is("Sunday, January 03, 2016")); } //Custom Expected Condition //Returns boolean value (True or False) private class ElementContainsText implements ExpectedCondition<Boolean> { private String textToFind; private By findBy; //Constructor (Set the given values) public ElementContainsText (final By elementFindBy, final String textToFind) { this.findBy = elementFindBy; this.textToFind = textToFind; } //Override the apply method with your own functionality @Override public Boolean apply(WebDriver webDriver) { //Find the element with given By method (By CSS, XPaht, Name, etc.) WebElement element = webDriver.findElement(this.findBy); //Check that the element contains given text? if(element.getText().contains(this.textToFind)) { return true; } else { return false; } } //This is for log message. I override it because when test fails, it will give us a meaningful message. @Override public String toString() { return ": \"Does " + this.findBy + " contain " + this.textToFind + "?\""; } } //Close Driver @AfterClass public static void quitDriver() { driver.quit(); } } |
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.
1 2 3 4 5 6 7 8 9 | //AdHoc Wait with Anonymous Class (Synchronization) wait.until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver webDriver) { //Implement your functionality(logic) here (Synchronization code should be implemented in here) //We are checking that Selected Text Pane contains "2016"? return webDriver.findElement(By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().contains("2016"); } }); |
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:
- Go to test URL.
- Maximize the window
- Declare WebDriverWait for 10 seconds.
- Wait until presence of date form container in DOM. (Synchronization)
- Get the selected date text before selecting date. (Before AJAX call)
- Print selected date text to the console. (Before AJAX call)
- Click 3rd January.
- (-) Removed: Wait until invisibility of loader. (We will not use built-in ExpectedCondition!)
- (-) Removed: Use custom named ExpectedCondition class. (We will not use!)
- (+) Added: Use custom anonymous ExpectedCondition
- Get the selected date. (After AJAX call)
- Print selected date text to the console. (After AJAX call)
- Check the expected text and actual text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package Synchronization; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; public class CustomExpConWithAnonymousClass { static WebDriver driver; private static String url = "http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx"; //Setup Driver @BeforeClass public static void setupTest() { driver = new FirefoxDriver(); driver.navigate().to(url); driver.manage().window().maximize(); } @Test public void CustomExpectedConditionWithAnonymousClassTest() { //Declare a Webdriver Wait WebDriverWait wait = new WebDriverWait(driver, 10); //Wait until presence of container wait.until(ExpectedConditions.presenceOfElementLocated (By.cssSelector(".demo-container.size-narrow"))); //Get the selected date text before AJAX call String selectedDateTextBeforeAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextBeforeAjaxCall to the console System.out.println("selectedDateTextBeforeAjaxCall: " + selectedDateTextBeforeAjaxCall +"\n" ); //Find 3rd January on the calendar WebElement thirdOfJanuary = driver.findElement(By.xpath(".//*[contains(@class, 'rcWeekend')]/a[.='3']")); //Click 3rd January thirdOfJanuary.click(); //AdHoc Wait with Anonymous Class (Synchronization) wait.until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver webDriver) { //Implement your functionality(logic) here (Synchronization code should be implemented in here) //We are checking that Selected Text Pane contains "2016"? return webDriver.findElement(By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().contains("2016"); } }); //Find Selected Dates Text String selectedDateTextAfterAjaxCall = driver.findElement( By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextAfterAjaxCall to the console System.out.println("selectedDateTextAfterAjaxCall: " + selectedDateTextAfterAjaxCall +"\n" ); //Check the Actual and Expected Text assertThat(selectedDateTextAfterAjaxCall, is("Sunday, January 03, 2016")); } //Close Driver @AfterClass public static void quitDriver() { driver.quit(); } } |
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:
1 2 3 4 5 6 7 8 9 | //Wrapped Anonymous Class private ExpectedCondition<Boolean> textDisplayed (final By elementFindBy, final String text){ return new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver webDriver) { return webDriver.findElement(elementFindBy).getText().contains(text); } }; } |
Extra Note: Below code is lambda expression of above method. It works only on JAVA 8 JVM installed machines.
1 2 3 | private ExpectedCondition<Boolean> textDisplayed (final By elementFindBy, final String text){ return webDriver -> webDriver.findElement(elementFindBy).getText().contains(text); } |
How to call wrapped anonymous class:
1 2 | //Wrapped Anonymous Class Call wait.until(textDisplayed(By.cssSelector("#ctl00_ContentPlaceholder1_Label1"),"2016")); |
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.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package Synchronization; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; /** * Created by ONUR on 05.01.2016. */ public class CustomExpConWithWrappedAnonymousClass { static WebDriver driver; private static String url = "http://demos.telerik.com/aspnet-ajax/ajaxloadingpanel/functionality/explicit-show-hide/defaultcs.aspx"; //Setup Driver @BeforeClass public static void setupTest() { driver = new FirefoxDriver(); driver.navigate().to(url); driver.manage().window().maximize(); } @Test public void CustomExpectedConditionWithWrappedAnonymousClassTest() { //Declare a Webdriver Wait WebDriverWait wait = new WebDriverWait(driver, 10); //Wait until presence of container wait.until(ExpectedConditions.presenceOfElementLocated (By.cssSelector(".demo-container.size-narrow"))); //Get the selected date text before AJAX call String selectedDateTextBeforeAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextBeforeAjaxCall to the console System.out.println("selectedDateTextBeforeAjaxCall: " + selectedDateTextBeforeAjaxCall +"\n" ); //Find 3rd January on the calendar WebElement thirdOfJanuary = driver.findElement(By.xpath(".//*[contains(@class, 'rcWeekend')]/a[.='3']")); //Click 3rd January thirdOfJanuary.click(); //Wrapped Anonymous Class (Synchronization) wait.until(textDisplayed(By.cssSelector("#ctl00_ContentPlaceholder1_Label1"),"2016")); //Find Selected Dates Text String selectedDateTextAfterAjaxCall = driver.findElement (By.cssSelector("#ctl00_ContentPlaceholder1_Label1")).getText().trim(); //Print selectedDateTextAfterAjaxCall to the console System.out.println("selectedDateTextAfterAjaxCall: " + selectedDateTextAfterAjaxCall +"\n" ); //Check the Expected and Actual Text assertEquals("Expected and Actual Text are not Equal!","Sunday, January 03, 2016", selectedDateTextAfterAjaxCall); } //Wrapped Anonymous Class private ExpectedCondition<Boolean> textDisplayed (final By elementFindBy, final String text){ return new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver webDriver) { return webDriver.findElement(elementFindBy).getText |