Skip to content

osanto/playwright-ts-tests-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Playwright TypeScript Test Automation Examples

Playwright Tests Test Report TypeScript Playwright Node.js

About

Playwright test automation examples using TypeScript with Page Object Model (POM) design pattern and custom fixtures.

Application Under Test: The Internet - Herokuapp

🎯 Features

  • βœ… Page Object Model (POM) - Clean separation of test logic and page interactions
  • βœ… Custom Fixtures - Reusable page object fixtures for cleaner test code
  • βœ… TypeScript - Type-safe test automation
  • βœ… Multi-Browser Testing - Chromium, Firefox, and WebKit support
  • βœ… HTML Reports - Built-in test reporting with Playwright

πŸ“‹ Test Coverage

The project includes automated tests for the following scenarios:

Test Suite Description
A/B Testing Validates A/B test variations and content
Add/Remove Elements Tests dynamic element addition and removal
Checkboxes Verifies checkbox interactions and state management
Context Menu Tests right-click context menu functionality
Dropdown Validates dropdown selection and options
File Download Tests file download functionality
File Upload Verifies file upload with validation
Hovers Tests hover interactions and tooltip displays
Key Presses Validates keyboard input handling

πŸ› οΈ Tech Stack

  • Playwright - ^1.53.0
  • TypeScript - Latest
  • Node.js - v16+ recommended
  • dotenv - Environment configuration

πŸ“¦ Installation

  1. Clone the repository:
git clone <repository-url>
cd playwright-ts-tests-examples
  1. Install dependencies:
npm install
  1. Install Playwright browsers:
npx playwright install
  1. Create a .env file in the root directory:
BASE_URL=https://the-internet.herokuapp.com

πŸš€ Running Tests

Run all tests (headless mode)

npm test

Run tests with visible browser

npm run test:headed

Run tests in UI mode (interactive)

npm run test:ui

Run tests in debug mode

npm run test:debug

Run tests on specific browser

npm run test:chromium
npm run test:firefox
npm run test:webkit

View test report

npm run report

πŸ“ Project Structure

playwright-ts-tests-examples/
β”œβ”€β”€ pages/                      # Page Object Models
β”‚   β”œβ”€β”€ base-page.ts           # Base page class with common methods
β”‚   β”œβ”€β”€ main-page.ts           # Main/home page object
β”‚   β”œβ”€β”€ ab-testing-page.ts     # A/B testing page object
β”‚   β”œβ”€β”€ checkboxes-page.ts     # Checkboxes page object
β”‚   β”œβ”€β”€ dropdown-page.ts       # Dropdown page object
β”‚   β”œβ”€β”€ file-upload-page.ts    # File upload page object
β”‚   └── ...                    # Other page objects
β”œβ”€β”€ tests/                      # Test specifications
β”‚   β”œβ”€β”€ ab-testing.spec.ts     # A/B testing tests
β”‚   β”œβ”€β”€ checkboxes.spec.ts     # Checkbox tests
β”‚   β”œβ”€β”€ dropdown.spec.ts       # Dropdown tests
β”‚   β”œβ”€β”€ file-upload.spec.ts    # File upload tests
β”‚   └── ...                    # Other test files
β”œβ”€β”€ test-data/                  # Test data files
β”‚   └── file-to-upload.txt     # Sample file for upload tests
β”œβ”€β”€ fixture-pages.ts            # Custom Playwright fixtures
β”œβ”€β”€ playwright.config.ts        # Playwright configuration
β”œβ”€β”€ package.json               # Project dependencies
└── .env                       # Environment variables (gitignored)

πŸ—οΈ Architecture

Page Object Model (POM)

Each page is represented by a class that extends BasePage:

export class CheckboxesPage extends BasePage {
  readonly pageHeader: Locator;
  readonly firstCheckbox: Locator;
  readonly secondCheckbox: Locator;

  constructor(page: Page) {
    super(page);
    this.pageHeader = this.page.locator("h3");
    this.firstCheckbox = this.page.locator("#checkboxes input").nth(0);
    this.secondCheckbox = this.page.locator("#checkboxes input").nth(1);
  }
}

Custom Fixtures

Page objects are injected as fixtures for cleaner test code:

test("Verify checkbox interactions", async ({ checkboxesPage }) => {
  await checkboxesPage.firstCheckbox.check();
  await expect(checkboxesPage.firstCheckbox).toBeChecked();
});

Base Page

Common functionality is abstracted in the BasePage class:

export class BasePage {
  readonly page: Page;

  constructor(page: Page) {
    this.page = page;
  }

  async navigateTo(path: string) {
    await this.page.goto(path.startsWith("/") ? path : `/${path}`);
  }
}

βš™οΈ Configuration

Playwright Configuration

The playwright.config.ts includes:

  • Multi-browser support (Chromium, Firefox, WebKit)
  • Parallel test execution
  • Retry logic for CI environments
  • HTML reporter
  • Trace on first retry

Environment Variables

Create a .env file with:

BASE_URL=https://the-internet.herokuapp.com

πŸ“Š Test Reports

View Latest Test Report

πŸ”— Live Test Report on GitHub Pages

The test report is automatically published after each CI run and includes:

  • Test execution summary
  • Pass/fail status
  • Execution time
  • Screenshots on failure
  • Trace files for debugging

View Local Test Report

After running tests locally:

npm run report

πŸ“ Best Practices

  • βœ… Use Page Object Model for maintainability
  • βœ… Keep tests independent and isolated
  • βœ… Use meaningful test and variable names
  • βœ… Add comments for complex logic
  • βœ… Use custom fixtures for reusability
  • βœ… Follow TypeScript best practices
  • βœ… Keep locators in page objects, not in tests

πŸ“„ License

MIT


Happy Testing! 🎭

About

Playwright test automation examples using TypesScript with Page Object Model and custom fixtures. Application under testing - https://the-internet.herokuapp.com

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published