Cypress executes test scripts on a website using the baseUrl defined in the configuration file (for example, in cypress.config.js). This value tells Cypress which web application to test by default. We can change the baseURL by any one of the following ways.
1. Changing the baseUrl at runtime in code
You can define the default baseUrl in your Cypress configuration file (for example, cypress.config.js or cypress.config.ts):
// cypress.config.js
const { defineConfig } = require(‘cypress’);
module.exports = defineConfig({
e2e: {
baseUrl: ‘https://example.com’,
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
You can update the base URL from config file and run it using cy.visit(‘/’);
This approach is useful when you want to switch environments (e.g., from staging to production) directly within your test scripts.
2. Changing the baseUrl from the command line
You can also override the baseUrl when running tests via the Command line without modifying your configuration file:
npx cypress run –config baseUrl=https://another-url.com
This command temporarily sets the baseUrl for that specific test run. It’s especially helpful when running tests in CI/CD pipelines or testing multiple environments.