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 '';

export default function () {
  let selectedCrodile;
  group('API GET list of crocodiles', function () {
    const listOfCrodiles = http.get('');
    check(listOfCrodiles, {
      'is list of crocodiles 200': res => res.status === 200
    selectedCrodile = randomItem(listOfCrodiles.json());

  group('API GET specific crocodile by Id', function () {
    const specificCrocodile = http.get(`${}/`);
    check(specificCrocodile, {
      'is specific crocodile 200': res => res.status === 200

Thanks @Elibarick Will check this.