Today, I’ll update the selenium automation test to wait on ajax calls.
We haven’t seen any problems so far because our web application is too fast -
not surprising as it is running with all data in memory on the local server.
Let’s start by slowing things down to see the problem.
I’ll do this by adding a query parameter to the getUserMap method
and using that parameter to request some sleep before returning the User set.
Now run the automation test twice with a 500 millisecond delay in the Chrome browser
(I did not see the problem in FireFox, but your mileage may vary).
On the second execution, you’ll see an error trying to add a user that already exists.
1
$ mvn test -Dbaseurl=http://localhost:8080/\?sleep=500 -Dbrowser=chrome
...
$ mvn test -Dbaseurl=http://localhost:8080/\?sleep=500 -Dbrowser=chrome
...
org.openqa.selenium.WebDriverException: unknown error: cannot focus element
(Session info: chrome=43.0.2357.124)
...
Examination of our automation task exposes the problem:
I search on the email address to test if the user already exists.
With the delay, I run this search before the browser has loaded the data.
Therefore, I try to add a user that already exists
I need to wait for the ajax call to complete.
Ideally, the developer will add something to explicitly mark the completion,
perhaps by using jQuery ajaxStart and ajaxStop.
I wasn’t so obliging when I wrote the application,
so I’ll rely upon the internal jQuery.active parameter instead.
The jQuery.active parameter is set to 0 when all jQuery ajax call are complete.
I test for this condition with a new JQueryAjaxDone class,
that casts the WebDriver to a JavascriptExecutor and then runs a javascript test on jQuery.active in the browser.