Intercepting network calls helps check if the app talks correctly with the API. It shows if data is sent and received as expected.
Why intercepting network validates API integration in Cypress
cy.intercept(method, url, response?)
method is the HTTP method like GET or POST.
url is the API endpoint to watch or mock.
cy.intercept('GET', '/api/users')
cy.intercept('POST', '/api/login', { statusCode: 200, body: { success: true } })
This test intercepts the GET request to '/api/users' and returns a fixed user list from 'users.json'. It waits for the API call and checks the status code is 200. Then it confirms the page shows a user named 'Alice'.
describe('API integration test', () => { it('checks user data from API', () => { cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers'); cy.visit('/users'); cy.wait('@getUsers').its('response.statusCode').should('eq', 200); cy.get('.user-list').should('contain', 'Alice'); }); });
Intercepting lets you control and check API calls without relying on the real server.
Use aliases (like '@getUsers') to wait for and assert on specific requests.
Mocking responses helps test edge cases like errors or empty data.
Intercepting network calls helps verify API communication in tests.
It allows checking requests, responses, and app behavior together.
Using intercept improves test reliability and speed by controlling API data.
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
