2

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

fleks95
  • 23
  • 2

2 Answers2

0

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.

Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • Thank you and apologies, I have already made these before asking the question, I should have mentioned that. I just was not sure if there was a way to include different URLs in the If statement. I appreciate the help. – fleks95 Jun 14 '22 at 14:52
0

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.

Nopal Sad
  • 516
  • 1
  • 7
  • It does not let me use this as it complains about (!Cypress.config("baseUrl") possibly being null. If I try to use (!Cypress.config("baseUrl", "**localhost**") then it's no longer a Boolean, it becomes a Void statement. TypeScript is in question. – fleks95 Jun 14 '22 at 14:49
  • You could resolve this by just requiring that `Cypress.config('baseUrl')` is not null - `!Cypress.config('baseUrl')!!.includes(...)` – agoff Jun 14 '22 at 16:32
  • It's a Typescript error, you can fix it with a [non-null assertion operator](https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci). – Nopal Sad Jun 14 '22 at 20:45
  • Thanks guys, I did use the non-null and added a rule for that next line to not be considered with es lint. Appreciate the help. This is the solution in the end and it works: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if (!Cypress.config().baseUrl!.includes("localhost")) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const url = new URL(Cypress.config().baseUrl!); const host = `${url.hostname}:${url.port}`; cy.ntlm([host], Cypress.env("USER"), Cypress.env("PASS")); }; – fleks95 Jun 16 '22 at 08:41