A scalable and maintainable test automation framework built with Playwright and TypeScript. This repository serves as a comprehensive learning resource for building professional-grade test automation solutions.
- Features
- Tech Stack
- Getting Started
- Core Concepts
- Running Tests
- Best Practices
- CI/CD Integration
- Contributing
- Learning Resources
- Page Object Model (POM): Organized and maintainable test structure
- TypeScript Support: Full type safety and IntelliSense for better development experience
- Fixtures: Custom fixtures for test setup and teardown
- Test Data Management: Centralized test data handling
- Cross-browser Testing: Run tests on Chromium, Firefox, and WebKit
- Parallel Execution: Execute tests concurrently for faster feedback
- Screenshots & Videos: Automatic capture on test failures
- CI/CD Ready: Pre-configured for GitHub Actions, Jenkins, and other CI/CD platforms
- Comprehensive Reporting: Detailed HTML reports with test results
| Technology | Version | Purpose |
|---|---|---|
| Playwright | Latest | End-to-end testing framework |
| TypeScript | 5.x+ | Type-safe JavaScript |
| Node.js | 16.x+ | JavaScript runtime |
| Jest/Mocha | Latest | Test runner (if configured) |
Language Composition:
- TypeScript: 57.1%
- JavaScript: 38.5%
- HTML: 3.3%
- CSS: 1.1%
Ensure you have the following installed on your system:
- Node.js: v16.x or higher (Download)
- npm or yarn: Package managers
- Git: Version control
-
Clone the repository
git clone https://github.com/RajatSharan/playwright-typescript-framework.git cd playwright-typescript-framework -
Install dependencies
npm install
-
Install Playwright browsers
npx playwright install
-
Verify installation
npm run test:help
playwright-typescript-framework/
βββ src/
β βββ pages/ # Page Object Model classes
β β βββ basePage.ts
β β βββ loginPage.ts
β β βββ dashboardPage.ts
β βββ fixtures/ # Custom Playwright fixtures
β β βββ fixtures.ts
β βββ utils/ # Utility functions
β β βββ logger.ts
β β βββ helpers.ts
β βββ config/ # Configuration files
β βββ testConfig.ts
βββ tests/
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ e2e/ # End-to-end tests
β βββ data/ # Test data
βββ reports/ # Test execution reports
βββ .github/
β βββ workflows/ # CI/CD configurations
βββ playwright.config.ts # Playwright configuration
βββ tsconfig.json # TypeScript configuration
βββ package.json # Project dependencies
βββ README.md # This file
The Page Object Model pattern encapsulates page elements and interactions into separate classes, making tests more maintainable and readable.
Example Page Class:
import { Page, Locator } from '@playwright/test';
export class LoginPage {
readonly page: Page;
readonly emailInput: Locator;
readonly passwordInput: Locator;
readonly loginButton: Locator;
constructor(page: Page) {
this.page = page;
this.emailInput = page.locator('input[name="email"]');
this.passwordInput = page.locator('input[name="password"]');
this.loginButton = page.locator('button:has-text("Login")');
}
async navigateTo() {
await this.page.goto('https://example.com/login');
}
async login(email: string, password: string) {
await this.emailInput.fill(email);
await this.passwordInput.fill(password);
await this.loginButton.click();
}
}Example Test:
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/loginPage';
test('User can login successfully', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.navigateTo();
await loginPage.login('user@example.com', 'password123');
expect(page.url()).toContain('/dashboard');
});Custom fixtures provide reusable setup and teardown logic for tests.
Example Fixture:
import { test as base } from '@playwright/test';
import { LoginPage } from '../pages/loginPage';
type TestFixtures = {
authenticatedPage: void;
loginPage: LoginPage;
};
export const test = base.extend<TestFixtures>({
loginPage: async ({ page }, use) => {
const loginPage = new LoginPage(page);
await use(loginPage);
},
authenticatedPage: async ({ page, loginPage }, use) => {
await loginPage.navigateTo();
await loginPage.login('user@example.com', 'password123');
await use();
},
});Centralize your test data for better maintainability.
Example Data File (tests/data/testData.ts):
export const testData = {
users: {
admin: {
email: 'admin@example.com',
password: 'AdminPassword123',
},
user: {
email: 'user@example.com',
password: 'UserPassword123',
},
},
urls: {
login: 'https://example.com/login',
dashboard: 'https://example.com/dashboard',
},
};npm run testnpm run test -- tests/e2e/login.spec.tsnpm run test -- --grep @smokenpm run test:headednpm run test:debugnpm run test:reportnpm run test:show-reportawait page.waitForSelector('button:text("Next")');// β Don't do this
await page.waitForTimeout(5000);
// β
Do this instead
await page.waitForLoadState('networkidle');// β Avoid
const button = page.locator('button:nth-of-type(3)');
// β
Use
const submitButton = page.locator('button[aria-label="Submit Form"]');- Each test should be able to run in isolation
- Don't depend on the order of test execution
- Use fixtures for setup and teardown
test('User can successfully log in with valid credentials', async ({ page }) => {
// test code
});tests/
βββ auth/
βββ checkout/
βββ profile/
Sample workflow file (.github/workflows/tests.yml):
name: Playwright Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Install Playwright
run: npx playwright install --with-deps
- name: Run tests
run: npm run test
- name: Upload report
if: always()
uses: actions/upload-artifact@v3
with:
name: playwright-report
path: playwright-report/
retention-days: 30- Playwright Official Documentation
- TypeScript Handbook
- Page Object Model Pattern
- Playwright API Reference
- Best Practices for E2E Testing
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please ensure your code follows the project's coding standards and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.
If you have questions or need help:
- Open an issue
- Check existing discussions
- Review the documentation and examples in this repository
Happy Testing! π
Made with β€οΈ for the test automation community.