0
0
Cypresstesting~10 mins

Asserting request properties in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a network request is sent with the correct URL and HTTP method when a button is clicked. It verifies the request properties to ensure the app behaves as expected.

Test Code - Cypress
Cypress
describe('Request properties test', () => {
  it('should send a GET request to /api/data when button clicked', () => {
    cy.intercept('GET', '/api/data').as('getData');
    cy.visit('https://example.com');
    cy.get('#load-data-btn').click();
    cy.wait('@getData').its('request').then((req) => {
      expect(req.method).to.equal('GET');
      expect(req.url).to.include('/api/data');
    });
  });
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1cy.intercept sets up a spy for GET requests to /api/data and aliases it as 'getData'No page loaded yet, intercept ready to capture matching requests-PASS
2cy.visit opens the page at https://example.comBrowser shows the example.com homepage with a button #load-data-btn visible-PASS
3cy.get finds the button with id 'load-data-btn' and clicks itButton clicked, triggering a network request to /api/data-PASS
4cy.wait waits for the intercepted request '@getData' to completeRequest to /api/data captured by interceptVerify request method is 'GET' and URL includes '/api/data'PASS
Failure Scenario
Failing Condition: The request to /api/data is not sent or uses a different HTTP method
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.intercept('GET', '/api/data').as('getData') do in the test?
AIt clicks the button with id 'getData'
BIt listens for GET requests to /api/data and names this intercept 'getData'
CIt sends a GET request to /api/data immediately
DIt waits for the page to load
Key Result
Always intercept and alias network requests before triggering actions that send them, so you can wait for and assert on the exact request properties.