cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.72k stars 3.16k forks source link

Unable to switch userAgent during test run #2100

Open hdjong1 opened 6 years ago

hdjong1 commented 6 years ago

Current behavior:

I'm testing an application that, as many these days, has a responsive design. Based on the viewport the layout changes. However, for some parts, it's also important that the user agent is set to mobile in order to trigger certain functionality. Cypress provides an userAgent option in cypress.json which can be used for this. Since my test suite contains tests for both desktop and mobile scenario's I would like to set the userAgent option during test runs. By default, I just leave it empty which takes care of the desktop scenarios. When a mobile scenario is run I want userAgent to be set to a mobile one, and switch back once a desktop scenario is encountered again. I'm using Cypress.config('userAgent', 'value') in my spec files in order to do so.

Steps to reproduce:

Test code:

describe('A certain page', () => { 
  describe('on mobile', () => {
    before(() => {
      console.log(Cypress.config('userAgent')); // outputs: null
      Cypress.config('userAgent', 'mobile_value'); // set userAgent
      console.log(Cypress.config('userAgent')); //outputs: mobile_value
      setUp(); //setup function where cookies and viewport (iphone-6) are set and cy.visit is called
    });

    it('should exhibit mobile behaviour', () => {
      cy.get('something').should('be.mobile.functionality');
    });
  });
}); 

Based on the above code I would expect to get the mobile version of my app served, but I'm still getting the desktop version through a mobile viewport. When I set the userAgent in cypress.json directly everything does work as expected (I get the mobile app served). So the functionality is working, but I can't seem to trigger it during a test run with Cypress.config('userAgent').

basarat commented 6 years ago

Verified.

describe('A certain page', () => {
  before(() => {
    console.log(Cypress.config('userAgent' as any)); // outputs: null
    Cypress.config('userAgent' as any, 'mobile_value'); // set userAgent
    console.log(Cypress.config('userAgent' as any)); //outputs: mobile_value
  });

  it('should exhibit mobile behaviour', () => {
    console.log(navigator.userAgent); // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
  });
}); 
describe('A certain page', () => {
  before(() => {
    console.log(Cypress.config('userAgent' as any)); // outputs: testValue
    Cypress.config('userAgent' as any, 'mobile_value'); // set userAgent
    console.log(Cypress.config('userAgent' as any)); //outputs: mobile_value
  });

  it('should exhibit mobile behaviour', () => {
    console.log(navigator.userAgent); // testValue
  });
}); 
mybrainishuge commented 6 years ago

Cypress version: 3.1.0

I'm experiencing what seems to be the same behavior as @hdjong1. If I log the userAgent value after setting it inside a test using Cypress.config('userAgent', 'mobile_UA_string'), it logs the mobile UA string as expected, but has no effect on my app. However, if I specify the same UA string in cypress.json and relaunch the Cypress app, the UA detection works as expected. Modifying the userAgent string from inside tests is clearly updating the config value, but it seems like that updated value isn't being passed the app being tested.

brian-mann commented 6 years ago

You cannot change the user agent in the middle of a test. What you can do is split up the mobile tests from the desktop tests and then run those groups separately via a different cypress run --config userAgent=...

The only way for us to be able to change the userAgent on the fly is at the network layer. We could make that work but that's really more applicable to the network stubbing rewrite.

mybrainishuge commented 6 years ago

@brian-mann Thanks for the quick reply and suggestion!

Is this limitation unique to setting the userAgent value or am I misunderstanding the purpose of Cypress.config as a method for modifying config values during tests?

brian-mann commented 6 years ago

Its a limitation that exists for a few different config values - basically anything that's not directly under Cypress's control like things such as timeouts or environment variables, etc.

Cypress.config(...) is a way of modifying the config but certain changes won't be respected because those values have to be applied during different stages sometimes before the browser is even launched.

mybrainishuge commented 6 years ago

I don't see this mentioned in the Cypress.config documentation. Granted, I haven't seen many other people talking about this, so it may be low priority, but some mention of this limitation in the docs could be helpful.

Thanks for your help!

brian-mann commented 6 years ago

It's been talked about in other issues - and I think the general consensus was to freeze or lock properties that cannot be changed so that it would throw and you'd immediately know.

mybrainishuge commented 6 years ago

Makes sense. That would be much quicker than consulting documentation, which would be convenient.

Next time it comes up you can mention the random guy in issue #2100 who supports the freeze/lock and throw idea if you need a tie breaker. In fact, popup my profile pic when it throws with the caption "No."

nobleach commented 6 years ago

So I see above that there might be a way to run cypress run with a config value. Should this work: cypress run --config userAgent='Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10'?

I'm getting a huge error in the UI when I attempt to run that.

mybrainishuge commented 6 years ago

@nobleach The value you're specifying for userAgent contains several characters that aren't being parsed how you want. I'm not sure the best solution, but my kind of hacky solution was to pass a simplified string to the run/open command and check for that string when loading plugins

"cypress:dev:mobile": "cypress open --env configFile=dev,userAgent=mobile",

In the plugins export, if "mobile" is specified, then I add the full user agent string to the config that gets returned

if (config.env.userAgent === 'mobile') {
  data.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';
}

It doesn't feel very pretty, but the user agent needs to be specified before Cypress loads the config and this is what I came up with.

nobleach commented 6 years ago

I suppose that's viable. I'm interested in how others handle it too. An adaptive app that renders a different experience for mobile/tablet/desktop isn't that far outside of reality, is it?

I very much appreciate you sharing your approach, so thank you!

raul782 commented 6 years ago

I've been migrating my tests to Cypress and I'm facing this issue where we have 3 platforms and each needs a different user agent among other specific configurations that are caught and injected in our plugin, the same spec is run for each platform, so we do a loop before starting each spec suite. We have a fixture organized by platform, so we can assert easily.

Doing it manually is not a viable solution, I thought I could also move away from gulp and just use npm run scripts, but so far the only way I could accomplish this is to have all the different configs in gulp.

Is there any other approach you can suggest us?

Thanks

nobleach commented 6 years ago

Using npm scripts shouldn't be a problem. We do it like this:

    "cypress:prod": "CYPRESS_baseUrl=http://localhost:8080 cypress run --env userAgent=mobile",
    "cypress:prod:tablet": "CYPRESS_baseUrl=http://localhost:8080 cypress run --env userAgent=tablet",
    "cypress:ci": "cypress:prod && cypress:prod:tablet",

I've used the above suggestion in my plugins/index.js:

module.exports = (on, config) => {
    // create new config settings
    const configOverride = {};
    if (config.env.userAgent === 'mobile') {
        configOverride.userAgent = 'Mozilla/5.0 (Linux; Android 6.0.1; SM-G532G Build/MMB29T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.83 Mobile Safari/537.36';
        configOverride.testFiles = 'mobile/**/*.*';
    } else if (config.env.userAgent === 'tablet') {
        configOverride.userAgent = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10';
        configOverride.testFiles = 'tablet/**/*.*';
    } else {
        configOverride.userAgent = 'none';
    }

    return Object.assign({}, config, configOverride);
};
Songyu-Wang commented 5 years ago

It would be nice if this can be mentioned in the online docs, it took me a while before figuring out what was wrong

jpalmieri commented 5 years ago

Another bump for clearer documentation on this (or some alert in the UI). It also took me some time to get here and figure out that this isn't possible.

urig commented 5 years ago

And another bump from me. I'm a paying customer and I wasted several hours till I was directed by someone to this issue.

Issue was reported al,ost a year ago. If you can't change the functionality to match the docs then please change the docs to match the functionality. ---> PR here: https://github.com/cypress-io/cypress-documentation/pull/1681

flotwig commented 5 years ago

Related issue that would make it an error to try to use Cypress.config on a non-overridable property: #3422

luokuning commented 4 years ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});
JingyulGary commented 4 years ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});

I was trying to use this method to trigger my React frontend to render the mobile page, but it didn't work. Any idea?

luokuning commented 4 years ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});

I was trying to use this method to trigger my React frontend to render the mobile page, but it didn't work. Any idea?

Maybe you should also change the viewport size, such as cy.viewport('iphone-x').

gianlucabaldin commented 4 years ago

same problem for us / annoying not to find this in the official docs but within the issues.

fixed with @luokuning 's solution - thanks a lot

feliperaul commented 3 years ago

I'd like to chime in my + 1. This is a must have feature 😬

I also lost one good hour trying to figure out why neither the second argument for describe or it didn't respect the user agent.

I finally found this issue (and, to be fair, this on the documentation, stating that userAgent would be ignored), but a much better developer experience would to be thrown an error about this, warning that you were trying to change an immutable value during the test run.

It's also confusing in the documentation that if you pass { browser: { userAgent: 'xxx' } you'll get a faded blue test stating "Ignored due to browser", because it's actually conditioning the test run, and not changing browser options.

Regarding workarounds, I managed to set my user agent doing it like this:

cy.visit('/my-url', {
    headers: { 'user-agent': 'mobile' }
});

However, unfortunately that still doesn't cut it for our tests because it will send that header only in the first request, but not honor it on any page redirects; so if your test clicks on a button and gets redirected to another page, the redirect won't have those headers.

Had I known this beforehand, I wouldn't have switched our test suite to Cypress. It's really a big deal for us because we use things like browser.device.mobile? in our Rails app all the time, to conditionally render elements or even alter redirect urls based on the browser's user agent.

The other workaround posted here by @luokuning unfortunately doesn't work in Cypress 6.

pamo commented 3 years ago

@feliperaul @jennifer-shehane any updated recommendations on persisting that user agent header throughout the whole test run?

melibe23 commented 2 years ago

On Config I have nothing set as userAgent

On my tests I have: I have

before(() => {
    cy.visit(path, {
      headers: {
        'user-agent': userAgentTablet,
      },
    })
    cy.scrollTo(0, 500)
  })

Still, I don't get iPad userAgent. I get the desktop one. I have my desktop/mobile/tablet split into 3 different specs.

I don't know what else to try.

melibe23 commented 2 years ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});

I don't think will work. This will set the user agent on the browser where the Test Runner is running, no where you visit your app.

melibe23 commented 2 years ago

There MUST be a dynamic way to set the user agent rather than thru a command line for each userAgent, isn't it? None of the workarounds mentioned above that uses plugin/index worked for me (maybe I am doing something wrong, of course)... but isn't over there any clear documentation on how to dynamically (based on spec name, relative path, something like this) set a user agent before the run starts?

I am also an enterprise client and this little thing is killing me (I love you tool, but there must be something over there that I am not fully catching) :pray:

AlvinaMohsin05 commented 2 years ago

I have used the approached that @luokuning mentioned, but it only seems to be working with the url I have passed with command, for instance, if I navigate to a different screen by clicking on a button or link, I lose the header object and the functionality returns to desktop view. Can someone please guide how to keep the the headers present with every subsequent click?

Masvi commented 2 years ago

You cannot change the user agent in the middle of a test. What you can do is split up the mobile tests from the desktop tests and then run those groups separately via a different cypress run --config userAgent=...

The only way for us to be able to change the userAgent on the fly is at the network layer. We could make that work but that's really more applicable to the network stubbing rewrite.

Any changes in this scenario? or should I go with the split tests approach?

brobee commented 2 years ago

I am unable to change/override userAgent via config file.

my cypress json contains:
env object: {
    "platform" : "mobile"
}

my plugins/index.js

on(config => {
    if (config.env.platform === 'desktop') {
      config.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36'
    } else if (config.env.platform === 'mobile') {
      config.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
    }
    return config
  }) 

The error is: Error: You must pass a valid event name when registering a plugin.

I totally get it, I am unable to change it during runtime, but before that via config should work. What did I miss?

emilyrohrbough commented 2 years ago

@brobee Try taking a look at the pluginsFile configuration setup here: https://docs.cypress.io/guides/references/configuration#Plugins

You should be able to slim this down set it up when the plugin is resolved:

// cypress/plugins/index.js
+ module.exports = (on, config) => {
- on(config => {
    if (config.env.platform === 'desktop') {
      config.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36'
    } else if (config.env.platform === 'mobile') {
      config.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
    }
    return config
-  }) 
}
brobee commented 2 years ago

@emilyrohrbough I tried this way without event, but it seems this particular code did not run and userAgent is still null. Is it possible to override the null? it seems weird to me

melibe23 commented 2 years ago

No news on this? No workaround? Sorry to be the one that ask this question but it seems Cypress team is not updating, only users are.

sebastianpennino commented 2 years ago

My workaround was to have a separated config file and manually running the tests for desktop and mobile. I know, it sucks but someone might find this useful.

I have my regular configuration with all my options on the root folder called cypress.json and I have a separated one called cypress-mobile.json that has all the same things, but has an extra property as you can see here:

{
  "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/15A372 Safari/604.1"
}

So on my project package.json on the scripts section now I have two entries:

"scripts": {
  "test": "cypress open",
  "test:mobile": "cypress open --config-file cypress-mobile.json",
}

Hope it helps someone.

hoguan commented 2 years ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});

This workaround does not work any more on Cypress 10. Any update on this ticket? Or What could I do on Cypress 10 if I need to switch userAgent during test run? Thanks

Masvi commented 2 years ago

I am unable to change/override userAgent via config file.

my cypress json contains:
env object: {
    "platform" : "mobile"
}

my plugins/index.js

on(config => {
    if (config.env.platform === 'desktop') {
      config.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36'
    } else if (config.env.platform === 'mobile') {
      config.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
    }
    return config
  }) 

The error is: Error: You must pass a valid event name when registering a plugin.

I totally get it, I am unable to change it during runtime, but before that via config should work. What did I miss?

It works for me. This configfile is for the cypress v10 and the file cypress-mobile.config.js has the mobile user-agent and the cypress.config.js has the desktop config.

const { defineConfig } = require('cypress');

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:3000',
    supportFile: 'cypress/support/e2e.js',
    specPattern: 'cypress/integration/my-tests-folder/**/*',
    userAgent:
      'Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
    pageLoadTimeout: 70000,
  },
});
mirobo commented 2 years ago

There are more issues/discussions related to userAgent: https://github.com/cypress-io/cypress/discussions?discussions_q=userAgent, https://github.com/cypress-io/cypress/issues/4402, https://github.com/cypress-io/cypress/issues/9319 .

As I saw in here: https://github.com/cypress-io/cypress/blob/b0e32f8f8ea196438e12308b0ab36fde1c17bd02/packages/server/lib/browsers/electron.js#L242 https://github.com/cypress-io/cypress/blob/b0e32f8f8ea196438e12308b0ab36fde1c17bd02/packages/server/lib/browsers/chrome.ts#L501 https://github.com/cypress-io/cypress/blob/b0e32f8f8ea196438e12308b0ab36fde1c17bd02/packages/server/lib/browsers/firefox.ts#L437

Cypress just picks up the same userAgent configuration for every browser.

I have an issue with a 3rd party library (tesseract) not working when "Electron" was in the userAgent string. So I removed it. I wonder why the same userAgent-String is forced by Cypress onto the browser and in case of Firefox they are even not realistic:

Cypress Firefox: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36 Real Firefox: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

I'd suggest that it's probably a better way to set the userAgent for each browser correctly the first time (IMO Firefox is just wrong?) or allow the user agent to be set via cypress.config.js based on the browser:

userAgent: 'my custom useragent', (should still work and be used as default if browserConfig is not set?),
browserConfig: {
  firefox: {
    userAgent: '..whatever Firefox/91'
  }
}

The other workaround would be to extend browser configuration in 'before:browser:launch'

launchOptions.preferences['general.useragent.override'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0';

But this way we just override what Cypress is already overriding.. which seems cumbersome.

PS. Also I think it would be fantastic feature to add two new "pseudo" browser types: "mobile-smartphone" and "mobile-tablet" (or separate them even by OS with mobile-smartphone-android / mobile-tablet-android (chrome-based), mobile-smartphone-ios and mobile-tablet-ios (webkit-based, once it's implemented). They would already start with a common viewport size and with a realistic userAgent?

I thinka a lot of people would profit from such quality of life enhancements.

rnovosad commented 1 year ago

Here is another workaround mentioned here, may be useful in some case:

before(() => {
    cy.visit(url, {
        onBeforeLoad: win => {
            Object.defineProperty(win.navigator, 'userAgent', {
                value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
            });
        },
    });
});

This workaround does not work any more on Cypress 10. Any update on this ticket? Or What could I do on Cypress 10 if I need to switch userAgent during test run? Thanks

we figured out how to fix it for Cypress 10 and basically 11

cy.origin(url, { args: { path } }, ({ path }) => {
    cy.visit(`/${path}`, {
      onBeforeLoad: (win) => {
        Object.defineProperty(win.navigator, "userAgent", {
          value:
            "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
        });
      }
    });
  });

that should help

but the problem that was solved for us in Cypress 10 with cy.origin() was broken again in 11 we had a mobile portal on a different subdomain and it stopped working with the error:

cy.origin() requires the first argument to be a different domain than top. 
You passed https://subdomain1.domain.com/ to the origin command,
while top is at https://subdomain2.domain.com.

Either the intended page was not visited prior to running the cy.origin block 
or the cy.origin block may not be needed at all.

how can we allow using cy.origin() on the same domain?

AtofStryker commented 1 year ago

@rnovosad since the super domains are the same same, are you able to just perform the visit without using cy.origin, like:

    cy.visit(url, {
      onBeforeLoad: win => {
        Object.defineProperty(win.navigator, 'userAgent', {
            value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        });
      },
    });

    cy.visit(`/${path}`, {
      onBeforeLoad: (win) => {
        Object.defineProperty(win.navigator, "userAgent", {
          value:
            "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
        });
      }
    });
rnovosad commented 1 year ago

@AtofStryker tried this, but it doesn't help...

AtofStryker commented 1 year ago

@AtofStryker tried this, but it doesn't help...

in other words the user agent fails to update?