Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright APIRequestContext Post

From Leeroopedia
Revision as of 11:34, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Playwright_APIRequestContext_Post.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains API_Testing, HTTP, Test_Setup
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for creating test fixtures and preconditions by making POST API calls to set up required server-side state before tests run, provided by the Playwright library.

Description

Playwright's test.beforeAll() hook combined with request.post() enables API-driven test setup. The beforeAll hook runs once before all tests in a file or describe block, receiving the request fixture (an APIRequestContext) as an argument. The post() method sends HTTP POST requests to create resources on the server.

The post() method supports multiple body formats:

  • JSON data: Pass a JavaScript object via the data option; Playwright automatically serializes it to JSON and sets the Content-Type: application/json header.
  • Form data: Pass an object via the form option; Playwright URL-encodes it and sets Content-Type: application/x-www-form-urlencoded.
  • Multipart data: Pass an object via the multipart option for file uploads; Playwright constructs the multipart/form-data body.

The response is returned as an APIResponse object that can be inspected for status codes and parsed for JSON body content, allowing the setup hook to extract created resource IDs for use in subsequent tests.

Usage

Use test.beforeAll() with request.post() when you need to create server-side resources (users, articles, orders, etc.) that multiple tests in the suite will operate on. Use test.beforeEach() when each test needs its own isolated setup.

Code Reference

Source Location

  • Repository: playwright
  • beforeAll hook: packages/playwright/types/test.d.ts:L6026-6067
  • post() method: packages/playwright-core/src/client/fetch.ts:L152-157

Signature

// Test setup hook
test.beforeAll(
  inner: (args: { request: APIRequestContext }, testInfo: TestInfo) => Promise<any>
): void;

// POST request
request.post(url: string, options?: {
  data?: string | Buffer | Serializable;
  form?: { [key: string]: string | number | boolean };
  multipart?: {
    [key: string]: string | number | boolean | ReadStream | {
      name: string;
      mimeType: string;
      buffer: Buffer;
    };
  };
  headers?: { [key: string]: string };
  params?: { [key: string]: string | number | boolean };
  timeout?: number;
  failOnStatusCode?: boolean;
  ignoreHTTPSErrors?: boolean;
  maxRedirects?: number;
  maxRetries?: number;
}): Promise<APIResponse>;

Import

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

I/O Contract

Inputs

Name Type Required Description
url string Yes The endpoint URL. If relative, it is combined with the context's baseURL. For example, /api/users with baseURL https://api.example.com becomes https://api.example.com/api/users.
options.data Buffer | Serializable No Request body. Objects are auto-serialized to JSON with Content-Type: application/json header set automatically.
options.form number | boolean } No Form data for URL-encoded submission. Sets Content-Type: application/x-www-form-urlencoded.
options.multipart number | boolean | ReadStream | FilePayload } No Multipart form data for file uploads. Sets Content-Type: multipart/form-data.
options.headers { [key: string]: string } No Additional headers merged with context-level headers. Per-request headers override context headers for the same key.
options.params number | boolean } No Query parameters appended to the URL.
options.timeout number No Request timeout in milliseconds. Defaults to 30000 (30 seconds).
options.failOnStatusCode boolean No Whether to throw an error if the response status code is not 2xx or 3xx. Defaults to false.
options.maxRedirects number No Maximum number of redirects to follow. Defaults to 20. Set to 0 to disable redirect following.
options.maxRetries number No Maximum number of retries for transient failures. Defaults to 0.

Outputs

Name Type Description
response Promise<APIResponse> The API response object containing status code, headers, and body. Use response.ok() to check for 2xx status, response.json() to parse the JSON body, response.status() for the numeric status code.

Usage Examples

Basic Example

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

test.use({
  baseURL: 'https://api.example.com',
  extraHTTPHeaders: {
    'Authorization': 'Bearer my-token',
  },
});

let articleId: number;

test.beforeAll(async ({ request }) => {
  // Create a test article via POST
  const response = await request.post('/api/articles', {
    data: {
      title: 'Test Article',
      body: 'This article was created for testing.',
      status: 'published',
    },
  });
  expect(response.ok()).toBeTruthy();

  const body = await response.json();
  articleId = body.id;
});

test('can retrieve the created article', async ({ request }) => {
  const response = await request.get(`/api/articles/${articleId}`);
  expect(response.ok()).toBeTruthy();

  const article = await response.json();
  expect(article.title).toBe('Test Article');
});

test('can update the created article', async ({ request }) => {
  const response = await request.patch(`/api/articles/${articleId}`, {
    data: { title: 'Updated Title' },
  });
  expect(response.ok()).toBeTruthy();
});

Example: Form-Encoded Login Setup

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

test.use({
  baseURL: 'https://app.example.com',
});

test.beforeAll(async ({ request }) => {
  // Create a test user via API
  await request.post('/api/admin/users', {
    data: {
      username: 'testuser',
      password: 'Test1234!',
      role: 'editor',
    },
  });

  // Verify login works with form-encoded POST
  const loginResponse = await request.post('/api/login', {
    form: {
      username: 'testuser',
      password: 'Test1234!',
    },
  });
  expect(loginResponse.ok()).toBeTruthy();
});

Example: Multipart File Upload Setup

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

test.beforeAll(async ({ request }) => {
  const filePath = path.resolve(__dirname, 'fixtures/test-image.png');
  const fileBuffer = fs.readFileSync(filePath);

  const response = await request.post('/api/uploads', {
    multipart: {
      description: 'Test image for API tests',
      file: {
        name: 'test-image.png',
        mimeType: 'image/png',
        buffer: fileBuffer,
      },
    },
  });
  expect(response.ok()).toBeTruthy();
});

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment