Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress.io: Is is possible to set global variables in Cypress and if yes; how?

I am trying to set a global variable (NOT A GLOBAL ENV VARIABLE) in Cypress.io. I would like to abstract a url that I will use over and over again AND something that I can use in multiple files (not just one).

Also, I do not want to set it as baseurl. I already have that set and I want to leave that alone.

Can anyone help me with this?

like image 566
smunizaga Avatar asked Jun 12 '19 16:06

smunizaga


1 Answers

I recently found the answer in another blog on github linked here: https://github.com/cypress-io/cypress/issues/1121

But the answer in this blog was answered my Brian Mann with...

"TL;DR - just use modules, not globals.

Cypress is just JavaScript. All of the underlying principles about structuring files apply to Cypress as it would your own application files.

In this case, you keep mentioning variables. Variables are things defined in a particular file and are never global. This means it's not possible for them to be shared. Variables are accessible based on the local scope in which they are defined.

In order to make them global you have to attach them to a global object: window. However, there's no reason to do this, Cypress automatically has module support built in. This enables you to import functions into each spec file, thus making them way more organized and obvious than using globals.

We have recipes of this here: https://docs.cypress.io/examples/examples/recipes.html#Node-Modules"

I hope this helps someone else who is on Stackoverflow for this answer!

like image 52
smunizaga Avatar answered Sep 30 '22 12:09

smunizaga