admin管理员组

文章数量:1426799

Our site does a lot of JavaScript/jQuery work in the $(document).ready() function, setting up buttons, loading content via ajax, etc. Selenium waits until the DOM is loaded, but then proceeds to test before .ready() has pleted.

A partial solution seems to be using a check to see if the browser has pending ajax requests:

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

However that doesn't ensure that we aren't still setting up bindings for buttons and things.

Any help will be greatly appreciated. The current 'best' suggestion is adding an element to the page at the end of the .ready() method, which Selenium can catch as a signal to start working, but the idea of adding code like this for testing purposes seems sketchy at best.

Our site does a lot of JavaScript/jQuery work in the $(document).ready() function, setting up buttons, loading content via ajax, etc. Selenium waits until the DOM is loaded, but then proceeds to test before .ready() has pleted.

A partial solution seems to be using a check to see if the browser has pending ajax requests:

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

However that doesn't ensure that we aren't still setting up bindings for buttons and things.

Any help will be greatly appreciated. The current 'best' suggestion is adding an element to the page at the end of the .ready() method, which Selenium can catch as a signal to start working, but the idea of adding code like this for testing purposes seems sketchy at best.

Share Improve this question asked Jan 14, 2011 at 19:22 David SmithDavid Smith 39.9k11 gold badges47 silver badges61 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I believe you can use the window load check. Try this:

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

The current 'best' suggestion is adding an element to the page at the end of the .ready() method, which Selenium can catch as a signal to start working, but the idea of adding code like this for testing purposes seems sketchy at best.

I don't personally think that modifying your code to improve testability is a bad thing. If you really would rather not pollute your production pages then use some templating tool like PHP to output the code only in development/debug mode.

Also, you don't need to use the DOM for this. Selenium has access to the the full javascript context in the page so you can just use a global variable as a flag. Maybe something that is unlikely to be used by others like SELENIUM_PAGE_FULLY_READY then have Selenium check for its existence.

本文标签: javascriptHow can I force Selenium to run tests only after (document)ready() has completedStack Overflow