Unable to switch userAgent during test run
See original GitHub issueCurrent 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')
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:27
- Comments:40 (9 by maintainers)
Top GitHub Comments
Using npm scripts shouldn’t be a problem. We do it like this:
I’ve used the above suggestion in my
plugins/index.js
:Here is another workaround mentioned here, may be useful in some case: