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?
Acy.stub()
Bcy.intercept()
Ccy.route()
Dcy.visit()
✗ 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?
ABy passing a function to req.reply()
BBy changing the URL in cy.visit()
CBy using cy.wait()
DBy modifying the DOM directly
✗ 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?
AClasses slow down the test execution
BClasses are too specific
CClasses are not supported by Cypress
DClasses can change often, making tests flaky
✗ 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?
AImprove browser compatibility
BSpeed up UI animations
CSimulate different server responses without backend changes
DAutomatically generate test data
✗ 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()?
The test fails with an error about req.reply not being a function. What is wrong?
medium
A. The callback receives req but req.reply is not a function; should use req.continue
B. The callback parameter should be interception and interception.reply used
C. The callback parameter is correct, but req.reply is only available on res
D. The callback parameter should be named res instead of req
Solution
Step 1: Understand intercept callback parameter
The callback receives an interception object, often named interception, which has the reply method.
Step 2: Identify the naming and usage error
Using req as parameter is allowed but req.reply is not a function because the object is not the request but the interception. The correct approach is to name it interception and call interception.reply().
Final Answer:
The callback parameter should be interception and interception.reply used -> Option B
Quick Check:
Use interception.reply() in intercept callback [OK]
Hint: Use interception.reply(), not req.reply(), in intercept callback [OK]
5. You want to stub a GET request to '/api/items' so that the response body depends on a query parameter 'type'. If 'type' is 'fruit', respond with { items: ['apple', 'banana'] }. If 'type' is 'vegetable', respond with { items: ['carrot', 'lettuce'] }. Otherwise, respond with { items: [] }. Which code correctly implements this dynamic response stubbing?