cypress

how I can run cypress script with the variable provided by user


I have cypress script that runs on different environment and the spec files look like this

/// <reference types="cypress" />

describe('Whitelabel Login', () => {
  it('Whitelabel Login', () => {
    const whitelabelBaseUrl = whitelabelUrl =>
      whitelabelUrl.replace('://', '://motivaction.')

    cy.visit(whitelabelBaseUrl(Cypress.config('whitelabelUrl')))
    cy.screenshot('Whitelbel Login page', { capture: 'fullPage' })
    cy.visit(`${whitelabelBaseUrl(Cypress.config('whitelabelUrl'))}/logout`)
    cy.screenshot('Whitelbel Logout page', { capture: 'fullPage' })
  })
})

So when I am running the script npx cypress run --env name=qa wl=seedling then I want motivaction to be replaced by seedling.


Solution

  • You can reference variables passed in through the --env flag in the CLI via Cypress.env('var-name'). Cypress documentation here

    const whitelabelBaseUrl = whitelabelUrl.replace('://', `://${Cypress.env('wl')}.`)
    

    Also, pass several env variables using commas and no space. See docs here

    npx cypress run --env name=qa,wl=seedling