Selenium 2 WebDriver Waits & AJAX wait
Best Practices

Web Testing box

Press Spacebar or Tab To Get Started

To view this presentation please use the latest Firefox, Chrome, or Safari browser.


Different wait methods

1. Thread.Sleep()

2. implicitlyWait()

3. WebdriverWait


1. Thread.sleep(): it is not recommended, and should not use it

Reasons:

1. You can never predict the exact wait time.
People tend to add extra seconds.

2. It increases overhead.
If you do 1000 tests, the extra seconds will slow your tests down.

3. It may not work with all computers.
Some computer are fast, and others are slow.  So in a slow computer, an element may not show up within the wait time.


2. implicitlyWait(): use it

implicitlyWait():
tells WebDriver to wait for an element if they are not immediately available. So, WebDriver does not throw NoSuchElementException immediately.

implicitly method example

3. WebDriverWait(): use it

WebDriverWait():
checks for the given condition every 500 milliseconds until it returns successfully or timeout.

webdriverwait method example

What is the best practice to wait?


The best practice is to set implicitlyWait() at the beginning of each test, and use WebDriverWait() for waiting an element, or AJAX element to load.


However, implicitlyWait() and WebDriverWait() do not work well together in the same test.

You would have to nullify implicitlyWait() before calling WebDriverWait because implicitlyWait() also sets the "driver.findElement()" wait time.


confusing isn't it


WaitTool

So, I create WaitTool

WaitTool solves the complexity of ImplicitWait and WebDriverWait, and provides easy methods to use.

WaitTool.java

WaitTool handles the following tasks at the behind scene.

1. nullifying implicitlyWait()
2. executing WebDriverWait (), and return element
3. reset implicitlyWait() again

wait meethod

Let’s test a AJAX Site.

1. Go to page: www.w3schools.com/ajax/default.asp
2. click the button to call AJAX
3. See the text change

go to w3schools page

At the beginning of a test, set implicitlyWait()

set implicitlyWait

At the test method:

1. Go to the page
2. click the button to call AJAX
3. Use WaitTool.waitForJavaScriptCondition()
4. See the text change

go to w3schools page

Here are the source codes

Project Home: https://github.com/ChonC/wtbox
wiki: https://github.com/ChonC/wtbox/wiki
WaitTool source code
AJAX wait example


Thank you!




Chon Chung - @chon_chung


< >