Selenium waiting for AJAX in Chrome

南笙酒味 提交于 2019-12-11 05:34:48

问题


I've been using Selenium to automate website testing. Part of the tests involves waiting for AJAX requests to finish. I've been using this line to do that with jQuery 1.4:

selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().jQuery.active == 0", "5000");

It worked fine in Chrome and Firefox 4, but not IE9. Then I upgraded to jQuery 1.5, and it magically stopped working in Chrome. I've tried the following variations, but none of them work:

selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().jQuery.active == 0", "5000");
selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().jQuery.ajax.active == 0", "5000");
selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().$.active == 0", "5000");
selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().$.ajax.active == 0", "5000");

The SeleniumException is that it cannot read property active or ajax of undefined, so I guess it's somehow not able to get to jQuery. My colleague is running the same tests on Firefox 4, and they pass.

Has anyone experienced this issue? Any suggestions?


回答1:


Apparently everything in Selenium can be fixed by throwing in random Thread.Sleep()s. The window hadn't finished loading in some cases, that's why $ was undefined. I put a sleep before the wait, and it works fine now.

I had to use the following line:

selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().$.active == 0", "5000");

Even though people are saying I should have used $.ajax.active if I'm using jQuery 1.5, it's undefined for me, whereas $.active works.



来源:https://stackoverflow.com/questions/5469722/selenium-waiting-for-ajax-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!