API Playground
Practice working with HTTP requests, responses, delays, and API errors.
Watching the network from a UI test
What happens when you click
Every button on this page sends a real HTTP request to the practice API and renders the response. A good UI test can check both sides: what the browser sent and received, and what the user sees.
Wait for a response, then assert on it
Start waiting before the click. If you click first, a fast response can arrive before you start listening, and the wait times out.
const responsePromise = page.waitForResponse('**/api/users');
await page.getByRole('button', { name: 'GET Users' }).click();
const response = await responsePromise;
expect(response.status()).toBe(200);
const body = await response.json();
// The table should show exactly what the API returned (+1 header row).
await expect(page.getByRole('row')).toHaveCount(body.users.length + 1);Check what the page sent
Use waitForRequest to verify the request the UI built from your input: its method, URL and JSON body.
const requestPromise = page.waitForRequest(
(request) =>
request.method() === 'POST' && request.url().endsWith('/api/users')
);
await page.getByLabel('First name').fill('Eve');
await page.getByLabel('Last name').fill('Adams');
await page.getByRole('button', { name: 'Create user' }).click();
const request = await requestPromise;
expect(request.postDataJSON()).toEqual({
firstName: 'Eve',
lastName: 'Adams',
role: 'User',
});Matching URLs
- A glob string such as
'**/api/users'must match the whole URL, so it does not match/api/users/1or/api/users?role=Admin. - Use a regular expression (
/\/api\/users\/\d+$/) or a predicate function when the URL varies. - A predicate can also check the method, so a GET and a POST to the same URL are not confused.
Listen to everything
page.on('response', (response) => {
if (response.url().includes('/api/')) {
console.log(response.status(), response.url());
}
});This page has no client-side validation, on purpose. Submit the create form with empty fields to see how the API rejects it with a
400, and how the page shows that error.Users API
Create User
Delayed Response
HTTP Errors
Test Data
Your API data is private to you: other visitors cannot see or change it. Reset it to the three original users at any time.
Next in Network & API · 2 of 3API Testing →Test the practice API directly with the request fixture, without a browser.
Related exercises
- Network & APIAdvancedNetwork MockingControl network responses to test states that are hard to get from a real backend.
- Network & APIAdvancedAPI TestingTest the practice API directly with the request fixture, without a browser.
- Debugging & TracingIntermediateTrace ViewerFind a bug that only an automated test hits, using a trace instead of guesswork.
Read the guide: Playwright network mocking and API testing