How to use "Random Variable" in JSON Extractor in Jmeter? - 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

Related

Unable to retrieve random DB values In JMeter

In JMeter, I am able to connect to DB and retrieve DB values.
I can view the DB_Value using variable which I specified in JDBC request(Variable Names field).
Example: with index of column value as CustomerID_1
I used BeanShell Sampler with below code:
${__BeanShell(vars.put("p_CustomerID_New","${CustomerID_1}"))}
with this p_CustomerID_New i am able to get db value and that works.
Now i have another random variable to replace above "1" with random numbers, so that i will be able to use different customers from DB and substitute in my API_Request.
When i use beanshell script with ${CustomerID}_${randomNo}, it only stores random number and CustomerID is not retrieved.
Any help would be much appreciated.
Thanks in advance.
You don't need to use any scripting at all, just to for __V() and __Random() function combination:
${__V(CustomerID_${__Random(1,${CustomerID_#},)},)}
According to JDBC Request Sampler documentation the following variables are getting generated when you run a Select statement:
Customer_ID_# - number of rows returned
Customer_ID_1 - value from the first row
Customer_ID_2 - value from the second row
etc.
So
${__Random(1,${CustomerID_#},)} function returns a random number between 1 and the number of returned rows
__V() function evaluates the generated expression and returns the random value
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
Going forward be informed that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting
First of all using of Beanshell is deprecated and it is not a recommended to use it in Testplan.
It is recommended to use JSR223 pre/post-processor according to your needs.
I used pre-processor for the sampler - it works well
import java.util.Random;
Random rand = new Random();
def varName= "CustomerID_";
int randBoundary = rand.nextInt(100);// this will generate values between 0-100
varName = varName + randBoundary;
vars.put("p_CustomerID_New", vars.get(varName));
You can use this sample code and modify according to your requirements.

Regular expression extractor with Inter-Thread communication

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.

How do I use a JMeter Variable declared in an extractor in a User Defined Variable config

I have an http request that uses an extractor to set a JMeter variable (lets call it test) from the body. When I look at the debug controller I can see this works fine. Next I want to append something to the beginning of the variable so I add a user defined variable node and add a variable with the name new and I set the value to ${test}. However when I look in the debug response I see ${test} instead of the value.
I tried the same thing setting the value manually in 2 different UDV nodes and it works fine, so how do I append to a JMeter variable declared in an extractor?
As per JMeter Documentation:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
So the User Defined Variables element will be read only once and only when the Test Plan is started.
If you need to overwrite the current variable with a new value you can go for i.e. __groovy() function, the relevant syntax would be something like:
${__groovy(vars.put('foo'\, 'some_prefix_' + vars.get('foo')),)}
Demo:
vars is a shorthand for JMeterVariables class instance, it provides read-write access to all JMeter Variables in the current thread scope. Check out The Groovy Templates Cheat Sheet for JMeter to learn what else you can do with Groovy scripting in JMeter tests
UDVs can't be used in dynamic way because they are process once at the start of the test.
Don't use UDV, use JSR223 Sampler (or PostProcessor) with vars;
vars.put("new", "prefix"+ vars.get("test"))
Another option is to use Set Variables Action plugin

Fetch Javascript variable in source section using Jmeter

I have a series of interconnected pages to test using JMeter. The problem is that the initial page has a Javascript variable in source section which is more of a session variable. This variable is passed in the URL for subsequent pages.
So basically I would like to fetch this javascript variable when I load the initial page from the source section and pass it to next URL(s).
Is there a way I can achieve this using JMeter.
Are you able to see the session variable in the response of initial page?
(in view result tree listener)
If yes, then correlate this value and pass the variable in to next request (use regular expression extractor for fetching the value, still if you are finding some issue in correlating the value than please share the response of first request over here so that I can provide you regx for that)
People mostly Regular Expression Extractor to fetch dynamic values from previous responses, in general the process looks like:
Add Regular Expression Extractor as a child of the request which returns desired data
Use Perl5-style regular expression to match what you're looking for
Provide a template to choose match group - ususally $1$ if you looking for a single value
Provide a reference name to refer the extracted value, i.e. foo
Use extracted value as ${foo} where required
You can visualise JMeter Variables using Debug Sampler and View Results Tree listener combination.
The easiest way to debug your regular expressions is using View Results Tree listener in "RegExp Tester mode"
See How to debug your Apache JMeter script article for more information on troubleshooting your JMeter test.

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