How to get Machine IP Address using beanshell in JMeter? - jmeter

I am trying to write a script in beanshell for jmeter through which I can get the IP address of a machine (using ipconfig command and extract only IP Address from the output)?
The following code is giving only the IP of the request of which needs to be passed to jmeter.
String IP = InetAddress.getByName(prev.getURL().getHost()).getHostAddress();
vars.put("IP", IP);
Can anyone guide me?

Try this one:
vars.put("IP", org.apache.jmeter.util.JMeterUtils.getLocalHostIP());
Demo:
You can also use __machineIP() function in the "Parameters" section and refer the value as Parameters or bsh.args[0] in the script body
References:
JMeterUtils.getLocalHostIp()
How to Use BeanShell: JMeter's Favorite Built-in Component

you can also use as follows:
log.info("IP " + InetAddress.getLocalHost().getHostAddress());
String IP = InetAddress.getLocalHost().getHostAddress();
vars.put("localIP", IP);
later, you can refer the IP using the following syntax:
${localIP} or vars.get("localIP")

Instead of your code you can use a single line variable ${__machineIP} which is given by JMeter. Make sure we utilize inbuilt feature given by JMeter and if it does not satisfy then only you should go for other options.
So you can use ${__machineIP} or ${__machineIP()}.
In some of the cases it needs to be inside double quotes(""), Make sure where you need to use "" and where is not.

Related

JMeter how to select randomely from a extracted set of values

I have a requirement to use randome url from a url list I extract from a json response.
Say I extract them in this mannser
imageUrls_1=https://blah01.com
imageUrls_2=https://blah02.com
imageUrls_3=https://blah03.com
imageUrls_4=https://blah04.com
imageURLs_matchNr=4
In a following JSSR223 sampler I was able to generate a variable called "url" with one of the url names selected randomely
("imageUrls_1","imageUrls_2",etc)
I was thinking to use them in my HTTP request to get the correcponding url as follows. ${${url}}. But soon found out its not giving me anything other than "${${url}}" :(.
JMeter Is it possible to place a varibale inside a varible name?
Basically I need to use one of the extracted urls randomely in my HTTP request.
The easiest way is going for __V() and __Random() functions combination like:
${__V(imageUrls_${__Random(1,${imageURLs_matchNr},)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables
Use __V function
${__V(url)}
The V (variable) function returns the result of evaluating a variable name expression.

why does print vars.put ,the result is null?

String pass = "123456789";
vars.put("token01",pass);
System.out.println(pass);
System.out.println(token01);
the result of System.out.println(pass) is right ,it is 123456789,
but the result of token01 is null, I can not understand .
If you need to access a JMeter Variable value programatically you need to consider one of the following options:
If your variable is a String: String value = vars.get("token01")
If your variable is an Object: Object value = vars.getObject("token01")
Demo:
References:
vars.get()
vars.getObject()
You may also find Groovy Is the New Black article useful.
You have to use vars.get to access jmeter variables.
So you should use System.out.println(vars.get("token01")). Also you can use debug sampler and view results tree combination to make sure your script is working fine see How To Debug Your Apache Jmeter Scripts
for more information on jmeter troubleshooting techniques.

How can I get name of HTTP Request from field Name with BeanShell?

I Need to get to variable field from http request which is called Name.
If anyone could give my examples how can I get other fields such as : Path, Server name or IP using beanshell?
Thank you in advance
Add Beanshell PreProcessor as a child of the request
Use following code lines to get the required values:
String name = sampler.getName(); // get parent sampler name
String path = sampler.getUrl().getPath(); // get path
String url = sampler.getUrl().getHost(); // get IP or hostname
you can also store values into JMeter Variables if required like
vars.put("name", name);
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for comprehensive information on using Beanshell scripting in your JMeter test.
Below code can give you the HTTP Request Name field value
ctx.getCurrentSampler().getName()
I think you can do it without beanshell. Just use inside your sampler
${__samplerName()}
https://jmeter.apache.org/usermanual/functions.html#__samplerName

How to Replace the env name from the domain name that is reading the from the CSV file in 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

Printing variable value from CSV data file to JMeter console

I am using JMeter to performance test an address validation service that finds valid addresses for an insurance quoting application. I am testing this from the back end using soap requests and I have a csv data config file with a large number of search strings.
In order to get a true idea of the performance of this service, I need to test with a large number of addresses i.e. over 30000 so that the server caching doesn't affect my performance results. I have a list of addresses in a csv spreadsheet but some of them cause failures for whatever reason (e.g. the address no longer exists, I have verified this when just submitting one request with the address in question). I want to remove all addresses that fail from my csv file.
So I wanted to use JMeter to print the search address to the console if the request with this particular search address fails. I tried to use an IF controller with this as the condition "${JMeterThread.last_sample_ok}" == "false" and the following in the name section so that it prints the address to the JMeter console. The parameter searchAddress comes from my CSV input file. When I try and run this it just prints ${searchAddress} to the console. So the if statement works but it doesn't recognise searchAddress as a variable.
If I can get this to work I would copy all the search strings to excel and use a formula to remove them from my list of addresses to be used by my JMeter thread.
Sorry for the lengthy question, but hope I have explained my issue clearly.
An alternative is to use a BeanShell PostProcessor as child of which ever component receives the error; In the Beanshell, something like:
String searchAddr = vars.get("searchAddress");
//Output to Console
System.out.println("Failed Address = " + searchAddr);
//Output to Log file etc.
log.info("Failed Address = " + searchAddr);

Resources