How to Replace the env name from the domain name that is reading the from the CSV file in jmeter - jmeter

I am reading the domain url from the CSVdatafile before hitting I need to replace the environment with some String
How I can achieve in Jmeter
data file entries
Tried following by adding the BeanShellPreprocesser
print("------Replcing the environment name------");
var str =new Stirng[]{${siteUrl}};
var res = str.replace("frep", ${env});
SampleResult.setResponseData(res);
still it is not working.
I need to read each entry from the Datafile and replace the "frep" with "abc" and then i need to hit the url
How I can achieve this in Jmeter?

According to your scenario Beanshell code should look like:
String siteUrl = vars.get("siteUrl");
siteUrl = siteUrl.replaceAll("frep", vars.get("env"));
vars.put("siteUrl", siteUrl);
Beanshell is more like Java, not JavaScript. You can use __javaScript() function to perform the substitution if you're more comfortable with it.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more detailed explanation of Beanshell scripting in JMeter.
Also be aware that

Related

How to use StringFromFile function within a JSR223 Sampler?

I can't find any good docs/examples on how to use the StringFromFile function to get multiple records from a source data file from within a JSR223 Sampler. Could somebody please help me?
My code which doesn't work:
String filename = "filename.csv";
String record = StringFromFile(filename,"id");
You shouldn't be using it directly in the scripts, as per JSR223 Sampler documentation:
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
So the solution would be using File.readLines() function which reads the file into a List of strings
More information: The Groovy Templates Cheat Sheet for JMeter

How to modify HTTP request before sending in JMeter through Beanshell pre processor?

I have test case in my csv file. The request URL has a custom variable.
Sample URL : .../abc/$id
I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:
String path = ctx.getCurrentSampler().toString();
path.replaceAll("$id", id);
I am not able to set this updated URL in jmeter context (ctx)
You need to assign new path value to path variable
You need to set sampler path to the new value using sampler.setPath() method
So you need to amend your code like:
String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);
Demo:
Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.
Try to avoid the pre / post processors if possible.
Your requirement is very simple and straight forward.
Directly use this in the path - assuming id is the name of variable which has the value.
/abc/${id}

how can i pass dynamic values to url in jmeter

I have to give dynamic values to url which takes number of users and their age , which can be selected though web page. but I want to give it in Jmeter using BeanShell PostProcessor.
Help me in this,since I'm new to Jmeter.
This is the path:
/destinations/packages?airports%5B%5D=LGW&units%5B%5D=000577%3ADESTINATION&when=29-05-2016&until=&flexibility=true&flexibleDays=3&noOfAdults=2&noOfSeniors=0&noOfChildren=1&childrenAge=3&duration=7114&first=0&searchType=search&searchRequestType=ins&sp=true&multiSelect=true
from what I've got looks like you can use CSV Data Set Config.
Create .txt file with the data you want to feed your test with;
Place the above .txt file to the folder where your .jmx file lies;
In your Test plan: Under your request sampler - place CSV Data set Config;
Then if you need to use your dynamic values within one threadgroup => refer these data as ${quanity}, ${age} in you url.
If you need to pass these values across the threadgroups => add BeanShell Assertion
Then (in the other Tread group) refer those as ${__property(_quantity)},${__property(_age)}.
Hope, it helps.
First of all you need Beanshell PreProcessor, not Beanshell PostProcessor.
All this parameters are basically name/value pairs which can be defined via HTTPSamplerBase class. HTTPSamplerBase class instance is available to Beanshell PreProcessor as sampler pre-defined variable so if you add the following code into the Beanshell PreProcessor "Script" area
sampler.addEncodedArgument("airports[]","LGW");
sampler.addEncodedArgument("units[]","000577:DESTINATION");
sampler.addEncodedArgument("when","29-05-2016");
sampler.addEncodedArgument("until","");
sampler.addEncodedArgument("flexibility", "true");
sampler.addEncodedArgument("flexibleDays","3");
sampler.addEncodedArgument("noOfAdults","2");
//etc
Your HTTP request will be populated with the values, you set via Beanshell.
JMeter Variables can be accessed via vars shorthand which stands for JMeterVariables class instance.
String airport = vars.get("airport");
sampler.addEncodedArgument("airports[]", airport);
//etc
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on how to use Beanshell Test Elements in Jmeter.
Remember that it is recommended to avoid scripting where possible so if there is another way of implementing your task - go for it.

jMeter How to get Multipart body in BeanShell PreProcessor

In jMeter How to get multi part body in BeanShell PreProcessor
I need to get the image data and post parameters
by using sampler.getArguments(); I am able to get the post parameters but not the image file
Please help me
You can use getHTTPFiles method of Sampler API.
sampler.getHTTPFiles() will return the file path HTTPFileArg in an array through which you can update new file at run time.
Update:
String path = sampler.getHTTPFiles()[0].getPath();
byte[] array = Files.readAllBytes(new File(path).toPath());
Something like:
File image = new File(sampler.getHTTPFiles()[0].getPath());
//do what you need with the image file
If you need extended image information take a look at ImageIO. For more Beanshell tips and tricks check out How to Use BeanShell: JMeter's Favorite Built-in Component

JMeter - how to randomize number of parameters in request

I am new to JMeter.
In my GET request I want to have random number of parameters, so sometimes I want to have:
a = value1
a = value2
a = value3
and sometimes I want to have
a = value1
a = value2
etc.
Can I achieve it in JMeter in another way than making separate request modules?
You can specify parameters dynamically in the Path field, using a variable:
The variable should be created / formatted before HTTP request is sent. For example here I am using counter and BeanShell pre-processor to create a proper set of parameters:
So if I run this with 3 iterations, I will get:
GET http://stackoverflow.com/x?a=value0
GET http://stackoverflow.com/x?a=value0&a=value1
GET http://stackoverflow.com/x?a=value0&a=value1&a=value2
etc. Of course the logic of creating params should be based on your needs, this is merely an example. The reusable part of that example is saving params in a string, and then saving them into variable:
String myDynamicParameters = "";
// your logic here
vars.put("myDynamicParameters", myDynamicParameters);
If you want to generate variable directly inside your GET parameters, the fastest way is to use inline snippets.
something like:
GET http://xx.com/${__Random(1,99999)}
You can do it via i.e. Beanshell PreProcessor like:
sampler.addArgument("name", "value");
See:
HTTPSasmplerBase.addArgument() method JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component for comprehensive information on using JMeter and Java APIs in your test.

Resources