Selenium RC 中 FireFox 和 IE 中的页面加载

发布于 2024-09-03 01:07:30 字数 815 浏览 3 评论 0原文

我在 Selenium RC 中遇到了奇怪的问题。当我使用 IE7 时,测试工作正常。但是,如果我使用 firefox 3.5,Selenium 会在页面完全加载之前获取页面。使用线程睡眠可以解决这个问题。

IE 和 FireFox 中如何处理页面加载标志有区别吗?

selenium.setTimeout(Timeout);
selenium.open("http://localhost");
selenium.waitForPageToLoad(Timeout);
selenium.windowMaximize();
selenium.windowFocus();
Thread.sleep(60000);
selenium.type("//html/body/table/tbody/tr[2]/td/div/form/table/tbody/tr[1]/td/table/tbody/tr[3]/td[2]/input","test");   
selenium.type("pwd","test");   
selenium.click("submit");
selenium.waitForPageToLoad(Timeout);        
Thread.sleep(60000);
System.out.println(selenium.getLocation());
System.out.println(selenium.getHtmlSource());
String[] ro=selenium.getAllLinks();
System.out.println("-----"+ro.length);

在 IE7 中,一切都与 thread.sleep 一起工作,但在 FireFox 中则不然。

I've met with strange issue in Selenium RC. When I use IE7 the test works fine. However if I use firefox 3.5, Selenium fetches the page before it completely loaded. Using a thread sleep resolves that matter.

Is there a difference between, how page load flag handled in IE and FireFox?

selenium.setTimeout(Timeout);
selenium.open("http://localhost");
selenium.waitForPageToLoad(Timeout);
selenium.windowMaximize();
selenium.windowFocus();
Thread.sleep(60000);
selenium.type("//html/body/table/tbody/tr[2]/td/div/form/table/tbody/tr[1]/td/table/tbody/tr[3]/td[2]/input","test");   
selenium.type("pwd","test");   
selenium.click("submit");
selenium.waitForPageToLoad(Timeout);        
Thread.sleep(60000);
System.out.println(selenium.getLocation());
System.out.println(selenium.getHtmlSource());
String[] ro=selenium.getAllLinks();
System.out.println("-----"+ro.length);

In IE7 everything works with the thread.sleep, not in FireFox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-09-10 01:07:30

在发送 Selenium 命令之前,页面加载时运行的任何 JavaScript 可能尚未完成。例如,如果您尝试输入的输入最初被禁用,那么 Selenium 可能会在启用它的 JavaScript 完成之前尝试输入该输入。

如果是这种情况,您可以尝试在测试中添加适当的 waitForCondition 命令,一旦满足条件,这将不断重新评估并继续,这比任意睡眠要好得多。

以下示例将等待某个元素启用:

selenium.waitForCondition("var value = selenium.isEditable('id=myElement'); value == true", "60000");

我还建议检查您的定位器,因为它目前是一个非常脆弱的 XPath,并且对 Web 应用程序的 HTML 源代码进行轻微更改就可能会破坏您的测试。尝试查找 id 属性并创建相对于该属性的 xpath。

例如,如果您的最终表的 idmyTable,您可以使用:

xpath=id('myTable')//tr[3]/td[2]/input

It could be that any JavaScript that runs on page load hasn't completed before your Selenium command is sent. For example, if the input you are attempting to type into is disabled initially then Selenium might be attempting to type into it before the JavaScript that enabled it has completed.

If this is the case, you could try adding an appropriate waitForCondition command in your test, this will continually re-evaluate and continue once the condition is met, which is much better than an arbitrary sleep.

The following example would wait for an element to become enabled:

selenium.waitForCondition("var value = selenium.isEditable('id=myElement'); value == true", "60000");

I would also recommend reviewing your locator, as it's currently a very brittle XPath, and just a slight change to your web application's HTML source could break your test. Try looking for an id attribute and creating your xpath relative to that.

For example if your final table had an id of myTable you could use:

xpath=id('myTable')//tr[3]/td[2]/input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文