Consider this Cypress test snippet that intercepts a GET request and modifies the response.
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData');
cy.visit('/dashboard');
cy.wait('@getData').its('response.statusCode').should('eq', 200);What will be the outcome of the assertion?
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData'); cy.visit('/dashboard'); cy.wait('@getData').its('response.statusCode').should('eq', 200);
Think about how cy.intercept() uses fixtures to mock responses and what status code is returned by default.
Using fixture: 'data.json' in cy.intercept() mocks the response with the fixture content and automatically sets the status code to 200. The alias '@getData' is correctly defined and waited on, so the assertion passes.
You have intercepted a POST request using cy.intercept('POST', '/submit').as('postSubmit'). After triggering the request, you want to assert that the method was indeed POST.
Which assertion code is correct?
cy.wait('@postSubmit').its('request.method').should(???);
Check the exact syntax for Cypress assertions and the case sensitivity of HTTP methods.
The correct Cypress assertion to check equality is .should('eq', value). HTTP methods are uppercase strings, so 'POST' is correct. Other options use invalid assertion commands or wrong case.
Given this code snippet:
cy.intercept('GET', '/api/items').as('getItems');
cy.visit('/items');
cy.wait('@getItems');The test fails with a timeout waiting for '@getItems'. What is the most likely reason?
Think about how cy.intercept() matches URLs and what happens if the request URL has extra parts.
cy.intercept() matches URLs exactly unless wildcards or patterns are used. If the actual GET request has query parameters like '/api/items?page=1', the intercept with '/api/items' won't catch it, causing the wait to timeout.
You want to simulate a slow network by delaying the response of a GET request to '/api/user'. Which cy.intercept() option correctly adds a 2-second delay before responding?
Check the official way to add delay in cy.intercept() using the reply callback.
Option A uses the correct syntax: inside the intercept callback, calling req.reply() with an object that includes delay and body. Option A uses a non-existent req.delay() method. Option A uses setTimeout but can cause timing issues.
If you define two intercepts for the same HTTP method and URL pattern, like:
cy.intercept('GET', '/api/data', { fixture: 'data1.json' });
cy.intercept('GET', '/api/data', { fixture: 'data2.json' });Which statement best describes what happens when a GET request to '/api/data' is made?
Consider how Cypress handles multiple intercepts for identical routes.
Cypress applies intercepts in the order they are declared. The last matching intercept overrides previous ones for the same route and method. So the response will come from the second intercept's fixture.