How to view the bean shell pre or post processor result - jmeter

I am trying to use beanshell post processor so I have started with simple http request where I am hitting the google home page. "${url}"=="https://www.google.co.in". After http request page I am adding the BeanShellPostProcessor where in Script area I am using ctx variable and some other code which is like :
int threadNum = ctx.getThreadNum();
String code = prev.getResponseCode();
String message = prev.getResponseMessage();
log.info(threadNum);
log.info("This line has been written by Beanshell Post Processor");
so I have two concerns -
i.The way I am using beanshell is this right ?
ii.Where is the console for beanshell processor in jmeter?
like sample request result can be viewed in listener. I tried with BeanShellListner, but it doesn't show any data.
Also I have kept the "log viewer" on.

You are using it almost right, the only error is that you cannot write an integer to the log file, you need to cast it to String first like:
log.info(String.valueOf(threadNum));
You won't be able to see the result of pre and post processors anywhere apart from jmeter.log file. In case of PostProcessor you can modify parent sampler response data via prev.setResponseData() method where prev is a shorthand to SampleResult class instance
I would also recommend considering switching to JSR223 Elements and Groovy language as this way you will get better performance, out of box support of XML, JSON, etc., and other Groovy's "syntax sugar". See Groovy Is the New Black article for more detailed explanation.

Related

jmeter prev.getResponseDataAsString getting wrong return

I have a looping process that will extract some values from a web service (working) and loop through to pull all the information for each value (working).
I need to capture the whole return into a variable so I can modify it and post it back up later.
Screenshot:
When the "Baseline for ..." get kicks in, I get the proper response
But the "Get response" BeanShell PreProcessor is picking up old responses
Screenshot:
Given where my "Get response" object is, I would assume the:
vars.put("ResponceData", prev.getResponseDataAsString());
...would grab the response from "Baseline for ${ID} of site ${callSite}". Please help!
You are using wrong test element. Beanshell PreProcessor is being executed before request therefore it acts properly and returns response from the previous request instead of current one. You need to change it to the Beanshell PostProcessor and your code will start working as you expect.
It is recommended to avoid scripting where possible, if you need to save response data into a JMeter Variable you can do it using i.e. Regular Expression Extractor. According How to Extract Data From Files With JMeter article the relevant configuration will be something like:
Reference Name: ResponceData
Regular Expression: (?s)(^.*)
Template: $1$
If you face a JMeter limitation which cannot be worked around without using scripting make sure you are using the most performing scripting language, since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language

How to modify HTTP request before sending in JMeter through Beanshell pre processor?

I have test case in my csv file. The request URL has a custom variable.
Sample URL : .../abc/$id
I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:
String path = ctx.getCurrentSampler().toString();
path.replaceAll("$id", id);
I am not able to set this updated URL in jmeter context (ctx)
You need to assign new path value to path variable
You need to set sampler path to the new value using sampler.setPath() method
So you need to amend your code like:
String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);
Demo:
Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.
Try to avoid the pre / post processors if possible.
Your requirement is very simple and straight forward.
Directly use this in the path - assuming id is the name of variable which has the value.
/abc/${id}

How to get HTTP POST request body in Beanshell Preprocessor?

I am facing some trouble using jmeter. This is my use case, I am using CSV data source parameters to construct a HTTP POST request, the request body is read from a CSV column
which contains some placeholders like ${source_id}
I want to replace these placeholders with jmeter parameters which I am initialising through a regex/json extractor(read from the response of last PUT request). I tried using the jmeter variable name in the CSV file but the variable values are not getting substituted. I guess I will have to use the beanshell pre-processor to modify the HTTP POST request body. Can anyone help with the methods I can use to get the HTTP POST request body.
Something like
String requestBody = sampler.getArguments().getArgument(0).getValue();
should help.
sampler is a shorthand to parent sampler class instance, in your case it will be HTTPSamplerProxy, see the JavaDoc for all available methods and fields.
I would recommend considering migration to JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black article for more details.

How force BeanShell Assertion to make verified result is shown in View Result Tree of JMeter?

I am using JMeter for testing:
How force BeanShell Assertion to make verified result is shown in View Result Tree?
I tried Log but it is not shown in View Result Tree:
props.put("result",vars.get("matchingIdCount_1"));
print(props.get("result"));
log.info("---------------------------");
log.error("error");
log shorthand will append message to jmeter.log file only, it won't be visible in any listener. To be able to see it in View Results Tree you need to amend response code, message, headers or data.
For example if you change your script to:
SampleResult.setResponseMessage("result -> " + vars.get("matchingIdCount_1"));
You'll be able to see the value in "Response Message" section:
SampleResult is a pre-defined variable which provides access to parent/associated SampleResult class instance methods and fields.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more Beanshell and JMeter related tips and tricks.

In JMeter, how do I save a thread variable in Javascript and reference it from Beanshell?

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.
How do I set the thread-group variable at the end of the Javascript WebDriver step.
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.
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.

Resources