[英]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.