Passing values between groups

Hi Team,

I see we have option of tags and groups in k6 . I have a scenario where i need to use a value from response of one api in another api and i scripted both the api’s in default function.

To make the code cleaner when i put the api’s in 2 diff groups …am not sure how to pass value from one group api to another group api.
Please suggest .

1 Like

Hi @Shabd you can define global variable in default function and using in each groups like this

import http from 'k6/http';
import { sleep, group, check } from 'k6';
import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';


export default function () {
  let selectedCrodile;
  group('API GET list of crocodiles', function () {
    const listOfCrodiles = http.get('https://test-api.k6.io/public/crocodiles');
    check(listOfCrodiles, {
      'is list of crocodiles 200': res => res.status === 200
    });
    selectedCrodile = randomItem(listOfCrodiles.json());
    console.log(selectedCrodile.id);
  });

  group('API GET specific crocodile by Id', function () {
    const specificCrocodile = http.get(`https://test-api.k6.io/public/crocodiles/${selectedCrodile.id}/`);
    check(specificCrocodile, {
      'is specific crocodile 200': res => res.status === 200
    });
  });
}
2 Likes

Thanks @Elibarick Will check this.