I am testing on a site which has multiple options.
**For example, **
let's say on the home page (1st sampler) we have two options Option A
and Option B. So if I choose option it will navigate to another page
(2nd sampler) and show me some results regarding option A and from
those results I will click on any of the results and go to the next
page (3rd sampler).
But when I click on Option B on the home page (1st sampler), there is no result to show me (in the 2nd sampler) so I don't want it to navigate and execute the 3rd sampler.
I want to stop the execution if there is no result coming in the result page of option B. How should I implement this in JMeter.
To accomplish this, you will need to add Beanshell post-processor on your Option B with the following code:
import org.apache.jmeter.threads.JMeterContextService;
String prevResult = JMeterContextService.getContext()
.getPreviousResult().getResponseDataAsString();
if(prevResult == null || prevResult.isEmpty()) // | ---> ||
JMeterContextService.getContext().getThread().stop();
This will check previous response and if its empty stop current thread. If response is not empty, test execution will continue.
I would recommend using
Regular Expression Extractor
If Controller
and Test Action Sampler
combination like:
Related
Below code is in While controller
${__jexl3(${__jm__While Controller__idx} < 3 && "${responseCode}" != "200",)}
enter image description here
enter image description here
Above is in Regular Expression extractor
Result: It is looping 3 times for successful requests also but I want to loop only for failed requests and for success requests it should execute once
Thanks for in advance
Your regular expression extractor configuration is not correct, you need to apply it to Response Code, not to the Body
With your current configuration it extracts first numeric value from the response body and HTTP status code is not there, it's a special response header
Following another solution with a While Controller, If Controllers, and Flow Control Action (FCA) controllers.
Add the following into the While controller
${__groovy(${__jm__WC__idx}.toInteger() < 5)}
Add the request and two If controllers below them with Flow Control Action elements as shown in the figure
Add the following into the first If Controller
${JMeterThread.last_sample_ok}
Select Break Current Loop in the FCA element
Add the following to the second If Controller
${__jexl3(!${JMeterThread.last_sample_ok})}
Set the FCA to Go to the next iteration of the next loop
If you want to do something else, in addition, to retry, Set pause in the FCA element, add a JSR 223 element inside the FCA and include the actions. For example, I wanted to log the user name of the failing requests and retry.
String username = vars.get("username")
int current_loop_count = vars.get("__jm__WC__idx").toInteger()
log.warn("Attempt# ${current_loop_count.next()} $username ")
current_loop_count.next()<5?:ctx.getThreadGroup().getOnErrorStartNextLoop()
Here is my scenario:
5 users log in to the website which purpose is to shorten url links
(like bilty.com or tiny.cc).
Each user inputs a unique link and gets shortened result.
What I need to do is - to get some kind of analysis of this "shorten" request.
Also, I need to:
see the output (the shortened link) in this analysis.
check if the link was created or not.
check if the shortened links are correct.
make sure that the shortened link actually leads to the same website
just like the corresponding link in the input.
EDIT: I deduced that it should be made via Response Assertion, but I can't figure out how exactly.
Approach1(Grey Out-1st Thread group):- Put two request in parallel. First is full and second is redirect. Check option redirect automatically in second http request which is redirecting. Now, put compare assertion and check. This takes lot of resource.
Approach2:-Put 2 HTTPs request in parallel. First is full HTTP request without any redirect and second HTTP request is for short url and having follow redirect option checked.
Then, use regex in both (two regex used) to fetch the URLs. Check option "apply on"-> sub-samples as show for short url sampler, if it is redirecting. Not required for sampler if there is no redirect. First sampler regex uses "apply on" as "Main Sample Only" as there is no redirect.
Last compare them in the JSR223 sampler to make last sample fail.
I have used JSR223 sampler. You can also choose to have other approach also for comparison.
Hope this helps.
Update:-
Assuming you have data in two columns in csv as shown below image. Go to bin>user.properties file and put sample_variables as two column names used in the csv. Please restart jmeter if already open after editing user.properties file.
Put the first parameter in the http sampler.
Put the assertion as dynamic using second variable from the csv.
Note:- Do check the options in assertion to get what is required in your scenario. Follow redirection and Redirect Automatically have difference which required different assertion "apply to":- Main Sample or Sub-Sample. Do check them, if required.
Please check if this helps.
There is a URL which I want to hit and save the response. The URL id needs to be incremented each time and save the response. For example -
First Get Request - http://google.com/getdata/?Id=1
First Response - one
Second Request - http://google.com/getdata/?Id=2
Second Response - two
and so on...
I want to hit the request with increment the id each time and save the response
I have tried using fiddler but unable to figure how to increment the id and save the response.
P.S. - I have to make around 6,00,000 hits
Since the 'Postman' tag is mentioned, I can help you regarding how to implement this in Postman.
Postman has a nice feature of using 'variables'.
You can use environment variables or globals.
Read more about these on their docs:
https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables
You can use a global variable such as 'counter' and set it to 1 / whatever starting point you want.
Then you can modify your request like so :
http://google.com/getdata/?Id={{iteration}}
Now, in the Tests script of the request you can write the following script
let i = parseInt(pm.globals.get('iteration')) + 1;
pm.globals.set('iteration', i);
Also, to access the response you can use the following command in Test script:
console.log(pm.response); // Use pm.response as per your needs
Save the request in a collection.
Now load the Postman's Runner and select the collection.
Now you can put an iteration count of 6,00,000 and hit run!
Remember, heavy iterations will cause performance degradation.
In JMeter you need to click , Ctrl+0 and Ctrl+1 to create , Thread Group and HTTP Request
In Thread Group put the number of hits you need in Number of Threads (users)
In HTTP Request Put in Server Name or IP www.google.com and in Path /getdata/?Id=${__threadNum}
__threadNum will create increasing number from thread 1 to number of hits.
For small number of hits or debugging you can add View Results Tree to view request/response by clicking Ctrl+9 in Test Plan/Thread Group level.
To save the response use Post Processor, especially by adding Regular Expression Extractor below HTTP Request by clicking Ctrl+2.
Allows the user to extract values from a server response using a Perl-type regular expression. As a post-processor, this element will execute after each Sample request in its scope, applying the regular expression, extracting the requested values, generate the template string, and store the result into the given variable name.
Import to notice that for load testing you need to work with non GUI mode, which means call jmeter using command line as jmeter -n -t myTest.jmx
you will use Command-line mode (called Non-GUI mode) to run it for the Load Test.
Don't run load test using GUI mode !
For saving all responses to a one file see save response data or if you want to save file per thread/user you can add Save Responses to a file
Fiddler:
Open script editor (Control + r ) then add the following code inside OnBeforeResponse
static function OnBeforeResponse(oSession: Session) {
if(oSession.oRequest["X-SAVE-ME"] != "")
{
oSession.SaveResponseBody("C:\\tempfiddler\\" + oSession.SuggestedFilename);
}
}
Go to the "Composer" tab and include the header X-SAVE-ME with any value, in the URL, replace your ID with # (just like this: http://google.com/getdata/?Id=#) fiddler will now ask for the starting and ending value of ID before executing;
Please find the snapshot below for your scenario.
Scenario_Testplan
First, go to user properties and put "sample_variables = ID, Response_File_Name" or whatever the name you choose for the variables. Restart jmeter.
Create the below plan:-
CSV data set config to have incremental values and response file name
HTTP request will use ${ID}
Save response to a file will use ${Response_File_Name}
Hope this will help.
I would do this by command line, using a while loop with a curl to the URL, storing the body result on the standard output to a file. It would look something like this:
for i in {1..600000}; do curl "http://google.com/getdata/?id=$i" > body-result-id-$i; done
I couldn't test the line above because I don't have any access to a console right now, but I think it should work.
In Burp you can do this using the Intruder tool. First, capture a sample request in Burp. If you're unsure how to do this, please consult the getting started documentation.
Then right-click the request and selected "Send to Intruder".
In the Positions tab within Intruder, first click "Clear" then select the section you want to vary, and click "Add"
In the Payloads tab select the Payload type as "Numbers" and configure the range.
Click "Start attack"
For more information, consult the documentation.
One Another solution is that you can use Counter in jmeter. That you can find from below path
Thread Group > configElement > Counter.
Configure the Counter as per your need.
Give the Reference Name i for counter.
Use the variable in your request
For more information.
I have the following script with 1 thread and 2 iterations.
Debug in Switch is not called. The second Google in the first iteration and the second Yahoo in the second iteration are not executed. Why?
Thank you for the help.
I added the image of Switch Controller.
Google and Yahoo are Simple Controllers with one HTTP Request Sampler.
Remove spaces in the Domains.csv file in the second column. As of now action= Google is checked instead of action=Google. so the behaviour.
Domains.csv
domain_1,domain_2
Google,Google
Yahoo,Yahoo
Note: As you are using Switch Controller, it executes only matching element inside of it.
Debug Sampler in Switch element will never be matched as you are looking for either Google or Yahoo.
As Edi Prayitno mentioned, you can keep it inside the Simple Controller, if you want to execute Debug Sampler in Switch every time.
Based on the help of Switch Controller above, you put the Switch Value = ${action}. It means you filled the Switch Value with the name of the subordinate element. When ${action} name = Google, it will execute subordinate element = Google. When ${action} = Yahoo, it will execute subordinate element name = Yahoo.That means Debug in Switch will be never be called.
If you want to put debug step inside of Switch Controller, you can re-arrange your test as below:
I hope that helps you.
I'm new to JMeter, I want to validate a JMeter test input variables defined as part of "User Defined Parameters". Let's say I have a variable "sessions" and my tester should pass input values in between 0 to 30 for sessions, if tester passes other than this range the test should not go further and should throw an error with appropriate message.
Is it possible with any kind of JMeter controllers/assertions/... without writing code for validation?
I am not sure is there any efficient/direct way of doing this. but I achieved your requirement as follows:
Add Setup Thread Group (before any other ThreadGroup). select radio button Stop Test Now in Action to be taken after a Sample error
Add Debug Sampler to the Setup Thread Group.
Add Response Assertion (RA) to the Debug Sampler.
In RA, Select JMeter Variable in Apply to section.
In RA, select Matches in Pattern Matching Rules section.
In RA, add the regex ^[0-9]$|^0[1-9]$|^1[0-9]$|^2[0-9]$|^30$ in Pattern to test text area.
Test will be stopped if you provide sessions value other than 0-30 in User Defined Variables, as Setup Thread Group is configured to Stop Test Now on Sample error.
Note1: Added View Results Tree Listener for confirmation whether the test is continued and also to check the error message. View Results Tree is added only for visual confirmation, must be removed during the load test, does not effect the actual logic.
Note2: I am not aware of any component, which can show custom message/alert to the user. so, used View Results Tree. We should remove this command during load testing. I added here for visual confirmation purpose. If not present also, Test will be stopped on wrong value for sessions, i.e., other than 0-30
Note3: We need a Sampler component in order to apply an Assertion. so, added Debug Sampler. Debug Sampler just reports all the JMeter variable values at the point of its execution.
Image references:
Setup Thread Group:
Response Assertion:
View Results Tree:
You cannot achieve such validation in GUI without amending JMeter source code but you can check the variable range using scripting.
For example, add a JSR223 Sampler as a child of the first request and put the following code into "Script" area:
import org.apache.commons.lang3.Range;
int sessions = Integer.parseInt(vars.get("sessions"));
String errorMessage = "Provided sessions number is not between 1 and 30, stopping test";
if (!Range.between(1, 30).contains(sessions)) {
log.info(errorMessage);
SampleResult.setSuccessful(false);
SampleResult.setResponseMessage(errorMessage);
SampleResult.setStopTest(true);
}
Demo:
Make sure you are using Groovy as a language (the option should be default as per JMeter 3.1, if you are using earlier JMeter version for some reason - you will have to choose groovy from the "Language" dropdown)