WebDriver Example in Java with ScreenShot

WebDriver Example in Java with ScreenShot

Here is an example of a JMeter + WebDriver test written in Java.  Comments in-line.

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

1 – We select Java as the language

2 – Optionally you can pass parameters into the script via Parameters.

 

Screen Shot 2015-11-10 at 5.50.55 PM

Example Code

// We import File, FileUtils for creating screenshots and storing them disk
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.util.Random;
import openqa.selenium.OutputType.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;

// Required to start counting time which will get reported at the end of this code.
WDS.sampleResult.sampleStart();

// We could have passed this in, just coding in the example
String baseUrl = "http://www.plexify.com";

// Load the home page
WDS.browser.get(baseUrl + "/");

// Find a css selector by ID and Click it
WDS.browser.findElement(By.id("plexSelector")).click();

// Find another element and send in the keys for our search box
WDS.browser.findElement(By.id("plexSelector")).sendKeys( new String[] { "iphone" } );

// We wait for the Ajax data to respond and update part of the page. 
for (int second = 0;; second++) {
  if (second >= 300 ){
        WDS.sampleResult.sampleEnd();
        WDS.sampleResult.setSuccessful(false);
        WDS.sampleResult.setResponseMessage("Failed to load page, too slow!");
        return;
    }
  // We are looking for some text in the drop down
  try { 
      if ( WDS.browser.findElement(By.xpath("//a[contains(text(),'openSourceIPhoneApps')]"))!= null ) break; 
     } catch (Exception e) {}
  Thread.sleep(1000);
}

// The random is because we don't need every single screen shot during our test. 
// If we ran 100 users, 10 iterations, on 5 machines we would end up with 100*10*5 screenshots. too much. 
// Most webdriver tests take screenshots during failures, we are just showing how here. 
Random r = new Random();
if ( r.nextInt(10) == 0 ){
 File mFile = ((TakesScreenshot)WDS.browser).getScreenshotAs(OutputType.FILE);

 // To have the file returned or displayed on RedLine13 load test we need to put the screenshot in output/
 // We have access to WDS.parameters in which we passed through the current thread-loopcounter for unique filenames.
 FileUtils.copyFile(mFile, new File("output/" + WDS.name + "-" + WDS.parameters + ".png"));
}

// Do a little more 
WDS.browser.findElement(By.xpath("//a[contains(text(),'openSourceIPhoneApps')]")).click();
WDS.browser.findElement(By.id("plexSelector")).sendKeys(new Keys[]{Keys.ENTER});

// End the test sampler, capturing length of time for the test.
WDS.sampleResult.sampleEnd();

1 Comment


Comments are closed.