WebDriver Example Javascript with ScreenShot

WebDriver Example Javascript with ScreenShot

Here is an example of a JMeter + WebDriver test written in JavaScript.  JMeter uses the Java WebDriver. This is a bridge between Javascript and you are scripting java – http://jmeter.apache.org/usermanual/functions.html#__javaScript

In JMeter the driver is passed into the test.

WDS object passed in

Specifically we are provided an object called WDS with the following properties.  (from the docs).   Take special notice of WDS.browser as this is the WebDriver we use to drive our test.

  1. WDS.name – is the value provided in the Name field (above).
  2. WDS.parameters – is the value provided in the Parameters field (above).
  3. WDS.args – is an array of the strings provided in the Parameters field, but split by the space ‘ ‘ character. This allows the scripter to provide a number of strings as input and access each one by position.
  4. WDS.log – is a Logger instance to allow the scripter to debug their scripts by writing information to the jmeter log file (JMeter provides a GUI for its log entries)
  5. WDS.browser – is the configured Web Driver browser that the scripter can script and control. There is detailed documentation on this object on the Selenium Javadocs page.
  6. WDS.sampleResult – is used to log when the timing should start and end. Furthermore, the scripter can set success/failure state on this object, and this SampleResult is then used by the JMeter reporting suite. The JMeter javadocs provide more information on the API of this object.

WDS.browser is the WebDriver we will use through the test to execute the steps.

Example Results

JMeter Sampler

  • Selecting javascript as the language
  • Passing in some information via Parameters
  • Screen Shot 2015-11-10 at 5.50.55 PM

Code with comments

// Importing packages (and all classes in package) from Java into Javascript
var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)

// We don't use wait in this very simple test, but here is way to access for more realistic testing
var wait=new support_ui.WebDriverWait(WDS.browser, 5)

// Start recording the time for this request
WDS.sampleResult.sampleStart();

// Let's get a page
WDS.browser.get("https://www.runsignup.com/")

// We don't record every screenshot as that would be a waste, just a single sample here.
if ( WDS.args[0] == 1 && WDS.args[1] == 1 ){
 // Simple to get the screenshot (full screen shot top to bottom)
 var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE)
 // write to disk, note in RedLine13 if you want the file back or rendered on screen it requires outpupt/ as the path
 screenshot.renameTo(new java.io.File("output/home-" + WDS.args[0] + "-" + WDS.args[1] + ".png" ))
}

// Record the time of the request
WDS.sampleResult.sampleEnd();

 

 

 

 

1 Comment


Comments are closed.