Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global variables usable in different js (test) files #1121

Closed
kemalbayar opened this issue Dec 22, 2017 · 5 comments
Closed

global variables usable in different js (test) files #1121

kemalbayar opened this issue Dec 22, 2017 · 5 comments

Comments

@kemalbayar
Copy link

kemalbayar commented Dec 22, 2017

is there a way to define variables in order to able to use them 'in different js (test) files'

@bahmutov
Copy link
Contributor

Cypress test files are just regular JavaScript files that can require and import other JS files. For example you can add shared code to your support folder index.js file https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Support-file

@kemalbayar
Copy link
Author

@bahmutov thanks

I tried this before but it didnt work. I defined alertSeverities variable in commands.js file and when I used that variable in a js file I encountered with this error ...
"ReferenceError: alertSeverities is not defined"

Also, i created a js file for storing my global variables and I indicated its name in index.js file as import './globalvariables' and then I defined alertSeverities variable in globalvariables.js file but when I ran the test it failed again.

I dont understand why it is not working

@brian-mann
Copy link
Member

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

@kemalbayar
Copy link
Author

Thank you very much for your answers. @brian-mann

I want to inform you about an issue (if i'm right...) in the following address,

https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/fundamentals__node-modules/cypress/support/commands.js

Cypress.addParentCommand() {....} used in the command.js file but this is not used anymore, isnt it?

cypress-example-recipes/examples/fundamentals__node-modules/cypress/support/commands.js

Cypress.addParentCommand("login", function(email, password){ ..........

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Dec 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants