执行期间检查WebDriver测试中的加载时间

pla*_*irt 8 java selenium selenium-webdriver

Selenium WebDriver 3.14Chrome浏览器中使用和测试.我需要在执行时间内测量页面的响应时间,以检查它是否在预定义的值下.如果它大于此值,则应执行一些其他操作.所以我需要不同的解决方案System.currentTimeMillis(),因为这个值的检查应该在后台自动完成.它是一个类似于AJAX的窗口,因此当加载时间过长时,应该通过脚本关闭它.窗口示例:

示例窗口

df7*_*899 6

对此的典型解决方案是针对等待的尝试/捕获.例如,如果下一步是单击加载完成后显示的按钮:

    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();
Run Code Online (Sandbox Code Playgroud)

但是,如果在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
Run Code Online (Sandbox Code Playgroud)