API Testing with Playwright

Playwright's APIRequestContext can test REST APIs without a browser.

API test

typescript
import { test, expect } from '@playwright/test';

test('GET /api/users returns list', async ({ request }) => {
  const response = await request.get('https://myqatools.com/api/users');
  expect(response.status()).toBe(200);
  const body = await response.json();
  expect(Array.isArray(body)).toBeTruthy();
  expect(body.length).toBeGreaterThan(0);
});

test('POST /api/users creates a user', async ({ request }) => {
  const response = await request.post('https://myqatools.com/api/users', {
    data: { name: 'Alice', email: '[email protected]' },
  });
  expect(response.status()).toBe(201);
  const user = await response.json();
  expect(user.name).toBe('Alice');
});