Clicking and Waiting in Selenium

Here is a description of the difference between using click and clickAndWait in Selenium. This is my response to a question posted on the Software Testing Club.

clickAndWait is used instead of click when loading a new page.

It simply waits for the page finish loading before executing the next command.  Because of this, any AJAX or non-page loading actions should use click instead of clickAndWait.

clickAndWait will fail after the timeout setting (default 30 seconds) has been reached.  You can change the default timeout, or call setTimeout.

If you have an action that takes time but does not reload the page, it may be necessary to insert a manual wait, or waitForElementPresent. For AJAX requests, you might need to check the document readystate property.

A typical method for testing a login form is to write test methods to validate each field and check for errors. The selenium commands you use for a happy path might be something like this:

open /login
type username aaron
type password secret
clickAndWait loginButton
verifyTextPresent Welcome back, Aaron

To check that it gives an error message when you type type wrong password, you could do:

open /login-example
type username aaron
type password secret
clickAndWait loginButton
verifyTextPresent invalid password

A variation that checks that a password can only be 8 characters might look like this:

open /login-example
type username aaron
type password secret123
verifyValue password secret12

Suppose there was a javascript check in case the user left either field blank before submitting.  In this case, you can’t use clickAndWait

open /login-example
type username aaron
click loginButton
waitForElementPresent validation_errors
verifyTextPresent password is required

Finally, a test with an ajax call on login might look like this:

open /login-example
type username aaron
type password wrong
click loginButton
setTimeout 60000
waitForEval selenium.browserbot.getCurrentWindow().document.readyState == "complete"
verifyTextPresent "invalid password"

You can see the sample login page at http://www.one-shore.com/aaron/login-example

Advertisements