Using different URLs for local and CI testing

Viewed 60

I want to use Cypress to test locally and on CI at the same time. On CI, I would like to test a production version of my application, which has a different baseUrl than the local version, obviously. I am using the https://github.com/bjowes/cypress-ntlm-auth package to ause windows authentication for the CI, and to do so I have to call cy.ntlm line in my tests. I want to make an IF function that calls the cy.ntlm line ONLY if the baseUrl matches the production one. If the baseUrl is localhost then I would like the cy.ntlm line to be ignored. So my bottom line questions are, how do I let cypress know that I want to use 2 different URLs and how do I pack that into an IF statement? Thank you

2 Answers

Assuming your cypress config file has the baseUrl. You can then update the baseUrl using the CLI during run time. For this create two different scripts with the staging and production URL's in your package.json like this:

"scripts": {
  "test:local": "cypress run --config baseUrl=https://example.com/staging",
  "test:ci": "cypress run --config baseUrl=https://example.com/production"
}

Then to run the scripts in CI use npm run test:ci and for local use npm run test:local.

You can check the baseUrl to conditionally call cy.ntlm,

const baseUrl = Cypress.config('baseUrl')!  // use non-null assertion operator

const isLocal = baseUrl.includes('localhost')

if (!isLocal) {
  cy.ntlm(...)
}

When using Typescript with Cypress you will get complaints because Typescript has no idea if you have set the baseUrl configuration or not.

You can overcome that by adding ! after getting the baseUrl value.
Ref Typescript - Non-null assertion operator

I separated the steps to make it clearer.

Related