Vazha
Vazha

Reputation: 33

How to use a JMeter variable in JavaScript code?

I want to automate several users login with different credentials on the web page. I wrote code in JMeter with Selenium. I want to read usernames and passwords from an CSV file. For that I want to pass a JMeter variable in my code because I need it to be dynamic.

for example:

findElement(By.name("username")).sendKeys(${username}) <- here I want to pass JMeter variable

WDS.sampleResult.sampleStart()

WDS.browser.get('https://www.google.com/')

WDS.browser.findElement(org.openqa.selenium.By.name("q")).
sendKeys(**${here I want to put JMeter variable}**)

WDS.sampleResult.sampleEnd()

Could you please advise me on the best approach here?

Upvotes: 1

Views: 418

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

There is WDS.vars shorthand which maps to JMeterVariables class instance so you can use it for getting JMeter Variables values

findElement(By.name("username")).sendKeys(WDS.vars.get("username")) <- here I want to pass JMeter variable

WDS.sampleResult.sampleStart()

WDS.browser.get('https://www.google.com/')

WDS.browser.findElement(org.openqa.selenium.By.name("q")).
sendKeys(WDS.vars.get("your_jmeter_variable_name"))

WDS.sampleResult.sampleEnd() 

More information:

Upvotes: 2

Related Questions