Beanshell preprocessor in jmeter to generate random number and use it in main script - jmeter

I am trying to create a random number and use it post request but getting errors, need help
HTTP request is like:
{
"firstName": "Jmeter",
"lastName": "LoadTest",
"userName": "Jmeter${random}",
"password": "pass8909",
"primaryEmailAddress": "Jmeter${random}#gmail.com",
"address": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
}
My Beanshell Preprocessor script is
vars.put("random",${__Random(1,1001)})
When i ran, it is not replacing ${random} with the value generated. Need help here

vars.put() function takes 2 Strings as parameters and you're trying to pass a non-String value to it. One of the solutions would be using vars.putObject() instead
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting, see Apache Groovy: What Is Groovy Used For? article for more details
You don't need any scripting there at all, just inline the __Random() function into your request body:
{
"firstName": "Jmeter",
"lastName": "LoadTest",
"userName": "Jmeter${__Random(1,1000,random)}",
"password": "pass8909",
"primaryEmailAddress": "Jmeter${random}#gmail.com",
"address": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
}

Related

JMETER - response assertion -> Response was null

I have checked several options how to validate response and it still not works in my case.
As you can see (Debug sampler), for last_name regex works and I receive some values, but if I want to reuse this response as "Response assertion", test failed, because I receive "Response was null".
JSON Extractor for last_name
Response assertion
Test failed
Also, I would like to count objects in "data", so I used http://jsonpath.herokuapp.com/?path=$..book[0,1] to make sure, the JSON file is correct. But "Debug sampler" shows me nothing :( so test failed again
Count of objects
Result
Any idea how to solve this problem please ?
JSON:
{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson#reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson#reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://reqres.in/img/faces/8-image.jpg"
},
{
"id": 9,
"email": "tobias.funke#reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://reqres.in/img/faces/9-image.jpg"
},
{
"id": 10,
"email": "byron.fields#reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://reqres.in/img/faces/10-image.jpg"
},
{
"id": 11,
"email": "george.edwards#reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://reqres.in/img/faces/11-image.jpg"
},
{
"id": 12,
"email": "rachel.howell#reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://reqres.in/img/faces/12-image.jpg"
}
],
You need to tick Compute concatenation var in JSON Extractor, otherwise you will get 2 separate JMeter Variables:
lastName_1=Lawson
lastName_2=Ferguson
In addition to point 1 you need to use lastName_ALL in the Response Assertion
You need to switch from Matches to Substring in "Pattern Matching Rules" of the Response assertion
Demo:
More information: How to Use the JSON Extractor For Testing

Jmeter - Compare/assert multiple data: Data from JSON against data from DB

For the purpose of my task i need to compare Data from JSON against data from DB, but i have few doubts how to build the scenario. My scenario is like:
1 Perform DB query
Which returns dynamic set like:
url secret
https://test1.com/ 1234
https://test2.com/ 1234
https://test3.com/ 1234
Based on this dynamic set, i drive my Loop controller to loop. Here from every single call, different JSON is produced like:
[
{
"adminLink": "",
"BTCAmount": 0,
"lastName": "test",
"amount": 1,
"clientId": "e1d4ab18517711eaa84cfa163eb75a2c",
"foundingSourceName": "test",
"secretId": "2938663415",
"txId": "",
"mcTxId": "1079249234",
"paymentAddress": "",
"result": "transaction timed out",
"firstName": "test",
"phoneNumber": "",
"currency": "USD",
"refoundAmount": 0,
"approveTime": 1582543463,
"email": "",
"status": 1,
"timestamp": 1581938595
},
{
"adminLink": "",
"BTCAmount": 0,
"lastName": "test",
"amount": 550,
"clientId": "ffe22f34742311eab73f06ed6719cf46",
"foundingSourceName": "test",
"secretId": "3096308675",
"txId": "",
"mcTxId": "1101155492",
"paymentAddress": "",
"result": "transaction timed out",
"firstName": "test",
"phoneNumber": "",
"currency": "USD",
"refoundAmount": 0,
"approveTime": 1586355699,
"email": "",
"status": 1,
"timestamp": 1585750862
}
]
2 From this dynamic json, i can extract: mcTxId with:
3.
For every single mcTxId , i need to perform JDBC query using:
select *
FROM affiliate_transaction
WHERE affiliate_id = 1 and mctxid = '${mcTxId_1}'
Which result to:
I managed to solve up to Loop Controller, and extract every single mcTxId, but i am stuck to nested looping logic, and assert each data.
How can i compare/assert every single clientId & approveTime between API call(json) and DB query, where their data set is always dynamic?
Any help is highly appreciated.
Apologies for the long post
Change your query to select client_id
FROM affiliate_transaction
WHERE affiliate_id = 1 and mctxid = '${mcTxId_1}' and store it into a JMeter Variable like client_id_from_db
Use JSON Extractor to get the client ID from the API and store it into a JMeter Variable like client_id_from_api
Once done you should be able to compare 2 JMeter Variables using Response Assertion

Parallel execution of Samplers in JMeter with different URL parameters

There is "ForEach" controller in JMeter which takes an array of items and executes a sampler with each item. This is very useful when you need to execute a sampler with different parameters based on data received in previous request. However, "ForEach" controller runs samplers one after the other.
What I am looking for is, execute the samples in parallel. There is a plug in available in JMeter called, "bzm - Parallel Controller". However, this doesn't accept any input variable like "ForEach" controller does.
For example, I have following data in database. Author along with their books.
[
{
"firstName": "William",
"lastName":"Shakespeare",
"Title": "Mr",
"id": "1",
"books": [
{
"id": "WS1",
"title": "King John",
"year":"1596"
},
{
"id": "WS2",
"title": "Julius Caesar",
"year": "1599"
},
{
"id": "WS3",
"title": "Romeo and Juliet",
"year": "1595"
}
],
"Nationality": "English"
},
{
"firstName": "Sidney",
"lastName":"Sheldon",
"Title": "Mr",
"id": "2",
"books": [
{
"id": "SS1",
"title": "The Naked Face",
"year":"1969"
},
{
"id": "SS2",
"title": "A Stranger in the Mirror",
"year": "1976"
},
{
"id": "SS3",
"title": "Bloodline",
"year": "1977"
}
],
"Nationality": "American"
},
{
"firstName": "Eiichiro",
"lastName":"Oda",
"Title": "Mr",
"id": "3",
"books": [
{
"id": "EO1",
"title": "Wanted",
"year":"1992"
},
{
"id": "EO2",
"title": "Ikki Yako",
"year": "1993"
},
{
"id": "EO3",
"title": "Monsters",
"year": "1994"
}
],
"Nationality": "Japanese"
}
]
In my JMeter Test plan, I have defined a CSV Data Set Config file to store the ids of all authors in my system.
And then, there is a Thread Group. Inside thread group, I have a HTTP Sampler, GET /authors/{id}/books. for Example GET /authors/1/books. This will get all the books written by author "William Shakespeare".
Using JSON Extractor, I can capture the array of book ids returned by GET /authors/{id}/books.
There are 3 books with ids WS1, WS2 and WS3. Now for each of the books, I need to run another HTTP Sampler
PUT /books/WS1
PUT /books/WS2
PUT /books/WS3.
When I use ForEach controller, I can specify the input variable which was captured from previous JSON extractor. And it loops through each PUT request, for each book id. I want to do the same, but in PARALLEL, not sequential.
Does anyone know how to achieve this? or we have to write custom groovy/BeanShell script for this? If custom Groovy/BeanShell script is the only way, can you please tell me how to write this
Many thanks
You can do that by placing each test in a separate threadGroup.
You have a flag in the main test plan that controls if threads are executed in parallel or consecutively.
You're using not very correct test element, consider switching to Parallel Sampler instead.
Given you have the following JMeter Variables:
book_1=WS1
book_2=WS2
book_3=WS3
book_matchNr=3
You can add a Parallel Sampler and configure it to hit URLs containing ${book_1}, ${book_2} and ${book_3}
All the URLs to Retrieve will be executed in parallel:
More information: How to Use the Parallel Controller in JMeter
You could dynamically add URLs to Parallel HTTP Requests sampler.
Add JSR223 PreProcessor like a child to Parallel HTTP Requests sampler.
To script area add something like that:
String url = "https://examle.url.com/?book=";
1.upto(vars.get('book_matchNr') as int, {
index -> {
sampler.addURL(url + vars.get('book_' + index))
}
});

How to extract multiple json values from a Json response

I am trying to extract multiple values from a JSON response on my jmeter script. Below is sample of my response:
{
"startDate": "2018-12-10T15:36:34.400+0000",
"userId": "7211111-2fa90",
"createdBy": "TEST",
"note": {
"content": "Application Submitted "
},
"Type": "SUBMITTED"
},
"currentEventState": "CLOSED",
{
"Xxxx": "test",
"Loc": null,
"Zipcode": [],
"Locality": 82,
"Address": {
"Add": 12302,
"Add2": "place",
"Zip": {
"Phone": "home",
"Email": "test#test.com"
}
},
"state": "MD",
"Cost": "E "
},
"AppID": "cd8d98e6-c2a79",
"Status": "CLOSED",
}
I am trying to extract userid and AppID for the case if the TYPE is Submitted and Status is Closed.I tried using the Json extractor with $.[?(#.Type=="SUBMITTED")].[*].?(#.Status=="CLOSED").userid,APPID, but couldn't get the expected result. Could anyone guide me on this.
You need to use an inline predicate to combine 2 clausees and a semicolon in order to store results into 2 separate JMeter Variables.
Add JSON Extractor as a child of the request which returns above JSON
Configure it as follows:
Names of created variables: userid;appid
JSON Path Expressions: $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].userId; $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].AppID
Default values: NA;NA
Here is the demo of single expression working fine:
And here are extracted values reported by the Debug Sampler:

Jmeter : How to extract first element from json array

I am trying to extract first element from a json array. Below mentioned is json array
[
{
"cohortDefinition": {
"Key": 1151,
"id": 1798,
"srcId": "3526",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2017-01-06-1483749546167",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T17:48:45Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
},
{
"cohortDefinition": {
"Key": 1150,
"id": 1796,
"srcId": "3525",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2016-10-27-1477620919644",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T16:57:11Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
}
]
From above json i would like to extract first value ie. srcId": "3526".
I tried doing following expression in Jmeter extractor
$..cohortDefinition.srcId[1]
However it is not working. If anyone know how to do this please do let me know.
After JMeter 3.0, you can use JSON Extractor, see:
https://stackoverflow.com/a/47043204/460802
Before JMeter 3.0:
Please follow the below steps to retrieve srcId.
Add a JSON Path Extractor to your request and configure below values.
Destination Variable Name - myVar
JSON Path Expression - $..cohortDefinition.srcId - this will extract all the srcIDs from the JSON.
Default Value - Not Found or Err
Add a Debug Sampler and View Results Tree to your test plan.
Save it and execute.
In Debug Sampler, you can view all the srcId as shown below.
You can now use myVar_1 and myVar_2 in your test plan
using ${myVar_1} ${myVar_2}
No need for Plugin, JMeter has a JSON Extractor that will provide this feature:
Notice:
JSON Path Expression is: $..cohortDefinition.srcId
Match No : 1

Resources