0
votes

I am using Jmeter with Selenium Webdriver sampler & JAVA. I have a CSV file with username and password. My test has CSV Data Set Config to read the file and Webdriver sampler. I am writing selenium code in java. I wants to read username and password from CSV file and use it in my selenium script. Here is my code:

Code CSV Config

import openqa.selenium.OutputType.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;

WDS.sampleResult.sampleStart();

var ctx = org.apache.jmeter.threads.JMeterContextService.getContext();
var vars = ctx.getVariables();

String userName = WDS.args[0]; -- the result of this line is "loginName" instead of actual log in name. same for 2nd line. 
//String userName = vars.get('loginName');

The issue i have is its not picking from CSV. The above two lines produce result "loginName" instead of picking actual login name from the file. I have used single quote, double quote etc, but no luck. Using ${loginName} gives error. Any Idea what is wrong and how to fix it?

1

1 Answers

0
votes
  1. Remove these 2 lines from your script as they are not required, moreover they will cause syntax errors given you use java as the language

    var ctx = org.apache.jmeter.threads.JMeterContextService.getContext();
    var vars = ctx.getVariables();
    
  2. Choose groovy from the Language dropdown. When you pick java it is not the "real" java, it is rather Beanshell interpreter so if you want to use normal Java syntax - choose Groovy. Moreover since JMeter 3.1 it is recommended to use Groovy for all forms of scripting. This is due to the fact that Groovy has much better performance than other scripting options.

Evidence that Groovy script works:

JMeter WebDriver Variables