Regular expression extractor with Inter-Thread communication - jmeter

I am using Inter Thread communication post processor to store values extracted using regular expression extractor. But, i am running into an issue when the request fails and the regular expression extrator has no value against the variable. Example: I am storing an ID against a variable ${Emp-ID} in the Inter communication post processor. When the request has failed and no value is returned from the regular expression extrator, but, ${EmpID} is stored in the queue. Is there a way to just ignore and not store any value?
Inter communication post processor

I think the easiest is switching to the JSR223 PostProcessor instead, example code:
if (vars.get('Emp-ID') != null) {
kg.apc.jmeter.modifiers.FifoMap.getInstance().put('EMP-Queue', vars.get('Emp-ID'))
}
where vars stands for JMeterVariables class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 test elements.

Related

How to use "Random Variable" in JSON Extractor in Jmeter?

I am capturing bunch of available seat list using JSON Extractor (Please refer "1_JSON_Extractor" ) , I am using -1 to capture all the ordinals, it is working fine (Refer "2_Debug_Sampler").
I need to take the random value from the list, Instead of using 0 for Random Ordinal, I am trying to use Random Variable here,( Please refer "3_Random_variable"). But it is not working fine.
I am planning to use the the Random variable "C_Seat_1" in the place of JSON Extractor Match No and get the random value. (Please refer "4_Json_Extractor")1_JSON_Extractor .2_Debug_Sampler3_Random_variable4_Json_Extractor
Can you help ?
Take a look at JMeter Test Elements Execution Order
0. Configuration elements
Pre-Processors
Timers
Sampler
Post-Processors (unless SampleResult is null)
Assertions (unless SampleResult is null)
Listeners (unless SampleResult is null)
Random Variable is a Configuration Element
JSON Extractor is a Post-Processor
Hence when Random Variable is evaluated JSON Extractor hasn't been executed yet therefore ${C_Selected_Seat_All_matchNr} is not defined
You can just go for __Random() and __V() functions combination to pick a random seat directly where required without any extra configuratino elements, something like:
${__V(C_Selected_Seat_All_${__Random(1,${C_Selected_Seat_All_matchNr},)},)}
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

How to use Regex for HTTP request once and not duplicate code - Jmeter

I have a question about Jmeter's regex and Json extractor.
I sent Http request and parsed the response, I see that the only way it worked is if the json extractor and regex are inside the Http request section.
The problem is that I want to create generic framework, so the user can disable some requests and enable another, thus if I need to put the json extractor in each request I will duplicate the parsing for each request individually, instead of one to all (remember only one at a time will be active).
Marked in Yellow the working scenario, and unmarked the expected scenario with only one parse.
the actual results is that I get null in the json extractor and regex
Can someone explain if this is the only way?
After investigating I saw that the problem is with the assertion, the assertion not cope with situation that it is out of the Http request, it say that the response is null while I saw that the variable is not null.
Provided Pic for this issue
JMeter Post Processors (including Regular Expression Extractor, JSON Extractor, whatever) are following Scoping Rules, like:
If you put a Post Processor to be a child of a certain sampler - it will be applied to this sampler only
If you put a Post Processor at the same level as samplers - it will be applied to all samplers which are on the same level with it
See The Scope of JMeter Assertions chapter of the How to Use JMeter Assertions in Three Easy Steps guide for more details, here is the visual representation:
Remember 2 things:
Post Processors have their cost so go for extending their scope only if each sampler produces the cid you're looking for, otherwise it will create an overhead and increase resources consumption
if a certain sampler won't have this cid value it will either become null or will have a default value so your test can become more "fragile"

Print response json in jmeter using beanshell

I want print my json response in jmeter. I used beanshell but it shows error. Below is the line to print json object extracted in "data":
log.info("========"+${data});
Don't ever inline JMeter Functions or Variables into scripts, they may resolve into something which will cause compilation/interpretation failure, use code-based equivalents instead or pass functions/variables via "Parameters" section.
Exhibit A: using vars shorthand
log.info("========" + vars.get("data"));
Exhibit B: using "Parameters" section
Using Beanshell isn't the best scripting option, consider migrating to JSR223 Elements and Groovy language as Beanshell has known performance problems. Moreover, Groovy has built-in JSON support See Apache Groovy - Why and How You Should Use It for details.
And finally your ${data} variable might not be defined (i.e. your extractor fails), in this case you will get interpretation failure on attempt to refer it as ${data}, double check its value using Debug Sampler and View Results Tree listener combination.

How to find and replace a substring in sampler's response for web services?

I am using two soap/xml request samplers wherein response of one is to be used in request of the other. The issue is that the response of Sampler1 contains multiple occurrences of "a:" which has to be replaced by "eas1:" which can be used in Sampler2. Kindly suggest a solution.
I tried using beanshell postprocessor but could not come to any positive result.
Add JSR223 PostProcessor as a child of the Sampler1
Put the following code into "Script" area
def response = prev.getResponseDataAsString()
def request = response.replaceAll('a:', 'eas1:')
vars.put('request', request)
Use ${request} in the "Body Data" section of the Sampler2
References:
prev is a shorthand to SampleResult class instance which provides access to the parent Sampler result
vars is a shorthand to JMeterVariables class instance, it provides read/write access to JMeter Variables
String.replaceAll() method reference
Groovy is the New Black - guide to Groovy scripting in JMeter

Using Extracted JSON Value in Another JMeter Thread

First, let me preface this question that I've only been using JMeter for 36 hours.
I've been able to successfully create a thread that performs a POST (json body) to generate a new record.
{
"id":1257697771,
"displayName":"TERM2",
"functionName":"f_1257697771",
"displayableSourceExpression":"TRUE",
"typeId":200,
"groupId":300,
"clobObjId":1257697772,
"typeCode":5,
..........
}
I need to take the new record's ID (1257697771) value returned in order to perform updates, get by ID, delete, etc. on this record in other threads.
After much reading, I've created a Regular Expression Extractor where:
Apply to: Main Sample Only
Field to Check: Body as Document
Reference Name: newRecord
Regular Expression: "id":(.+?)\,"displayName"
Template: $1$
Match No: 1
Default Value: NONE
At this point, I'm not sure if my Regular Expression is formatted correctly where (.+?) is valid.
Also, I'm confused if I can either just specify the new reference (newRecord) in another thread's HTTP request's Parameters or use a BeanShell Post-Processor, or a Response Assertion, etc....
There a lot of answers for the same function of "Passing". Not being a programmer, I've tried to follow the discussion "how to extract json response data in jmeter using regular expression extractor?", but I'm still not clear.
Any insight is appreciated. Thanks.
JMeter Variables are local to Thread Group, you need to convert your variable to JMeter Property.
Use:
__setProperty() function in the Thread Group where you define your newRecord variable like:
${__setProperty(newRecord,${newRecord},)}
__P() function to access property value like:
${__P(newRecord,)}
See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation.
Also be aware of the Function Helper Dialog as it looks like JMeter functions syntax was developed by aliens.
To pass a value between threads you need to use the jmeter property function.
In a jsr223 postprocessor using groovy the code to get the value is as follows:
def userProperty = props.get('propertyToGet')
vars.put('userProperty', String.valueOf(userProperty))
You would then access the variable in your thread using:
${userProperty}
Or you can use shorthand directly:
${__P('propertyToGet')}
Variables in jmeter are thread specific.
Thanks everyone. I was able to resolve it with your help!
In the first thread:
set the Reg Expression Extractor Regular Expression = "id":(.+?)\,"displayName"
added a Bean Assertion where Parameters = ${__setProperty(newRecord,${newRecord},)}
In the second thread:
appended the Path url with ${__P(newRecord,)}
Executing the first thread (POST) resulted an new record with a unique ID. (1257698108)
Executing the 2nd thread (GET) shows
GET http://server/.../.../.../.../1257698108
And returns the exact data generated in the first thread.
Thanks everyone for your help!

Resources