开发者

How can I force Selenium to run tests only after $(document).ready() has completed?

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 completed.

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 开发者_如何学CSelenium can catch as a signal to start working, but the idea of adding code like this for testing purposes seems sketchy at best.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜