Search code examples
groovyjmeterparameter-passingjsr223threadgroup

Always failed to pass the token on the first try, but succeeded on the second try


The Jmeter I am using is version 5.5, I want to pass the token generated by Thread Group 01, to Thread Group 02. After doing some research, I know how to do it, but it always fails at the first try.

In Thread Group 01, I performed the following configuration:

  1. JSON Extractor to get the token and store it into bearerToken variable,

  2. JSR223 PostPocessor & groovy to pass token to another thread, I wrote this ${__setProperty(bearerToken,${bearerToken})}

    JSR223 Post Processor

In Thread Group 02, I performed the following configuration:

  1. User Defined Variables, then using __property() function

    property function

  2. And use it as a header

    http header

Every time it is run for the first time, the http-request in Thread Group 02 always fails to run because it doesn't get a token. But if it is run again for the second time, it will succeed.

view result tree

The yellow box is the first time Thread Group 01 (Login & Debug Sampler) and Thread Group 02 (Index) are run, the blue box is the condition when run for the second time.

I thought initially Thread Group 02 was run first before Thread Group 01, that's why I enabled the "Run Thread Groups consecutively (i.e. one at a time)" option in the Test Plan. I ran it and checked the listener, the result was the same, Thread Group 01 (Login & Debug Sampler) was successfully run but Thread Group 02 (Index) was not successful because it did not get an access token.

Run Thread Groups consecutively

nb:I can't show the image because it needs at least 10 reputation


Solution

    1. Amend your JSR223 PostProcessor code to use props and vars shorthands instead of inlining the function there:

      props.put('bearerToken', vars.get('bearerToken'))
      
    2. In your HTTP Header Manager use __P() function as

      ${__P(bearerToken,)}