0
votes

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 !

3

3 Answers

2
votes
  1. There is a JSON Path Assertion available via JMeter Plugins project, I believe you can do what you need via it.
  2. The correct ways of initializing a JMeter Variable in Beanshell are:

    • String optinValue = "${Optinurl}";

      or

    • String optinValue = vars.get("Optinurl");
  3. 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.

1
votes

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");

0
votes

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)