Creating Helper API Methods for Testing with Playwright and Fixtures

Published

Updated

Originally posted on LinkedIn

In this post, we’ll demonstrate how to create helper API methods for testing using the Playwright test framework and pass these methods as fixtures. This approach will streamline the process of testing your API interactions.

Step 1: Set up Playwright

First, install the Playwright test runner:

npm install --save-dev @playwright/test

Create a playwright.config.ts file in your project root to configure Playwright:

import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: 'tests',
  use: {
    baseURL: 'https://your-api-base-url.com',
  },
});

Step 2: Create a helper file

Create a helper file (e.g., apiHelpers.ts) that contains all your API helper methods. These methods will handle making HTTP requests to your API.

Step 3: Define helper methods

For each API endpoint, create a corresponding helper method. For example, let’s assume we have an API endpoint to fetch an order:

GET /api/v2/orders/:order_number

Create a helper method for this endpoint:

import type { APIRequestContext, APIResponse } from '@playwright/test';

export const getOrder = async (
  request: APIRequestContext,
  accessToken: string,
  orderNumber: string,
): Promise<APIResponse> => {
  return await request.get(`/api/v2/orders/${orderNumber}`, {
    params: { access_token: accessToken },
  });
};

Step 4: Create a fixtures file

Create a fixtures file (e.g., customFixtures.ts) that imports your helper methods and exports them as fixtures:

import { test as baseTest } from '@playwright/test';
import * as apiHelpers from './apiHelpers';

export const test = baseTest.extend<{ apiHelpers: typeof apiHelpers }>({
  apiHelpers: async ({}, use) => {
    await use(apiHelpers);
  },
});

export { expect } from '@playwright/test';

Step 5: Write tests using helper methods and fixtures

Now you can write tests using the helper methods passed as fixtures. Create a test file (e.g., tests/order.test.ts) and import your custom test and expect:

import { test, expect } from '../customFixtures';

test('Fetch an order by number', async ({ apiHelpers, request }) => {
  const accessToken = 'your-access-token';
  const orderNumber = 'your-order-number';

  const response = await apiHelpers.getOrder(request, accessToken, orderNumber);

  expect(response.ok()).toBeTruthy();
  const order = await response.json();
  expect(order.id).toBeDefined();
  expect(order.number).toEqual(orderNumber);
});

In this test, we’re using the apiHelpers fixture to call the getOrder method and validate the response.

Conclusion

Following these steps, you can create helper API methods for testing and pass them as fixtures using the Playwright test framework. This approach allows you to create a well-structured test suite for your API interactions and ensures your application works as expected. Remember to update your tests as your API evolves to maintain a reliable and well-tested application.

Updated November 19, 2026: the code samples now compile against current Playwright. The originals had syntax errors and used APIs that do not exist (fixtures.build(), test.request).