2

This is a continuation of Is there a programmatic way to change user agent in Cypress.io?. I am making a simple page that directs a user to the appropriate application store to download the native app. I rely on the user agent string to identify Android/iOS/other and then present the corresponding text/button/link.

Based on the question linked above, it appears as thought the only way to accomplish this to run Cypress multiple times: npx cypress run --config userAgent=[userAgent string] for each userAgent string. Is this correct? Or is there a way to do it without creating a new script?

BobtheMagicMoose
  • 2,246
  • 3
  • 19
  • 27

1 Answers1

3

Yes, it seems that is the solution.

For example:

$ cypress run --config userAgent="Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0"

The problem with setting user agent at runtime is that Cypress ignores it and a default value is sent to the server. You can find out in DevTools after the test finishes running.

Cypress mentions in the docs that it can be done with setting userAgent config value, one mention of that is on cy.visit() doc page: https://docs.cypress.io/api/commands/visit#User-agent

pavelsaman
  • 7,399
  • 1
  • 14
  • 32