Jmeter http path value finder using beanshell - jmeter

Consider we have a URL path as below,
https://www.google.co.in/search?q=${query_string)
where query_string is a variable passed from a csv file.
Now in Jmeter bean shell pre/post processor i need the original URL before assigning the variable value, ie, https://www.google.co.in/search?q=${query_string).
Do we have any way to retrieve this?

I wouldn't recommend using Beanshell as it has known performance problems so consider switching to JSR223 Test Elements and Groovy language.
The relevant groovy code you can use in JSR223 PreProcessor or JSR223 PostProcessor would be something like:
def url = sampler.getUrl();
def protocol = url.getProtocol()
def host = url.getHost()
def path = url.getPath()
log.info('Full URL: ' + url.toString())
log.info('URL you want: ' + protocol + '://' + host + path)
Demo:
See Apache Groovy - Why and How You Should Use It article for more details about Groovy scripting in JMeter tests.

Save your url in a variable myUrl
https://www.google.co.in/search?q=${query_string}
When you use the url execute __V function
${__V(myUrl)}
This will return https://www.google.co.in/search?q=myString
When you need the original URL use
${myUrl}

Related

Assigning value to the variable based on the request

I have created a jmeter script as below
I am using the user defined variable with function __P() and passing the Url from .sh file
My requirement is, if the url passed is "www.abc.com" then set the value of the variable ${Prefix} to "foo" else set it to "bar"
I tried using JSR223 PostProcessor, but JSR223 PostProcessor has to have a sample associated with it.
Any suggestion how do I achieve it?
Use the following __groovy() function in the HTTP Request sampler directly:
${__groovy(if (vars.get('Url') == 'www.abc.com') {return 'foo'} else {return 'bar'},Prefix)}
For subsequent requests (once the __groovy() function has been run) you will be able to use just ${Prefix}
More information: Apache Groovy - Why and How You Should Use It
If you're not too comfortable with Groovy scripting you can consider using __if() function, it's a part of Custom JMeter Functions bundle, can be installed usign JMeter Plugins Manager

How to fetch parent url of the current failing url in jmeter?

I am trying to fetch the parent url of the current failing request (for eg consider a travel website ,parent url will be search creteria and current url is selecting flights page).How can we do it .I tried it with putting a if controller after failing url and a beanshell so that it fetches the parent url of all the failings flights request. But what is happening is ,if the current flight request is falining it wont go to the next if controller at all. Execution stops. Can someone guide me with the better way?
First of all forget about Beanshell, you should be using JSR223 Test Elements and Groovy language for scripting starting at least from JMeter version 3.1 (for earlier versions it was also recommended, however Groovy engine wasn't included in JMeter distribution)
Add JSR223 PostProcessor as a child of the 2nd request
Put the following code into "Script" area:
if (!prev.isSuccessful()) {
log.info('Previous sampler name: ' + ctx.getPreviousSampler().getName())
log.info('Previous sampler URL: ' + ctx.getPreviousSampler().getUrl().toString())
}
That's it, you should see the name of the previous sampler and its URL in the Log Viewer panel and in jmeter.log file
You can save the URL into a JMeter Variable like:
vars.put('url', ctx.getPreviousSampler().getUrl().toString())
once done you will be able to access the value as ${url} where required.
References:
prev stands for current SampleResult
ctx is a shorthand for JMeterContext
vars is a shorthand for JMeterVariables
See JavaDoc for the above classes for description of used functions and Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

How to find and replace a substring in sampler's response for web services?

I am using two soap/xml request samplers wherein response of one is to be used in request of the other. The issue is that the response of Sampler1 contains multiple occurrences of "a:" which has to be replaced by "eas1:" which can be used in Sampler2. Kindly suggest a solution.
I tried using beanshell postprocessor but could not come to any positive result.
Add JSR223 PostProcessor as a child of the Sampler1
Put the following code into "Script" area
def response = prev.getResponseDataAsString()
def request = response.replaceAll('a:', 'eas1:')
vars.put('request', request)
Use ${request} in the "Body Data" section of the Sampler2
References:
prev is a shorthand to SampleResult class instance which provides access to the parent Sampler result
vars is a shorthand to JMeterVariables class instance, it provides read/write access to JMeter Variables
String.replaceAll() method reference
Groovy is the New Black - guide to Groovy scripting in 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 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

Resources