This is a comprehensive Playwright testing framework built with TypeScript using the Page Object Model (POM) design pattern. The framework provides a scalable and maintainable structure for writing end-to-end tests.
QDC-Playwright/
├── pages/ # Page Object Model classes
│ ├── BasePage.ts # Base page class with common functionality
│ ├── LoginPage.ts # Login page object
│ └── HomePage.ts # Home page object
├── tests/ # Test files (Playwright)
│ ├── example.spec.ts # Example test file
│ └── login.spec.ts # Login page tests
├── features/ # Cucumber BDD feature files
│ ├── login.feature # Login feature scenarios
│ ├── home.feature # Home page feature scenarios
│ ├── step-definitions/ # Cucumber step definitions
│ └── support/ # Cucumber support files
├── utils/ # Utility files and helpers
│ ├── TestData.ts # Test data constants
│ ├── Helpers.ts # Helper functions
│ └── constants.ts # Framework constants
├── playwright.config.ts # Playwright configuration
├── cucumber.config.js # Cucumber configuration
├── tsconfig.json # TypeScript configuration
└── package.json # Project dependencies
- Node.js (v16 or higher)
- npm or yarn package manager
-
Install dependencies:
npm install
-
Install Playwright browsers:
npx playwright install
-
Run all tests:
npm test -
Run tests in headed mode (with browser UI):
npm run test:headed
-
Run tests in debug mode:
npm run test:debug
-
Run tests with UI mode:
npm run test:ui
-
View test report:
npm run test:report
-
Generate code with Playwright Codegen:
npm run test:codegen
-
Run all Cucumber tests:
npm run test:cucumber
-
Run Cucumber tests in headed mode:
npm run test:cucumber:headed
-
Run specific feature file:
npx cucumber-js --config cucumber.config.js features/login.feature
-
Run a specific test file:
npx playwright test tests/login.spec.ts -
Run tests matching a pattern:
npx playwright test --grep "login"
-
Run tests in a specific browser:
npx playwright test --project=chromium
The BasePage class provides common functionality that all page objects inherit:
- Navigation methods
- Element interaction methods
- Wait methods
- Screenshot capabilities
-
Create a new page class extending BasePage:
import { Page } from '@playwright/test'; import { BasePage } from './BasePage'; export class NewPage extends BasePage { readonly someElement = () => this.page.locator('#some-element'); constructor(page: Page) { super(page); } async navigate(): Promise<void> { await this.goto('/new-page'); await this.waitForLoadState(); } }
-
Use the page object in tests:
import { test } from '@playwright/test'; import { NewPage } from '../pages/NewPage'; test('example test', async ({ page }) => { const newPage = new NewPage(page); await newPage.navigate(); await newPage.someElement().click(); });
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
test.describe('Login Tests', () => {
let loginPage: LoginPage;
test.beforeEach(async ({ page }) => {
loginPage = new LoginPage(page);
});
test('should login successfully', async () => {
await loginPage.navigate();
await loginPage.login('username', 'password');
// Add assertions here
});
});Centralized test data constants:
import { TestData } from '../utils/TestData';
await loginPage.login(TestData.VALID_USERNAME, TestData.VALID_PASSWORD);Utility functions for common operations:
import { Helpers } from '../utils/Helpers';
const randomEmail = Helpers.generateRandomEmail();
await Helpers.takeScreenshot(page, 'test-name');Framework constants:
import { Constants } from '../utils/constants';
await page.waitForTimeout(Constants.TIMEOUT.MEDIUM);Edit playwright.config.ts to customize:
- Browser projects (Chromium, Firefox, WebKit)
- Test directory
- Timeouts and retries
- Screenshots and videos
- Reporters
Edit tsconfig.json to customize TypeScript compiler options.
- Use Page Object Model: Keep page-specific logic in page objects
- Reuse BasePage: Extend BasePage for common functionality
- Use Utilities: Centralize test data and helper functions
- Descriptive Test Names: Use clear, descriptive test names
- Isolated Tests: Each test should be independent
- Proper Wait Strategies: Use Playwright's built-in waiting mechanisms
- Error Handling: Implement proper error handling in page objects
- Use
test.only()to run a single test - Use
test.debug()or run with--debugflag - Use Playwright Inspector:
npx playwright test --debug - Check screenshots and videos in
test-results/folder
This framework also supports Cucumber BDD testing with Playwright. See the features/README.md for detailed documentation on Cucumber integration.
- Feature files are located in
features/directory - Step definitions are in
features/step-definitions/ - Run Cucumber tests:
npm run test:cucumber
Feature: Login Functionality
Scenario: Successful login
Given I am on the login page
When I enter username "user@example.com" and password "password123"
And I click the login button
Then I should be redirected to the home pageAll step definitions use the Page Object Model pattern, ensuring consistency between Playwright and Cucumber tests.
- Playwright Documentation
- TypeScript Documentation
- Playwright Best Practices
- Cucumber.js Documentation
- Gherkin Syntax
- Create a new branch for your feature
- Write tests following the POM pattern
- Ensure all tests pass
- Submit a pull request
ISC