cypress
cypress copied to clipboard
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)
@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.
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.
Agreed. We use the netlify cypress plugin and because we cannot set it in cypress.json, then all tests are run via Electron.
We have these config options in out cypress-*.json
so I'm quite sure it used to work. Warnings about headless
and browser
being disallowed in the config started showing up recently, so maybe the first workaround here is to downgrade Cypress? If yes, to which version?
I tried finding anything in the migration guide and elsewhere but no luck.
@sesam This configuration option was never available through Cypress by itself. We likely added stricter checks around configuration values at some point which is why you see a warning about it.
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.
I have nothing to add, but this would really help me out as well.
Good to have it in cypress.json
"browser": "chrome",
"headless": true
Incase this is already available please advise me. thank you
I have similar issue , can some one give me solution , I need torun test aginst multple envn in chrome browser when I try npx cypress run --headed --browser chrome --env configFile=staging , getting error as Can't run because you've entered an invalid browser name. Browser: 'chrome' was not found on your system or is not supported by Cypress. Cypress supports the following browsers:
- chrome
- chromium
running npx cypress run --headed --browser chrome is successful
Also , I have cy.lighthouse in my test which is supported ony in Chrome
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.
My use case is specific. I am testing an Ionic app, which will always run on chrome and depend on specific Chrome behavior.
All test work as expected with Cypress open
and choosing chrome, but some fail with cypress run
because it use Electron.
Using Cypress run
with the browser flag set to chrome passes all the time.
It would be easier to set that (and the version) in the json config so developers do not have to wonder why some tests fail when running locally.
This functionality exists today in Cypress, via plugins. You can modify config.browsers
to restrict it to only the ones you want available.
Docs: https://docs.cypress.io/api/plugins/configuration-api#Customize-available-browsers
For the use case of only allowing Chrome (@distante @xeger and several others) you can do something like this:
// cypress/plugins/index.js
module.exports = (on, config) => {
// inside config.browsers array each object has information like
// {
// name: 'chrome',
// family: 'chromium',
// channel: 'canary',
// displayName: 'Canary',
// version: '80.0.3966.0',
// path:
// '/Applications/Canary.app/Contents/MacOS/Canary',
// majorVersion: 80
// }
return {
browsers: config.browsers.filter((b) => b.name === 'chrome'),
}
}
Closing since this is already functionality available in Cypress and we do not anticipate adding a config option for it at this time.
@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?
@flotwig That doesn't work. When I filter
config.browsers
to only have thechrome
entry,cypress run
complains that theelectron
browser isn't available:
I ran into a similar issue and resolved it by changing the filter
assertion from name
to family
(as mentioned in the docs):
return {
...config,
browsers: config.browsers.filter((b) => b.family === "chromium")
}
This exposes both "chrome" and "electron" in the browsers list.
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
.
same here. somehow it puzzles me why the default browser cannot be set in cypress.json. If I understand, I might be able to accept ;-)
Reopening as this is a feature request that isn't adequately available in Cypress today still.
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:
- "E2E Testing"
- "Start E2E Testing in your-browser-here"
- 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.
Not being able to choose a browser for a CLI run doesn't make any sense. Seems to be a basic necessity for a tool that can run tests in various browsers.
@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.
Ah, would really make sense to have it on the config
file rather than doing --browser chrome
I used to run cypress in the CLI with the default Electron. But then I discovered that some tests were failing in Electron, but passed in Chrome and Firefox. In reviewing the failures, I could not find anything wrong. Since then, I've specified chrome in every test. Electron is useless to me. This option should definitely be in the config file. As of now, there's a default -- put in by Cypress -- that I can't change, and that makes it a pain to use.
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'
}
}
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.
In the file, load Cypress and in cypress.run define the chosen browser.
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
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
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
).
➕ 1️⃣ from me and my team on this as well. It would be nice to have something that was the default that could be overridden in specific cases. 99% of the time our users are using chrome and we want this to be the default instead of electron.