设置firefoxWebDriver.get(...)超时时间。

3

我希望能够访问一些不受我的控制的页面。这些页面可能会执行一些缓慢的GET请求,但主要的HTML已经完全加载并显示。我尝试了很多选项,但都无法解决。在某些网站上,firefoxWebDriver.get(...)在现实时间内无法终止。

为了重现问题,我编写了这个小的单元测试来展示问题:

public class Timeout  {

    private FirefoxDriver   driver;

    @Before
    public void setup() {
        final FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("dom.max_script_run_time", 0);
        profile.setPreference("webdriver.load.strategy", "fast");

        this.driver = new FirefoxDriver(profile);

//      this.driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
//      this.driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);

    }

    @Test(timeout = 15000)
    public void shouldRetriveREDCAFEPageQuiteFast() {
        this.driver.get("http://redcafe.vn/Home/su-kien-binh-luan/kagawa-tu-choi-mac-ao-so-7");
    }

    @Test(timeout = 15000)
    public void shouldRetriveMUFCPageQuiteFast() {
        this.driver.get("http://news.mufc.vn/detail/172-hoan-tat-giay-phep-lao-dong-m-u-chinh-thuc-so-huu-kagawa.html");
    }

    @After
    public void tearDown() {
        this.driver.close();
    }
}

感谢您的帮助。
1个回答

2
<driver>.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 

这将把页面加载超时设置为60秒,超过这个时间就会抛出一个错误。在第一次调用get()之前需要设置它。

该API从Webdriver 2.20.0版本开始支持。

有关新的Timeout API,请参阅API参考


嗨,Ashwin Prabhu,这并不能解决问题。正如您所见,这个选项已经在构造函数中进行了测试(当前已注释掉)。遗憾的是,我们的建议仍然无法通过单元测试。 - d0x

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接