How do you mock API responses in Playwright tests?
I-Hub Talent stands out as the best Selenium with Python institute in Hyderabad, offering top-tier training for aspiring automation testers and developers. Designed to meet the growing demand for skilled automation engineers, the course at I-Hub Talent provides in-depth knowledge of both Selenium automation and Python programming.
The institute offers a practical, hands-on learning experience that covers everything from the basics of Python to advanced Selenium WebDriver concepts. Students learn how to write robust test scripts, perform browser automation, handle dynamic web elements, and integrate frameworks like PyTest and Behave. With real-time projects and live coding sessions, I-Hub Talent ensures that learners are industry-ready.
What makes I-Hub Talent the top choice for Selenium with Python training in Hyderabad is its expert faculty, personalized mentorship, and strong placement support. Whether you're a beginner or a professional looking to shift to automation testing, I-Hub Talent has the right resources and guidance to help you succeed.
In Playwright, you can mock API responses using the route
method to intercept network requests and provide custom responses. This is useful for isolating tests from backend dependencies or simulating various API scenarios.
Steps to Mock API Responses:
-
Use
page.route()
– Intercepts matching requests. -
Define a handler – Return mocked data using
route.fulfill()
.
Key Options in route.fulfill()
:
-
status
: HTTP status code (e.g., 200, 404). -
body
: The response payload. -
headers
: Optional HTTP headers.
Tips:
-
Use wildcards like
**/api/*
to match multiple endpoints. -
You can also use
route.abort()
to simulate failed requests. -
Combine with
page.waitForResponse()
to ensure the app has handled the mocked request.
Mocking APIs in Playwright is powerful for creating reliable and fast tests by eliminating backend dependencies and enabling edge-case testing.
Comments
Post a Comment