簡體   English   中英

執行期間檢查WebDriver測試中的加載時間

[英]Check loading time in WebDriver test during execution

我使用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

如果我理解正確,你可以減少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
}

我認為你正在尋找API測試,而不是自動化測試。

Postman API測試設置和使用教程

希望這會有所幫助

編輯:

或者,更輕量級的API測試解決方案:

在線API測試器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM