In jmeter, can we use few parameters with in what we declared in the HTTP request parameter section - jmeter

In my case i have created one HTTP Request with all the possible parameter as below -
My .csv file is looking as below -
For some test case i need to send details in one or two parameter only, not for all. Now how can i do that in the same HTTP request without creating a new one?

Theoretically you can just send empty parameter values, just make sure that you have a blank value in the CSV file, i.e.:
param1,param2
foo,bar
baz,
,qux
Alternatively if you want to completely remove the parameters with empty values from the request you can add a JSR223 PreProcessor as a child of the HTTP Request sampler and put the following code into "Script" area:
def newData = new org.apache.jmeter.config.Arguments()
0.upto(sampler.getArguments().size() - 1, { idx ->
def arg = sampler.getArguments().getArgument(idx)
if (!arg.getValue().equals('')) {
newData.addArgument(arg)
}
})
sampler.setArguments(newData)
This way JMeter will remove the parameters which don't have their respective values from the request:
In the above example sampler stands for HTTPSamplerProxy, see the JavaDoc for all available functions decriptions
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

Related

Extracting value from jmeter post request

I want to extract value of the parameter sent through post request in jmeter.
I know the use of regular expression for extracting response value or request URL but here I would like to extract the value of post request.
I've been thorough how to extract value from request in Jmeter but it didn't worked.
Not sure why do you need it as given you sending "something" you should already have that "something" hence you don't need to extract it, however here you go:
In order to save 1st parameter value (or the whole post data if you use "Body Data" mode):
Add Beanshell PostProcessor as a child of the HTTP Request.
Put the following code into the PostProcessor's "Script" area:
String request = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
vars.put("request", request);
You will be able to access extracted value as ${request} where required.
Clarifications:
ctx - shorthand for JMeterContext class instance
getCurrentSampler() - in case of HTTP Request sampler stands for HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java API from Beanshell test elements in your JMeter test.
I added a Beanshell PostProcessor in my http request with following code.
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
Argument arg = argz.getArgument(i);
String a = arg.getValue();
vars.put("EMAIL",a);
}
Explanation: I get a my request as a json and put it in EMAIL. Now I can use EMAIL as a variable in my other request.
Then, I added a jp#gc Json Path Extractor and I applied it to a Jmeter Varaible.
Now, Email will be used as variable, which contains my json request and I can extract using jsonPath Extractor.
An easy way to do this is using the JSON Path Extractor.
There are just
For the example you gave
{ "data" : { "name" : "john_doe", } }
'Variable Name: YourNewVar'
'JSON Path: $.data.name'
Should work, but you may need to do some experimenting.
You may want to add a "debug sampler" (its one of the standard samplers) and put in its title $YourNewVar so you can see what is being extracted.
Beanshell and "Regular Expression Extractor" will work, of course, but may be a little harder to use if you are not familiar with them.

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

JMeter - do not pass post value in post data if null

In my JMeter script, I have one HTTP request which has 4 different parameters to be passed in post body. I have corresponding variables. Values of these variables are not available every time, depending on configuration.
If a value is not available, I get an error "bad request". How do I see if a variable is not null and only then pass corresponding parameter in request post body?
Given you have the following configuration:
and you don't want to send foo parameter if ${bar} variable is not defined
Add Beanshell PreProcessor as a child of your HTTP Request Sampler
Put the following code into the PreProcessor's "Script" area:
if (vars.get("bar") == null) {
sampler.getArguments().removeArgument("foo");
}
Where:
vars - is a shorthand to JMeterVariables class instance
sampler - shorthand to parent sampler implementation class instance, in this case - HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Java and JMeter API from Beanshell scripts.
Just use the Logic Controller - If Controller. It allows to define the if statement using your variables. So, you can perform your actions only in case all parameters are not equal to null:
I've defined one single User Defined Variable in this example. Jmeter sends HTTP request only if it has a value defined.

How to Handle Dynamic Requests in JMeter - Which may/may not occur with each run

In Web Application :
There is Single Page having Different Sections/Partitions
Each section Retrieves the Data with the Help of Filter Query.
If there is no matching Result, Section will Send below Request only :
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
If the Query returns any matching Result, Section will Send below Two Requests:
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
Request 2: domain/search/jobs/csuser__search2_1413357426.1559/results_preview
How can I manage Request 2, which may or may not occur with each run.
Currently I am manually Recording entire Network calls, Removing unnecessary ones & running it for 'N' Loop Count. How can I make sure while test is runing if any section has matching results Request 2 Should also be taken care which I might not have benn recorded on my first execution.
You can use combination of Beanshell PostProcessor and If Controller to work it around as follows:
Add a Beanshell PostProcessor as a child of the Query request
Put the following code into the PostProcessor's "Script" area:
int length = prev.getSubResults().length;
if (length > 1) {
String path = prev.getSubResults()[length - 1].getURL().getPath();
if (path.contains("results_preview")) {
vars.put("resultPresent", "true");
}
} else {
vars.put("resultPresent", "false");
}
Add an If Controller after the request
Depending on your scenario put on of the following conditions to If Controller's "Condition" input
${resultPresent}==true - children will be executed if the query returns results
${resultPresent}==false - children will be executed if the query doesn't return anything matching
Beanshell code does the following:
check how many requests were executed
if there were more than 1 requests, path of the last request is extracted
if path contains "results_preview" variable resultPresent is being set with the value of "true"
if there was only 1 request then resultPresent variable is false
References:
How to use BeanShell: JMeter's favorite built-in component
JMeter “if controller” with parameters
SampleResult class JavaDoc
As Dimitri said. The processor should run after (post) request 1. The if controller will then act on the results of the processor. Section two is only run when the condition is not "false".

Conditionally sending jmeter variables with HTTP request

I am using JMeter to send HTTP POST requests.
My body of the request is JSON, for example something like {"Var1": "${Var1}","Var2": ${Var2},"Var3":"${Var3}"}.
These are set in the parameters of the HTTP requests with no name for the parameter. This works fine and I am able to send requests using the variables that I set in a beanshell pre processor (by setting the variables and using vars.put() ).
My question is how can I send programmatically through the preprocessor part of the parameters? For example:
if(a){
send parameters `{"Var1": "${Var1}","Var2": ${Var2}` as my JSON
}
else {
send parameters `{"Var3":"${Var3}"}` as my JSON
}
vars.remove() doesn't work for me as it removes the value from the variable but still sends it in the request (for example as "${Var1}").
Replace the preprocessor by a Beanshell Sampler that will compute a boolean value a and put it as a var:
vars.put("a", value)
Then use 2 If Controllers where each one will contain a sampler with the different parameters.
Condition of first one will be ${a} and for be it will be the negation of ${a}.
Just use the "Body Data" tab. You can conditionally create the JSON string and then just "print" the variable in the body data using normal placeholders.
The easiest and fastest way of achieving what you want to do is to use the JMeter if controller (Add -> Logic controller -> If controller).
You add an if controller to the Thread Group that you're working on and place your expression that returns a boolean in Condition (default Javascript). As a child node for the if controller you place the HTTP Request sampler that you want to fire in case the if is successful.
Suppose you want to send a request if a property that you are passing to JMeter exists:
${__P(media)}.length > 0
The you add another if controller with a negated condition for what you just checked with another HTTP Request sampler.
You're done.

Resources