How to set JMeter Vars from within WebDriver Sampler?
Asked Answered
C

4

2
// I had previously used a CSS/JQuery extractor to get a URL from a page and add it to JMeter vars - accessing it here
var pageURL = "${valueFromJmeterVars}";

// navigate to that url
WDS.browser.get(pageURL); 

// selecting an element
var button = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.cssSelector(buttonLocator)));                                                                                                                                               

// log desired boolean value to console, so I can confirm is as expected
WDS.log.info('reserveASpotButton:' + reserveASpotButton.isEnabled());

// add my boolean to JMeter vars, so I can access later from beanshell post-processor (where I do my assertions)
vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled());

The last line above doesn't work.

I can successfully use CSS/JQuery Extractor to add values to JMeter vars...

But how can I do the same from within WebDriver Sampler?

Covetous answered 6/11, 2013 at 16:27 Comment(0)
M
5

You can access JMeter API classes from within the WebDriver Sampler, it's implemented as JSR 223 standard for instance you can refer JMeter Variables (aka vars as follows)

In the WebDriver Sampler:

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

vars.put('foo','bar')

Now you have ${foo} variable with the value of bar

See The WebDriver Sampler: Your Top 10 Questions Answered guide for more WDS sampler tips and tricks.

Mcdowell answered 7/4, 2015 at 11:0 Comment(0)
P
1

I believe that you need to cast it to String first, as per Using Selenium with JMeter's WebDriver Sampler guide JMeter Variables are basically Strings and you can't put boolean there.

just replace

vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled());

with

vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled().toString());

And it should work.

Potful answered 9/11, 2013 at 16:36 Comment(0)
K
1

It won't work because 'vars' in not defined in WebDriver Sampler as in for instance BeanShell Sampler.

Karleen answered 4/3, 2014 at 9:56 Comment(0)
A
0

There isn't a clean way to do this, but it is possible. You can set the response headers in your WebDriver sampler:

WDS.sampleResult.setResponseHeaders(reserveASpotButton.isEnabled())

Then you can use a regular expression extractor to pull the data from the response headers.

Abbreviate answered 26/3, 2015 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.