Skip to content

dhirajraut1/playwright-api-testing

Repository files navigation

Playwright API Testing Framework

A robust, easy-to-use API testing framework built with Playwright and TypeScript. Designed for QA engineers and developers.

🚀 Features

  • Minimal code required for API tests
  • Built-in authentication helpers
  • Comprehensive assertion helpers
  • Faker integration for test data
  • Positive & negative testing support
  • TypeScript support
  • Parallel execution and reporting

📁 Project Structure

src/
  |- helpers/         # Assertion and utility helpers
  |- tests/           # Test files
    |- orders	      # Order Test cases
    |- products	      # Product Test cases
    |- users	      # User Test cases
playwright.config.ts
package.json
tsconfig.json

🛠️ Installation

  1. Download and install Node v18+ for your device
  2. Install pnpm
npm i -g pnpm
  1. Clone the repo
git clone <repository-url>
cd playwright-api-testing
  1. Install dependencies
pnpm i
  1. Install Playwright browsers
pnpm exec playwright install --with-deps
  1. Copy .env.example and create .env
cp .env.example .env

🧪 Writing Tests

Example test:

import { test } from "@playwright/test";
import { post } from "../helpers/requestHelper";
import {
  expectStatus,
  expectSuccess,
  expectBodyContains,
  expectBodyDoesNotContain,
} from "../helpers/assertions";

test("should create a new user", async ({ request }) => {
  const userData = { name: "John Doe" };
  const response = await post(request, "/users", userData);

  await expectStatus(response, 201);
  await expectSuccess(response);
  await expectBodyContains(response, { name: "John Doe" });
  await expectBodyDoesNotContain(response, { error: "message" });
});

🧩 Assertion Helpers

await expectStatus(response, 200);
await expectStatusIn(response, [200, 201]);
await expectSuccess(response);
await expectFailure(response);

await expectBodyContains(response, { key: "value" });
await expectBodyDoesNotContain(response, { key: "unexpected" });

await expectApiSuccess(response);
await expectApiFailure(response);
await expectApiMessage(response, "Operation Successful.");
await expectBodyContains(response, {
  message: "Operation Successful.",
});
await expectBodyDoesNotContain(getProductResponse, { stock: 210 });

await expectHeader(response, "Content-Type", "application/json");
await expectHeaderContains(response, "X-Custom", "value");
await expectHeaderExists(response, "X-Header");
await expectHeaderDoesNotContain(response, "X-Header", "unexpected");
await expectContentType(response, "application/json");
await expectJsonContentType(response);

▶️ Running Tests

pnpm exec playwright test
pnpm exec playwright show-report

🐛 Debugging

Add console logs in your tests as needed:

console.log("Response body:", await response.text());

Run in debug mode:

pnpm exec playwright test --debug

🤝 Contributing

  • Clone the repository
  • Create a feature branch
  • Commit and push your changes
  • Open a Pull Request

Available Request Helpers

// GET request
const response = await get(request, "/users/123");

// POST request
const response = await post(request, "/users", userData);

// PUT request
const response = await put(request, "/users/123", updatedData);

// PATCH request
const response = await patch(request, "/users/123", patchData);

// DELETE request
const response = await del(request, "/users/123");

// Custom headers
const response = await post(request, "/users", userData, {
  "X-Custom-Header": "value",
});

📊 Test Reports

HTML Report

After test execution, open the HTML report:

pnpm exec playwright show-report

🐛 Debugging Tips

The request URL, headers, method and response body are logged by default in the console for easier debugging.

Enable Debug Mode

pnpm exec playwright test --debug

Add Console Logging

test("debug test", async ({ request }) => {
  const response = await makePostRequest(request, "/users", userData);
  console.log("Response status:", response.status());
  console.log("Response body:", await response.text());
});

The sample tests in this project are based on my another project called sample-api. Feel free to try out.

About

This project contains a sample API testing framework in Playwright

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published