Skip to content

pia-team/playwright-cucumber-api-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Below is a production-quality, well-structured README.md you can use directly in your repository. It’s written to be clear for newcomers, credible for senior engineers, and useful for CI / long-term maintenance.


API Test Automation Framework

Playwright + Cucumber + TypeScript


πŸ“Œ Overview

This project is a scalable and maintainable API test automation framework built using:

  • Playwright – fast, reliable HTTP client & test runner
  • Cucumber (BDD) – business-readable tests with Gherkin
  • TypeScript – strong typing, better refactoring, fewer runtime errors

The framework is designed with clean architecture principles, focusing on:

  • Separation of concerns
  • Reusability
  • Readability
  • Easy onboarding for both technical and non-technical stakeholders

🧠 Key Concepts

  • BDD-first approach: Business scenarios are written in Gherkin
  • Service-based API abstraction: API logic lives in service classes
  • Centralized API client: Headers, auth, retries handled in one place
  • Typed models: Request & response objects are strongly typed
  • Environment-driven: Easy switching between Dev / Test / UAT

πŸ›  Tech Stack

Tool Purpose
Playwright API request handling & assertions
Cucumber BDD / Gherkin test definitions
TypeScript Type safety & maintainability
Node.js Runtime
ts-node TypeScript execution
AJV / Zod (optional) Schema validation
CI/CD Jenkins / GitHub Actions compatible

πŸ“‚ Project Structure

api-test-framework/
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/          # Environment & endpoint configuration
β”‚   β”œβ”€β”€ core/            # Framework core (API client, context, hooks)
β”‚   β”œβ”€β”€ features/        # Gherkin feature files & step definitions
β”‚   β”œβ”€β”€ models/          # Typed request/response models
β”‚   β”œβ”€β”€ services/        # API business logic layer
β”‚   β”œβ”€β”€ utils/           # Reusable helpers & utilities
β”‚   └── types/           # Custom TypeScript types
β”‚
β”œβ”€β”€ reports/             # Test execution reports
β”œβ”€β”€ cucumber.js          # Cucumber configuration
β”œβ”€β”€ tsconfig.json        # TypeScript configuration
β”œβ”€β”€ package.json         # Dependencies & scripts
└── README.md

πŸ— Architecture Overview

Feature (Gherkin)
   ↓
Step Definitions
   ↓
Service Layer (Business API Logic)
   ↓
API Client (Playwright)
   ↓
HTTP Request

Responsibilities

  • Feature files β†’ Describe what is being tested

  • Step Definitions β†’ Glue code only, no API logic

  • Services β†’ Encapsulate API calls and endpoints

  • Core β†’ Authentication, request context, hooks

  • Utils β†’ Common reusable helpers (logging, validation, data handling)


πŸš€ Getting Started

1️⃣ Prerequisites

  • Node.js v18+
  • npm or yarn
  • Access to target API environments

2️⃣ Install Dependencies

npm install

3️⃣ Environment Configuration

Set environment variables:

export BASE_URL=https://api.dev.example.com
export API_TOKEN=your-token

Or use .env file if supported.


4️⃣ Run Tests

Run all API tests:

npm test

Run a specific feature:

npx cucumber-js src/features/user/user.feature

✍ Writing Tests

Feature File Example

Feature: User API

  Scenario: Create a new user
    Given I have a valid user payload
    When I send a create user request
    Then the response status should be 201

Step Definition Guidelines

βœ… Keep steps thin ❌ Do not call Playwright directly in steps βœ… Always delegate to services


🧩 Service Layer Example

export class UserService {
  constructor(private api: ApiClient) {}

  createUser(payload: User) {
    return this.api.post('/users', payload);
  }
}

πŸ§ͺ Assertions & Validation

  • HTTP status checks
  • Response body validation
  • Optional schema validation (AJV / Zod)
  • Custom reusable validators

πŸ” Authentication Strategy

  • Token-based authentication supported

  • Centralized in authHelper

  • Easy to extend for:

    • OAuth2
    • JWT refresh
    • API keys

πŸ“Š Reporting

  • Cucumber HTML reports

  • Easily extendable to:

    • Allure
    • JSON reports
    • CI dashboards

Reports are generated under:

reports/

βš™ CI/CD Ready

The framework is designed to run seamlessly in:

  • Jenkins
  • GitHub Actions
  • GitLab CI

Supports:

  • Headless execution
  • Parallel runs
  • Environment-based configuration

βœ… Best Practices Followed

βœ” Clean architecture βœ” DRY principles βœ” Typed API contracts βœ” Centralized configuration βœ” Easy scalability βœ” CI-friendly execution


πŸ›£ Roadmap

Planned improvements:

  • πŸ”Ή OAuth token refresh support
  • πŸ”Ή API schema contract testing
  • πŸ”Ή Allure reporting
  • πŸ”Ή Performance assertions
  • πŸ”Ή Test data generation
  • πŸ”Ή AI-assisted test creation

πŸ‘₯ Contribution Guidelines

  1. Follow existing folder structure
  2. Add services for new APIs
  3. Keep step definitions minimal
  4. Write meaningful Gherkin scenarios
  5. Ensure tests are environment-agnostic

πŸ“„ License

This project is intended for internal / enterprise use. License can be defined based on organizational needs.


πŸ“ž Support

For questions or improvements:

  • Contact the QA / Automation team
  • Raise an issue or pull request

About

test-assistant

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors