Search code examples
jmeterweb-api-testingtaurus

How to extract the Bearer token from an API response and pass it to the header of other API in Taurus?


I have an API endpoint named generateAccessToken which will generate an accessToken and it will send the access token with the response.

Response of generateAccessToken API:

{
"data": {
   "userId": "sdfjojeit8hg48ut39h3hr89",
   "accessToken": "fjwem9t3e435ttu3t9u298ywt89gnm389ncr2x99829mntcmx" 
  }
}

And there is another API endpoint named getUserProfile which will fetch the user profile and return the response. This endpoint needs the accessToken in the Bearer header.

The header of the getUserProfile API:

Content-Type: application/json
Authorization: Bearer fjwem9t3e435ttu3t9u298ywt89gnm389ncr2x99829mntcmx

And there are many similar APIs that will require accessToken in the Authentication header.

I need to test these APIs with a single yml file.

The test need to hit the generateAccessToken API and fetch the accessToken from the response of the generateAccessToken API and store it in a variable and other variables will add this variable to its Authorization header.

I'm able to do this with Jmeter using JsonExtractor and HTTPHeaderManager

I'm new to Taurus and quiet confused how to configure yml file to do this. so far, I have created this one. But not sure how to store and use accessToken in header.

execution:
  - concurrency: 100
    ramp-up: 1m
    hold-for: 2m
    scenario: TestUser

scenarios:
  TestUser:
    requests:
      - url: 'https://mywebapplication.com/generateAccessToken'
        method: POST
        headers:
          Content-Type: 'application/json'
      - url: 'https://mywebapplication.com/user/profile'
        method: GET
        headers:
          Content-Type: 'application/json'
          Authorization: 'kjdfoejgejfoskdfoeieio4etg94gn4880'

Solution

  • For particular your case Taurus doesn't add any value, it will just create additional overhead so given you have a working JMeter script you can just run in in JMeter's command-line non-GUI mode or if you need certain Taurus feature like real-time reporting you can run the existing JMeter .jmx test script using Taurus as:

    bzt /path/to/your/test.jmx
    

    If for any reason you still want to implement this scenario in Taurus YAML - take a look at Extractors chapter of Taurus manual. You should be able to get what you want by amending your test as follows:

    execution:
      - concurrency: 100
        ramp-up: 1m
        hold-for: 2m
        scenario: TestUser
    
    scenarios:
      TestUser:
        requests:
          - url: 'https://mywebapplication.com/generateAccessToken'
            method: POST
            headers:
              Content-Type: 'application/json'
            extract-jsonpath:
              token:
                jsonpath: $.data.accessToken
          - url: 'https://mywebapplication.com/user/profile'
            method: GET
            headers:
              Content-Type: 'application/json'
              Authorization: 'Bearer ${token}'