Playwright API Testing
A restaurant kitchen can be inspected directly for cleanliness and food quality, without needing a customer to sit down and order a full meal first. API testing checks a website's backend the same direct way, skipping the browser entirely and talking straight to the server.
API Testing Versus Browser Testing
Browser Testing: Test -> Browser -> Webpage -> Server -> Response API Testing: Test -> Server -> Response (no browser, no webpage involved)
Why Skip the Browser
A browser test needs to load images, run scripts, and render a full page before checking anything, which takes real time. An API test asks the server for data directly, receiving a plain response in a fraction of a second.
Sending a GET Request
const response = await request.get('https://example.com/api/products');
expect(response.status()).toBe(200);This asks the server for a list of products and confirms it responds with a success status code, meaning the request completed without error.
Sending a POST Request
const response = await request.post('https://example.com/api/login', {
data: { username: 'admin', password: 'secret123' },
});
expect(response.ok()).toBeTruthy();This sends login credentials directly to the server's login endpoint and confirms the request succeeded, without ever opening a login page visually.
Reading and Checking the Response Body
const data = await response.json();
expect(data.username).toBe('admin');
expect(data.role).toBe('administrator');This reads the returned data as a structured object and checks specific fields inside it, confirming the server returns the correct account details.
Sending a Request With an Authentication Token
const response = await request.get('https://example.com/api/orders', {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
expect(response.status()).toBe(200);Many APIs require proof of identity before returning private data. This attaches a token in the request header, mimicking how a logged-in application would normally send it.
Deleting Data Through the API
const response = await request.delete('https://example.com/api/cart/items/42');
expect(response.status()).toBe(204);This removes item number forty-two from a shopping cart directly, confirming the server accepted the deletion with the expected status code.
A Real-World Example: Preparing Test Data Fast
A UI test needs an account with ten past orders already in its history. Instead of clicking through the website ten separate times to create those orders manually, an API call creates all ten orders in under a second, and the UI test then simply logs in and checks the order history page.
Combining API Speed With UI Confidence
API tests run fast but cannot confirm what a real user actually sees on screen. A balanced test suite uses API tests for data setup and backend checks, while reserving browser tests for confirming the visible page truly works correctly.
Quick Practice Task
Find a public API online, such as a free weather or joke API. Write a test that sends a GET request to it and checks that the response contains expected data.
Key Takeaways
- API tests talk directly to a server without opening a browser.
- GET requests read data, while POST, PUT, and DELETE requests change data.
- Both the response status and the response body deserve checking.
- API tests run much faster than full browser-based tests.
