Can Playwright Be Used for API Testing?
Yes. Playwright can be used for API testing.
It provides a built-in APIRequestContext that allows you to send HTTP requests directly without opening a browser.
Supported HTTP methods include:
- GET
- POST
- PUT
- DELETE
Simple Analogy
API testing is like checking only the order message and response, without watching the delivery person.
Instead of validating the user interface, you validate the communication between the application and the backend.
Creating an API Client
Playwright creates an API client using:
request.newContext()
Real Project Usage
According to your project notes, Playwright API testing was primarily used for:
- Backend validation
- Login token generation
Generating authentication tokens through APIs avoids repeatedly automating the login user interface and supports faster automation execution.
Intercepting Network Calls and Mocking API Responses
Playwright supports API mocking through network interception.
Instead of allowing requests to reach the real backend server, Playwright intercepts the request and returns a custom response.
Methods Used
Intercept Network Requests
page.route()
Return a Mock Response
route.fulfill()
This allows you to return:
- Custom status codes
- Custom JSON responses
Summary
| Aspect | Value |
|---|---|
| Interception Method | page.route() |
| Mock Response | route.fulfill() |
| Response Type | Custom status code and JSON |
When API Mocking Is Used
Backend Not Ready
Frontend testing can continue even if backend development has not been completed.
Testing Edge Cases
Mock responses make it easy to simulate scenarios such as:
- Empty responses
- Error responses
- Invalid data
- Special business cases
These situations may be difficult to reproduce consistently using a real backend.
Interview Answer
"We mock API responses in Playwright using
page.route(). It intercepts API requests and allows us to return our own custom response usingroute.fulfill()."
Validating API Responses
Playwright validates API responses using APIRequestContext.
Workflow
- Create the API client.
- Send the request.
- Validate the response.
Response Validation
Common validations include:
- Response status code
- Response headers
- JSON response body
Playwright uses its built-in expect() assertions to verify these values.
Interview Answer
"Playwright provides APIRequestContext to send HTTP requests. After receiving the response, we validate the status code, response headers, and JSON response body using
expect()."
This uses the same assertion library that is used for UI automation.
Blocking Unnecessary Network Requests
Playwright allows unnecessary requests to be blocked before they are sent.
Methods Used
Intercept Requests
page.route()
Block Requests
route.abort()
Commonly Blocked Resources
- Images
- Fonts
- Advertisements
- Analytics scripts
Playwright intercepts every request and determines whether it should continue or be blocked.
Benefits
Blocking unnecessary resources:
- Speeds up test execution
- Reduces network traffic
- Eliminates third-party dependencies
- Improves automation stability
Summary
| Aspect | Value |
|---|---|
| Interception Method | page.route() |
| Block Action | route.abort() |
| Common Targets | Images, Fonts, Ads, Analytics |
FAQs
Can Playwright perform API testing?
Yes.
Playwright provides APIRequestContext, which allows direct execution of HTTP requests such as GET, POST, PUT, and DELETE without launching a browser.
It is commonly used for backend validation and login token generation.
How do you mock API responses in Playwright?
Playwright intercepts requests using:
page.route()
and returns custom responses using:
route.fulfill()
This is especially useful when:
- The backend is unavailable.
- Testing error scenarios.
- Simulating edge cases.
How do you validate API responses?
Create an API client using APIRequestContext, send the request, and validate:
- Status code
- Response headers
- JSON response body
using Playwright's built-in expect() assertions.
How do you block unnecessary network requests?
Use:
page.route()
to intercept requests and:
route.abort()
to block resources such as:
- Images
- Fonts
- Advertisements
- Analytics scripts
What creates the API client in Playwright?
The API client is created using:
request.newContext()
Why combine UI and API testing in the same framework?
Using one framework allows both frontend and backend validation.
For example:
- Generate authentication tokens through APIs.
- Reuse those tokens during UI automation.
- Reduce login execution time.
- Improve overall automation efficiency.