繁体   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