0

I have several suites and in each one some spec files.

What I noticed is that after wdio runs all tests in any spec file, it closes the browser and opens a fresh instance of the browser. Even the local storage is removed in this case.

So I have a hard time finding a place to define a variable that it's value persists throughout all tests.

I tried defining an array on top of my wdio.base.conf.js file.

let globalStuff = []

const config = { ...


beforeTest: function (test) {
      globalStuff.push(Math.random())
},

afterSession: function (config, capabilities, specs) {
    console.log(globalStuff)
    },
 }

Observation: The values of globalStuff are reseted each time and only contains x items (x is the number if it tests in a spec file)

Expectation: The values of globalStuff should be an aggregation of all pushed values.

Confront
  • 93
  • 5

1 Answers1

1

One easier way we are achieving this is using global object.

you can set something like global.platform = web in your config file and this should be accessible in all your tests. Similarly, you can set any number of unique properties on global object and they should be accessible in your tests.

Your snippet should be as below.

global.globalStuff = []

const config = { ...
  beforeTest: function(test) {
    globalStuff.push(Math.random())
  },

  afterSession: function(config, capabilities, specs) {
    console.log(globalStuff)
  },
}

Here is a sample project implementing this.

Raju
  • 1,314
  • 2
  • 10
  • 14
  • Thanks; But how do I initial this `global. globalStuff` ? if I replace my `globalStuff` in my example with `global. globalStuff` then I get the same result. I think the problem is here: `global.globalStuff = []` on top of config file. – Confront Jun 05 '20 at 21:10
  • 1
    I have updated my answer. Could you try with that snippet? – Raju Jun 05 '20 at 21:43
  • ``` '------- afterSession ---------' [ '0.14', '0.67' ] '------- afterSession ---------' [ '0.69', '0.67', '0.89', '0.37' ] ``` So each session gets a new set of values !!! I expected to see this after the second log: [ '0.14', '0.67' , '0.69', '0.67', '0.89', '0.37' ] – Confront Jun 05 '20 at 22:16
  • 1
    Maybe you should move this to `before` hook so that it just runs only once per execution. – Raju Jun 06 '20 at 23:03