Webdriver采样器WebDriverWait占用的是隐式时间,而不是显式时间



在Jmeter webdriver中,当我尝试使用显式时间等待元素时,wait语句占用了浏览器的隐式时间。在下面的代码中,我将浏览器等待时间设置为60秒,但在等待功能中,我只通过了20秒,条件就失败了。但是等待语句花了整整60秒才使失败

var selenium = org.openqa.selenium
var time = JavaImporter(java.util.concurrent.TimeUnit)
WDS.browser.manage().timeouts().implicitlyWait(60, time.TimeUnit.SECONDS)
var support_ui=JavaImporter(org.openqa.selenium.support.ui) 
var EC = org.openqa.selenium.support.ui.ExpectedConditions
WDS.sampleResult.sampleStart()
WDS.log.info("Load the application");
WDS.sampleResult.subSampleStart('Load the application')
WDS.browser.get('https://ea-webapp-int.xxx.com/getting-started')
WDS.log.info("Pass 1")
try {
var Disc_Agree_button = waitByElement(selenium.By.xpath("//button/span[text()='Agree & Login']"), EC.presenceOfElementLocated, 20)
WDS.log.info("Pass 2")
if (undefined !== Disc_Agree_button && Disc_Agree_button.length) {
WDS.log.info('Disclaimer screen displayed and clicking on Agree button')
Disc_Agree_button.click()
}else{
WDS.log.info('Disclaimer screen not displayed')
}
} catch (error) {
WDS.log.info(error)
WDS.log.info('Disclaimer screen not displayed')
}
WDS.sampleResult.subSampleEnd(true)
WDS.sampleResult.sampleEnd()
function waitByElement(elementBy, conditionToExpect, timer){
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
WDS.log.info(conditionToExpect)
try{
var wait = new support_ui.WebDriverWait(WDS.browser,timer)
element = wait.until(conditionToExpect(elementBy));
return element;
}catch (error){
WDS.log.info(error)
}
}

如果您检查下面控制台输出中的时间戳,我们可以注意到第一次通过和第二次通过之间的时间花费了60秒。

2022-03-29 17:04:41,737 INFO c.g.j.p.w.s.WebDriverSampler: Load the application 
2022-03-29 **17:04:45,689 INFO c.g.j.p.w.s.WebDriverSampler:Pass 1** 
2022-03-29 17:05:45,875 INFO c.g.j.p.w.s.WebDriverSampler: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath:
//button/span[text()='Agree & Login'] (tried for 20 second(s) with 500
milliseconds interval) 
2022-03-29 **17:05:45,876 INFO c.g.j.p.w.s.WebDriverSampler: Pass 2** 
2022-03-29 17:05:45,877 INFO c.g.j.p.w.s.WebDriverSampler: Disclaimer screen not displayed

您尝试过阅读Selenium关于等待的文档吗?

默认情况下,隐式等待元素出现是禁用的,需要在每个会话的基础上手动启用。混合显式等待和隐式等待将导致意外后果,即即使元素可用或条件为true,也要等待睡眠最长时间

警告:不要混合隐式和显式等待。这样做可能会导致不可预测的等待时间。例如,设置10秒的隐式等待和15秒的显式等待可能会导致20秒后发生超时。

此外,您会默默地忽略catch块中的所有异常,也许值得在处理错误后将其抛出,而不是抑制。

你可能会发现《WebDriver Sampler:Your Top 10 Questions Answered》这篇文章也很有用。

最新更新