Why intercepting network validates API integration in Cypress - Automation Benefits in Action
describe('API Integration Validation via Network Interception', () => { it('should intercept API call and validate request and response', () => { // Intercept the API call and alias it cy.intercept('GET', '/api/data').as('getData'); // Visit the page that triggers the API call cy.visit('/data-page'); // Trigger the action that causes the API call cy.get('button#loadData').click(); // Wait for the API call to complete cy.wait('@getData').then(({ request, response }) => { // Assert request URL and method expect(request.method).to.equal('GET'); expect(request.url).to.include('/api/data'); // Assert response status code expect(response.statusCode).to.equal(200); // Assert response body contains expected data expect(response.body).to.have.property('items').and.be.an('array'); expect(response.body.items.length).to.be.greaterThan(0); }); }); });
This Cypress test intercepts the API call to /api/data using cy.intercept() and assigns it an alias @getData. It then visits the page that triggers the API call and clicks the button to load data.
Using cy.wait('@getData'), the test waits for the API call to complete and accesses both the request and response objects.
Assertions check that the request method is GET and the URL contains the expected endpoint. It also verifies the response status code is 200, indicating success, and that the response body contains an items array with at least one element.
This approach validates that the frontend correctly integrates with the backend API by confirming the request is sent properly and the response data is as expected.
Now add data-driven testing with 3 different API endpoints and verify each response
Practice
Solution
Step 1: Understand intercept purpose
Intercepting network calls lets tests watch and control API requests and responses.Step 2: Connect intercept to validation
This helps confirm the app sends correct data and reacts properly to API replies.Final Answer:
It allows verifying that the app sends correct requests and handles responses properly. -> Option CQuick Check:
Intercept = Verify API communication [OK]
- Thinking intercept speeds UI rendering
- Believing intercept fixes API errors automatically
- Assuming intercept disables all network calls
Solution
Step 1: Recall Cypress intercept syntax
The correct syntax is cy.intercept(method, url) where method is 'GET' and url is '/api/users'.Step 2: Compare options
cy.interceptGet('/api/users') uses a non-existent command. cy.intercept('/api/users', 'GET') reverses parameters. cy.route('GET', '/api/users') uses deprecated cy.route. cy.intercept('GET', '/api/users') matches the correct syntax.Final Answer:
cy.intercept('GET', '/api/users') -> Option AQuick Check:
cy.intercept(method, url) = correct syntax [OK]
- Swapping method and URL parameters
- Using deprecated cy.route instead of cy.intercept
- Using non-existent commands like cy.interceptGet
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData');
cy.visit('/dashboard');
cy.wait('@getData').its('response.statusCode').should('eq', 200);Solution
Step 1: Analyze intercept with fixture
Using a fixture returns a mocked response with status code 200 by default.Step 2: Check wait and assertion
cy.wait('@getData') waits for the intercepted call, then asserts response.statusCode equals 200, which is true.Final Answer:
The test will pass because the intercepted response status is 200. -> Option BQuick Check:
Fixture response status = 200 [OK]
- Assuming fixture responses lack status codes
- Forgetting to alias intercept for cy.wait
- Expecting real network calls instead of mocked
cy.intercept('POST', '/api/login', (req) => {
req.reply({ statusCode: 201, body: { success: true } });
}).as('login');
cy.visit('/login');
cy.get('button[type=submit]').click();
cy.wait('@login').its('response.statusCode').should('eq', 201);Solution
Step 1: Check intercept alias usage
The intercept does not have an alias set with .as('login'), so cy.wait('@login') will fail.Step 2: Verify other parts
Status code 201 is valid for POST creation. The selector and req.reply usage are correct.Final Answer:
The intercept is missing an alias '@login' for cy.wait. -> Option AQuick Check:
Missing .as('login') causes cy.wait failure [OK]
- Not adding .as() alias to intercept
- Confusing valid HTTP status codes
- Misunderstanding req.reply usage
Solution
Step 1: Simulate delay correctly in intercept
Using { delay: 2000 } as the static route handler correctly adds a 2-second delay to the response.Step 2: Verify spinner visibility timing
Check the loading spinner is visible before waiting for the delayed response to finish with cy.wait('@getItems').Final Answer:
Use cy.intercept('GET', '/api/items', { delay: 2000 }).as('getItems'); Then check spinner visibility before cy.wait('@getItems'). -> Option DQuick Check:
{ delay: 2000 } simulates slow response [OK]
- Using invalid req.on('response', res => res.setDelay()) syntax
- Calling delay method directly on req
- Checking spinner only after wait instead of before
Ready to prove it?
Pass the test and this pattern moves unknown
