JMeter If Controller condition is not reached?

1.6k views Asked by At

I'm trying to create a test under a token-based authorization mechanism. Everything is almost working fine until the accesstoken is expired. Then I'm trying to refresh it, however it seems some piece of the test is not performed.

This is the Authorization dance:

     foreach user in users.csv:
     {
         Get Authorization Code: Http sampler
         Extract AuthzCode on ${authorizationcode}: JSON extractor
         if ${JMeterThread.last_sample_ok}
         {
             Get Access Token: Http sampler
             Extract AccessToken on ${accesstoken}: JSON extractor
             Extract RefreshToken on ${refreshtoken}: JSON extractor
             if ${JMeterThread.last_sample_ok}
             {
                 foreach data in data.csv
                 {
                     Send data in body: Http sampler
                     if ${JMeterThread.last_sample_ok} == false *******************
                     {
                         Extract error on ${expiredaccesstokenerror}: JSON extractor
                         if (${expiredaccesstokenerror} == "expired_accesstoken")
                         {
                             Get new accessToken using ${refreshtoken}: Http Request
                             Extract AccessToken on ${accesstoken}: JSON extractor
                             Extract RefreshToken on ${refreshtoken}: JSON extractor
                         }
                     }
                 }
             }
         }
     }
    

Everything works fine until the AccessToken expires and server returns me this body:

{
    "error_description":"Access token expired",
    "suberror":"expired_accesstoken",
    "error":"invalid_grant"
}

enter image description here

After that, I'm expecting JMeter reach the inside if controller with ${JMeterThread.last_sample_ok} == false and then the next if controller with condition ${expiredaccesstokenerror} == "expired_accesstoken". However I'm not able to figure out why this code is not reached.

As you can see in the herewith image, I've implemented this process in JMeter as:

enter image description here

1

There are 1 answers

0
Dmitri T On

Change your 2nd condition to:

"${expiredaccesstokenerror}" == "expired_accesstoken"

Mind quotation marks around the variable. If Controller's conditions are evaluated by JavaScript engine (Rhino or Nashorn) and if you compare something with string you'll get "blabla is not defined" exception. When it was about 2 boolean types (${JMeterThread.last_sample_ok} and false) - it was fine, but if you want to compare a JMeter Variable to some string - you need to explicitly cast the variable to string. See How to Use JMeter's 'IF' Controller and get Pie article for more details.

Few more tips:

  • In every situation when JMeter test is not working as expected take a look into jmeter.log file
  • If jmeter.log file doesn't contain anything suspicious - go for Debug Sampler and View Results Tree listener combination.