# GlobalThis variable using for Multiple scenarios

**URL:** <https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116>\
**Category:** Grafana k6\
**Tags:** dashboard-variables\
**Created:** [March 4, 2024, 9:44pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116 "2024-03-04T21:44:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayareva](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jayareva](https://community.grafana.com/u/jayareva)\
**Post date:** [March 4, 2024, 9:44pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/1 "2024-03-04T21:44:07Z")

</div>

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

```javascript
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

```javascript
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.

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [March 11, 2024, 12:50pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/3 "2024-03-11T12:50:40Z")

</div>

Hi @jayareva 👋

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 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![jayareva](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jayareva](https://community.grafana.com/u/jayareva)\
**Post date:** [March 11, 2024, 1:45pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/4 "2024-03-11T13:45:49Z")

</div>

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

Testcasefile.js

```javascript
export function JDMBaseline() {
  testCase02_JDMBaseline();
}

export function pricingProjectBaseline() {
 testCase01_PricingProj();
}

export function jobsBseline() {
  testCase01_Jobs()
}

export function peerAutomatch() {
  testCase02_JobSuggestion_NewJobswithSurveyData()
}

```

JDM.js

```javascript
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

```javascript
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

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [March 11, 2024, 2:09pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/5 "2024-03-11T14:09:14Z")

</div>

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](https://github.com/oleiade/xk6-kv) extension which offers access to shared key-value store, or even use the [`k6/experimental/redis` module](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/redis/) 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 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![jayareva](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jayareva](https://community.grafana.com/u/jayareva)\
**Post date:** [March 11, 2024, 3:55pm UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/6 "2024-03-11T15:55:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [March 12, 2024, 8:20am UTC](https://community.grafana.com/t/globalthis-variable-using-for-multiple-scenarios/116116/7 "2024-03-12T08:20:12Z")

</div>

Hey @jayareva 👋

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 🙇‍♂️
