0

我有一个用 Java 编写的 Selenium Web 驱动程序测试,针对 Liferay 站点。

// Login
driver.get(baseUrl + "/");
driver.findElement(By.id("_58_login")).sendKeys(login);
driver.findElement(By.id("_58_password")).sendKeys(password);
driver.findElement(By.xpath("//input[@value='Sign In']")).click();

// Try to navigate to dashboard and expect error
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Liferay")));
driver.get(baseUrl + "/user/tm2/so/dashboard");
driver.findElement(By.xpath("//h3[contains(., 'Not Found')]"));

问题是间歇性地(大约 50% 的时间),测试失败并出现异常org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//h3[contains(., 'Not Found')]"}

在测试期间观察 Web Driver 的浏览器行为时,会发生登录例程按预期工作,单击“登录”按钮。但是一旦页面到达登录页面,它就会挂起 30 秒(我的隐式超时设置),并抛出异常。

我怀疑driver.get(baseUrl + "/user/tm2/so/dashboard")电话以某种方式被跳过了。你可以看到我已经设置了一个 WebDriverWait 来试图阻止 driver.get 导航发生,直到登录例程完成,但这似乎没有帮助。

有任何想法吗?

更新:我发现问题与 Selenium 无关 - 这是应用程序本身的问题,有时导航到 URL 只会刷新当前页面。

4

1 回答 1

1

没问题,把代码改成这样:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, SECONDS)
           .pollingEvery(5, SECONDS)
           .ignoring(NoSuchElementException.class, ElementNotFoundException.class);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Liferay")));
driver.get(baseUrl + "/user/tm2/so/dashboard");
driver.findElement(By.xpath("//h3[contains(., 'Not Found')]"));
于 2013-09-13T20:02:26.437 回答