Skip to content

BrunoMedley/playwright-testkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Playwright TypeScript Automation Framework

A production-ready Playwright automation framework built with TypeScript, following best practices and industry standards.

πŸš€ Features

  • βœ… TypeScript with strict type safety
  • βœ… Page Object Model (POM) pattern
  • βœ… Multi-browser support (Chromium, Firefox, WebKit)
  • βœ… Mobile testing support (iOS and Android viewports)
  • βœ… Environment configuration (dev, staging, prod) via .env files
  • βœ… Allure + Playwright HTML reporting
  • βœ… Video, screenshot, and trace on failure
  • βœ… Parallel execution and retries in CI
  • βœ… API testing capabilities
  • βœ… Data-driven testing with JSON
  • βœ… Custom fixtures for authenticated sessions
  • βœ… GitHub Actions CI/CD integration

πŸ“ Project Structure

.
β”œβ”€β”€ config/
β”‚   └── playwright.config.ts      # Playwright configuration
β”œβ”€β”€ env/
β”‚   β”œβ”€β”€ .env.dev                  # Development environment variables
β”‚   └── .env.staging              # Staging environment variables
β”œβ”€β”€ test-data/
β”‚   └── users.json                # Test data for data-driven tests
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── users.api.spec.ts     # API test examples
β”‚   β”œβ”€β”€ e2e/
β”‚   β”‚   └── login.spec.ts         # E2E test examples
β”‚   β”œβ”€β”€ fixtures/
β”‚   β”‚   └── authenticatedUser.ts  # Custom fixtures
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ LoginPage.ts          # Page Object Model - Login
β”‚   β”‚   └── DashboardPage.ts      # Page Object Model - Dashboard
β”‚   └── utils/
β”‚       β”œβ”€β”€ apiClient.ts          # API client utility
β”‚       └── testData.ts           # Test data utilities
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── playwright.yml        # GitHub Actions CI workflow
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── README.md

πŸ› οΈ Setup

Prerequisites

  • Node.js 18+
  • pnpm (recommended) or npm

Installation

  1. Install dependencies:

    pnpm install
    # or
    npm install
  2. Install Playwright browsers:

    pnpm exec playwright install
    # or
    npx playwright install
  3. Configure environment variables:

    • Copy .env.dev and .env.staging files
    • Update with your actual environment URLs and credentials

πŸ“ Usage

Running Tests

# Run all tests
pnpm test

# Run tests in UI mode
pnpm test:ui

# Run tests in debug mode
pnpm test:debug

# Run tests in headed mode
pnpm test:headed

# Run tests for specific browser
pnpm test:chromium
pnpm test:firefox
pnpm test:webkit

# Run mobile tests
pnpm test:mobile

# Run API tests only
pnpm test:api

# Run E2E tests only
pnpm test:e2e

Environment Configuration

Set the environment before running tests:

# Development
NODE_ENV=dev pnpm test

# Staging
NODE_ENV=staging pnpm test

Reports

# View HTML report
pnpm report

# Generate Allure report
pnpm allure:generate

# Open Allure report
pnpm allure:open

# Serve Allure report (auto-generates)
pnpm allure:serve

πŸ§ͺ Writing Tests

Page Object Model Example

import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';

test('login test', async ({ page }) => {
  const loginPage = new LoginPage(page);
  await loginPage.goto();
  await loginPage.login('username', 'password');
  // ... assertions
});

Using Custom Fixtures

import { test, expect } from '../fixtures/authenticatedUser';

test('test with authenticated user', async ({ authenticatedPage, dashboardPage }) => {
  await dashboardPage.verifyOnDashboard();
  // User is already logged in
});

API Testing Example

import { test, expect } from '@playwright/test';
import { ApiClient } from '../utils/apiClient';

test('API test', async ({ request }) => {
  const apiClient = new ApiClient(request);
  const response = await apiClient.get('/users');
  expect(response.status()).toBe(200);
});

Data-Driven Testing

import { loadTestData } from '../utils/testData';

test('data-driven test', () => {
  const testData = loadTestData();
  for (const user of testData.validUsers) {
    // Test with each user
  }
});

πŸ”§ Configuration

Playwright Config

The main configuration is in config/playwright.config.ts. Key features:

  • Multi-browser projects: Chromium, Firefox, WebKit
  • Mobile projects: Pixel 5, iPhone 12
  • Reporting: HTML, Allure, JSON
  • Failure artifacts: Video, screenshots, traces
  • CI optimizations: Retries, workers configuration

TypeScript Config

Strict TypeScript configuration with path aliases:

  • @tests/* β†’ tests/*
  • @config/* β†’ config/*
  • @utils/* β†’ tests/utils/*
  • @pages/* β†’ tests/pages/*
  • @fixtures/* β†’ tests/fixtures/*

πŸš€ CI/CD

GitHub Actions

The workflow (.github/workflows/playwright.yml) includes:

  • βœ… Automatic test execution on push/PR
  • βœ… Parallel test execution with sharding
  • βœ… Artifact uploads (reports, screenshots, videos)
  • βœ… Allure report generation
  • βœ… pnpm support

CI Secrets

Configure these secrets in GitHub:

  • BASE_URL: Base URL for tests
  • API_BASE_URL: API base URL
  • API_KEY: API authentication key

πŸ“Š Best Practices

  1. Page Object Model: All page interactions are abstracted in Page classes
  2. Type Safety: Strict TypeScript ensures type safety
  3. Environment Management: Use .env files for different environments
  4. Test Data: Externalize test data in JSON files
  5. Reusable Utilities: Common functionality in utils
  6. Custom Fixtures: Reusable test setup/teardown
  7. Parallel Execution: Tests run in parallel for faster execution
  8. Failure Artifacts: Automatic capture of videos, screenshots, traces

πŸ› Debugging

Debug Mode

pnpm test:debug

Trace Viewer

Traces are automatically captured on failure. View them:

pnpm exec playwright show-trace trace.zip

Screenshots and Videos

Automatically saved in test-results/ directory on failure.

πŸ“š Additional Resources

πŸ“„ License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors