0
votes

In JMeter, how do I save a thread variable in Javascript and reference it from Beanshell on the very next step OR the next threadgroup?

I am using the WebDriver sampler plugin and the WebDriver steps seem to be required to be Javascript commands. I wrote a WebDriver code block in Javascript that goes to a website and gets a cookie value.

  1. How do I set the thread-group variable at the end of the Javascript WebDriver step.
  2. How do I retrieve this value from a Beanshell step.

I have tried various things and haven't got anything to work. For example vars.put works in the WebDriver sampler but props.put says 'props' is not defined. The debug sampler shows that my JMeter property was set wonky like this:

"varName"= varName

After setting it in Javascript like this:

props.put( 'varName', varName )

And that doesn't look right AT ALL.

1

1 Answers

0
votes

Tricky thing is that vars isn't accessible to WebDriver Sampler as well, you'll need to be more creative. First of all you need to return value as Sampler result as follows:

WDS.sampleResult.setResponseData("My var value is:" + varName);

After all you need to extract this from response with i.e. Regular Expression Extractor using regular expression like:

My var value is: (.+?)

and varName as Reference Name

And finally add Beanshell Post Processor to your WebDriver Sampler with the code like:

props.put("varName", vars.get("varName"));

to convert JMeter Variable to JMeter Property which has "global" scope and can be re-used in other Thread Groups.


The solution I ultimately found was not the same. Using 'WDS.sampleResult.setResponseData' does not work since the WebDriver Sampler code seems to not allow this.

Turns out that I was able to find a workaround using "headers". Using a Beanshell Sampler step right after the WebDriver Sampler, I am able to modify the headers of the previous result (even though I cannot modify the response data itself) and so from the WebDriver Sampler I am able to store the header using 'WDS.sampleResult.setResponseHeaders( "iCookie:" + iCookie )' . Then, from a subsequent Beanshell Sampler (not a post-processor) I am able to get to that value using 'String header = ctx.getPreviousResult().getResponseHeaders();' as long as I stored the header in this format: "HeaderName:HeaderValue" from WebDriver Sampler.

It is really annoying that I cannot do a similar thing using the .setResponseData method. But, I am happy there is a workaround of some kind since I cannot find any documentation anywhere on what the normal process is for this kind of thing.