0
0
Cypresstesting~10 mins

Fixture-based response mocking in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a fixture file to mock the server response for a user list API call. It verifies that the mocked data is displayed correctly on the page.

Test Code - Cypress
Cypress
describe('User List with Fixture Mocking', () => {
  it('displays mocked user data from fixture', () => {
    cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers');
    cy.visit('/users');
    cy.wait('@getUsers');
    cy.get('[data-cy=user-name]').should('have.length', 3);
    cy.get('[data-cy=user-name]').first().should('contain.text', 'Alice');
  });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Set up intercept to mock GET /api/users with fixture 'users.json' and alias it as 'getUsers'No page loaded yet, intercept ready to catch API call-PASS
2Visit the /users page in the browserBrowser loads the /users page, triggering API call to /api/users-PASS
3Wait for the intercepted API call '@getUsers' to completeAPI call intercepted and responded with fixture dataVerify the mocked response was usedPASS
4Find all elements with data-cy attribute 'user-name' on the pagePage displays user names from mocked dataCheck that there are exactly 3 user name elementsPASS
5Check that the first user name element contains text 'Alice'First user name element visible with text 'Alice'Verify the first user name matches the fixture dataPASS
Failure Scenario
Failing Condition: The fixture file 'users.json' is missing or malformed, or the intercept does not match the API call
Execution Trace Quiz - 3 Questions
Test your understanding
What does the intercept command do in this test?
AIt clicks on the first user name element
BIt waits for the page to load completely
CIt replaces the real API response with data from the fixture file
DIt verifies the number of user elements on the page
Key Result
Using fixture-based mocking helps isolate frontend tests from backend changes and makes tests faster and more reliable by controlling API responses.