22

I'm looking for a way to store global constants in a file that could be used across all my spec files. Can anyone help?

Ryan
  • 1,482
  • 2
  • 11
  • 19
  • Do you want to write to these global variables from a spec file? – Ackroydd Apr 10 '18 at 01:04
  • @Ackroydd I want to be able to write my global variables in one file and reference that file for global variables across several spec files. – Ryan Apr 10 '18 at 15:44

6 Answers6

21

Use the cypress.json file that is in your project root like this:

{
    "env": {
        "your_var": "your_value"
    }
}

https://docs.cypress.io/guides/references/configuration.html

Once you set some env variables, you can reference them from your specs like this: Cypress.env('your_var');

Jöcker
  • 5,281
  • 2
  • 38
  • 44
TJH
  • 766
  • 2
  • 9
  • 21
  • I have decided to create a cypress.env.json file with a single environment variable ```{ "usernameField": "input[name=j_username]" }``` and calling it in my spec file ```cy.get(Cypress.env('usernameField')).type('username')``` . When I run my spec file I'm getting a ```TypeError: Cannot read propety 'split' of undefined``` Any ideas? – Ryan Apr 10 '18 at 19:15
  • I believe it is because the environment variables are strings. Is there a way to create an environment variable that is not a string so that it may be used as I explained in my example above? – Ryan Apr 10 '18 at 19:22
  • You should test that cypress.env.json is actually being read. Where are you putting this file? – Ackroydd Apr 10 '18 at 19:41
  • I had it in the root of the cypress folder. Although I was able to get it to work by utilizing the cypress.json instead of the cypress.env.json. – Ryan Apr 10 '18 at 20:28
  • The docs are a bit vague on some details. Looking at the Github source, cypress.env.json is read in from the same location as cypress.json (which is the project root, not the cypress folder). – Ackroydd Apr 10 '18 at 20:51
5

The following link might help with an easy way to set and get from env variables. https://docs.cypress.io/api/cypress-api/env.html#Syntax

describe('My First Test', function() {
  it('it set value to global variable', function() {
    Cypress.env('some_variable', "hello")
  })
  it('it get value to global variable', function() {
    expect(Cypress.env('some_variable')).to.equal('hello')
  })
})
4

I like to create a constants file such as constants.js as an exportable constants object:

export const NAME = {
FIRST: 'John',
LAST: 'Smith',
};

and in my spec files import them: test.spec.js

import { NAME } from '../../support/constants';

describe('Landing page', () => {
  beforeEach(() => cy.login());

  cy.get(NAME.FIRST).assertion('verify the name');
});
2

Global variables - sounds like fixtures.

See writefile - JSON - Write response data to a fixture file

cy.request('https://jsonplaceholder.typicode.com/users').then((response) => {
  cy.writeFile('cypress/fixtures/users.json', response.body)
})

// our fixture file is now generated and can be used
cy.fixture('users').then((users) => {
  expect(users[0].name).to.exist
})

Care to share why you want to do so?
Sounds like it may be interesting.

Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
  • Hi, I'm making a test suite where I will have several .spec files doing different tests. I want to store all my variables in one file that will then be used in all my spec test files. Ie. so that I can change a URL in one spot and have the URL update across all my test files. – Ryan Apr 10 '18 at 15:49
  • 1
    Ok, so technically they are not **variables**, they are **constants**. – Richard Matsen Apr 10 '18 at 19:18
  • I agree the question is vague. It should follow to [how to ask a good question](https://stackoverflow.com/help/how-to-ask) – Ackroydd Apr 10 '18 at 19:46
  • @Ackroydd My apologies. This is my first post on stackoverflow and I will be sure to consult the page you linked in your comment, in the future. – Ryan Apr 10 '18 at 20:38
  • @RichardMatsen Yes, I apologize for that mixup. – Ryan Apr 10 '18 at 20:39
2

since cypress is js, it is possible to define const in on js file (my-const.js) as

export const ARE_YOU_SURE='Are you sure';

and use them in another file as

import * as constants from "../[proper path]/my-conste.js";
...
var s = constants.ARE_YOU_SURE + ' about this?'
Sasha Bond
  • 984
  • 10
  • 13
1

You can use environment variables stored in either cypress.json or cypress.env.json.

cypress.json

{
  "env": {
    "myVar": "value..."
  }
}

cypress.env.json

{
  "myVar": "value..."
}

You can use your environment variable using Cypress.env('myVar').

const myVar = Cypress.env('myVar')

You can review your environment variables in the Settings tab of the cypress runner.

cypress runner Settings

Bienvenido David
  • 4,118
  • 1
  • 25
  • 16