[英]Check loading time in WebDriver test during execution
I use Selenium WebDriver 3.14
and test is executed in Chrome
browser. 我使用
Selenium WebDriver 3.14
并在Chrome
浏览器中执行测试。 I need to measure response time of a page in execution time to check it is under a predefined value. 我需要在执行时间内测量页面的响应时间,以检查它是否在预定义的值下。 If it is greater than this value some additional actions should be done.
如果它大于此值,则应执行一些其他操作。 So I need different solution than
System.currentTimeMillis()
, because check of this value should be done automatically in background. 所以我需要与
System.currentTimeMillis()
不同的解决方案,因为这个值的检查应该在后台自动完成。 It is an AJAX like window, so when loading takes too long time, it should be closed by script. 它是一个类似于AJAX的窗口,因此当加载时间过长时,应该通过脚本关闭它。 Window example:
窗口示例:
The typical solution to this is a try/catch against a wait. 对此的典型解决方案是针对等待的尝试/捕获。 Eg if the next step is to click a button that shows once loading completes:
例如,如果下一步是单击加载完成后显示的按钮:
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();
However, there is a common problem if you are using implicit timeouts in Selenium. 但是,如果在Selenium中使用隐式超时,则会出现一个常见问题。 This doesn't work too well, particularly if the implicit timeout is longer than the LOADING_TIMEOUT, as this slows down the polling cycle in the
wait.until()
. 这不能很好地工作,特别是如果隐式超时比LOADING_TIMEOUT长,因为这会减慢
wait.until()
的轮询周期。
In this case, the simplest solution is to temporarily reduce the implicit timeout: 在这种情况下,最简单的解决方案是暂时减少隐式超时:
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
If I understand correctly, you could decrease time in selenium.waitForPageToLoad("100000");
如果我理解正确,你可以减少
selenium.waitForPageToLoad("100000");
to a wanted predefined value, let us say 20 seconds. 对于想要的预定义值,让我们说20秒。 So if you want the page loading to stop if it is not loaded in 20 seconds, try something like this:
因此,如果您希望页面加载在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.");
}
You should try setting Logging Preferences through capability CapabilityType.LOGGING_PREFS
for performance-log . 您应该尝试通过功能
CapabilityType.LOGGING_PREFS
为性能日志设置日志记录首选项 。
For example: 例如:
LoggingPreferences logs = new LoggingPreferences();
logs .enable(LogType.PERFORMANCE, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logs);
you can get performance log entries as below. 您可以获得如下的性能日志条目。
for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)) {
System.out.println(entry.toString());
//do the needful
}
I think you're looking for API testing not Automation Testing. 我认为你正在寻找API测试,而不是自动化测试。
Postman API Testing Setup and Usage Tutorial Postman API测试设置和使用教程
Hopefully this will help 希望这会有所帮助
edit: 编辑:
Alternatively, a more lightweight solution for API testing: 或者,更轻量级的API测试解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.