All the environment configuration of our Cypress project will be under the Cypress.json but we can also create our own environment
Even when the Environment variable are defined in the Cypress.json it will only provide priory to the environment variable provided in the command line when running spec file from the terminal.
Terminal Command
Syntax :
node_modules\.bin\cypress run --spec "cypress\integration\examples\<Spec file>" --headed --env <name>=<Value>
Example :
node_modules\.bin\cypress run --spec "cypress\integration\examples\Environment Variables.js" --headed --env url=https://google.com
CODE : Cypress.json
{
"env": {
"url": "http://the-internet.herokuapp.com"
}
}
CODE : Environment Variables.js
/// <reference types = "Cypress" />
describe('Environment Variables',function(){
context('Test Cases',function(){
it('Test 01',function(){
cy.visit(Cypress.env('url'))
})
it('Test 02',function(){
cy.visit(Cypress.env('url')+"/abtest")
})
})
})
EXPLANATION