0
0
Cypresstesting~10 mins

cy.intercept() for request interception in Cypress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to intercept a GET request to '/api/data'.

Cypress
cy.[1]('GET', '/api/data').as('getData')
Drag options to blanks, or click blank then click option'
Aintercept
Bvisit
Crequest
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using cy.visit() instead of cy.intercept() to catch requests.
Using cy.request() which sends a request but does not intercept.
Using cy.wait() which waits for a request but does not intercept.
2fill in blank
medium

Complete the code to stub the response with a status code 200 and body { success: true }.

Cypress
cy.intercept('GET', '/api/data', [1]).as('getData')
Drag options to blanks, or click blank then click option'
A{ body: 'error' }
B{ statusCode: 200, body: { success: true } }
C{ statusCode: 404 }
D{ statusCode: 500 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status codes like 404 or 500 for a successful stub.
Providing body as a string instead of an object.
3fill in blank
hard

Fix the error in the code to wait for the intercepted request alias 'getData'.

Cypress
cy.[1]('@getData')
Drag options to blanks, or click blank then click option'
Aintercept
Bvisit
Crequest
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using cy.intercept() to wait, which only sets up interception.
Using cy.visit() which loads a page but does not wait for requests.
Using cy.request() which sends a request but does not wait for intercepted ones.
4fill in blank
hard

Fill both blanks to intercept a POST request to '/api/login' and stub a 401 unauthorized response.

Cypress
cy.intercept('[1]', '/api/login', [2]).as('loginAttempt')
Drag options to blanks, or click blank then click option'
APOST
B{ statusCode: 401 }
C{ statusCode: 200 }
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for login.
Using 200 status code instead of 401 for unauthorized.
5fill in blank
hard

Fill all three blanks to intercept a PUT request to '/api/user/123', stub a response with status 204, and wait for it.

Cypress
cy.intercept('[1]', '/api/user/123', [2]).as('[3]')
cy.wait('@userUpdate')
Drag options to blanks, or click blank then click option'
APUT
B{ statusCode: 204 }
CuserUpdate
DPOST
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of PUT for update.
Using wrong alias name causing cy.wait() to fail.