Jmeter - pause when response assertion triggers? - jmeter

I've got a Jmeter test up and running on an API I'm building.
The API returns this if it gets overloaded:
{"status":{"type":"failure","cause":"internal","http":500}}
What I'd like to do is have JMeter also PAUSE if it gets that result.
I already have set up a Response Assertion that captures these errors. It seems like this should be a simple thing to set up, but I'm not seeing it. I see that I can add an If Controller, but that only works with Javascript vars.
Do I need to add a Regular Expression Extractor, grab that 'failure' as 'type' variable, and then add that to the If Controller?
Seems a little over-complicated?
Am I missing something?

Try to use BeanShell Timer to handle this situation.
It
allows scripting so you can program timer behavior how you need;
has access to jmeter context - via ctx variable - so you can handle ResponseCode condition.
Here you can find good article with example how to use.
And here - how to access beanshell variables and handle response code.
Your condition will be something like this one:
if (prev.getResponseCode().equals("500") == true) {
. . .
}
PLEASE NOTE: script used in BeanShell Timer should return value is used as the number of milliseconds to wait.
Another question is what the reason to do this.
If you have load/stress-test scenario then these results are something you should get during your tests and analyze then.
If you have kind of "functional" test-scenario so you have to handle this situation in more general way, adding any kind of properly configured timer to each sampler - to avoid overload or simulate "real-life" scenario.
Am I missing something?

Related

Code in JMeter JSR223 Sampler comments is executed

Please tell me why the code in comments (both /*something*/ and //something) is executed using JSR223 Sampler & BeanShell sampler?
For example, I have:
and in the next JSR223 Sampler I have:
and the result is:
and the question is: why this code: "/${__setProperty(checkProperty, 50)};/" is executed regardless of that it is in comment and it is in wrong condition?
JMeter Functions are being executed in the place where they're found, no matter where it is, in Sampler label, comments section, sampler body, etc.
Actually inlining JMeter Functions and/or Variables into JSR223 scripts is not the best idea as
it might conflict with Groovy's string interpolation syntax
the function or variable might resolve into something causing script compilation failure or logic error
and last but not the least Groovy will cache the first occurrence and use it for subsequent iterations
So if you need to set a property - use props.put() function like
props.put('foo', 'bar')
And finally I'm not sure that using props.clear() is a good idea because there are some pre-defined JMeter properties (you can check yourself using Debug Sampler and View Results Tree listener combination) and it might result into unexpected behaviour if a test element will be relying on that property existence and/or value

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 change the value of a variable in Jmeter

I am trying to use the variables captured in the Debug Sampler in Jmeter and then convert those variables into some other value. And then use it somewhere in the script.
I have added a BeanShell Sampler along with the Debug Sampler and tried to get the variables displayed in the Debug Sampler.
Below is the piece of code I have written in Jmeter.
Jmeter
Is my approach correct? Am completely new to Jmeter and have little Java knowledge. So please help me here and let me know how can I convert or use a variable through a Custom code in Jmeter.
It is almost correct, you have a couple of syntax errors (missing closing bracket and undefined SomeCharacter)
Also it is better to use JSR223 Elements and Groovy language rather than Beanshell, as Groovy performance is much better and it is more Java-compliant, see Groovy Is the New Black article for detailed explanation.
Final code should look something like:
def myVariable = vars.get("Corr_ContextN")
if (myVariable.equals("002056653")) {
vars.put("myvariable1", "SomeCharacter")
}
Keep in mind that you are not changing original Corr_ContextN, you are creating new variable myvariable1. Also in order to see new variable you need to move the Debug Sampler after the Beanshell Sampler
Your got the concept but your code has the following errors:
imports are wrong and useless. You only need to import Classes for unbound variables, ie the ones that are advertised in the component.
There's a missing ')' in the if clause
SomeCharacter is not defined
And you should avoid Beanshell and favor JSR223 Test Element with Groovy as per those recommandations:
http://www.ubik-ingenierie.com/blog/jmeter_performance_tuning_tips/
Note that there is also a __groovy function for your use case.

JMeter treating "${COOKIE_[cookiename]}" as a string

I have looked at numerous examples of setting properties from cookies, and they all seem to be indicate that using a BeanShell PostProcessor, I should be able to do the following, given a cookie named 'FOO'.
props.put( "fooCookie", "${COOKIE_FOO}" );
However, when I try to write that value to the console, as you see here...
print( props.get( "fooCookie" ) );
... the value is always the string ${COOKIE_FOO} as if the dollar/curly bracket notation is not being parsed.
I feel like I must be missing something painfully obvious here, but after several hours of fighting this, I am bringing it to the experts. Any advice would be appreciated.
EDIT: Adding a bit more detail. This is the layout of my test plan
Test Plan
User Defined Variables
HTTP Cookie Manager
HTTP Request Defaults
Login Thread (setup)
[page request - login POST]
HTTP Header Manager
BeanShell PostProcessor
[more page requests]
And I do indeed have CookieManager.save.cookies=true set in the jmeter.bat file that I am launching it with.
Do you have HTTP Cookie Manager in your test plan? If not, you need to have that.
You also need to set the CookieManager.save.cookies=true in the jmeter.properties file which you can find in JMETER_HOME/bin folder.
${COOKIE_FOO} will return the actual cookie value.
Check that your ${COOKIE_FOO} variable is really set using Debug Sampler and View Results Tree listener combination. Your code is OK so my expectation is that the variable is not set.
Those who suggest using Beanshell where it is possible to handle the situation using JMeter built-in test elements should probably consider quitting IT. There are:
__setProperty() function to put something to JMeter Properties
__P() and/or __property() function to read something from them
See How to Use Variables in Different Thread Groups guide for real-life example.

Anyway to auto set jmeter assertions to initial responses?

I am building out a test suite in jmeter and want to set the assertions to the initial responses that I get from the api calls. Is there any way to do this besides copy pasta?
For clarity each individual call has its own assertion, which for the moment is the response it is receiving.
I want the assertions to be populated by the responses.
If I understood currectly - you are coming up with some JMeter-Baseline script - you also assume that the response you get now is correct - that is what you expect to get it in future as well , so you want to put the response data in the assertion.
If yes, JMeter will not remember the initial responses. So you need to create your script accordingly.
You can have a beanshell postprocessor to write the the responsedata in a CSV file. Later you modify the script to look for the assertion string from CSV file to compare. This is the statement to get the Response Data.
prev.getResponseDataAsString()
As per How to Use JMeter Assertions in 3 Easy Steps guide, JMeter Assertions have their scope.
If you put an assertion at the same level as all requests it'll apply to all of them. See image below for explanation:
I would use a CSV file with the URL and the assertion string you want to match.

Resources