JMeter Get Body Data before Sending - jmeter

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

Related

Saving matched values in JMeter postprocessors

In just starting to use JMeter I am trying to set variables of the form taskId_1, taskId_2, taskId_3 (defined in "User Defined Variables") and use them in HTTP Samples (REST requests). When I run postprocessors none of my JSON Extractors or Regular Expression Extractors save the values matched (and I tested the extracted regular expression using RegExp tester.)
The response sent from the GET request that I am parsing looks like (edited for readability):
{
"case-data": {
"Description": "100 parallel tasks",
"Workflow": {
"com.MyCompany": {
"workflow-case-id": null,
"stages": [
{
"stage-guid": "470D2E00-A9E1-11EB-887B-4226FC2CA371",
"tasks": [
{
"task-name": "Stage1Task1P",
"user-group-id": "Joe",
"task-id" : 52
},
{
"task-name": "Stage1Task2P",
"user-group-id": "Joe",
"task-id" : 73
},
{
"task-name": "Stage1Task3P",
"user-group-id": "Joe",
"task-id" : 123
}
]
} ] }}}}
Following the BlazeMeter tutorials, for the Regular Expression Extractor I use
Apply To "Main sample and sub-samples"
Field to check "Response Message"
Name of created variable taskId
Regular expression task-id"\ :\ (.\d+)$
$1$
(I have also tried taskId_1 - my ideal solution would set all the taskId's at once).
For the JSON Extractor that I just tried I use:
Names of created variables: taskId
JSON Path Expressions: $."task-id" (LOG ALL indicates Could not find JSON Path - so I will revise)
Match No: -1
Default Values: taskId not matched
It is as if none of these strings are ever matched so the values are not set - not even default values. I also have a Debug Sample, Debug Postprocessor and a View Results Tree included in the parent Logic Controller - but see no values of variables set anywhere (even in the logs).
I have other JSON extractors that attempt to traverse down the object tree from the top but they also are not setting my User Variables although LOG ALL indicates they are setting the variables of the same name.
What is wrong with my match expressions and assignment parameters?
How can I change my usage of Debug Sampler/Postprocessor/View Result Tree/Properties and variable viewer (which has JMeter variables = True) to observe variables?
Is there a way to run a test plan one step at a time (e.g., a preprocessor, a request, a postprocessor then another postprocessor) by clicking on UI elements?
Regular Expression Extractor:
"task-id"\s*:\s*(\d+)
More information: JMeter Regular Expressions
JSON Extractor:
$..task-id
More information: Jayway JsonPath
To view generated variables just add a Debug Sampler somewhere at the bottom of your script and add View Results Tree listener so Debug Sampler would be in its scope, see How to Debug your Apache JMeter Script for more details.

Storing the output json parameters as variable in jmeter using it in next request

I have a http request which gives json output as :
{
"MESSAGE_CODE":200,
"MESSAGE_DESCRIPTION":"OTP Generated Successfully",
"data":
{
"otp":"123456",
"otpGeneratedDate":"yyyy-mm-dd"
}
}
I want to use otp as the input parameter in json for my next http request.
I have added JSON extractor with following configuration :
enter image description here
Names of created variable : OTP
JSON path expressions : $..data.otp
Match No. : 1
But still when I am calling this parameter as
"otpNumber": "${OTP}" in my next input JSON http request, its not getting called.
and value is passed as ${OTP} for otpNumber
How can I handle this
As per JMeter Documentation:
Variables, functions (and properties) are all case-sensitive
So you need to change this line:
"otpNumber": "${OTP}"
to this one:
"otpNumber": "${otp}"
and your test should start working as expected.
You can observe which JMeter Variables are defined along with their values using Debug Sampler and View Results Tree listener combination.

Best way to validate entire Json response in JMeter

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

Jmeter assertion result listener as variable

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.

JMeter Regular Expression Extractor I am not getting the value out of a variable from the url

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.

Resources