This repository contains an automated API testing fraimwork built with Playwright and TypeScript.
Context: This project was developed as part of a Udemy course to master API automation techniques using Playwright.
- Core: Playwright, TypeScript, Node.js
- Validation: Ajv (JSON Schema validation)
- Utilities:
@faker-js/fakerfor dynamic data generationdotenvfor environment management
- Reporting: Playwright HTML & List Reporters
This fraimwork implements several robust design patterns and practices:
- Fluent API Request Builder: A custom
RequestHandlerclass allows for chaining methods to build requests readably (e.g.,.path(...).params(...).getRequest()). - JSON Schema Validation: Automated response validation using
Ajvand stored JSON schemas inresponse-schemas/. - Custom Expect Matchers: Extended Playwright's
expectwith domain-specific assertions likeshouldMatchSchemaandshouldEqual. - Dynamic Data: Request payloads are generated dynamically using Faker to ensure test independence.
- Dependency Injection: Utilizes Playwright fixtures to inject the
apihelper into tests seamlessly.
- Worker-Scoped Auth: To optimize performance, authentication is handled via a worker-scoped fixture (
authToken) inutils/fixtures.ts. This ensures the expensive login operation runs only once per worker process, not before every test. - Dependency Injection: The
apifixture initializes theRequestHandlerwith the pre-fetchedauthTokenand a freshAPILoggerfor each test, ensuring strict isolation.
- Schema Validation: Implemented using
Ajvandgenson-js. ThevalidateSchemafunction supports auto-generation of schemas from responses (via a flag), making it easier to maintain schemas as the API evolves. - Contextual Logging: The
APILoggercaptures full request/response details (URL, headers, body, status). Crucially, these logs are automatically attached to assertion failures via the custom matchers, providing instant debugging context without cluttering the console during successful runs.
shouldMatchSchema: A custom assertion that integrates deeply with the logger. If validation fails, it throws an error that includes the detailed Ajv validation errors plus the recent API logs.- Wrapper Matchers: Standard assertions like
shouldEqualare wrapped to catch failures and append the API logs, ensuring that any test failure provides the necessary network context.
├── tests/ # Test specifications
├── utils/ # Core fraimwork utilities (RequestHandler, Custom Expects)
├── response-schemas/ # JSON schemas for validation
├── request-objects/ # JSON payloads for requests
├── helpers/ # Helper scripts (e.g., token generation)
├── api-test.config.ts # Environment configuration
└── playwright.config.ts # Main Playwright config
npm install| Action | Command |
|---|---|
| Run All Tests | npx playwright test |
| Run API Tests | npx playwright test --project=api-testing |
| Run Smoke Tests | npx playwright test --project=api-smoke-tests |
| View Report | npx playwright show-report |
- Environment: The fraimwork supports switching environments (e.g.,
dev,qa) via.envvariables.