What if you could test your website on every device size with just one line of code?
Why cy.viewport() for screen sizes in Cypress? - Purpose & Use Cases
Imagine testing a website manually on your computer, then resizing the browser window every time to check how it looks on a phone, tablet, or desktop screen.
You have to do this for many pages and features, over and over again.
Manually resizing the browser is slow and tiring.
You might forget to test some screen sizes or make mistakes resizing.
This leads to missed bugs and unhappy users on different devices.
Using cy.viewport() in Cypress lets you quickly set the screen size in your tests.
This means your tests automatically check how your site works on phones, tablets, and desktops without manual resizing.
Visit page
Resize browser manually
Check layout
Repeat for each sizecy.viewport('iphone-6') cy.visit('/page') cy.checkLayout()
You can easily test your website on many screen sizes automatically, catching layout bugs early and saving time.
A developer tests a shopping site to make sure the 'Add to Cart' button is visible and clickable on phones and tablets, not just desktops.
Manual resizing is slow and error-prone.
cy.viewport() sets screen size in tests automatically.
This helps catch layout issues on different devices quickly.
Practice
cy.viewport() in Cypress tests?Solution
Step 1: Understand the function of
This command sets the browser's screen size to simulate different devices or resolutions.cy.viewport()Step 2: Identify the purpose in testing
Changing screen size helps test how the website looks and behaves on various devices, ensuring responsive design works.Final Answer:
To change the screen size and test responsive layouts -> Option AQuick Check:
cy.viewport() changes screen size = D [OK]
- Confusing viewport with element selectors
- Thinking viewport waits for elements
- Assuming viewport takes screenshots
Solution
Step 1: Recall the correct
The command accepts width and height as two separate number arguments:cy.viewport()syntaxcy.viewport(width, height).Step 2: Check each option
cy.viewport(1024, 768); uses two numbers correctly. cy.viewport('1024x768'); uses a string which is invalid. cy.viewport({width: 1024, height: 768}); uses an object which is not supported. cy.setViewport(1024, 768); uses a wrong command name.Final Answer:
cy.viewport(1024, 768); -> Option BQuick Check:
cy.viewport(width, height) = C [OK]
- Passing size as a string like '1024x768'
- Using an object instead of two numbers
- Typing wrong command name like cy.setViewport
cy.viewport('iphone-6');
cy.viewport(800, 600);Solution
Step 1: Understand command order in Cypress
Commands run in order. The lastcy.viewport()call sets the final size.Step 2: Analyze the code
First sets viewport to iPhone 6 size, then immediately changes it to 800x600 pixels.Final Answer:
800 pixels wide and 600 pixels tall -> Option AQuick Check:
Last cy.viewport() sets size = B [OK]
- Thinking first viewport call persists
- Assuming test fails on multiple viewport calls
- Confusing preset names with pixel sizes
cy.viewport(1024);
What is the problem?
Solution
Step 1: Check the cy.viewport() parameters
The command requires both width and height as numbers, e.g.,cy.viewport(1024, 768).Step 2: Identify the error
Only width (1024) is provided, height is missing, so viewport size won't change correctly.Final Answer:
Missing height argument in cy.viewport() -> Option CQuick Check:
Both width and height needed = A [OK]
- Passing only one number instead of two
- Thinking cy.viewport accepts only device names
- Believing viewport must be set before visiting page
Solution
Step 1: Understand sequential viewport changes
You can callcy.viewport()multiple times to change screen size during a test.Step 2: Check the code snippets
cy.viewport('iphone-x');\ncy.get('#menu').should('be.visible');\ncy.viewport(1280, 720);\ncy.get('#menu').should('be.visible'); sets iPhone X viewport, runs a check, then sets desktop viewport and runs the check again. This tests both views properly. cy.viewport('iphone-x', 1280, 720);\ncy.get('#menu').should('be.visible'); uses invalid syntax with multiple arguments. cy.viewport(1280, 720);\ncy.viewport('iphone-x');\ncy.get('#menu').should('be.visible'); changes viewport twice before any check. cy.viewport('iphone-x');\ncy.viewport(1280, 720);\ncy.get('#menu').should('be.visible'); runs only one check after both viewport changes, missing the first test.Final Answer:
Set viewport, test, change viewport, test again -> Option DQuick Check:
Change viewport before each test step = A [OK]
- Passing multiple sizes in one cy.viewport() call
- Changing viewport without running tests between
- Running tests only after last viewport change
Ready to prove it?
Pass the test and this pattern moves unknown
