我使用 Selenium WebDriver 3.14
并在 Chrome
浏览器中执行测试。我需要在执行时间内测量页面的响应时间,以检查它是否低于预定义值。如果它大于此值,则应执行一些额外的操作。所以我需要不同于 System.currentTimeMillis()
的解决方案,因为这个值的检查应该在后台自动完成。它是一个类似于 AJAX 的窗口,因此当加载时间过长时,应该通过脚本将其关闭。窗口示例:
最佳答案
对此的典型解决方案是尝试/捕获而不是等待。例如。如果下一步是单击加载完成后显示的按钮:
WebDriverWait wait = new WebDriverWait(driver, LOADING_TIMEOUT);
WebElement webElement;
try {
webElement = wait.until(elementToBeClickable(By.id(id)));
} catch (TimeoutException ex) {
// Close loading window
return;
}
webElement.click();
但是,如果您在 Selenium 中使用隐式超时,则存在一个常见问题。这不太有效,特别是如果隐式超时比 LOADING_TIMEOUT 长,因为这会减慢 wait.until()
中的轮询周期。
在这种情况下,最简单的解决方案是暂时减少隐式超时:
WebDriverWait wait = new WebDriverWait(driver, LOADING_TIMEOUT);
WebElement webElement;
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
webElement = wait.until(elementToBeClickable(By.id(id)));
} catch (TimeoutException ex) {
// Delay any further interaction until the timeout has been restored
webElement = null;
} finally {
driver.manage().timeouts().implicitlyWait(DEFAULT_TIMEOUT,
TimeUnit.SECONDS);
}
if (webElement != null)
webElement.click();
else
// Close loading window
关于java - 在执行期间检查 WebDriver 测试中的加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533265/