In Overriding Options - Command Line
When running Cypress from the Command Line you can pass a --config flag
For command line usage
cypress open --config userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0"
For package.json usage (note escaped double quotes in the string)
"scripts: {
...
"cy:ua-moz": "cypress open --config userAgent=\"Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0\""
Check it in the Cypress runner under Settings/Configuration - it is flagged with the "set from CLI arguments" color.
Or programatically within the test, Cypress.config()
and Is there a programmatic way to change user agent in Cypress
const userAgents = {
"Firefox": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0",
"Opera": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41",
"Safari": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"
}
userAgents.forEach(ua => {
Cypress.config('userAgent', userAgents[ua]); // set outside of test
// see caveat below
it(`Test with user agent ${ua}`, () => {
// test here
}
}
ALTHOUGH some indication of trouble with this method
Not all config values can be changed at all times
Some configuration values cannot be changed while running a test. Anything that’s not directly under Cypress’s control - like timeouts, userAgent, or environment variables - will be ignored at run-time.