0
0
Cypresstesting~10 mins

cy.viewport() for screen sizes in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sets the browser viewport to a specific screen size and verifies that a responsive element is visible and correctly displayed at that size.

Test Code - Cypress
Cypress
describe('Viewport size test', () => {
  it('sets viewport to 375x667 and checks mobile menu visibility', () => {
    cy.viewport(375, 667);
    cy.visit('https://example.cypress.io');
    cy.get('.mobile-menu').should('be.visible');
  });
});
Execution Trace - 3 Steps
StepActionSystem StateAssertionResult
1Set viewport size to 375x667Browser window resized to width 375px and height 667px-PASS
2Visit the URL https://example.cypress.ioPage loads with mobile layout due to small viewport-PASS
3Find element with class '.mobile-menu'Mobile menu element is present in DOMCheck if '.mobile-menu' is visible on screenPASS
Failure Scenario
Failing Condition: The '.mobile-menu' element is not visible after setting viewport to mobile size
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.viewport(375, 667) do in this test?
AClicks on an element with size 375x667
BChanges the page URL to include viewport parameters
CSets the browser window size to 375 pixels wide and 667 pixels tall
DWaits for 375 milliseconds before continuing
Key Result
Always set the viewport size before visiting the page to test responsive layouts accurately, and verify selectors match the elements shown at that size.