jmeterperformance-testingjson-extract

Jmeter json extractor only passes default value


I am using JMeter's JSON extractor to pass a variable id from one request to another.

This is the response of the first request (https://reqres.in/api/users?page=2):

{
  "page": 2,
  "per_page": 6,
  "total": 12,
  "total_pages": 2,
  "data": [
    {
      "id": 7,
      "email": "michael.lawson@reqres.in",
      "first_name": "Michael",
      "last_name": "Lawson",
      "avatar": "https://reqres.in/img/faces/7-image.jpg"
    },
    {
      "id": 8,
      "email": "lindsay.ferguson@reqres.in",
      "first_name": "Lindsay",
      "last_name": "Ferguson",
      "avatar": "https://reqres.in/img/faces/8-image.jpg"
    },
    {
      "id": 9,
      "email": "tobias.funke@reqres.in",
      "first_name": "Tobias",
      "last_name": "Funke",
      "avatar": "https://reqres.in/img/faces/9-image.jpg"
    },
    {
      "id": 10,
      "email": "byron.fields@reqres.in",
      "first_name": "Byron",
      "last_name": "Fields",
      "avatar": "https://reqres.in/img/faces/10-image.jpg"
    },
    {
      "id": 11,
      "email": "george.edwards@reqres.in",
      "first_name": "George",
      "last_name": "Edwards",
      "avatar": "https://reqres.in/img/faces/11-image.jpg"
    },
    {
      "id": 12,
      "email": "rachel.howell@reqres.in",
      "first_name": "Rachel",
      "last_name": "Howell",
      "avatar": "https://reqres.in/img/faces/12-image.jpg"
    }
  ],
  "support": {
    "url": "https://reqres.in/#support-heading",
    "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
  }
}

In the extractor I named the variable userID and JSON path $..id match no. (1) and default value NoIDFound main sample only I'm also using a debug sampler and it passes with no problem Now I'm passing the variable in the second request as (https://reqres.in/api/${userID})

I'm getting 404 Not Found and the request that passed is (https://reqres.in/api/users/NoIDFound)

Can someone tell what i'm doing wrong?


Solution

  • It's hard to say anything without seeing your Test Plan, I have one assumption, your JSON Extractor might be incorrectly placed. See JMeter Scoping Rules user manual chapter for more information

    So if you have the following setup:

    enter image description here

    it means that the JSON Extractor will be executed after each Sampler, after HTTP Request 1 it will have correct anticipated value and after Debug Sampler it will not because the JSONPath expression won't match anything and you will end up with the default value.

    So make the JSON Extractor a child of the HTTP Request 1

    enter image description here

    Or use Debug PostProcessor instead of Debug Sampler

    More information: How to Use the JSON Extractor For Testing