0
0
Cypresstesting~20 mins

cy.viewport() for screen sizes in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Viewport Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the viewport size after this Cypress command?
Consider the following Cypress test code snippet:
cy.viewport(1024, 768);

What will be the viewport width and height set by this command?
Cypress
cy.viewport(1024, 768);
AWidth: 1024px, Height: 768px
BWidth: 768px, Height: 1024px
CWidth: 1024px, Height: 1024px
DWidth: 768px, Height: 768px
Attempts:
2 left
💡 Hint
The first argument is width, the second is height.
assertion
intermediate
2:00remaining
Which assertion correctly verifies the viewport size?
You want to check if the viewport width is 375 pixels after setting it with cy.viewport(375, 667). Which assertion is correct?
Cypress
cy.viewport(375, 667);
// Which assertion below correctly checks the width?
Acy.document().its('width').should('equal', 375);
Bcy.window().its('innerWidth').should('equal', 375);
Ccy.get('body').should('have.css', 'width', '375px');
Dcy.viewport().should('have.property', 'width', 375);
Attempts:
2 left
💡 Hint
The window object holds the viewport size properties.
🧠 Conceptual
advanced
1:30remaining
What happens if you use cy.viewport() with a preset device name?
In Cypress, what does this command do?
cy.viewport('iphone-6');

Choose the correct explanation.
ASets the viewport to the screen size of an iPhone 6 but does not change device pixel ratio or user agent.
BSets the viewport to the exact screen size of an iPhone 6 including device pixel ratio and user agent.
CSets the viewport to a random mobile device size similar to iPhone 6.
DThrows an error because device names are not supported in cy.viewport().
Attempts:
2 left
💡 Hint
Device presets set viewport size but do not emulate device pixel ratio or user agent.
🔧 Debug
advanced
2:00remaining
Why does this viewport test fail?
Given this test:
cy.viewport(800, 600);
cy.window().its('innerWidth').should('equal', 600);

Why does the assertion fail?
ABecause the viewport height is 600, so innerWidth should be 600.
BBecause cy.viewport() does not change innerWidth property.
CBecause the assertion syntax is incorrect.
DBecause innerWidth is the width, so it should be 800, not 600.
Attempts:
2 left
💡 Hint
Remember which value is width and which is height in cy.viewport().
framework
expert
3:00remaining
How to test responsive design for multiple screen sizes efficiently in Cypress?
You want to run the same test on multiple screen sizes: 320x480, 768x1024, and 1280x720. Which approach is best to write this in Cypress?
AUse a loop inside the test to call cy.viewport() with each size and run assertions inside the loop.
BWrite separate tests for each screen size with hardcoded cy.viewport() calls.
CUse Cypress custom commands to set viewport and call the test logic once per size using a data-driven approach.
DSet cy.viewport() once globally and rely on browser resizing manually.
Attempts:
2 left
💡 Hint
Think about reusability and clean test code.