我在Jmeter中使用WebDriverSampler进行了一些测试,可以正常使用chromedriver。它是一个selenium脚本,它打开一个网页并检查它是否包含一系列元素。一切正常,直到我尝试了chromedriver无头选项。
在这种情况下,我得到异常“预期条件失败:等待位于以下位置的元素的存在:By.xpath:...”,好像该元素尚不存在尚未加载。我不知道会发生什么,因为如果我停止使用无头选项,如果一切正常并找到真正存在的元素。
这是使用的代码示例(它没有无头选项):
var wait = new support_ui.WebDriverWait(WDS.browser, 30);
var conditions = org.openqa.selenium.support.ui.ExpectedConditions
WDS.sampleResult.sampleStart();
WDS.sampleResult.getLatency();
WDS.browser.get('http://mi-app/');
try{
wait.until(conditions.presenceOfElementLocated(pkg.By.xpath('/ruta_de elemento_existente')));
WDS.log.info('OK')
}catch(e){
WDS.sampleResult.setSuccessful(false);
WDS.sampleResult.setResponseMessage('Fail');
WDS.log.error(e.message)
}
try{
wait.until(conditions.presenceOfElementLocated(pkg.By.xpath('/ruta_de elemento2_existente')));
WDS.log.info('OK2')
}catch(e){
WDS.sampleResult.setSuccessful(false);
WDS.sampleResult.setResponseMessage('Fail2');
WDS.log.error(e.message)
}
WDS.sampleResult.sampleEnd();
我希望有人可以帮我解决这个问题,因为我需要使用无头选项。非常感谢您的宝贵时间。
WDS.log.info(WDS.browser.getPageSource())
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File('test.html'), WDS.browser.getPageSource())
WDS.browser.getScreenshotAs(org.openqa.selenium.OutputType.FILE).renameTo(new java.io.File('test.png'))
查看The WebDriver Sampler: Your Top 10 Questions Answered文章了解更多信息。另请注意,如果您运行Selenium测试的机器没有GUI,您仍然可以正常使用Linux上的Xvfb或Windows上的Local System帐户启动浏览器