I make retry logic with while loop. It will to be retry when response code is not 200.
Retry times is 3 times, if after 3 times still failure, give up.
However, even request is success after retry. The failed request does show in result tree and summary report.
Is it possible don't show failed request when success after retry in report. Only show request when after 3 time retry but still failure .
Now Situation
Request:
request(fail) -> request(fail) -> request(success)
Result tree:
request(fail)
request(fail)
request(success)
This is image I want.
Request:
request(fail) -> request(fail) -> request(success)
Result tree:
request(success)
Request:
request(fail) -> request(fail) -> request(fail)
Result tree:
request(fail)
Put your request under Loop Controller and give the number of loops as 3
Add JSR223 PostProcessor as a child of your request and put the following code into "Script" area
if (prev.getResponseCode() == '200') {
ctx.setTestLogicalAction(ctx.TestLogicalAction.BREAK_CURRENT_LOOP)
}
else {
if (vars.get('__jm__Loop Controller__idx') as int < 2) {
prev.setIgnore()
}
}
where:
prev stands for the previous response SampleResult
ctx stands for JMeterContext
and vars for JMeterVariables
More information on these JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy
Related
I was tasked with running some performance tests in order to benchmark our systems. I designed the scenario as below :
var scn: ScenarioBuilder = scenario("Sample Scenario")
.exec(
forever(
exec(<first request>)
.pause(2, 5)
.doIf(session => !session.isFailed) {
randomSwitch(
70d -> exec(<second request>),
15d -> exec(<third request),
15d -> exec(<fourth request>)
)
}
)
)
The issue I'm facing cropped up when we were performing fail-over tests in our system. I noticed that during the simulation all of requests stopped executing after the "first request" once a failure was introduced in the system.
I checked the logs and the "first request" was successful and as per my understanding !session.isFailed condition should be true allowing for further execution of the scenario.
Can anyone please share why the condition is being marked as false instead of true?
As per Gatling.io team:
The session keeps its status even after several requests.
As you mentioned, a failure was introduced in the system
One of the primary goals is to have points in your scenario where the virtual user may leave (because of the failure) with exitHereIfFailed
If you want to recover, you should mark your session as succeeded.
var scn: ScenarioBuilder = scenario("Sample Scenario")
.exec(
forever(
exec(session => session.markAsSucceeded) // <---- reset the status
.exec(<first request>)
.pause(2, 5)
.doIf(session => !session.isFailed) {
randomSwitch(
70d -> exec(<second request>),
15d -> exec(<third request),
15d -> exec(<fourth request>)
)
}
)
)
How to handle negative cases in JMETER, for example my expected output response is 400("There are no records") for an GET API?
In JMETER response is coming as failure or warning.
Is JMeter only handle positive scenarios like for all GET API response code should be 200 ?
Add Response Assertion as a child of the HTTP Request sampler which returns HTTP Status Code 400
Configure it as follows:
Tick Ignore status box
Set "Field to test" to Response code
Set "Pattern matching rules" to Equals
Add 400 as a "Pattern to test"
This way JMeter will pass only if the parent HTTP Request sampler returns 400 status code, otherwise it will fail.
You can add to HTTP Request Response Assertion with Ignore status checked
HTTP Responses with statuses in the 4xx and 5xx ranges are normally regarded as unsuccessful. The "Ignore status" checkbox can be used to set the status successful before performing further checks. Note that this will have the effect of clearing any previous assertion failures, so make sure that this is only set on the first assertion.
I tried with this, by adding a BeanShell Assertion with following code.
import org.apache.jmeter.assertions.AssertionResult;
String failureMessage = "";
String ResCode = SampleResult.getResponseCode();
if (!ResCode.equals("400")) {
failureMessage = "Got Response Code" + ResCode;
AssertionResult result = new AssertionResult("Expected Response 400");
result.setFailure(true);
result.setFailureMessage(failureMessage);
prev.addAssertionResult(result);
prev.setSuccessful(false);
SampleResult.setStartNextThreadLoop(true);
} else {
//failure criteria
}
Can I resolve the following scenario by JMeter?:
Send request1
Insert response of request1 to request2
Send request2
Send request3
Compare response3 with responce1
If response3 = responce1 -> setResponseOK() for request2 ELSE Failed
Thanks in advance.
Suggestion to Resolve this situatuion
Send request 1
Capture the response of request 1 in var1(Using correlation-->depending up on your requirement)
Send request 3 first (as you don't need response of request 2 for input of req 3)
Capture response of request 3 in var2
Now hit Request 2 by passing the response of request 1 as an input which is store in var1
Apply beanshell post processor as a child of request 2
Now compare Respose of request 1 and 3 inside if condition:
if matches make the previous request (i.e. Request 2) pass, else fail
if(vars.get("var1").equals(vars.get("var2")))
{
prev.setSuccessful(true);
}
else
{
prev.setSuccessful(false);
}
Design your test as follows:
Request 1
Regular Expression Extractor to extract the whole response into a JMeter Variable configured as:
Reference Name: anything meaningful, i.e. response1
Regular Expression: (?s)(^.*)
Template: $1$
Request 2: use ${response1} where required
Request 3
Response Assertion configured like:
Pattern Matching Rules: Equals
Patterns to Test -> ${response1}
Reference material:
Regular Expressions
How to Use JMeter Assertions in Three Easy Steps
I created a script in jmeter, few positive cases and few are negative cases.
For Positive Cases - Response Code will come as 200
For Negative Cases - Response Code will come as 412.
As per Jmeter if Response Code 4xx or 5xx will be considered as Fail but in my case i am expecting result as 412 in negative cases and i want to consider that as Pass.
I tried with BeanShell Assertion but i didn't get the expected.
Code is as below:
String ErrorValue = "${ExpectedError}";
if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
Failure = false;
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
Failure = false;
}
else
{
Failure=true;
}
with about code i am able to check the expected error and response is same but if that is same how to change the status to pass i didn't get.
Please anyone help me.
Thanks
Sarada
Your Failure = false bit sets only Beanshell Assertion success. As far as I understand you need to change status of the parent sampler. In order to do so you need to invoke SampleResult.setSuccessful() method and set it to "true" as follows:
SampleResult.setSuccessful(true);
Full code:
String ErrorValue = "${ExpectedError}";
if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else
{
Failure=true;
}
References:
SampleResult class JavaDoc
How to Use JMeter Assertions in 3 Easy Steps
If you are expecting a HTTP Response Code "failure" in JMeter but wish to flag the sample as successful this can be accomplished by a response assertion:
For example:
When validating a DELETE call works, we might want to re-try a GET and validate 404 as expected. Normally JMeter would consider this a failure, but in the context of our test it is not.
Add A Response Assertion to the after-delete GET call.
Apply To: Main Sample
Response Field to Test: Response Code
Check off "Ignore Status"
Pattern Matching Rules: Equals
Pattern to Test: 404
The status of failed or not is always ignored. However, only if the assertion of 404 matches will the request be a success.
For example, if the call returned a 500 jmeter would still ignore the "failed" status, but mark the sample as a failure because 500 != 404.
-Addled
I'd like to practice IF controller basically as follow:
TEST_PLAN
+Thread_group: (user: 3, ramp: 3, loop:2)
+Loop_Controller(loop:4)
++ HTTP request1: go to page_1
+IF_Controller1
++ HTTP request2: go to page_2
++ counter (config element)
+IF_Controller2
++ HTTP request3: click Back button
My condition "HTTP request 2" is only executed if loop (output "Loop_Controller" >4). How to do that ?
And, for IF_Controller2: it's will be executed after each of question on this page has checked (page has 5 questions, 1 question has 5 answer radio button.)
I also follow the Counter element and Counter functions, but don't know how to combine them?
and one more things: to use the variable in IF statement, they should be put on previous activities ? (in my case: IF controller: ${count}, thereby variable "count" should be put on HTTP request 1)
Thanks,
You need to place COUNTER as a child of Loop Controller, not If Controller. In this case you'll get the following scenario:
HTTP Request 1 (loop 1)
HTTP Request 1 (loop 2)
HTTP Request 1 (loop 3)
HTTP Request 1 (loop 4)
** HERE we're getting out of Loop Controller and go to 2nd Loop of Thread Group, If controller doesn't fire as counter value is 4 and condition is >4**
HTTP Request 1 (loop 5)
HTTP Request 1 (loop 6)
HTTP Request 1 (loop 7)
HTTP Request 1 (loop 8)
** HERE counter value is 8 and If controller condition is met so **
HTTP Request 2
Being child of If Controller counter isn't getting incremented
Counter Configuration:
Start: 1
Increment: 1
Reference Name: N
If Controller Configuration:
${N} > 4
See How to use JMeter's 'IF' Controller and get Pie guide for more details.