Home > Net >  Is there a way to Parameterize a baseUrl in Cypress?
Is there a way to Parameterize a baseUrl in Cypress?

Time:08-05

I have an app where we set a baseUrl in cypress i.e. "http://localhost:3000/" but I would like to parameterize the port number. Is there a way to do this in Cypress? I see that there is a --port={x}flag for cypress. Can I access that?

CodePudding user response:

Yes, you can override your port number by using the --port flag. Something like this:

npx cypress run --port 8080

Also, you can override the entire base URL through CLI, like this:

CYPRESS_BASE_URL=http://localhost:8000/ cypress run

CodePudding user response:

By itself, setting --port in the command line does nothing to your baseUrl.

It's not the same port as in http://localhost:4000, instead it is used by the Cypress runner.

Change it only if you know what you are doing!

Instead, use a custom property

yarn cypress open --env buport=8000

cypress.config.js

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      if (config.env.buport) {
        config.baseUrl = config.baseUrl.replace('4000', config.env.buport)
      }
      return config
    },
    experimentalSessionAndOrigin: true,
    baseUrl: 'http://localhost:4000'    // defaults to port 4000
  },
});
  • Related