GlobalThis variable using for Multiple scenarios

Hi ,
I am trying to run multiple scenarios and trying to access variables in the csv file using globalthis variables.

I see that only the first testcase values are read and the second test gets undefined value. And also, am using two different csv files for both.

First test

export function testCase03_Baseline(){
  globalThis.chosenUser1 = users[(vu.idInTest - 1)]

  if (vuToken === undefined) {
    vuToken = getToken(users[exec.vu.idInTest - 1].Email, users[exec.vu.idInTest - 1].Password)

 LoadPage();
 sleep(10)
}

Second test

export function testCase02_Baseline1(){
  globalThis.chosenUser2 = users[(vu.idInTest - 1)]

  if (vuToken === undefined) {
    vuToken = getToken(users[exec.vu.idInTest - 1].Email, users[exec.vu.idInTest - 1].Password)

 LoadPage();
}

Both LoadPage call different functions and some of the end points need values from csv files.

The login works correctly but the second testcase loadpage end point gets undefined.
And these variables are declared chooseuser1 and chosenuser2 are declared in the base login page and Imported.

Hi @jayareva :wave:

Could you please post an example that demonstrates your configuration too?

My understanding is that those two test cases you showed are executed as part of different scenarios, but I’d like to make sure of it before proceeding with a more specific answer :bowing_man:

Yes, these two are different scenarios and i am calling two different functions.

Testcasefile.js

export function JDMBaseline() {
  testCase02_JDMBaseline();
}

export function pricingProjectBaseline() {
 testCase01_PricingProj();
}

export function jobsBseline() {
  testCase01_Jobs()
}

export function peerAutomatch() {
  testCase02_JobSuggestion_NewJobswithSurveyData()
}

JDM.js

testCase02_JDMBaseline();

export function testCase02_JDMBaseline() {
  globalThis.chosenUser = users1[(vu.idInTest - 1)] if (vuToken === undefined) {
  
  //getToken is from baselong.js
  vuToken = getToken(users1[exec.vu.idInTest - 1].Email, users1[exec.vu.idInTest - 1].Password) 
}

//JDM save job is calling  jdm.js file
JDM_SaveJob()

Jdm.js

export function JDM_SaveJob() {
  //let response
  let jobFamily2=`${globalThis.chosenUser.jobFamily}`
  
  group(
    `page_2 - JDM_SaveJob`,
    function () {
  
      response 
= http.get(`${baseUri}/JobDescription(${globalThis.chosenUser.JobID})/Default.GetDetail`

And similarily for the other testcase defining the global variable and calling another function for the endpoint where i am passing the global variable to read from the csv file

Thanks
Jaya

If my understanding of your setup is correct, I believe what you’re experiencing is due to scenarios being run in isolation from each other. k6 currently does not allow sharing state between scenarios, neither through global variables, nor through the globalThis object as you’ve attempted here.

We’re aware of that limitation, and although providing a solution that offers a good developer experience is not trivial, it is on our radar, and we’re considering options to solve it in the future.

The most immediate workaround I would suggest is if you are able to use the xk6 extension-system, you could either use the xk6-kv extension which offers access to shared key-value store, or even use the k6/experimental/redis module if you are able to run or access a redis server, and store the state you intend to share there.

Let me know if that helped :bowing_man:

But in my case, I am not using the same csv file and the login works fine, but I see the other variables in both csv files are having issues.

Hey @jayareva :wave:

To avoid misunderstandings, could you please put together a minimal script reproducing your test setup, that I could copy/paste locally and run to reproduce the issue you’re experiencing please?

I have a feeling I might be missing something :bowing_man:

1 Like