0
JMeter test plan has 1 user defined variable Message with a value of <Case Number 000808009 Created.>

1 debug sampler with this BeanShell PostProcessor:

    ``` lang-js
    log.info("Strip out case number");
    log.info("CaseCreated<${Message}>");
    ${__split(${Message},MessagePieces," ")};
    log.info("MessagePieces_3<${MessagePieces_3}>");
    vars.put("CaseNumber", ${MessagePieces_3});
    log.info("CaseNumber<${CaseNumber}>");
    ```


The Debug PostProcessor shows these variables:

Message=Case Number 000808009 Created.
MessagePieces=Case Number 000808009 Created.
MessagePieces_1=Case
MessagePieces_2=Number
MessagePieces_3=000808009
MessagePieces_4=Created.
MessagePieces_n=4

CaseNumber does not appear.

Log shows this:

2022-12-30 15:59:23,580 INFO o.a.j.u.BeanShellTestElement: Strip out case number 2022-12-30 15:59:23,580 INFO o.a.j.u.BeanShellTestElement: CaseCreated<Case Number 000808009 Created.> 2022-12-30 15:59:23,580 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``log.info("Strip out case number"); log.info("CaseCreated<Case Number 000808009 C . . . '' Encountered "000" at line 3, column 13.


2022-12-30 15:59:23,580 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``log.info("Strip out case number"); log.info("CaseCreated<Case Number 000808009 C . . . '' Encountered "000" at line 3, column 13.


I've tried every syntax I could Google to no avail.  Any thoughts/suggestions?  It seems to get confused with the actual message line as part of the inline evaluation of the beanshell.

I have already found this solution that I can use, but I would prefer to use the split function.

    ``` lang-js
    String line = vars.get("Message");
    String[] words = line.split(" ");
    for (int i = 0; i < words.length; i++) {
    log.info(words[i]);
    if (i == 2) {
    log.info("Third word is: " + words[2]);
    }
    }
    ```

1 Answers1

0
  1. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting. If you want to extract 000808009 from the User Defined Variable and save it into CaseNumber variable you could use the following Groovy code:

    vars.put('CaseNumber', vars.get('Message').split(' ')[2])
    

    Apart from this there are following problems in your script:

    • you're inlining JMeter Functions and Variables into code which is not something you should be normally doing
    • in Beanshell/Java/Groovy arrays indices are zero-based so 3rd item in the split array will have index of 2

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

  2. You could also avoid scripting completely and extract the case number using Regular Expression Extractor configured like:

    enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks! vars.put('CaseNumber', vars.get('Message').split(' ')[2]) worked. Still don't understand why I shouldn't be able to use the $__split function, but I'm moving on. Thanks! – user20896661 Jan 05 '23 at 21:32