JMeter / Beanshell "Error invoking bsh method: eval Sourced file:"

sidd picture sidd · Aug 14, 2014 · Viewed 38.7k times · Source

I'm having an issue in JMeter wherein I receive this error

2014/08/14 14:13:26 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String RequestUrl = vars.get("RequestUrl"); String[] params = RequestUrl.split(" . . . '' : Typed variable declaration 
2014/08/14 14:13:26 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String RequestUrl = vars.get("RequestUrl"); String[] params = RequestUrl.split(" . . . '' : Typed variable declaration 

I have no clue whats wrong, and the code otherwise seems to be working. Can anyone give me some advice?

Here is the block of code in question:

String RequestUrl = vars.get("RequestUrl");
String[] params = RequestUrl.split("\\?");
String RequestTask = params[1].split("\\&")[1].split("=")[1];
System.out.println(RequestTask);
vars.put("RequestTask",RequestTask);

it should probably be mentioned that the code is in a post processor, which is paired with an Xpath extractor for "RequestUrl"

Edited to include entire error

Answer

Dmitri T picture Dmitri T · Aug 16, 2014

I don't see your URL and what does XPath query return but in any case your URL parsing logic looks flaky as it strongly dependent on parameters order and presence and may bite you back in future in case of request URL change i.e. extra parameter or changed parameters order or something encoded, etc.

See below for reference:

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.net.URI;
import java.util.List;

String url = vars.get("RequestUrl");

List params = URLEncodedUtils.parse(new URI(url), "UTF-8");

for (NameValuePair param : params) {            
    if (param.getName().equals("put your actual param name here")) {                 
        vars.put("RequestTask", param.getValue());
    }
}

Also it worth checking out How to use BeanShell: JMeter's favorite built-in component for troubleshooting tips. In general to localize error logging should be used like:

log.info("something");
log.error("something else");

So if you don't see message in the log than Beanshell wasn't able to execute the line and failed somewhere above.

Also Beanshell error messages aren't very informative, I use the following construction in my scripts:

try {
    //script logic here
}

catch (Throwable ex) {
    log.error("Failed to do this or that", ex);
}

So error stracktrace could be read in jmeter.log file.

Hope this helps.