Tips for those who use Selenium – WebDriver Sampler with JMeter

Many people find it very challenging to convert their normal Selenium Tests scripts to adhere the WebDriver Samplers in JMeter. If you are very much used to the fact of writting Selenium scripts in a day to day basis, writing them on the WebDriver Sampler can not be very tedious. I faced a few challenges in the use of Selenium in JMeter when working on client projects. Following are 5 of those challenges and ways on how I over came them.

  1. Overcome unwanted crashes due to delay in page load
    The test scripts usually tend to fail when the Selenium Driver tries to search for a WebElement way before the page could completely load. For this we usually use the ExplicitWait() function in the normal selenium scripting world. In the WebDriver Sampler you could use the following.

    var pkg = JavaImporter(org.openqa.selenium)
    var support_ui = JavaImporter(
    var ui = JavaImporter(
    var wait = new support_ui.WebDriverWait(WDS.browser, 7000)
    //Here 7000 is the amount of milliseconds that code wait till it sees the specified username element


  2. Can screenshots be taken for my JMeter Selenium Tests? Yes, with unique names by using parameters.
    You would like to take screenshots of your tests and verify them at the end of the test as the JMeter tests run so quickly and you can have it part of your test execution. This can be achieved in the following manner.

    var fileUtils = JavaImporter(
    var io = JavaImporter(
    var location = 'D:/Users/cthalayasingam/Documents/jmeterTest/'
    var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE)
    fileUtils.FileUtils.copyFile(screenshot, new io.File(location+'messages1.png'))
  3. Can we use the JMeter User Defined Variables or JMeter Functions in the Selenium WebDriver Sampler Script? Yes, Can be achieved in the following ways (the first method is more preferred)
    If the JMeter variable is called cDate and you need to use it in send keys or some specific place in the code as shown below.


    The other is to mention the variable in the parameters field and then call it as shown in the following manner.

    Screen Shot 2017-08-31 at 12.40.52

  4. Checking if an element is present or displayed.
    WDS.browser.findElement(pkg.By.ByXPath("//div[contains(@class,'column2')]//div[contains(@class, 'dashlet')]//div[contains(.,'Resend Messages')][1]")).isDisplayed())
  5. Logging info messages.'Messages all are deleted');
  6. How can we perform Drag And Drop function?
    var actions = new org.openqa.selenium.interactions.Actions(WDS.browser)
    wait.until(ui.ExpectedConditions.visibilityOfElementLocated(pkg.By.ByXPath("//ul[@class='availableList']//div[contains(@title,'Resend Messages')]")))
    var resendDashlet = WDS.browser.findElement(pkg.By.ByXPath("//ul[@class='availableList']//div[contains(@title,'Resend Messages')]"))
    var col2 = WDS.browser.findElement(pkg.By.ByXPath("//ul[@id='template_x002e_customise-dashlets_x002e_customise-user-dashboard_x0023_default-column-ul-2']"))
    //the element resendDashlet needs to be dragged and dropped into col2


There are many out there who had a few queries on how can the above be achieved. Hope the above tips were helpful. If there are more will keep updating this page or create more.

Uploading a document from same source document with jMeter.

This is another script that you can use to upload the same document to your system using jMeter. This is another way of doing as I have mentioned here.

import java.text.*;
import java.lang.*;

//Take the fileLocation from the user defined variable
//fileLocation = “/Users/cthalayasingam/Documents/jmeterTest/PerformanceTests/”;
fileLocation = vars.get(“fileCreatedPath”);
// The string portrays the current location of the document (Source file)
mainFile = “images.png”;
//The string portrays the location of the document which is renamed.
start = System.currentTimeMillis();
fileName = start + “images.png”;
//Save the name of the document with the time so that can be DELETED later
vars.put(“fileName1”, fileName);
//Get current document location
File file = new File( fileLocation + mainFile);
//Create location for renamed location
File file2 = new File( fileLocation + fileName);
//Copy the file to the document to the new location
FileUtils.copyFile(file, file2);

After you have entered the above script in the Bean Shell pre processor. Then put the script in the delete script found below in a BeanShell Post Processor, that applies the delete for the document you have copied to the new destination.

import java.text.*;
import java.util.*;
import java.lang.*;

csvDir = vars.get(“fileCreatedPath”);
fileName2 = vars.get(“fileName1”);
File file1 = new File( csvDir + fileName2);
if (file1.exists()) {

Deleting a particular CSV file using Jmeter

You might come across situations where you will have to delete a document before running a process. Say you have a situation where you have to create a document with all the data in that is needed for the particular test. Every time you run the test you would need create new documents. For that case you would need to delete the previously created document. If that is the case how do you delete the document. Following is the way to do so.

  1. Initially Add a BeanShell PreProcessor in the start of the Thread if you need to Delete the document in the beginning of the Thread.Screen Shot 2016-04-11 at 11.24.58Screen Shot 2016-04-11 at 11.26.02
  2. Once you have added the Beanshell PreProcessor, now you can add the following script. to delete the document.Screen Shot 2016-04-11 at 11.47.52
    import java.text.*;
    import java.util.*;
    downloadDoc = "download.csv";
    csvDir = vars.get("fileLocation");
    File file1 = new File( csvDir + downloadDoc);
    if (file1.exists()) {

Now that the the Delete script is ready to be run and now you can delete the document you want to delete from the specific directory.


Randomly selecting values from .csv file for Jmeter Tests im on the

There might be instances where you will have to grab dynamic values from .csv file to run your Jmeter Performance tests. You can not use the CSV Data Config as the point here to select values randomly from the .csv file

The following script in a BeanShell PreProcessor will help to randomly select values from the CSV file that has the values. It randomly selects each row from the the CSV you can later break down the row as you prefer, in case a row has multiple values.

import java.text.*;
import java.util.*;

String [] params = Parameters.split(“,”);
csvTest = “download.csv”;
csvDir = vars.get(“fileLocation”);
ArrayList strList = new ArrayList();

try {
File file = new File( csvDir + csvTest);

if (!file.exists()) {
throw new Exception (“ERROR: file ” + csvTest + ” not found in ” + csvDir + ” directory.”);

BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null) {


Random rnd = new java.util.Random();
//String NodeId= strList.get(rnd.nextInt(strList.size()));
//System.err.println(“Parts jjj “+q);
//String[] parts = q.split(“,”);

//System.err.println(“Parts 0 “+parts[0]);
//System.err.println(“NodeName “+parts[0]);
System.err.println(“NodeId “+NodeId);

//String correctName = parts[0].replace(“%20″,””);
catch (Exception ex) {
IsSuccess = false;
catch (Throwable thex) {

Screen Shot 2016-04-08 at 11.14.15 AM (2).png

You can now use this scripts and randomly select values from CSV file when running your Jmeter Tests.

Uploading same document by renaming with Jmeter

Most of the time when we do performance testing we would require heavy data uploaded in the the system. When creating the data we would face issues where we might not be able to upload documents with duplicate names. How do we cater to this need.

The solution is quite simple. Rename the document every time you try to upload the document in that way the same document can be uploaded with different names.

  1. Create a Thread Group within a Test Plan. (You can name Thread Group as you wish)Screen Shot 2016-04-07 at 10.41.14
  2. Add a Loop Controller within the Thread Plan, and mention total amount of documents you would like to upload in the system with in the Loop Count as shown below.Screen Shot 2016-04-07 at 10.54.36
  3. Add two Counters one would have your the minimum value as the Start and the maximum value as Maximum. The other would have have the maximum value plus one as Maximum and minimum value as the Start. Mention the preferred Number Format and Increment. Reference Names for Counter1 and Counter2 should be given, as these names will be used for renaming.Screen Shot 2016-04-07 at 11.25.36   Screen Shot 2016-04-07 at 11.25.56
  4. Add the HTTP Request in the file data mention the loop_counter1 infront of the name of the document as shown below.Screen Shot 2016-04-07 at 12.11.48.pngMake sure that you have the initial or the minimum number mentioned in the name of the document in the Directory. Here the initial number we use 123000 as the Number Format is 123000.Screen Shot 2016-04-07 at 12.16.22.png
  5. Now Create the Script as shown below.Screen Shot 2016-04-07 at 12.31.19import java.text.*;
    import java.util.*;
    //Take the fileLocation from the user defined variable
    fileLocation = vars.get(“fileLocation”);
    //Take the counter 1 value to indexNum
    indexNum = vars.get(“loop_counter”);
    //Take the counter 1 value to indexNum1
    indexNum1 = vars.get(“loop_counter1”);
    //The string portrays the current location of the document (with current name)
    mainFile = indexNum + “Download.ppt”;
    //The string portrays the location of the document which is renamed.
    fileName = indexNum1 + “Download.ppt”;
    //Get current document location
    File file = new File( fileLocation + mainFile);
    //Create location for renamed location
    File file2 = new File( fileLocation + fileName);
    //Rename document with the new location
     boolean success = file.renameTo(file2);
  6. Now you will be able to upload same document with different with the amount you need.
  7. Finally if you would like to rename the thread to the initial name you can run a BeanShell Sampler in the tear down Thread Group do the same scripting and rename the document.Screen Shot 2016-04-07 at 13.14.29.png