A comprehensive TypeScript-based API test framework using Playwright and Cucumber (Gherkin/BDD) for testing REST APIs with enterprise-grade validation patterns.
- β TypeScript for type safety and better IDE support
- β Playwright for powerful API testing capabilities
- β Cucumber/Gherkin for BDD-style test scenarios
- β Contract Validation for testing APIs with external data
- β Advanced Testing Patterns (HTTP protocol, identity, sorting, idempotency, performance, data quality)
- β 76 Scenarios covering comprehensive API validation (409 test steps)
- β Parallel execution for faster test runs
- β Multiple reporters (HTML, JSON, List)
- β Helper utilities for common API operations
- β Authentication examples (Bearer tokens, API keys, OAuth)
- β Data validation patterns
- β Request examples (GET, POST, PUT, DELETE)
api-tests/
βββ features/ # Gherkin feature files (BDD)
β βββ api-tests.feature # Main API scenarios (9 scenarios)
β βββ authentication.feature # Auth scenarios (4 scenarios, @skip)
β βββ validation.feature # Data validation scenarios (6 scenarios)
β βββ contract-validation.feature # Contract/schema validation (13 scenarios)
β βββ http-protocol.feature # HTTP protocol validation (10 scenarios)
β βββ identity-validation.feature # ID and referential integrity (6 scenarios)
β βββ sorting-pagination.feature # Sorting and pagination (8 scenarios)
β βββ idempotency.feature # Idempotency testing (5 scenarios)
β βββ performance.feature # Performance and reliability (7 scenarios)
β βββ data-quality.feature # Data quality validation (8 scenarios)
β βββ step_definitions/
β βββ api-steps.ts # Step implementations (~100+ steps)
βββ tests/ # Playwright test files (direct)
β βββ config/
β β βββ test-config.ts # Configuration and test data
β βββ helpers/
β β βββ api-helper.ts # Reusable API helper functions
β βββ get-requests.spec.ts # GET request examples
β βββ post-requests.spec.ts # POST request examples
β βββ put-requests.spec.ts # PUT request examples
β βββ delete-requests.spec.ts # DELETE request examples
β βββ data-validation.spec.ts # Data validation examples
β βββ authentication.spec.ts # Authentication patterns
βββ cucumber.js # Cucumber configuration
βββ playwright.config.ts # Playwright configuration
βββ tsconfig.json # TypeScript configuration
βββ README.md # This file
βββ GHERKIN-GUIDE.md # Gherkin/BDD usage guide
βββ CONTRACT-VALIDATION-GUIDE.md # Contract validation guide
βββ ADVANCED-TESTING-GUIDE.md # Advanced testing patterns guide
βββ package.json # Project dependencies
- Node.js 20.x or higher (required for Cucumber)
- npm or yarn
-
Install dependencies:
npm install
-
Install Playwright browsers (optional for API testing):
npx playwright install
npm testnpx playwright test get-requests.spec.tsnpm run test:uinpm run test:headednpm run test:debugnpm run test:reportnpm run test:cucumber# Basic API tests
npm run test:cucumber:api
# Advanced testing patterns
npx cucumber-js features/http-protocol.feature
npx cucumber-js features/identity-validation.feature
npx cucumber-js features/sorting-pagination.feature
npx cucumber-js features/idempotency.feature
npx cucumber-js features/performance.feature
npx cucumber-js features/data-quality.feature
# Contract validation
npx cucumber-js features/contract-validation.feature# Run smoke tests only
npx cucumber-js --tags @smoke
# Run protocol validation tests
npx cucumber-js --tags @protocol
# Run performance tests
npx cucumber-js --tags @performance
# Run identity validation tests
npx cucumber-js --tags @identitynpx cucumber-js --tags "@smoke"
npx cucumber-js --tags "not @skip"start cucumber-report.htmlThis framework includes comprehensive documentation:
π GHERKIN-GUIDE.md - Complete guide to using Gherkin/BDD with examples
π CONTRACT-VALIDATION-GUIDE.md - Contract and schema validation patterns
π ADVANCED-TESTING-GUIDE.md - Enterprise-level testing patterns:
- HTTP Protocol Validation (status codes, headers, error handling)
- Identity Validation (ID format, uniqueness, foreign keys)
- Sorting & Pagination (ordering, boundaries, consistency)
- Idempotency (GET safety, no side effects)
- Performance (response time, payload size, baselines)
- Data Quality (sanitization, formats, control characters)
| Category | Scenarios | Steps | Status |
|---|---|---|---|
| Basic API Tests | 9 | 40 | β Passing |
| Authentication | 4 | 24 | β Passing |
| Validation | 6 | 26 | β Passing |
| Contract Validation | 13 | 80 | β Passing |
| HTTP Protocol | 10 | 36 | β Passing |
| Identity Validation | 6 | 37 | β Passing |
| Sorting & Pagination | 8 | 44 | β Passing |
| Idempotency | 5 | 32 | β Passing |
| Performance | 7 | 30 | β Passing |
| Data Quality | 8 | 44 | β Passing |
| TOTAL | 76 | 409 | β 76 Passing |
Update the base URL in playwright.config.ts:
use: {
baseURL: 'https://your-api-url.com',
}Update authentication tokens in tests/config/test-config.ts:
auth: {
bearerToken: 'your-actual-token',
apiKey: 'your-actual-api-key',
}test('should get all posts', async ({ request }) => {
const response = await request.get('/posts');
expect(response.status()).toBe(200);
const data = await response.json();
expect(Array.isArray(data)).toBeTruthy();
});test('should create post with auth', async ({ request }) => {
const response = await request.post('/posts', {
data: { title: 'Test', body: 'Content', userId: 1 },
headers: { 'Authorization': `Bearer ${token}` }
});
expect(response.status()).toBe(201);
});test('should validate response schema', async ({ request }) => {
const response = await request.get('/posts/1');
const post = await response.json();
expect(post).toHaveProperty('id');
expect(post).toHaveProperty('title');
expect(typeof post.title).toBe('string');
});The ApiHelper class provides convenient methods for API requests:
import { ApiHelper } from './helpers/api-helper';
test('example using helper', async ({ request }) => {
const helper = new ApiHelper(request);
// GET with authentication
const response = await helper.get('/posts/1', {
token: 'your-token'
});
// POST with authentication
const createResponse = await helper.post('/posts', data, {
token: 'your-token'
});
});Tests run in parallel by default. Configure in playwright.config.ts:
workers: process.env.CI ? 1 : undefined, // Parallel locally, sequential in CI
fullyParallel: true,The framework generates multiple reports:
- HTML Report: Visual test results in
playwright-report/ - JSON Report: Machine-readable results in
test-results/results.json - List Report: Console output during test execution
- Organize tests by feature/endpoint
- Use descriptive test names
- Validate status codes AND response data
- Use test.beforeEach for common setup
- Leverage the ApiHelper for reusable operations
- Keep sensitive data in environment variables
- Add proper error handling and assertions
Create a .env file for sensitive data:
API_BASE_URL=https://api.example.com
API_TOKEN=your-token-here
API_KEY=your-api-key-hereThen load it in your config:
import * as dotenv from 'dotenv';
dotenv.config();Example GitHub Actions workflow:
name: API Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/- Verify
baseURLinplaywright.config.ts - Check network connectivity
- Validate authentication credentials
- Run
npm installto ensure dependencies are installed - Check
tsconfig.jsonconfiguration
- Ensure test run completes
- Check
playwright-report/directory - Run
npm run test:reportto view
Contributions are welcome! Please see CONTRIBUTING.md for details on:
- Reporting issues
- Adding new test scenarios
- Code style guidelines
- Pull request process
This project is licensed under the MIT License - see the LICENSE file for details.
- Playwright API Testing Docs
- Cucumber.js Documentation
- TypeScript Documentation
- JSONPlaceholder API (used in examples)
Give a βοΈ if this project helped you!
ISC