How to get Selenium to run tests only after the completion of $ (document) .ready ()?

Our site runs a lot of JavaScript / jQuery in the function $ (document) .ready (), customizes buttons, loads content via ajax, etc. Selenium waits for the DOM to load, but then proceeds to testing until .ready () is complete.

The partial solution seems to use validation to check if the browser is waiting for pending ajax requests:

selenium.browserbot.getCurrentWindow().jQuery.active == 0 

However, this does not guarantee that we are not yet setting bindings for buttons and things.

Any help would be greatly appreciated. The current β€œbest” sentence adds an element to the page at the end of the .ready () method, which Selenium can catch as a signal to get started, but the idea of ​​adding code like this for testing purposes seems fragmentary at best.

+7
source share
2 answers

I believe you can use window load checking. Try the following:

 $(window).load(function(){ selenium.browserbot.getCurrentWindow().jQuery.active == 0 }); 
+3
source

The current "best" suggestion is to add an element to the page at the end of the .ready () method, which Selene may get as a signal to start working, but the idea of ​​adding code like this for testing purposes seems fragmentary at best.

I personally do not think that changing the code to improve testing is bad. If you really do not pollute your production pages, use some template tool, such as PHP, to output code only in development / debugging mode.

In addition, you do not need to use the DOM for this. Selenium has access to the full javascript context on the page, so you can just use the global variable as a flag. Maybe something that is unlikely to be used by others, for example, SELENIUM_PAGE_FULLY_READY , then checks Selenium for its existence.

+1
source

All Articles