0

Can I use same environment variables in multiple export function ?

export function login() {
    group('Login API', function () {
        __ENV.code = 'code getting from api response';
        console.log("code is : " + __ENV.code);             // getting correct value
    });
  group('Login API', function () {
        console.log("code is : " + __ENV.code);             // getting correct value
    });
}

export function api1() {
    group('1. APIs', function () {
        console.log(`code in 1 : ${__ENV.code}`);          // getting undefined value
    });
    group('2. APIs', function () {
        console.log("code in 2 : " + __ENV.code);          // getting undefined value
    })
}

If I'm using same env variable in same export function, then I'm getting correct value. Also I'm able to use same env variable on other pages/screens. But on same page if I'm using that env variable in different export function then I'm getting undefined response. As shown in above code.

What's wrong I'm doing? (I'm new(beginner) in K6)

knittl
  • 246,190
  • 53
  • 318
  • 364
jenny
  • 7
  • 8
  • It seems like you might be using `scenarios`, and have defined `code` as part of the `env` property only of the `login` scenario, but not the `api1` scenario? – na-- Oct 07 '20 at 16:16
  • I'm trying to use __ENV.code variable in all export functions. . I can use it in multiple groups but not in export function. Where as getting value from login function and using it in api1(). – jenny Oct 07 '20 at 16:25
  • Have you read through https://k6.io/docs/using-k6/environment-variables and https://k6.io/docs/using-k6/scenarios ? – na-- Oct 08 '20 at 07:10
  • Yes. I got it. Checked - (k6.io/docs/using-k6/scenarios) – jenny Oct 09 '20 at 14:46

1 Answers1

0

You can only set an environment variable from outside the script, for example by running k6 run -e CODE=abc your_script.js

You have then access to the value "abc" by reading __ENV.CODE in your script. You can not set it to a different value while the script is running (as you have found out).

knittl
  • 246,190
  • 53
  • 318
  • 364