[英]Check loading time in WebDriver test during execution
对此的典型解决方案是针对等待的尝试/捕获。 例如,如果下一步是单击加载完成后显示的按钮:
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
如果我理解正确,你可以减少selenium.waitForPageToLoad("100000");
对于想要的预定义值,让我们说20秒。 因此,如果您希望页面加载在20秒内未加载时停止,请尝试以下操作:
long start = System.currentTimeMillis();
try {
selenium.waitForPageToLoad("20000");
System.out.println("The page load is too long!");
} catch {
long timeToLoad= (System.currentTimeMillis()-start);
System.out.println("The page loaded in " +timeToLoad+ " seconds.");
}
您应该尝试通过功能CapabilityType.LOGGING_PREFS
为性能日志设置日志记录首选项 。
例如:
LoggingPreferences logs = new LoggingPreferences();
logs .enable(LogType.PERFORMANCE, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logs);
您可以获得如下的性能日志条目。
for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)) {
System.out.println(entry.toString());
//do the needful
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.