Jmeter WebDriverSampler失败,Chromedriver无头

问题描述 投票:1回答:1

我在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();

我希望有人可以帮我解决这个问题,因为我需要使用无头选项。非常感谢您的宝贵时间。

selenium-webdriver jmeter selenium-chromedriver jmeter-plugins google-chrome-headless
1个回答
0
投票
  1. 您可以使用以下函数将页面源打印到jmeter.log fileWDS.log.info(WDS.browser.getPageSource())
  2. 甚至将其保存到单独的文件中,如: org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File('test.html'), WDS.browser.getPageSource())
  3. 或者take screenshot失败如: 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帐户启动浏览器

© www.soinside.com 2019 - 2024. All rights reserved.