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

Comments

7 participants
@z-shan

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.

@brian-mann

This comment has been minimized.

Show comment
Hide comment
@brian-mann

brian-mann Oct 3, 2017

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@raphaeleidus

raphaeleidus Oct 20, 2017

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

raphaeleidus commented Oct 20, 2017

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

@brian-mann

This comment has been minimized.

Show comment
Hide comment
@brian-mann

brian-mann Oct 20, 2017

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.

Member

brian-mann commented Oct 20, 2017

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

This comment has been minimized.

Show comment
Hide comment
@demetriusnunes

demetriusnunes Nov 1, 2017

@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?

demetriusnunes commented Nov 1, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@brian-mann

brian-mann Nov 1, 2017

Member

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

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

@bivainis

This comment has been minimized.

Show comment
Hide comment
@bivainis

bivainis 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?

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

This comment has been minimized.

Show comment
Hide comment
@mpigsley

mpigsley 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?

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

This comment has been minimized.

Show comment
Hide comment
@brian-mann

brian-mann Nov 3, 2017

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@mpigsley

mpigsley Nov 3, 2017

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

mpigsley commented Nov 3, 2017

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

@bivainis

This comment has been minimized.

Show comment
Hide comment
@bivainis

bivainis 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);
    });
});

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);
    });
});

brian-mann added a commit that referenced this issue Dec 12, 2017

@brian-mann brian-mann added this to the 1.2.0 milestone Dec 13, 2017

@jennifer-shehane

This comment has been minimized.

Show comment
Hide comment
@jennifer-shehane
Member

jennifer-shehane commented Dec 13, 2017

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

@brian-mann

This comment has been minimized.

Show comment
Hide comment
@brian-mann

brian-mann Dec 15, 2017

Member

Released in 1.2.0.

Member

brian-mann commented Dec 15, 2017

Released in 1.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment