0
0
Cypresstesting~20 mins

cy.intercept() for request interception in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Intercept Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the test result of this cy.intercept() usage?

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?

Cypress
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData');
cy.visit('/dashboard');
cy.wait('@getData').its('response.statusCode').should('eq', 200);
AThe test passes but the status code is 404 since the fixture is missing.
BThe test passes because the intercepted response returns status 200 from the fixture.
CThe test fails because the alias '@getData' is not defined correctly.
DThe test fails because cy.intercept() does not support fixtures as responses.
Attempts:
2 left
💡 Hint

Think about how cy.intercept() uses fixtures to mock responses and what status code is returned by default.

assertion
intermediate
1:30remaining
Which assertion correctly verifies the intercepted request's method?

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?

Cypress
cy.wait('@postSubmit').its('request.method').should(???);
A.should('equals', 'POST')
B.should('equal', 'post')
C.should('be', 'POST')
D.should('eq', 'POST')
Attempts:
2 left
💡 Hint

Check the exact syntax for Cypress assertions and the case sensitivity of HTTP methods.

🔧 Debug
advanced
2:00remaining
Why does this cy.intercept() not catch the request?

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?

AThe actual request URL includes query parameters, so '/api/items' does not match exactly.
Bcy.intercept() only works for POST requests by default.
CThe alias '@getItems' must be declared after cy.visit().
Dcy.wait() cannot be used with aliases created by cy.intercept().
Attempts:
2 left
💡 Hint

Think about how cy.intercept() matches URLs and what happens if the request URL has extra parts.

framework
advanced
2:30remaining
How to stub a response with a delay using cy.intercept()?

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?

Acy.intercept('GET', '/api/user', (req) => { req.reply({ delay: 2000, body: { name: 'Alice' } }) })
Bcy.intercept('GET', '/api/user', { delay: 2000, body: { name: 'Alice' } })
Ccy.intercept('GET', '/api/user', (req) => { req.delay(2000); req.reply({ name: 'Alice' }); })
Dcy.intercept('GET', '/api/user', (req) => { setTimeout(() => req.reply({ name: 'Alice' }), 2000); })
Attempts:
2 left
💡 Hint

Check the official way to add delay in cy.intercept() using the reply callback.

🧠 Conceptual
expert
3:00remaining
What is the effect of multiple cy.intercept() calls for the same route?

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?

ABoth intercepts run in order, merging their responses into one.
BThe first intercept is used, and the second is ignored.
CThe second intercept overrides the first, so the response uses 'data2.json'.
DAn error is thrown because multiple intercepts for the same route are not allowed.
Attempts:
2 left
💡 Hint

Consider how Cypress handles multiple intercepts for identical routes.