My JMeter test plan looks like this:
HTTP Request
- Assertion
HTTP Request
- Assertion
HTTP Request
- Assertion
Assertion Result Listener
I'd like to define all assertion results from the listener as a variable and use that variable in a POST call to JIRA, so the description contains an overview of all assertions and failure and pass of each assertion.
Assertion Result Listener
I know I can save the assertion results to a file and upload that, but I need the assertion results as text in the JIRA. Any ideas how I can do that?
edit: this is for a functional test suite.
Add JSR223 Listener to your Thread Group
Put the following code into "Script" area
def result = vars.get('result')
StringBuilder builder = new StringBuilder()
if (result != null) {
builder.append(result).append(System.getProperty('line.separator'))
}
prev.getAssertionResults().each { assertionResult ->
builder.append(prev.getSampleLabel()).append(System.getProperty('line.separator'))
if (assertionResult.isFailure()) {
builder.append('\t').append(assertionResult.getFailureMessage()).append(System.getProperty('line.separator'))
}
}
vars.put('result', builder.toString())
props.put('result', builder.toString())
Add tearDown Thread Group to your Test Plan
Refer the generated string holding the assertion results using __P() function as ${__P(result,)}
Demo:
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.
Related
In my JMeter test plan, I want to set a flag in case of failure in every HTTP request. So I created a JSR223 PostProcessor in request with the following snippet:
if (!prev.isSuccessful()) {
int abc = 1
props.put('result', vars.get('abc'))
}
where result is defined as global in the thread.
In teardown I want to exit JMeter by comparing with the value of the flag . So I am doing the following:
if ((props.get('result') as int) == 1) {
System.exit(1);
}
Can anyone help me what wrong I am doing in this? Is there any other way by which I can do this.
This statement vars.get('abc') will return null because you just declare an integer called abc and not writing it to JMeter Variables.
You need to amend your code to something like:
if (!prev.isSuccessful()) {
int abc = 1
props.put('result', abc)
}
also there is no need to cast it to the integer back, it's stored as the object of the given type
if (props.get('result') == 1) {
System.exit(1);
}
More information:
Properties aka props
JMeterVariables aka vars
Top 8 JMeter Java Classes You Should Be Using with Groovy
You may also find AutoStop Listener useful
I would like to know best way to verify entire Json response in Jmeter. Can somebody help. Thanks.
The easiest way is use JSR223 Assertion and JsonSlurper class like:
Put the anticipated JSON into expected JMeter Variable using i.e. User Defined Variables test element like:
Add JSR223 Assertion as a child of the request you want to validate
Put the following code into "Script" area:
def expected = new groovy.json.JsonSlurper().parseText(vars.get('expected'))
def actual = new groovy.json.JsonSlurper().parse(prev.getResponseData())
if (expected != actual) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Mismatch between expected and actual JSON')
}
If there will be any difference between expected and actual JSON - the sampler will be marked as failed
for example i have http body data like this
{
"signature" : "${signatureCreate}",
"paramA" : "1A02",
"paramB" : "aaa",
"paramC" : "asass"
}
how could i get all params (paramA, paramB, paramC) in my BeanShell Preprocessor? i have to get all these 3, encrypt it, and put it back in "signature" param
i also tries using JSR223 PreProcessor like this (only to try to get the paramA value, but still no luck)
def body = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put("signatureCreate", body.paramA);
thanks in advance
Your code should work fine (given you choose Groovy in the "Language" dropdown), just add Debug Sampler and View Results Tree listener to your Test Plan, you will be able to see signature variable value in the "Response Data" tab of the Debug Sampler
More information: How to Debug your Apache JMeter Script
I've made login process with the help of jmeter. In of of request samplers I'm expecting to get response code "401". I've added BeanShell Assertion
if (ResponseCode.equals("401") == true) {
SampleResult.setResponseOK();
SampleResult.setSuccessful(true);
}
And my Results Tree is looking like this now.
My question is - what i need to add to BeanShell in order to make child of the second sample green (passed) as well as it's parent sample?
The easiest way is using Response Assertion configured like:
If you are still looking for Beanshell solution - you need to process all sub-results along with the main result so you should amend your code like:
import org.apache.jmeter.samplers.SampleResult;
//process main sample
if (SampleResult.getResponseCode().equals("401")) {
SampleResult.setResponseCodeOK();
SampleResult.setSuccessful(true);
}
//process all subsamples
for (SampleResult subResult : SampleResult.getSubResults()){
if (subResult.getResponseCode().equals("401")){
subResult.setResponseCodeOK();
subResult.setSuccessful(true);
}
}
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter test scripts.
In Jmeter I am trying to get a value out of a variable from the url using the Regular Expression Extractor. I am also using the BeanShell Sampler to get the value out of the variable and print it out to the log file. I can then see in the log file what value I am getting.
I don't think my Regular Expression Extractor setting is correct I am getting the following error from my BeanShell Script:
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``String session_ID = vars.get("sessionID"); log.info("session_ID = " + session_I . . . '' Encountered "vars" at line 4, column 1.
An example URL I have is:
http://localhost:8080/test1/gp/gw/ajax/desktop/herotator/record-impressions.html?ie=UTF8&aPTID=16001&cmpnId=205103067&cnttId=1&h=1A52D1&pIdent=desktop&rId=0DA6FXQ35E8JDNVES8C59&sid=14&slotName=PS4-some-testdata
I would like to get the value from the variable PTID and output it to the log file. I can then use the value in other Http requests when i need to.
My BeanShell Sampler script is:
String session_ID = vars.get("sessionID");
log.info("session_ID = " + session_ID)
vars.put("sessionID", session_ID);
My Regular Expression Extractor is:
Field to check = URL is ticked
Reference Name = sessionID
Regular Expression = PTID="(.+?)"
Template = $1$
Match No. (0 for Random): 1
Default value = session id not found
My Test Plan set up is as follows:
Test Plan
--Thread Group
----Http Request Default
----Http Header Manager
----Recording Controller
------Http Request
------Regular Expression Extractor
------BeanShell Sampler
------more Http Requests
----Access Log Sampler
----View Results Tree
Also nothing is being written to the log file when i run the script.
Access Log Sampler the log file location is to:
E:\RL Fusion\projects\JMeter\apache-jmeter-2.13\jmeter.log
In the View Results Tree for the Http Request I can see there is a PTID value in the Response data Tab.
My regular expression extractor is not getting this value out.
I am new to JMeter, If i have anything in the wrong order please do let me know.
Thanks,
Riaz
My expectation is that something is wrong with your Beanshell script. Most likely you're missing a semicolon at the end of statement on 3rd line.
To get to the bottom of a problem in Beanshell script you can use the following approaches:
Add debug(); directive to your Beanshell script (make it first line). It will trigger debug output to console window where you launched JMeter from
Put your Beanshell code into "try" block like:
try {
// your code here
}
catch (Throwable ex) {
log.error("Problem in Beanshell", ex);
throw ex;
}
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter.