【问题标题】:Timeout after opening the URL打开网址后超时
【发布时间】:2013-02-17 01:34:39
【问题描述】:

我正在将 Selenium RC 与 Junit 一起使用。

运行以下简单脚本后,我收到超时错误。

com.thoughtworks.selenium.SeleniumException: 30000 毫秒后超时

我已经记录了使用 IDE 可以正常工作的 IDE 脚本。当使用 Junit 格式格式化相同的代码并尝试通过 Eclipse 和 Junit 运行时,它会出现上述超时错误。

package script;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class ComparePrice extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://www.landmarkonthenet.com/", "*firefox");
    }
    public void testComparePrice() throws Exception {
        selenium.open("http://www.landmarkonthenet.com/");
        selenium.click("link=Books");
        selenium.waitForPageToLoad("60000");
        selenium.type("id=TopSearch", "junit");
        selenium.click("css=button[type=\"submit\"]");
        selenium.waitForPageToLoad("60000");
        selenium.click("xpath=(//a[contains(text(),'Desc')])[2]");
        selenium.waitForPageToLoad("60000");
        String P1 = selenium.getText("xpath=/html/body[@id='department']/div[@id='page-body']/div[@id='main-content']/div[@id='page-content']/div[3]/div[1]/article/div[2]/p/span[1]");
        System.out.println(P1);
        String P2 = selenium.getText("xpath=/html/body[@id='department']/div[@id='page-body']/div[@id='main-content']/div[@id='page-content']/div[3]/div[2]/article/div[2]/p/span[2]");
        System.out.println(P2);
        String T3 = selenium.getEval("var A = Number(\"" + P1 + "\".substr(3).replace(/,/g,'')); var B= Number(\"" + P2 + "\".substr(3).replace(/,/g,'')); var c = false; if(A>=B) C=true; C");
        System.out.println(T3);
    }
}

【问题讨论】:

    标签: junit rc


    【解决方案1】:

    您可以尝试以下方法-> 1. 当您尝试浏览应用程序时。找出应用程序中的一些静态元素,并使用 For 循环和内部循环应用动态等待控制,检查该元素是否存在。

    for (int second = 0;; second++) {
            if (second >= 160) fail("timeout");
            try { if (selenium.isTextPresent(" Web Element on the page")) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }
    

    【讨论】:

      【解决方案2】:

      而不是使用 selenium.waitForPageToLoad("60000");

      每次点击后尝试使用以下内容

      for (int second = 0;; second++) {
                  if (second >= 60) Assert.fail("timeout");
                  try { if (selenium.isVisible("next element")) break; } catch (Exception e) {}
                  Thread.sleep(1000);
              }
      

      然后您将能够找出其超时错误的确切位置 例如

      public void testComparePrice() throws Exception {
      
      selenium.open("http://www.landmarkonthenet.com/");
      
      selenium.click("link=Books");
      
      for (int second = 0;; second++) {
          if (second >= 60) Assert.fail("timeout");
          try { if (selenium.isVisible("id=TopSearch")) break; } catch (Exception e) {}
          Thread.sleep(1000);
              }
      
      selenium.type("id=TopSearch", "junit");
      selenium.click("css=button[type=\"submit\"]");
      

      等等

      【讨论】:

        【解决方案3】:

        将 Selenium RC 独立 jar 更新到最新:http://docs.seleniumhq.org/download/2.31.0

        使用 waitForElementToPresent 而不是 waitForPageToLoad 加载 URL 后,放置一些等待您期望出现的下一个元素

        使用windowMaximize 以获得更好的视图。

        如果您的完整会话超时,请使用setTimeOut

        【讨论】:

          【解决方案4】:

          尝试使用 Selenium Remote Control 1.0.2 你可以在这里Selenium Remote Control 1.0.2阅读它并在那里下载jar:

          【讨论】:

          • 1.我用 Selenium RC 1.0.2 试过了。仍然遇到同样的问题。 2. 另一个问题我要注意的是,当我运行脚本时,它会打开两个浏览器,一个是 rc 服务器,另一个是我在脚本中使用的测试 url。谁能告诉我第一个浏览器的目的是什么,即显示一些 RC 服务器详细信息。我们如何避免它
          【解决方案5】:

          你在哪一行得到超时错误? 通常是当 selenium 无法识别 DOM 中的元素时,请检查您的 xpaths 是否返回任何内容。 我建议您使用 xpath finder: https://addons.mozilla.org/en-us/firefox/addon/xpath-finder/

          【讨论】:

          • 当我跑到课堂上时。它只是打开站点landmarkonthenet.com 并在几秒钟后关闭浏览器并给出上述错误。按照步骤,它应该单击 Books 链接并重定向到以下页面 landmarkonthenet.com/books.. 这不会发生。我检查了链接(书籍)的 Xpath,即 /html/body[@id='home']/header/div[2]/div[@id='mainnav']/nav[@id='browse ']/ul/li[1]/a
          • 使用哪个 Firefox 版本?请注意,有时 Selenium 能够正确打开浏览器并执行导航,例如打开网站,但是当识别 DOM 元素失败时。
          猜你喜欢
          • 1970-01-01
          • 2018-11-13
          • 2014-11-26
          • 2019-12-02
          • 2013-07-13
          • 2011-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多