最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论