JMeter: Is it possible to add HTTP Request sampler during run? - jmeter

I have an HTTP request that returns a JSON object that contains multiple urls (mostly image resources).
I do not know the number of the urls that will be in the response JSON and that number may vary during time.
I need to create an HTTP request for each of the urls that i received.
Is it possible to create HTTP Request samplers while running the JMeter flow?
Is there a different solution one can advise me to do in order to send HTTP requests to the list of the urls?
Thanks

Add JSON Extractor as a child of the main request and come up with a JSON Path expression to extract links to images and whatever else URLs you would like to hit
Add ForEach Controller and configure it to read the JMeter Variable reference name from step 1 and set output variable to anything meaningful
Add HTTP Request sampler as a child of the ForEach Controller and configure it to use the output variable
That's it, JMeter will trigger a HTTP Request sampler for each URL present in the original JSON
Given you have the following JSON:
{
"urls": [
{
"url": "http://example.com"
},
{
"url": "http://jmeter.apache.org"
},
{
"url": "http://jmeter-plugins.org"
}
]
}
You can extract the URLs into JMeter Variables using the following JSON Extractor setup:
it will give you the following JMeter Variables:
url_1=http://example.com
url_2=http://jmeter.apache.org
url_3=http://jmeter-plugins.org
url_matchNr=3
now if you add ForEach Controller and configure it like:
you will be able to refer the URLs as ${current_url} in the HTTP Request sampler which is the child of the ForEach Controller

Related

I want to send the data that has been url encoding in Jmeter

I want to send a data that has been encryption.
So, I used JSR223 Sampler.
---skip---
def encrypted = Crypto.encrypt("AES/CBC/PKCS5Padding", "{"MY_SENTENCE"}", Crypto.generateSecretKey(), Crypto.generateIv())
vars.put("enc_data", encrypted);
Body Data in HTTP Request.
{ "data": "${enc_data}" }
In Results Tree
Request Body data was not url-encoding.
I want to send a data of url encoding, what should I do?
I don't know that.
I wrote Body Data in HTTP Request. So, I can't click the Parameters.
And I added Content encoding (UTF-8) it was not working too.
Either use __urlencode() function in the Body Data tab directly:
{ "data": "${__urlencode(${enc_data})}" }
JMeter Functions can be placed anywhere in the test plan and they're evaluated in the place where they're present so your ${enc_data} variable will be url-encoded in the runtime. See Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept.
or call URLEncoder.encode() function in your Groovy script directly:
vars.put("enc_data", URLEncoder.encode(encrypted,"UTF-8"));

Jmeter: Capturing of tokens (Say 50 tokens) from single request and pass one token at a time to the next upcoming 50 requests

we have certain token mechanism recently implemented into our project. What it does is, say it generates 50 tokens while triggering the corresponding API. Now we have to capture this tokens and pass it sequentially one by one to the next 50 API’s request body. One way is by writing multiple json path extractors I have to capture this values individually and pass it to all the requests. But readability is not proper this way. Is there any other way to capture this tokens and pass it to all the next requests one each. Say capturing those into a file and passing it sequentially or something.
Sample Response from where tokens generated:
{
"data" : {
"requestTokens" : [ "9FDE794DD00E4A09122343BDCAF214E9", "616C5DFFC1234516A925824AEA6EFE9F", "7A8C507EC1DF4AD88E0912345E1DB409", "763C32CF67899946B6BC946949BD1344", "3C143F2FC25E495012345500E0F343DF", "3FD78335C763420B1234574061D9417F", "C43C368A1E612345AB17D2BA2693BEAF", "56E8FA9036D3486123451DE3237004DC", "5867B8E399FB4E12345626337D0E143C", "D06B30BDEAFC4A7D8618BF67712345DB", "F795258D390D4812345EB62C83BEFC58", "F0829D861234560392DE432E121B3CD4", "C8B9D5E6BE6A465FB91B0123459FBA9A", "4169D93D97204123457BA5A98C914D27", "784809E5BCBF4F123459D7D848AD67CE", "D0BFDCDC13994C0123455B2B110C35F9", "4F59619BBEDE4FE812345EA14C5E785", "E3942AE182214605BE91234595D95C18", "2005C506659C425EAD2022446123459B", "FE716E2A13A74C759C12345AED5AD54C" ]
}
}
Add JSON JMESPath Extractor as a child of the request which returns this JSON
Configure it as follows:
Add ForEach Controller after the request which returns this JSON
Configure it as follows:
Put your "next request" as a child of the ForEach Controller
Refer the token as ${token} in this request
That's it, ForEach Controller will loop as many times as there are tokens in the first response and each iteration the ${token} JMeter Variable will be updated with the new value
Demo:

I am getting the Billdesk URL in the response of booking API, I have to navigate to the URL and make payment on browser. Can we do it using Jmeter?

I am new to Jmeter. I am getting the below-mentioned code in the response of the Booking API. I have to click on the 'requestUrl' field. It will redirect me to the browser and opens the billdesk page where I have to make payment. After successfull payment, my booking will be done. Each time we are getting the different URL. Can we do this using Jmeter?
"data": {
"requestUrl": "https://uat.billdesk.com/xyz",
"redirectUrl": "http:xyz",
"orderNumber": "5904"
}
Add JSON Extractor as a child of the request which returns the above JSON
Configure it as follows:
Names of created variables: anything meaningful, i.e. requestUrl
JSON Path Expressions: a JsonPath query matching the request URL, i.e. $.data.requestUrl
Other fields can be left intact:
Add HTTP Request sampler after the first request and put ${requestUrl} into "Path" field:
That's it, in the runtime the ${requestUrl} JMeter Variable will be substituted by the value from the previous request JSON response:
More information: API Testing With JMeter and the JSON Extractor

ForEach Controller won't run multiple HTTP Requests

I have a ForEach Controller that loops over an array of values which works. Inside the ForEach block I have two HTTP requests and one extractor.
ForEach Loop {
+ HTTP Request 1 (This uses attribute from ForEach)
+ Extractor
+ HTTP Request 2 (This uses attribute from Extractor)
}
The first HTTP request runs and the extractor as well but the second HTTP request fails to run.
I've checked the logs and nothing is mentioned about this HTTP Request. I also tried moving the Extractor out from under the HTTP Requestor 1 but that also doesn't work.
There is no problem to have multiple Samplers as ForEach Controller children
The possible reasons for not executing the 2nd HTTP Request are in:
Your extractor fails somewhere somehow, double check that the variable is set and has expected value using Debug Sampler and View Results Tree listener combination
Your 2nd HTTP Request configuration is not correct, i.e. invalid characters in "Server Name" field or unsupported protocol or the request is actually being executed but takes too long, etc. I would recommend looking into jmeter.log file as normally it has enough troubleshooting information

Set the whole request url in jmeter

I have a request, which gives upload url as response body.
{
"uploadUrl": "https://test.com:9000/sample_uploadurl"
}
I'm able to extract the uploadUrl using JSON extractor.
I want to use above upload url to in next http request. How to set the new request here ?
adding directly doent work, because JMeter prepends http/https before request, in this case we already have https.
It got failed because it has https://[https://test.com:9000/sample_uploadurl]
Leave HTTP Request fields empty except Path, put there the variable and it will be executed
As a special case, if the path starts with "http://" or "https://" then this is used as the full URL.
example
The options are in:
Put the whole ${UPLOAD_URL} to "Path" field of the HTTP Request sampler like:
however this approach might be flaky and cause the malfunction of certain configuration elements like HTTP Cookie Manager. So it's better to go for option 2.
Split the ${UPLOAD_URL} into individual components like protocol, host, port and path.
Add JSR223 PostProcessor after the JSON Extractor
Put the following Groovy code into "Script" area:
URL url = new URL(vars.get('UPLOAD_URL'))
vars.put('protocol', url.getProtocol())
vars.put('host', url.getHost())
vars.put('port', url.getPort() as String)
vars.put('path', url.getPath())
The above code will generate the following JMeter Variables
So you should be able to use the generated variables in the next HTTP Request sampler:

Resources