Custom Test
Write custom Node.js test scriptsEdit
Write custom test scripts for advanced scenarios that don't fit into browser, API, or database categories. Custom tests run in an isolated environment with the Playwright, HTTP, data, and database packages included.
Create Test
Go to Create → Custom Test
Write TypeScript/JavaScript code
Run and debug
Save when passing
Example
Set API_URL to a test API you control that persists posts and supports POST /posts, GET /posts/:id, and DELETE /posts/:id. Public mock APIs that simulate writes cannot verify this workflow.
import { expect, test } from '@playwright/test';
const API_URL = getVariable('API_URL');
// Use serial mode to share state (createdId) between tests
test.describe.serial('posts CRUD', () => {
let createdId: number | undefined;
test('creates a post', async ({ request }) => {
const response = await request.post(API_URL + '/posts', {
data: { title: 'Playwright', body: 'API example', userId: 1 },
});
expect(response.status()).toBe(201);
const json = await response.json();
createdId = json.id;
expect(json).toMatchObject({ title: 'Playwright', body: 'API example' });
});
test('fetches the created post', async ({ request }) => {
// Skip if creation failed
test.skip(!createdId, 'create step failed');
const response = await request.get(API_URL + '/posts/' + createdId);
expect(response.ok()).toBeTruthy();
const json = await response.json();
expect(json.id).toBe(createdId);
});
// Cleanup after all tests in this group
test.afterAll(async ({ request }) => {
if (!createdId) return;
await request.delete(API_URL + '/posts/' + createdId);
});
});Tests can reach public hosts over TCP; private-network destinations are blocked. The filesystem is read-only except /tmp. Use readFile() and getFile() to access uploaded files.