Recall & Review
beginner
What is dynamic response stubbing in Cypress?
Dynamic response stubbing is when you intercept a network request and provide a custom response that can change based on the test's needs or input data.
Click to reveal answer
intermediate
How do you intercept a GET request and stub a dynamic response in Cypress?
Use cy.intercept() with a function as the response to return different data based on the request or test logic.
Click to reveal answer
beginner
Why is dynamic response stubbing useful in testing?
It allows tests to simulate different server responses without changing the backend, making tests faster and more reliable.
Click to reveal answer
intermediate
Example: What does this Cypress code do?
cy.intercept('GET', '/api/user', (req) => {
req.reply({ name: req.query.name || 'Guest' })
})
It intercepts GET requests to '/api/user' and replies with a JSON object where the name is taken from the query parameter or defaults to 'Guest'.
Click to reveal answer
beginner
What is a best practice when choosing locators for dynamic response stubbing tests?
Use stable and descriptive selectors like data-* attributes to avoid flaky tests when UI changes.
Click to reveal answer
What Cypress command is used to stub network responses dynamically?
✗ Incorrect
cy.intercept() is the modern Cypress command to intercept and stub network requests dynamically.
In dynamic stubbing, how can you customize the response based on the request?
✗ Incorrect
Passing a function to req.reply() lets you customize the response dynamically based on the intercepted request.
Why should you avoid using CSS classes as locators in tests with dynamic stubbing?
✗ Incorrect
CSS classes often change during UI updates, which can cause tests to fail unexpectedly.
What is the main benefit of dynamic response stubbing in tests?
✗ Incorrect
Dynamic stubbing lets you simulate various server responses easily, improving test flexibility and reliability.
Which of these is a valid way to reply with a dynamic response in cy.intercept()?
✗ Incorrect
Passing a function to req.reply() allows dynamic response generation based on the request.
Explain how dynamic response stubbing works in Cypress and why it is useful.
Think about how you can change the server reply during a test.
You got /4 concepts.
Describe best practices for choosing locators when writing tests that use dynamic response stubbing.
Consider what makes a locator reliable over time.
You got /4 concepts.