Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress How to store global constants in a file that can be used across all spec files?

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?

like image 507
Ryan Parker Avatar asked Apr 06 '18 20:04

Ryan Parker


Video Answer


2 Answers

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');

like image 83
TJH Avatar answered Oct 28 '22 06:10

TJH


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')
  })
})
like image 28
Arulmozhi Manikandan Avatar answered Oct 28 '22 05:10

Arulmozhi Manikandan