Building a Custom Test Framework with Playwright
✅ Building a Custom Test Framework with Playwright
Playwright is a powerful Node.js library for automating modern web apps. It supports multiple browsers (Chromium, Firefox, WebKit), multiple languages (JavaScript, TypeScript, Python, etc.), and advanced features like network interception, multi-tab automation, and mobile emulation.
Creating a custom test framework using Playwright gives you full control over test execution, reporting, and structure—great for enterprise or large-scale automation needs.
๐ง 1. Why Build a Custom Test Framework?
Full flexibility over test structure and config
Integrate custom logging, reporting, and retries
Use your own folder structure and test lifecycle
Tailor test execution for CI/CD, multi-browser runs, etc.
๐งฑ 2. Core Components of a Custom Framework
Your custom framework will typically consist of:
Test runner setup (e.g. using Playwright Test or an alternative like Mocha)
Configuration file (browser, timeouts, base URLs, etc.)
Custom base test class or wrapper
Test utilities and selectors
Reporting (e.g. Allure, custom logs)
Folder structure
๐งฐ 3. Initial Setup
๐ฆ Initialize Project
bash
Copy
Edit
mkdir playwright-framework
cd playwright-framework
npm init -y
npm install -D @playwright/test
๐ Folder Structure
plaintext
Copy
Edit
/playwright-framework
│
├── tests/ # Test cases
├── pages/ # Page Object Models
├── utils/ # Helpers, custom functions
├── config/ # Test configurations
├── reports/ # Test reports
├── test-runner.js # Custom runner logic (optional)
└── playwright.config.js
⚙️ 4. Configuring playwright.config.js
Create a base configuration for your framework.
js
Copy
Edit
// playwright.config.js
import { defineConfig } from '@playwright/test';
export default defineConfig({
timeout: 30000,
retries: 1,
use: {
baseURL: 'https://your-website.com',
headless: true,
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
projects: [
{ name: 'chromium', use: { browserName: 'chromium' } },
{ name: 'firefox', use: { browserName: 'firefox' } },
],
reporter: [['list'], ['html', { outputFolder: 'reports/html' }]],
});
๐งฉ 5. Building a Base Test Class
Use the Page Object Model (POM) for maintainable tests.
js
Copy
Edit
// pages/LoginPage.js
export class LoginPage {
constructor(page) {
this.page = page;
this.usernameInput = page.locator('#username');
this.passwordInput = page.locator('#password');
this.loginButton = page.locator('#login');
}
async login(username, password) {
await this.usernameInput.fill(username);
await this.passwordInput.fill(password);
await this.loginButton.click();
}
}
๐งช 6. Writing Tests Using the Framework
js
Copy
Edit
// tests/login.spec.js
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
test.describe('Login Tests', () => {
test('Valid login', async ({ page }) => {
const loginPage = new LoginPage(page);
await page.goto('/');
await loginPage.login('admin', 'password123');
await expect(page.locator('text=Welcome')).toBeVisible();
});
});
๐ 7. Reporting and Logs
Use built-in reporters or plugins like Allure.
Install Allure (optional)
bash
Copy
Edit
npm install -D allure-playwright
Then modify your config:
js
Copy
Edit
reporter: [['allure-playwright']],
Generate report:
bash
Copy
Edit
npx allure generate ./allure-results --clean -o ./reports/allure-report
๐ 8. Running Tests
Run all tests
bash
Copy
Edit
npx playwright test
Run tests in a specific browser
bash
Copy
Edit
npx playwright test --project=firefox
Run a specific file
bash
Copy
Edit
npx playwright test tests/login.spec.js
๐งฉ 9. Extending the Framework
Parallel Execution: Set workers in config
Custom Commands: Add helpers to utils/
Test Hooks: Use beforeAll, afterEach, etc.
CI Integration: Add GitHub Actions, Jenkins, or GitLab pipelines
✅ 10. Summary
Feature Tool / Method
Test Runner Playwright Test
Config Management playwright.config.js
Page Object Model Custom classes in pages/
Reporting HTML, Allure, Console
Test Execution CLI, CI/CD
Flexibility Full control over structure and flow
Learn Playwright Training Course in Hyderabad
Read More
Handling CAPTCHA with Playwright
Authentication Testing with JWT in Playwright
Component Testing with Playwright
Accessibility Testing with Playwright
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment