Bird
Raised Fist0
Cypresstesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using cy.viewport() in Cypress tests?
easy
A. To change the screen size and test responsive layouts
B. To select elements on the page
C. To wait for an element to appear
D. To take screenshots of the page

Solution

  1. Step 1: Understand the function of cy.viewport()

    This command sets the browser's screen size to simulate different devices or resolutions.
  2. 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.
  3. Final Answer:

    To change the screen size and test responsive layouts -> Option A
  4. Quick Check:

    cy.viewport() changes screen size = D [OK]
Hint: Remember: viewport changes screen size for responsive tests [OK]
Common Mistakes:
  • Confusing viewport with element selectors
  • Thinking viewport waits for elements
  • Assuming viewport takes screenshots
2. Which of the following is the correct syntax to set the viewport size to 1024 pixels wide and 768 pixels tall in Cypress?
easy
A. cy.viewport({width: 1024, height: 768});
B. cy.viewport(1024, 768);
C. cy.viewport('1024x768');
D. cy.setViewport(1024, 768);

Solution

  1. Step 1: Recall the correct cy.viewport() syntax

    The command accepts width and height as two separate number arguments: cy.viewport(width, height).
  2. 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.
  3. Final Answer:

    cy.viewport(1024, 768); -> Option B
  4. Quick Check:

    cy.viewport(width, height) = C [OK]
Hint: Use two numbers, not strings or objects, in cy.viewport() [OK]
Common Mistakes:
  • Passing size as a string like '1024x768'
  • Using an object instead of two numbers
  • Typing wrong command name like cy.setViewport
3. What will be the viewport size after running this Cypress code?
cy.viewport('iphone-6');
cy.viewport(800, 600);
medium
A. 800 pixels wide and 600 pixels tall
B. iPhone 6 preset size
C. Default viewport size
D. Test will fail due to conflicting viewport commands

Solution

  1. Step 1: Understand command order in Cypress

    Commands run in order. The last cy.viewport() call sets the final size.
  2. Step 2: Analyze the code

    First sets viewport to iPhone 6 size, then immediately changes it to 800x600 pixels.
  3. Final Answer:

    800 pixels wide and 600 pixels tall -> Option A
  4. Quick Check:

    Last cy.viewport() sets size = B [OK]
Hint: Last cy.viewport() call overrides previous sizes [OK]
Common Mistakes:
  • Thinking first viewport call persists
  • Assuming test fails on multiple viewport calls
  • Confusing preset names with pixel sizes
4. You wrote this code but the viewport size does not change as expected:
cy.viewport(1024);

What is the problem?
medium
A. Viewport size must be set before visiting the page
B. cy.viewport() does not accept numbers
C. Missing height argument in cy.viewport()
D. cy.viewport() only accepts device names

Solution

  1. Step 1: Check the cy.viewport() parameters

    The command requires both width and height as numbers, e.g., cy.viewport(1024, 768).
  2. Step 2: Identify the error

    Only width (1024) is provided, height is missing, so viewport size won't change correctly.
  3. Final Answer:

    Missing height argument in cy.viewport() -> Option C
  4. Quick Check:

    Both width and height needed = A [OK]
Hint: Always provide width and height numbers in cy.viewport() [OK]
Common Mistakes:
  • Passing only one number instead of two
  • Thinking cy.viewport accepts only device names
  • Believing viewport must be set before visiting page
5. You want to test your website's mobile and desktop views in one test. Which code snippet correctly sets the viewport to iPhone X size, runs a test, then changes to desktop size 1280x720 and runs another test?
hard
A. cy.viewport('iphone-x');\ncy.viewport(1280, 720);\ncy.get('#menu').should('be.visible');
B. cy.viewport('iphone-x', 1280, 720);\ncy.get('#menu').should('be.visible');
C. cy.viewport(1280, 720);\ncy.viewport('iphone-x');\ncy.get('#menu').should('be.visible');
D. cy.viewport('iphone-x');\ncy.get('#menu').should('be.visible');\ncy.viewport(1280, 720);\ncy.get('#menu').should('be.visible');

Solution

  1. Step 1: Understand sequential viewport changes

    You can call cy.viewport() multiple times to change screen size during a test.
  2. 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.
  3. Final Answer:

    Set viewport, test, change viewport, test again -> Option D
  4. Quick Check:

    Change viewport before each test step = A [OK]
Hint: Call cy.viewport() before each test step to change screen size [OK]
Common Mistakes:
  • Passing multiple sizes in one cy.viewport() call
  • Changing viewport without running tests between
  • Running tests only after last viewport change
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard