【问题标题】:Selenium test failing headless, succeeding in firefoxSelenium 测试失败,在 Firefox 中成功
【发布时间】:2015-04-08 09:41:52
【问题描述】:

我正在运行 Selenium 自动化测试来浏览向导。每个步骤都有一个阶段计数器来跟踪当前步骤。我断言等于(在屏幕上)隐藏的舞台元素,以断言我在正确的页面上。

在向导的每个页面上,都需要填写一些内容,例如用户名。然后点击next进入下一页,stage counter增加1。

最后一个阶段是第 4 阶段,当提交最后一个阶段时,您会被重定向到一个新的向导,其中阶段计数再次为 1。

向导显示在 Twitter Bootstrap 3 模式窗口中。

当我在无头模式下运行测试时断言此新向导中的阶段计数为 1 失败。 它仍然是 4。我在这里也找不到任何元素。因此,在无头模式下,此页面似乎未正确加载。 每次单击后,我已经尝试过 Thread.sleep(5000) 以确保它正确加载。我还尝试等到元素出现后再断言阶段计数更改为 1。 结果是 No such element exception。

所以我的问题是,无头模式有什么不同会导致这种情况发生?

    driver.findElement(By.xpath("ElementImLookinFor")).clear();
    driver.findElement(By.xpath("ElementImLookinFor")).sendKeys("test");
    driver.findElement(By.name("submit")).click();
    stageElementCheck("4");
    driver.findElement(By.xpath("TermsOfUseElement")).click();
    driver.findElement(By.name("submit")).click();

    stageElementCheck("1"); //expected 1 was 4

    driver.findElement(By.xpath("ElementImLookinFor")).click();
    driver.findElement(By.name("submit")).click();


public void stageElementCheck(String i){
      assertTrue(isElementPresent(By.xpath("HiddenStageElement")));
      assertEquals(i,driver.findElement(By.xpath("HiddenStageElement")).getAttribute("value"));
  }

实现无头:

driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24);
        ((HtmlUnitDriver) driver).setJavascriptEnabled(true);
        Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        driver.manage().timeouts().implicitlyWait(timeoutSeconds, TimeUnit.SECONDS);

【问题讨论】:

  • 你能分享你的代码吗?
  • 您使用什么设置在“无头模式”下运行测试?实现“无头模式”的方法有很多种。 (使用此信息编辑您的问题。)

标签: java twitter-bootstrap selenium


【解决方案1】:

我建议你使用像 Xvfb x server 这样的工具在无头模式下运行 selenium UI 测试。它易于配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    相关资源
    最近更新 更多