Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify user-agent on each call #364

Closed
z-shan opened this issue Dec 27, 2016 · 12 comments
Closed

modify user-agent on each call #364

z-shan opened this issue Dec 27, 2016 · 12 comments
Assignees
Labels
pkg/server This is due to an issue in the packages/server directory type: feature New feature that does not currently exist
Milestone

Comments

@z-shan
Copy link

z-shan commented Dec 27, 2016

Should be able to modify user-agent string on each request.
This would give us more flexibility to work on apps that are modified version of chrome.

@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Dec 27, 2016
@brian-mann
Copy link
Member

brian-mann commented Oct 3, 2017

We can add userAgent as a configuration option in cypress.json and can normalize it across:

  • Electron (native API's)
  • Chrome (CLI flags or webExtension)
  • Firefox (prefs or webExtension)
  • Edge (webExtension)
  • Safari with a hack

Another option is to set userAgent headers at the network proxy layer. This would set it for all requests going to the current domain / origin under test but would not affect other traffic.

Once we finish #684 users will be able to change the flags if we haven't finished implementing this yet.

@raphaeleidus
Copy link

I really need this feature. My website varies based on user agent.

@brian-mann
Copy link
Member

Understood. You could achieve this right now by booting your server in that specific user agent mode so it serves all requests as if they all shared the same user agent.

Also now that Cypress is open source you are welcome to submit a PR to add this functionality.

@demetriusnunes
Copy link

@brian-mann I'd be willing to try my hand at a PR if you could please point me in the right direction in terms of where in the code should I be doing it. I see that Chrome/Chromium has a --user-agent command line flag according to https://peter.sh/experiments/chromium-command-line-switches/#user-agent - so it might not be that hard. I'm thinking of doing it in a more generic configuration, like chromeFlags. What do you think?

@brian-mann
Copy link
Member

brian-mann commented Nov 1, 2017

The problem is that you have to normalize this behavior across all browsers we support (or intend to support).

For instance exposing the command line would work for Chrome but instantly fail for Electron.

This issue has gained a lot of traction recently so I'll look to see if I can implement it in the next few days.

Regarding exposing the flags - we plan on doing that with the Plugins API which is almost done. #684

If you did want to poke around it would be in packages/server/lib/browsers

@jennifer-shehane jennifer-shehane added pkg/server This is due to an issue in the packages/server directory stage: needs investigating Someone from Cypress needs to look at this labels Nov 1, 2017
@bivainis
Copy link

bivainis commented Nov 3, 2017

Being able to specify user agent would also allow testing Ionic based projects in mobile mode. Ionic uses userAgent string and sets different styles and behavior for on Android/iOS projects.
Hope this is implemented soon!

Does anyone have workarounds for the time being?

@mpigsley
Copy link

mpigsley commented Nov 3, 2017

Seems like the proposed implementation would set the user agent for all tests via the command line or configuration file (correct me if I'm wrong). I would need to set this dynamically for different tests in the suite. Would that be possible?

@brian-mann
Copy link
Member

brian-mann commented Nov 3, 2017

The problem is that we could allow you to change the user agent dynamically (inside of tests) for Chrome and Electron, but it would not be possible to do this for the rest of the browsers.

An alternative is if we did user agent modification at the network layer (outside of the browser) we could do this for all traffic coming from the origin under test - but other network requests (other than to origin) would not have their user agent set properly. Also the Dev Tools network panel would always indicate the wrong user agent (since it would be modified outside the browser in flight). The upside to this is that it would work on all browsers.

I'm more in favor of not doing it at the network layer. I don't see needing to switch user agents within the same spec file very valuable. Instead you could simply partition your tests into mobile + non-mobile specs and then specifically run one or the other. Yes you wouldn't be able to run them all in one shot, but I don't see another solution that covers all the bases here.

@mpigsley
Copy link

mpigsley commented Nov 3, 2017

That makes sense. Thanks for the info @brian-mann!

@bivainis
Copy link

bivainis commented Nov 6, 2017

I personally wouldn't mind if only Chrome is supported. The main purpose for me is to test integration between our web application and hybrid mobile application (both running on different ports on localhost), as it is very time consuming to test.

A simplified example could look like this:

describe('change username', () => {
    let userName = 'MyNewUser';

    it('should change user name on web app', () => {
        cy.visit(config.webAppUrl + '/profile');

        cy.get('#new-username')
            .type(userName);
    });

    it('should check if username was updated correctly', () => {
        // for Ionic projects, this should change user agent too,
        // because Ionic relies on it to detect correct platform.
        cy.viewport('iphone-6');
        cy.visit(config.mobileAppUrl + '/profile');

        cy.get('#username')
            .should('have.value', userName);
    });
});

@jennifer-shehane
Copy link
Member

Requires documentation: cypress-io/cypress-documentation#328

@brian-mann
Copy link
Member

Released in 1.2.0.

@jennifer-shehane jennifer-shehane removed the stage: needs investigating Someone from Cypress needs to look at this label Dec 15, 2017
@cypress-io cypress-io locked as resolved and limited conversation to collaborators Dec 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/server This is due to an issue in the packages/server directory type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

7 participants