3

I have some tests with WebDriverSampler in Jmeter that work correctly with chromedriver. It is a selenium script that opens a web page and checks that it contains a series of elements. Everything works right until I've tried with the chromedriver headless option.

In this case I get the exception "Expected condition failed: waiting for presence of element located by: By.xpath: ..." as if that element did not exist yet to be loaded. I do not know what can happen, because if I stop using the headless option, if everything works correctly and find the element that really exists.

This is an example of code used(it works without the headless option):

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

I hope someone can help me with this problem, because I need to use the headless option. Thank you very much for your time.

Esther_5
  • 151
  • 2
  • 10

1 Answers1

0
  1. You can print the page source to jmeter.log file by using the following function:

    WDS.log.info(WDS.browser.getPageSource())
    
  2. Or even save it into a separate file like:

    org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File('test.html'), WDS.browser.getPageSource())
    
  3. Or take screenshot on failure like:

    WDS.browser.getScreenshotAs(org.openqa.selenium.OutputType.FILE).renameTo(new java.io.File('test.png'))
    

    Check out The WebDriver Sampler: Your Top 10 Questions Answered article for more information.


Also be aware that if the machine where you run your Selenium tests doesn't have GUI you can still normally launch browsers using i.e. Xvfb on Linux or under Local System account on Windows

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for the reply, I've already tried that. But my problem is I do not understand because if I locate the elements in normal mode, and when I use the headless mode the exception that is jumps. What is the difference? If the page is the same and checked in the normal way that the elements exist and the xpath is correct. Thank you. – Esther_5 Feb 27 '19 at 09:33