Playwright test automation examples using TypeScript with Page Object Model (POM) design pattern and custom fixtures.
Application Under Test: The Internet - Herokuapp
- β 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
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 |
- Playwright - ^1.53.0
- TypeScript - Latest
- Node.js - v16+ recommended
- dotenv - Environment configuration
- Clone the repository:
git clone <repository-url>
cd playwright-ts-tests-examples- Install dependencies:
npm install- Install Playwright browsers:
npx playwright install- Create a
.envfile in the root directory:
BASE_URL=https://the-internet.herokuapp.comnpm testnpm run test:headednpm run test:uinpm run test:debugnpm run test:chromium
npm run test:firefox
npm run test:webkitnpm run reportplaywright-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)
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);
}
}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();
});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}`);
}
}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
Create a .env file with:
BASE_URL=https://the-internet.herokuapp.com
π 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
After running tests locally:
npm run report- β 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
MIT
Happy Testing! π