cypress: Cannot specify the default running browser in cypress configuration file

Current behavior:

The only way to specify the browser to use is via CLI options cypress --browser chrome --headless for example. There is no field in the configuration that allows users to specify the browser and headless mode.

Desired behavior:

To be able to specify the browser and headless mode in the JSON configuration file, for example

{
  "browser": "chrome",
  "headless": true
}

Then running cypress directly via cypress run would use chrome headless.

Versions

Cypress 4.1.0 Debian Buster (from cypress/included docker image)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 74
  • Comments: 25 (4 by maintainers)

Most upvoted comments

This is just the only option that cannot be specified in the cypress.json file, if you look at test specs, they can be specified in cypress.json so you don’t need to pass it to every command you run. In the same way, it is puzzling that one cannot specify the default browser to run the tests in cypress.json. For just the sake of consistency, one would argue all options should be able to be configured in the configuration file as well as being overridden in the CLI.

In our specific use case, we configured our package.json file to run cypress tests with slightly different options/environment variables by having npm run test and npm run test:ci, npm run test:extra etc. In order to use Chrome headless, we’ll have to copy & paste --browser chrome --headless to all of them (or using npm env vars which aren’t very readable). Today, all other options are specified in cypress.json and we could just override some in the command if we want to have different behaviours. We really would like to also have the browser specified in cypress.json to have all the options in a single file.

Reopening as this is a feature request that isn’t adequately available in Cypress today still.

Ah, would really make sense to have it on the config file rather than doing --browser chrome

I am part of a company where 99% of users (+30 million) use Chrome and by default I would like to leave this information fixed, because in Electron there are errors in which Chrome does not show any errors. A great example is the php debug bar, which in Electron overlaps in some elements, while in Chrome it does not.

I would like an easy implementation like:

In the cypress.config.js file

{
   e2e: {
    defaultBrowser: 'chrome'
    }
}

Agreed. We use the netlify cypress plugin and because we cannot set it in cypress.json, then all tests are run via Electron.

I found a workaround using runner. Finally!

In the root of the project, create a .js file of any name. This will be your runner. image

In the file, load Cypress and in cypress.run define the chosen browser. image

const cypress = require('cypress');

cypress.run({
  // specs to run here
  spec: "cypress/e2e/",
// browser to run here
  browser: 'chrome'
})

To test it, just run node <filename>.js image

In cypress.run it is possible to define several things, such as specs, browser, configs… https://docs.cypress.io/guides/guides/module-api#cypress-run

Hope this helps

@flotwig That doesn’t work. When I filter config.browsers to only have the chrome entry, cypress run complains that the electron browser isn’t available:

Can't run because you've entered an invalid browser name.

Browser: 'electron' was not found on your system or is not supported by Cypress.

Cypress supports the following browsers:
- chrome
- chromium
- edge
- electron
- firefox

You can also use a custom browser: https://on.cypress.io/customize-browsers

Available browsers found on your system are:
- chrome

Is there no other way to specify the default browser other than the --browser CLI flag?

I would also like to see this implemented. The app I test is only supported in Chrome and I have about 10 different config files. Passing in --browser chrome --headless really bloats all of the different test scripts in my package.json.

This exposes both “chrome” and “electron” in the browsers list.

@mikedidthis But I’m not trying to filter the list for cypress open, I’m trying to set the default browser for cypress run. As others here have mentioned, having to repeat the --browser flag every time you run cypress run is annoying and there really should be a way to set the default browser in cypress.json.

@flotwig did you even try your suggestion before closing the issue? first: is doesn’t even work second: restricting one’s browsers is not the same as choosing default browser.

While I really do love the sleek new UI in Cypress 10, the increase in the number of clicks required between running cypress open without args and actually running a test has increased enough to warrant a priority review for this, IMO - purely in the interest of quality of life.

Prior to Cypress 10, I would run cypress open, it would have my last-used browser selected by default, and with one click of a spec file name, I had a test running. Now the same arg-less command brings me from that one up to three clicks:

  1. “E2E Testing”
  2. “Start E2E Testing in your-browser-here”
  3. My spec file name

The best way to avoid this is to add CLI args --e2e and --browser, which would be less of a nuisance if this wasn’t a new (since Cypress 10) obstacle to tests running quickly.

So, in the interest of quality of life, I’d love to have an option to specify these in cypress.json, just like everything else I have default values for, so I could get back to running a spec, after running cypress open, with a single click 😃 Regardless, thanks so much for all the team’s hard work on such a great tool.

An additional temporary way to specify a default browser with yarn is:

// scripts/runCypress.mjs

import { execSync } from 'node:child_process'
import process from 'node:process'

try {
  // Extracting parent shell arguments and preventing JSON ones from subshell parsing
  const args = process.argv
    .slice(2)
    .map(arg => (arg.startsWith('{') ? `'${arg}'` : arg))
    .join(' ')
  // Specify the desired fixed flags here, e.g., `--e2e` or `--browser electron`
  execSync(`cypress ${args} --browser chrome`, { stdio: 'inherit' })
} catch (error) {
  process.stderr.write(`Error: ${error.message}\n`)
  process.exitCode = 1
}

Then in package.json:

{
  ...
  "scripts": {
    ...
    "cypress": "node scripts/runCypress.mjs",
  }
}

Now, both yarn cypress run and yarn cypress open will work using the correct browser and will correctly ingest the CLI options (e.g., yarn cypress run --reporter cypress-multi-reporters).

@librehat Can you explain a bit more about why you need this feature?

This would make the browser choice static per project.

  • For cypress run, it would seemingly always attempt to run the tests in the browser of choice.
  • For cypress open, it would choose this browser by default in the UI.

And then the CLI option would override this?

I’d just like to understand more about why this is necessary for your project in particular so we can approach this at the problem you are having, as opposed to this specific proposed solution.

Under Cypress 9.2, Electron hangs and crashes constantly in my CI environment – it’s yet another bug related to screenshots and/or video recording and I haven’t been able to root-cause it, just to isolate it. If I use Chrome headless in CI, I can avoid the crash.

Ultimately I want to switch back to Electron, but given that I need to use Chrome as a workaround, I’d strongly prefer if all Cypress modes were to use Chrome as the default browser –open, run, or whatnot – so that developers are more likely to write and maintain tests using the same browser that CI will later be using to run their tests.

My use case will (hopefully) wane in importance once Electron becomes stable again, but I can see how others would have a more consistent need to run tests in a specific browser, e.g. if their test suite relies on browser-specific extensions or technologies. It’s a minor pain and a footgun to force developers to choose the right browser.