I'm working on testing some API's (10-15 in number) that uses Bearer token for authentication. The token has an expiration condition post which I have to re-generate the token. I've not worked on this situation on Jmeter so looking for answers how this can be done.

Condition -

  • At start of tests, generate the Token
  • Use the token in API's for let say 5 users
  • If the following conditions are met, re-generate token
    • If the token has been used 50 times
    • If the token timeout period is reached, which is 10 minutes.

As per my understanding, if I am creating 5 threads for the API tests, and passing the Token to these 5 threads, then the first condition gets reached once there are 50 requests made and so I have to re-generate the token mid test and use that again.

Is this achievable in Jmeter? I've see couple of examples where the second condition regarding timeout can be done, but here I have two separate conditions?

OS - Not sure if this needs to be done via Groovy scripting as I have never worked on a groovy script before.

1 Answer 1

  1. When you generate the token use __time() function to store the token generation time into a JMeter Variable like ${__time(,tokenGenerationTime)}
  2. Depending on the way you design your test either use __counter() function or Counter test element or a pre-defined JMeter variable which holds the current iteration of the Loop Controller or While Controller or Thread Group iteration to identify how many times the token was used
  3. Use If Controller with __jexl3() function to compare:

    • current time with the last token generation time
    • current counter value with 50

      ${__jexl3((${__time(,)} - ${tokenGenerationTime}) > 600000 || ${counter} > 50,)}

    and if time delta is >= 10 minutes and/or counter is > 50 - call regenerate token action like it's described in the Easily Write a GOTO Statement in JMeter article

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.