Set Browser Screen Size in Cypress: A Step-by-Step Guide
In this article
Setting the browser screen size in Cypress is essential for testing responsive designs and ensuring your application looks and behaves correctly across different viewport dimensions. By default, Cypress runs at a 1000x660 viewport, but you can easily change this to match specific devices or test scenarios. This guide walks you through the methods to set the viewport size, both globally and per test, and covers common pitfalls and troubleshooting tips.
What You'll Need
- A Cypress project already set up (i.e.,
npm install cypressand the Cypress Test Runner opened at least once). - Basic familiarity with Cypress test files (usually in
cypress/e2eorcypress/integration). - Your application's URL or a local dev server running.
Step-by-Step
- Open your Cypress test file. This is typically a
.cy.jsor.spec.jsfile inside thecypress/e2efolder. If you don't have one, create a new file, e.g.,viewport.spec.js. - Use the
cy.viewport()command to set the size for a single test. The command takes width and height as numbers (in pixels) or a preset string like'iphone-6'or'macbook-13'. For example, to set a desktop size of 1440x900, add this as the first line inside yourit()block:cy.viewport(1440, 900)
This will apply for the rest of that specific test. - Set the viewport before each test using
beforeEach(). If you want the same size for all tests in a describe block, place thecy.viewport()call inside abeforeEach()hook. Example:describe('My tests', () => { beforeEach(() => { cy.viewport(1280, 720) }) it('test 1', () => { ... }) it('test 2', () => { ... }) }) - Set a global viewport in the Cypress configuration file. Open
cypress.config.js(orcypress.jsonin older versions). Add aviewportWidthandviewportHeightproperty inside thee2eobject (or top-level for older configs). For example:module.exports = { e2e: { viewportWidth: 1920, viewportHeight: 1080, // other config... }, }
This sets the default for all tests, but individualcy.viewport()calls will override it. - Use preset device names for common screen sizes. Cypress includes presets like
'iphone-x','ipad-mini', and'macbook-15'. You can use them directly:cy.viewport('iphone-x'). This is handy for quick mobile/tablet testing. - Reset the viewport to the default. If you changed the viewport in a test and want to go back to the default size (or the one in config), call
cy.viewport('reset'). - Run your tests. Execute
npx cypress run(for headless) ornpx cypress open(for the interactive runner) to see the changes take effect. In the interactive runner, you can also manually change the viewport using the dropdown in the top-right corner, but that doesn't affect the test code.
Common Mistakes
- Forgetting to set viewport before visiting the page. If you call
cy.visit()beforecy.viewport(), the page may load at the default size, and the viewport change will trigger a resize, which can cause layout shifts or unexpected behavior. Always set the viewport first. - Using the wrong unit.
cy.viewport()expects numbers (pixels) or a string preset. If you pass a string like'1440x900', it won't work. Usecy.viewport(1440, 900). - Overriding global config incorrectly. In the config file, make sure you place
viewportWidthandviewportHeightunder the correct section (e.g.,e2e). If you put them at the top level in a newer config, they may be ignored. - Not resetting viewport between tests. If you set a viewport in one test without a
beforeEach, the next test will inherit that size unless you change it again. UsebeforeEachor explicitly reset.
Tips
- Use a
beforeEachin a shareddescribeblock to keep your tests consistent and avoid repetition. - For responsive testing, create a separate test file for each breakpoint (e.g.,
mobile.spec.js,desktop.spec.js) and set the viewport at the top of each file using abeforeEach. - Take advantage of preset names for common devices—they are well-tested and save you from looking up pixel dimensions.
- If you need to test both portrait and landscape orientations, use
cy.viewport('iphone-x', 'portrait')or'landscape'as the second argument. - Remember that viewport size affects not only visual layout but also JavaScript media queries, element visibility, and even network requests (e.g., responsive images). Test thoroughly.
Troubleshooting
- Viewport changes don't seem to apply. Check if you have multiple
cy.viewport()calls in the same test—the last one wins. Also ensure you are not usingcy.visit()after the viewport change in a way that reloads the page with a different size (though it shouldn't). Clear your browser cache and re-run. - Preset names not working. Make sure you are using the exact string as documented (e.g.,
'iphone-6'not'iPhone 6'). Refer to the Cypress docs for the full list. - Config file changes not taking effect. After editing
cypress.config.js, you must restart the Cypress Test Runner (close and reopen) or re-run the tests. The config is read at startup. - Tests pass locally but fail in CI with different sizes. Ensure your CI environment uses the same Cypress version and that you haven't set a different viewport in the CI config. Also, check if your CI has a smaller screen resolution—Cypress runs in a headless browser that respects the viewport you set, but the physical screen size doesn't matter.
- Page elements are hidden or not visible. After changing viewport, some elements might be off-screen or hidden due to responsive CSS. Use
cy.scrollIntoView()or adjust your test to account for the new layout.
Frequently Asked Questions
Can I set the viewport size in the Cypress config file?
Yes, you can set viewportWidth and viewportHeight in cypress.config.js under the e2e object. This sets the default for all tests.
How do I set the viewport to a specific mobile device like iPhone 12?
Use a preset name: cy.viewport('iphone-x') or define custom dimensions: cy.viewport(390, 844) (iPhone 12 dimensions).
Does cy.viewport() affect the browser window size or just the viewport?
It sets the viewport size, which is the visible area of the page. The browser window itself may be larger, but the viewport is what your app sees.
Can I change the viewport mid-test?
Yes, you can call cy.viewport() at any point in a test. It will resize the viewport and trigger a resize event, which can be useful for testing responsive behavior.
How do I reset the viewport to the default size?
Use cy.viewport('reset') to go back to the default size (the one set in the config or the Cypress default of 1000x660).
Why does my test fail when I set a very small viewport?
Some elements may be hidden or the layout may break. Check your responsive CSS, use cy.scrollIntoView(), or ensure that the element you're interacting with is visible within the viewport.