Skip to content

adequatica/api-testing-playwright

Repository files navigation

Technical Requirements for a Test Automation Framework for End-to-End API Testing

Run Playwright E2E API Tests

And how Playwright satisfies these requirements:

  1. Use only standard components (yes, Playwright):
    • Easy to install dependencies (a single NPM package)
    • Easy to configure (config file)
    • Easy to use HTTP library (suitable for API testing. API testing with Playwright does not require browsers installation. However, browser will be needed if you decide to run the tests in UI Mode for debugging)
  2. Well tests organization:
  3. Various ways to bypass the instability of the fragile tested infrastructure (unexpected errors, timeouts) which leads to false-positive fails:
  4. Scaling:
    • Parallel execution (parallelism) and sequential launch if needed (this is configurable at the test and workes level)
    • Filtering tests: «running by tag», exclude tests from a run, run only a single test (by --grep and --grep-invert CLI options or by the test.only method, as well as on the the describe level)
    • Skipping tests (by test.skip and test.fixme methods, as well as on the describe level)
  5. Good test reports:

Stack

A basic set of packages for API testing with Playwright:

  • Playwright — testing framework
  • Zod — JSON schema validator
  • Daj.js — minimalist date utility library
  • Prettier — code formatter
  • ESLint — code linter
  • Vitest — unit testing framework (optional)

Example API for testing: NASA API

How to Use

You have to have Node.js >= 24 (LTS):

  1. Clone repository
  2. Install dependencies: npm install
  3. Run tests: npm run test

CLI Options

  • A different tested host can be passed to the tests through an environment variable (it can be useful for testing different environments):
BASE_URL=https://api.nasa.gov npm run test
  • An individual API key can be passed to the tests through an environment variable; otherwise, the DEMO_KEY value will be used.
API_KEY={api_key} npm run test
npm run test -- --grep @nasa
npm run test -- --ui

Concepts

  • All interactions with environment variables are carried out in /utils/env.ts file
  • All constants (endpoints, timeouts, etc.) are contained in their respective directories
  • Each request and response is logged in the report (read more about it)
  • Expect messages should be written in BDD (Behavior-Driven Development) style, such as «something should have something»
  • Most utils/helpers functions should have unit tests (optional)

About

End-to-End API testing with Playwright

Topics

Resources

Stars

Watchers

Forks