2

I have a use case of running cypress tests in different mobile/tablet browsers. I did read about setting userAgent in cypress through Configuration file: https://docs.cypress.io/guides/references/configuration.html#Options, but this will be set in configuration file.

I am looking for ways to set UserAgent through CLI or run time...meaning I want to run the same test for different userAgents. Did anyone work on this use case? How do we set userAgent in this case?

Thanks, Saahith

sgv393
  • 133
  • 2
  • 9

2 Answers2

1

You can override an existing configuration value in the cypress.json file using cypress_. You can look here in the cypress docs

For example, if you already have a userAgent set in your cypress.json file but for your test execution you need a different userAgent, you execute the command:

cypress_userAgent="value" npx cypress run
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
1

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.

  • Note: I noticed that long userAgent strings would throw an error in Cypress! `npx cypress open --config userAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A356 Safari/604.1"` gives this error: Cypress encountered an error while parsing the argument: --config You passed: userAgent=Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A356 Safari/604.1 Making it much smaller fixed it! – BobtheMagicMoose Mar 23 '22 at 14:26