0
0
Cypresstesting~5 mins

Dynamic response stubbing in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acy.stub()
Bcy.intercept()
Ccy.route()
Dcy.visit()
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
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
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
Which of these is a valid way to reply with a dynamic response in cy.intercept()?
Areq.reply(() => ({ statusCode: 200, body: { success: true } }))
Breq.reply('static response')
Creq.send()
Dreq.wait()
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.