Learn API testing with Playwrightβs powerful APIRequestContext
Playwright provides built-in API testing with:
# Install Playwright
npm init playwright@latest
# Or add to existing project
npm install -D @playwright/test
playwright.config.ts:
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests',
use: {
baseURL: 'https://jsonplaceholder.typicode.com',
extraHTTPHeaders: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
},
});
tests/api-basic.spec.ts:
import { test, expect } from '@playwright/test';
test.describe('JSONPlaceholder API Tests', () => {
test('GET - Fetch all posts', async ({ request }) => {
const response = await request.get('/posts');
await expect(response).toBeOK();
expect(response.status()).toBe(200);
const posts = await response.json();
expect(posts).toHaveLength(100);
});
});
Why
await expect(response).toBeOK()instead ofexpect(response.ok()).toBeTruthy()?toBeOK()is a real Playwright matcher (APIResponseAssertions), not a plain boolean check β when it fails, it prints the actual status, headers, and body in the error output instead of just βexpected true, got false.β Reach forresponse.ok()directly only when you need the raw boolean for your own conditional logic, not for an assertion.
test.describe('GET Requests', () => {
test('Get all posts', async ({ request }) => {
const response = await request.get('/posts');
// Status validation
await expect(response).toBeOK();
expect(response.status()).toBe(200);
// Headers validation
expect(response.headers()['content-type'])
.toContain('application/json');
// Body validation
const posts = await response.json();
expect(Array.isArray(posts)).toBeTruthy();
expect(posts).toHaveLength(100);
// First post validation
expect(posts[0]).toHaveProperty('userId');
expect(posts[0]).toHaveProperty('id');
expect(posts[0]).toHaveProperty('title');
expect(posts[0]).toHaveProperty('body');
});
test('Get single post by ID', async ({ request }) => {
const postId = 1;
const response = await request.get(`/posts/${postId}`);
expect(response.status()).toBe(200);
const post = await response.json();
expect(post.id).toBe(postId);
expect(post.userId).toBeGreaterThan(0);
expect(post.title).toBeTruthy();
expect(post.body).toBeTruthy();
});
test('Get posts with query parameters', async ({ request }) => {
const response = await request.get('/posts', {
params: {
userId: 1
}
});
await expect(response).toBeOK();
const posts = await response.json();
posts.forEach((post: any) => {
expect(post.userId).toBe(1);
});
});
test('Handle 404 error', async ({ request }) => {
const response = await request.get('/posts/99999');
expect(response.status()).toBe(404);
});
});
test.describe('POST Requests', () => {
test('Create new post', async ({ request }) => {
const newPost = {
title: 'Test Post',
body: 'This is a test post',
userId: 1
};
const response = await request.post('/posts', {
data: newPost
});
expect(response.status()).toBe(201);
const createdPost = await response.json();
expect(createdPost).toHaveProperty('id');
expect(createdPost.title).toBe(newPost.title);
expect(createdPost.body).toBe(newPost.body);
expect(createdPost.userId).toBe(newPost.userId);
});
test('Create post and validate response type', async ({ request }) => {
const response = await request.post('/posts', {
data: {
title: 'New Post',
body: 'Content',
userId: 1
}
});
const post = await response.json();
expect(typeof post.id).toBe('number');
expect(typeof post.title).toBe('string');
expect(typeof post.body).toBe('string');
expect(typeof post.userId).toBe('number');
});
test('Validate required fields', async ({ request }) => {
const response = await request.post('/posts', {
data: {
// Missing title and body
userId: 1
}
});
// JSONPlaceholder is lenient, but in real API:
// expect(response.status()).toBe(400);
});
});
test.describe('PUT Requests', () => {
test('Update existing post', async ({ request }) => {
const updatedPost = {
id: 1,
title: 'Updated Title',
body: 'Updated content',
userId: 1
};
const response = await request.put('/posts/1', {
data: updatedPost
});
expect(response.status()).toBe(200);
const post = await response.json();
expect(post.title).toBe(updatedPost.title);
expect(post.body).toBe(updatedPost.body);
expect(post.id).toBe(1);
});
test('Partial update with PATCH', async ({ request }) => {
const response = await request.patch('/posts/1', {
data: {
title: 'Partially Updated'
}
});
await expect(response).toBeOK();
const post = await response.json();
expect(post.title).toBe('Partially Updated');
});
});
test.describe('DELETE Requests', () => {
test('Delete post', async ({ request }) => {
const response = await request.delete('/posts/1');
expect(response.status()).toBe(200);
});
test('Delete and verify', async ({ request }) => {
// Delete post
const deleteResponse = await request.delete('/posts/1');
await expect(deleteResponse).toBeOK();
// Verify deletion (in real API, should return 404)
// const getResponse = await request.get('/posts/1');
// expect(getResponse.status()).toBe(404);
});
});
test('API with Bearer token', async ({ request }) => {
const response = await request.get('/protected-endpoint', {
headers: {
'Authorization': 'Bearer YOUR_TOKEN_HERE'
}
});
await expect(response).toBeOK();
});
test('API with Basic Auth', async ({ playwright }) => {
const context = await playwright.request.newContext({
httpCredentials: {
username: 'user',
password: 'pass'
}
});
const response = await context.get('/secure-endpoint');
await expect(response).toBeOK();
});
test('API with API Key', async ({ request }) => {
const response = await request.get('/api/data', {
headers: {
'X-API-Key': 'your-api-key'
}
});
await expect(response).toBeOK();
});
test('Complete API validation', async ({ request }) => {
const response = await request.get('/posts/1');
// Status code
await expect(response).toBeOK();
expect(response.status()).toBe(200);
expect(response.statusText()).toBe('OK');
// Headers
const headers = response.headers();
expect(headers['content-type']).toContain('application/json');
// Response time (approximate)
const startTime = Date.now();
await request.get('/posts/1');
const duration = Date.now() - startTime;
expect(duration).toBeLessThan(2000);
// Body
const post = await response.json();
expect(post).toMatchObject({
id: expect.any(Number),
userId: expect.any(Number),
title: expect.any(String),
body: expect.any(String)
});
// Specific values
expect(post.id).toBe(1);
expect(post.title.length).toBeGreaterThan(0);
});
test('Custom assertions', async ({ request }) => {
const response = await request.get('/posts');
const posts = await response.json();
// Array assertions
expect(posts.length).toBeGreaterThan(0);
expect(posts.length).toBeLessThanOrEqual(100);
// Every item check
posts.forEach((post: any) => {
expect(post).toHaveProperty('id');
expect(post).toHaveProperty('title');
});
// Schema validation
expect(posts[0]).toEqual(
expect.objectContaining({
userId: expect.any(Number),
id: expect.any(Number),
title: expect.any(String),
body: expect.any(String)
})
);
});
test.describe('With shared context', () => {
let apiContext;
test.beforeAll(async ({ playwright }) => {
apiContext = await playwright.request.newContext({
baseURL: 'https://jsonplaceholder.typicode.com',
extraHTTPHeaders: {
'Accept': 'application/json'
}
});
});
test.afterAll(async () => {
await apiContext.dispose();
});
test('Use shared context', async () => {
const response = await apiContext.get('/posts/1');
await expect(response).toBeOK();
});
});
test('Create post via API, verify in UI', async ({ page, request }) => {
// Create via API
const response = await request.post('/posts', {
data: {
title: 'New Post',
body: 'Content',
userId: 1
}
});
const createdPost = await response.json();
// Verify in UI (hypothetical)
await page.goto(`/posts/${createdPost.id}`);
await expect(page.locator('h1')).toContainText('New Post');
});
request fixture for API tests// helpers/api-helpers.ts
export async function createPost(request, postData) {
const response = await request.post('/posts', {
data: postData
});
return await response.json();
}
export async function deletePost(request, postId) {
return await request.delete(`/posts/${postId}`);
}
// Use in tests
test('Using helpers', async ({ request }) => {
const post = await createPost(request, {
title: 'Test',
body: 'Content',
userId: 1
});
expect(post.id).toBeDefined();
await deletePost(request, post.id);
});
# Run all tests
npx playwright test
# Run specific file
npx playwright test tests/api-basic.spec.ts
# Run with UI mode
npx playwright test --ui
# Generate HTML report
npx playwright test --reporter=html
# Debug mode
npx playwright test --debug
Resources:
β Great job! Youβve completed all three API testing examples! β
| Made with β€οΈ for QA-to-SDET Journey | April 2026 |