I am trying to access the file name each test runs in from the afterTest
hook in my wdio config file. However, none of the parameters provided to the hook includes the file name. I tried maybe getting the file name from the onWorkerStart
hook, since the specs
parameter provided to that hook includes the file name, so something like this:
onWorkerStart(cid, caps, specs) {
const fileName = specs[0];
}
But I'm not sure how to access the fileName
variable I created in the onWorkerStart
hook in my afterTest
hook. I tried maybe using it as a global variable:
onWorkerStart(cid, caps, specs) {
global.fileName = specs[0];
}
afterTest() {
console.log(global.fileName);
}
But this just prints "undefined". And if I print it from the onWorkerStart
hook too, it does in fact print the name of the file from there first, but then "undefined" from the afterTest
hook.
I finally figured it out. I was unaware that global variables in the wdio config had to be made inside the before
hook, and luckily, the before
hook includes a parameter specs
which contains the file name. So all that needs to be done to access the file name in all the other hooks is to define global.specFileName = specs[0]
inside the before
hook. And now specFileName
can be used in the afterTest
hook.