Cypress baseUrl for different e2e folders

I have two folders in my e2e for two different web projects. I am using cypress.config.js in the source root to define my base Urls. Problem is I need to use different URLs for each different project inside the e2e folder.

cypress-tests-mobile\cypress\e2e\EW

cypress-tests-mobile\cypress\e2e\FW

The config file looks like this :

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

module.exports = defineConfig({   defaultCommandTimeout: 30000,    "reporter": "cypress-mochawesome-reporter",   "reporterOptions": {
    "charts": true,
    "reportPageTitle": "Cypress Tests",
    "embeddedScreenshots": true,
    "inlineAssets": true   },   e2e: {
    setupNodeEvents(on, config) {
      require("cypress-mochawesome-reporter/plugin")(on);
      const version = config.env.version || "prodFW";

      const urls = {
        prodFW: "https://www.url1.com/",
        prodEW: "https://www.url2.com/",
      };

      // choosing version from urls object
      config.baseUrl = urls[version];

Whenever I run the tests i just use –env version like this and it runs fine.

yarn cypress run –env version=”prodEW”

First problem I have is that no matter from where I execute yarn cypress run, it always executes all tests in the e2e folder and the other project always fails since getting wrong baseUrl.
Im running it from “\cypress-tests-mobile\cypress\e2e\FW”

I was trying to solve it with different approaches

  1. making cypress.config.js for each of the projects in e2e folder

that didnt work because whenever I open cypress, I get a message that there is no config and creates a sample cypress.config.js in my source folder.

  1. make nested list

    const urls = {
    prod: [
    [
    prodFW: “https://www.url1.com/”,
    prodEW: “https://www.url2.com/”
    ],
    ],
    };

and for each of the projects i would access the correct URL like this

Cypress.config("baseUrl")[0]
Cypress.config("baseUrl")[1]

but then upon starting i get following error

Your configFile at cypress.config.js set an invalid value:

Expected baseUrl to be a fully qualified URL (starting with http:// or https://).

I am not sure how to handle this so I would be happy for any advice.

Is it generally bad idea having two different websites in e2e?

Thank you

From the documentation Test Configuration

‼️ Note: The configuration values below are all writeable and can be changed via per test configuration. Any other configuration values are readonly and cannot be changed at run time.

  • baseUrl

so one option you have is to leave baseUrl empty in cypress.config.js and each spec file specify the correct value it needs for baseUrl.

describe('tests for url1', {baseUrl:'https://www.url1.com/'}, () => {
  ...

or

describe('tests for url1', () => {
  Cypress('baseUrl', 'https://www.url1.com/')
  ...

Other choices are (via command line)

Leave a Comment