Jmeter WebDriverSampler因Chromedriver无头而失败

时间:2019-02-27 07:49:10

标签: selenium-webdriver jmeter selenium-chromedriver jmeter-plugins google-chrome-headless

我在Jmeter中使用WebDriverSampler进行了一些测试,这些测试可与chromedriver一起正常使用。它是一个硒脚本,可打开网页并检查其是否包含一系列元素。一切正常,直到我尝试使用chromedriver无头选项为止。

在这种情况下,我得到异常“预期条件失败:等待元素的存在:By.xpath:...”,好像该元素尚不存在。我不知道会发生什么,因为如果我停止使用无头选项,那么如果一切正常,并找到真正存在的元素。

这是所用代码的一个示例(不带headless选项即可使用):

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

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

1 个答案:

答案 0 :(得分:0)

  1. 您可以使用以下功能将页面源打印到jmeter.log file

    WDS.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帐户启动浏览器