JMeter Regular Expression Extractor I am not getting the value out of a variable from the url - jmeter

In Jmeter I am trying to get a value out of a variable from the url using the Regular Expression Extractor. I am also using the BeanShell Sampler to get the value out of the variable and print it out to the log file. I can then see in the log file what value I am getting.
I don't think my Regular Expression Extractor setting is correct I am getting the following error from my BeanShell Script:
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``String session_ID = vars.get("sessionID"); log.info("session_ID = " + session_I . . . '' Encountered "vars" at line 4, column 1.
An example URL I have is:
http://localhost:8080/test1/gp/gw/ajax/desktop/herotator/record-impressions.html?ie=UTF8&aPTID=16001&cmpnId=205103067&cnttId=1&h=1A52D1&pIdent=desktop&rId=0DA6FXQ35E8JDNVES8C59&sid=14&slotName=PS4-some-testdata
I would like to get the value from the variable PTID and output it to the log file. I can then use the value in other Http requests when i need to.
My BeanShell Sampler script is:
String session_ID = vars.get("sessionID");
log.info("session_ID = " + session_ID)
vars.put("sessionID", session_ID);
My Regular Expression Extractor is:
Field to check = URL is ticked
Reference Name = sessionID
Regular Expression = PTID="(.+?)"
Template = $1$
Match No. (0 for Random): 1
Default value = session id not found
My Test Plan set up is as follows:
Test Plan
--Thread Group
----Http Request Default
----Http Header Manager
----Recording Controller
------Http Request
------Regular Expression Extractor
------BeanShell Sampler
------more Http Requests
----Access Log Sampler
----View Results Tree
Also nothing is being written to the log file when i run the script.
Access Log Sampler the log file location is to:
E:\RL Fusion\projects\JMeter\apache-jmeter-2.13\jmeter.log
In the View Results Tree for the Http Request I can see there is a PTID value in the Response data Tab.
My regular expression extractor is not getting this value out.
I am new to JMeter, If i have anything in the wrong order please do let me know.
Thanks,
Riaz

My expectation is that something is wrong with your Beanshell script. Most likely you're missing a semicolon at the end of statement on 3rd line.
To get to the bottom of a problem in Beanshell script you can use the following approaches:
Add debug(); directive to your Beanshell script (make it first line). It will trigger debug output to console window where you launched JMeter from
Put your Beanshell code into "try" block like:
try {
// your code here
}
catch (Throwable ex) {
log.error("Problem in Beanshell", ex);
throw ex;
}
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter.

Related

JMeter Bean Shell PostProcessor with embedded special characters

I am extracting a variable from the HTTPReponse body which contains a string containing special characters. When I try to access the variablein the script, I am getting the following error. Is there a way to access these vars while preserving the special characters?
jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: `` token += "" + auQV8OGH47fz50YFm9rS/dQjTcUuGi55ryzC7S3YInNcaciCVR3/frSHwv8CE/mJD . . . '' Encountered "oSQ" at line 1, column 269.
Most probably you are accessing JMeter Variable in your script body as ${variable_name_here} which is not very recommended.
Beanshell should handle JMeter Variables without any issues given you access them via vars shorthand (or through "Parameters" section)
Given you have a JMeter Variable ${foo} the correct ways of accessing its value will be:
Using vars shorthand:
String foo = vars.get("foo");
Using "Parameters" section (assumes you have ${foo} there)
String foo = Parameters;
String foo = bsh.args[0];
Demo:
Other troubleshooting techniques:
You can add debug() command to the beginning of your script so debugging output will be printed into JMeter console window
You can put your Beanshell code inside the try block like:
try {
//your code here
}
catch (Throwable ex) {
log.error("Beanshell failure", ex);
throw ex;
}
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell scripting in JMeter tests

JMeter - Using response data as variable

The response I get back from a post is just a plain text guid seen here I'd like to pull this guid and use it in a variable for my following Get statement:
post response data
And I've tried a bunch of configurations in my regular expression extractor.
But it just ends up pulling Null when what I want is that guid.
Null
I'm new to jmeter - so thank you in advance for the help.
If you need the whole response use the following Regular Expression Extractor configuration:
Reference Name: response
Regular Expression: (?s)(^.*)
Template: $1$
As per How to Extract Data From Files With JMeter guide:
() = grouping
(?s) = single line modifier
^ = line start
. = wild-card character
* = repetition
So it will return the whole response.

Parsing error on trying to retrieve value of a variable extracted using JSON path extractor in Jmeter

I have to assert JSON response of an API.
So extracted value of a field (state) using JSON path extractor and save it in variable (Optinurl)
"state":"opted_in"
In the Debug Sampler, i see the value of Optinurl as
Optinurl=
[
: "opted_in"
]
Optinurl_1=opted_in
Optinurl_matchNr=1
When i try to retrieve value of variable Optinurl in Beanshell assertion as below,
String optinValue = ${Optinurl}
i get
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: String optinValue = '["opted_in"]';'' Token Parsing Error: Lexical error at line 1, column 23. Encountered: "\"" (34), after : "\'["
2016/03/07 14:40:15 WARN - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:String optinValue = '["opted_in"]';'' Token Parsing Error: Lexical error at line 1, column 23. Encountered: "\"" (34), after : "\'["
Thanks for your help in advance !
There is a JSON Path Assertion available via JMeter Plugins project, I believe you can do what you need via it.
The correct ways of initializing a JMeter Variable in Beanshell are:
String optinValue = "${Optinurl}";
or
String optinValue = vars.get("Optinurl");
The error you're getting is not connected with your Optinurl variable initialization. Looking into
Lexical error at line 1, column 23.
It appears you have some syntax error in the very first script line. So the options are:
Double check your code, make sure that parentheses, quotation marks, etc. are matching, statements are ending with semicolon, quotation marks in strings are escaped, etc.
Adding debug(); line as the first line of your script produces comprehensive debug output to STDOUT
Surrounding your code into try/catch block allows to having more informative error stracktraces
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more detailed information on using Beanshell in your JMeter tests.
I think you want to store [ : "opted_in" ] into a string variable so use this:
String optionValue= vars.get("Optinurl");
into your beanshell assertion
and if you want only opted_in to store in to a variable then use
String optionValue= vars.get("Optinurl_1");
Thanks Dmitri, Kaushlendra for replying.
I updated my script as below and it is working fine in GUI/command line. Since vars.get("Optinurl") returns ["opted_in"], so had to remove quotes and square brackets before comparing Strings.
String optinValue = vars.get("Optinurl"). replace("[","").replace("]","").replace("\"","");
String expectedState = "${EXPECTED_STATE}";
log.info(optinValue);
log.info(expectedState);
if(!optinValue.equals(expectedState)){
Failure = true;
FailureMessage = "Values of state field for Campaign id " + "${CAMPAIGN_ID}" + " dont match ";
}
I could not use String optinValue = vars.get("Optinurl_1") because it fails when i run tests from command line (works fine in GUI mode though)

Extracting a value from jmeter response and exporting it to a csv file

I need to know how can I extract a value from response in jmeter and export it to a csv file.
Suppose my response is like this:
<ns3:UpdateConsumerResponse
xmlns:ns3="http://Pandora.NextGenCRM.Integrations.UpdateConsumerResponse"
xmlns:ns0="http://tempuri.org/"
xmlns:ns1="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Request"
xmlns:ns2="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Response">
<MasterConsumerID>
CRM-CONID-000000519344
</MasterConsumerID>
</ns3:UpdateConsumerResponse>
I need to extract the master consumer value and export it to an csv file.
First of all you have to add an regular expression extractor as a child of this request
and then mention below inputs
1.Reference Name: MasterConsumer (or any variable)
2.Regular expression: abc(.*?)d (suppose your value is like abcCRM-CONID-000000519344d
then provided reg ex will work, now replace abc with your left
boundary and d with right boundary which you can get from your response. if still you need more help then
please provide this value along with more text from both side )
3.Template: $1$
4.Match No:1
5.Default Value: null
now you have your value stored in MasterConsumer variable (apply debug sampler to verify). Just you need to write into csv file, so add beanshell post processor as a child of same request and write below code for printing data into csv file
MasterConsumer =vars.get("MasterConsumer");
f = new FileOutputStream("Path"-Output.csv",true);
p=new PrintStream(f);
this.interpreter.setOut(p);
p.println(MasterConsumer);
f.close();
Add XPath Extractor as a child of the request, which returns above value
Configure it as follows:
Check Use Namespaces box
Reference Name: anything meaningful, i.e. ID
XPath query: /ns3:UpdateConsumerResponse/MasterConsumerID/text()
Add Beanshell PostProcessor after the XPath Extractor
Add the following code into the PostProcessor's "Script" area:
import org.apache.commons.io.FileUtils;
String file = "path_to_your_file.csv";
String ID = vars.get("ID");
String newline = System.getProperty("line.separator");
FileUtils.writeStringToFile(new File(file),ID + newline, true);

Jmeter extract data using BeanShell PreProcessor and add parameters

Having the following request:
From this I extract using the Regular Expression Extractor the following string:
%5B1172%2C63%2C61%2C66%2C69%2C68%5D
I decode this using the urldecode function: ${__urldecode(${Groups_g2})}
Decoded: [1172,63,61,66,69,68]
On the following request I want to extract the values using the BeanShell PreProcessor to obtain a list of parameters like this one:
I know that I have to use sampler.addArgument but i can't figure how to extract data from the list and add the values as parameters.
Try the following:
Put ${__urldecode(${Groups_g2})} into Beanshell PreProcessor's Parameters input field
Enter the following code into Script area
String params = Parameters.substring(1, Parameters.length() - 1); // remove square brackets
int counter = 1;
for (String param : params.split(",")) {
sampler.addArgument("parameter" + counter, param);
counter++;
}
I have no idea what parameter names need to look like, hopefully above information will be helpful.
HTTP Request with no parameters:
Beanshell PreProcessor
Parameters in View Results Tree Listener
For more information on Beanshell scripting in Apache JMeter check out How to use BeanShell: JMeter's favorite built-in component guide.

Resources