1. Introduction to a Playwright TypeScript Boilerplate Project
Playwright is a modern browser automation framework developed by Microsoft. It supports Chromium, Firefox, and WebKit and provides powerful features for end-to-end testing.
When working on real-world automation projects, writing individual test scripts is not enough. QA engineers need a proper framework with reusable components, configuration files, test data management, reporting, and debugging capabilities.
A Playwright TypeScript boilerplate project provides a ready-made structure that helps developers and testers start automation development without creating everything from scratch.
In this tutorial, we will build a practical Playwright TypeScript starter project with:
- Page Object Model
- Reusable fixtures
- Test data management
- Environment variables
- Common utility functions
- Cross-browser execution
- Parallel testing
- Screenshots and traces
- HTML reporting
- GitHub Actions and Jenkins integration
By the end, you will have a reusable automation framework that can be customised for real-world software quality assurance projects.
2. What Is a Boilerplate Project and Why Is It Useful?
A boilerplate project is a preconfigured project structure containing commonly required files, dependencies, configurations, and reusable components.
Instead of writing the same setup code repeatedly, developers can use an existing template and customise it according to project requirements.
Benefits of a Playwright TypeScript boilerplate project
1. Faster development: Start writing test cases without configuring everything manually.
2. Code reusability: Reuse page objects, fixtures, and utility functions.
3. Easy maintenance: Separate test logic from application interactions.
4. Scalability: Add new test modules without changing the entire framework.
5. Consistent execution: Use common configurations across development environments.
6. Better reporting: Generate HTML reports and collect failure screenshots.
7. CI/CD integration: Execute automated tests through GitHub Actions and Jenkins.
For QA Automation Engineers and SDETs, a properly designed boilerplate improves productivity and makes automation projects easier to maintain.
3. Prerequisites and Installation
Before creating the Playwright TypeScript boilerplate project, install the required software.
| Software | Purpose |
| Node.js | Executes JavaScript and TypeScript tools |
| npm | Manages project dependencies |
| Visual Studio Code | Code editor |
| Playwright | Browser automation |
| Git | Version control |
Download Node.js from https://nodejs.org/.
Verify installation:
node -v
npm -v
Create a project folder:
mkdir playwright-boilerplate
cd playwright-boilerplate
Initialize the project:
npm init playwright@latest
Select these options:
- TypeScript
- Tests folder: tests
- GitHub Actions: Yes
- Install Playwright browsers: Yes
Install an additional package for environment variable management:
npm install dotenv
Open the project:
code .
Your Playwright environment is now ready.
4. Creating the Playwright TypeScript Starter Project
The installation wizard creates the basic Playwright project.
However, for a professional automation framework, we will organise the files into separate folders.
Create the following structure:
playwright-boilerplate/
│
├── tests/
│ └── login.spec.ts
│
├── pages/
│ └── LoginPage.ts
│
├── fixtures/
│ └── test-fixtures.ts
│
├── test-data/
│ └── users.ts
│
├── utils/
│ └── test-utils.ts
│
├── config/
│ └── environment.ts
│
├── .github/
│ └── workflows/
│ └── playwright.yml
│
├── .env
├── .env.example
├── .gitignore
├── package.json
├── playwright.config.ts
└── tsconfig.json
Understanding the folder structure
| Folder or file | Responsibility |
| tests | Contains automation test cases |
| pages | Contains Page Object Model classes |
| fixtures | Manages reusable test setup |
| test-data | Stores test input data |
| utils | Contains common helper functions |
| config | Manages environment configuration |
| playwright.config.ts | Controls Playwright execution |
| .env | Stores local environment variables |
This structure separates responsibilities and makes the framework easier to maintain as the project grows.
5. Configuring playwright.config.ts and TypeScript Settings
The playwright.config.ts file is the central configuration file.
It controls browser selection, retries, timeouts, parallel execution, and reporting.
Replace its contents with:
import { defineConfig, devices } from ‘@playwright/test’;
import dotenv from ‘dotenv’;
dotenv.config();
export default defineConfig({
testDir: ‘./tests’,
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 2 : undefined,
timeout: 30000,
reporter: [
[‘list’],
[‘html’, { open: ‘never’ }]
],
use: {
baseURL: process.env.BASE_URL,
headless: true,
screenshot: ‘only-on-failure’,
video: ‘retain-on-failure’,
trace: ‘on-first-retry’
},
projects: [
{
name: ‘chromium’,
use: { …devices[‘Desktop Chrome’] }
},
{
name: ‘firefox’,
use: { …devices[‘Desktop Firefox’] }
},
{
name: ‘webkit’,
use: { …devices[‘Desktop Safari’] }
}
]
});
Important configuration options
- testDir: Defines where test files are located.
- fullyParallel: Allows tests to execute in parallel.
- retries: Retries failed tests.
- workers: Controls parallel worker processes.
- timeout: Sets the maximum test execution time.
- reporter: Generates execution reports.
- screenshot: Captures screenshots when tests fail.
- trace: Collects execution traces for debugging.
- projects: Defines browser configurations.
For TypeScript, keep the generated tsconfig.json initially. Ensure strict type checking is enabled if your project requires it.
6. Setting Up Environment Variables and Test Data
Environment variables allow you to configure different application environments without changing test scripts.
Create .env:
BASE_URL=https://www.saucedemo.com
Create .env.example:
BASE_URL=https://example.com
Add .env to .gitignore:
node_modules/
playwright-report/
test-results/
.env
Never commit passwords, API tokens, or other secrets to Git.
Create an environment configuration file
Create config/environment.ts:
export const environment = {
baseUrl: process.env.BASE_URL
};
if (!environment.baseUrl) {
throw new Error(‘BASE_URL is not configured’);
}
Create reusable test data
Create test-data/users.ts:
export const users = {
validUser: {
username: ‘standard_user’,
password: ‘secret_sauce’
}
};
This keeps test input separate from test implementation.
The credentials above are example credentials for the public SauceDemo practice application. Use suitable test accounts for your own application.
7. Creating Reusable Page Object Model Classes
Page Object Model is an important design pattern in automation frameworks.
It separates page elements and browser interactions from test cases.
Create pages/LoginPage.ts:
import { Page, Locator } from ‘@playwright/test’;
export class LoginPage {
readonly username: Locator;
readonly password: Locator;
readonly loginButton: Locator;
constructor(private page: Page) {
this.username = page.locator(‘#user-name’);
this.password = page.locator(‘#password’);
this.loginButton = page.locator(‘#login-button’);
}
async open() {
await this.page.goto(‘/’);
}
async login(username: string, password: string) {
await this.username.fill(username);
await this.password.fill(password);
await this.loginButton.click();
}
}
This class contains reusable login actions.
Whenever another test needs login functionality, it can reuse this page object instead of repeating locator definitions.
8. Setting Up Reusable Fixtures and Utilities
Fixtures help manage reusable test setup and dependencies.
Create fixtures/test-fixtures.ts:
import { test as base } from ‘@playwright/test’;
import { LoginPage } from ‘../pages/LoginPage’;
type MyFixtures = {
loginPage: LoginPage;
};
export const test = base.extend<MyFixtures>({
loginPage: async ({ page }, use) => {
const loginPage = new LoginPage(page);
await use(loginPage);
}
});
export { expect } from ‘@playwright/test’;
Now tests can access the LoginPage directly through a custom fixture.
Create reusable utility functions
Create utils/test-utils.ts:
import { Page } from ‘@playwright/test’;
export async function captureScreenshot(
page: Page,
name: string
) {
await page.screenshot({
path: `test-results/${name}.png`,
fullPage: true
});
}
This utility can be reused whenever a screenshot is required.
9. Adding Assertions, Screenshots, Traces, and HTML Reporting
Now let’s create a complete test using the reusable framework components.
Create tests/login.spec.ts:
import { test, expect } from ‘../fixtures/test-fixtures’;
import { users } from ‘../test-data/users’;
test(‘Verify successful login’, async ({ loginPage, page }) => {
await loginPage.open();
await loginPage.login(
users.validUser.username,
users.validUser.password
);
await expect(page).toHaveURL(/inventory.html/);
await expect(
page.getByText(‘Products’)
).toBeVisible();
});
This test performs the following operations:
- Opens the login application.
- Reads credentials from test data.
- Enters the username and password.
- Clicks the login button.
- Verifies the resulting URL.
- Confirms that the Products page is visible.
Playwright automatically captures configured failure artifacts.
npx playwright show-report
The report displays passed tests, failed tests, execution duration, and available attachments.
For debugging, use:
npx playwright test –debug
You can inspect the failed action and use Trace Viewer to understand the execution history.
10. Running Tests Across Browsers and in Parallel
One important advantage of a Playwright TypeScript boilerplate project is that the same test scripts can run across multiple browser projects.
Execute all configured browsers:
npx playwright test
Run only Chromium:
npx playwright test –project=chromium
Run only Firefox:
npx playwright test –project=firefox
Run only WebKit:
npx playwright test –project=webkit
Run with four workers:
npx playwright test –workers=4
npx playwright test tests/login.spec.ts
Parallel execution helps reduce regression testing time. However, tests should use independent data and avoid modifying shared resources in ways that cause conflicts.
11. Integrating the Boilerplate with GitHub Actions and Jenkins
A professional automation framework should support continuous integration.
This allows tests to execute automatically whenever developers push code or create pull requests.
GitHub Actions configuration
Create .github/workflows/playwright.yml:
name: Playwright Automation
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
– uses: actions/checkout@v4
– uses: actions/setup-node@v4
with:
node-version: 22
– run: npm ci
– run: npx playwright install –with-deps
– run: npx playwright test
– uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: |
playwright-report/
test-results/
The workflow installs dependencies, downloads browsers, executes tests, and uploads reports.
Jenkins integration
You can also use Jenkins to execute the same framework.
pipeline {
agent any
stages {
stage(‘Install Dependencies’) {
steps {
sh ‘npm ci’
sh ‘npx playwright install –with-deps’
}
}
stage(‘Execute Tests’) {
steps {
sh ‘npx playwright test’
}
}
}
post {
always {
archiveArtifacts(
artifacts: ‘playwright-report/**,test-results/**’,
allowEmptyArchive: true
)
}
}
}
The Jenkins agent must have Node.js, npm, and the required operating system dependencies installed.
For production pipelines, configure secure environment variables and publish test reports using your organisation’s reporting tools.
12. Common Setup Mistakes and Best Practices
Common mistakes
| Mistake | Solution |
| Writing all code in one file | Separate tests, pages, fixtures, and utilities |
| Hardcoding environment URLs | Use environment variables |
| Repeating locators | Create reusable Page Object classes |
| Using fixed waits | Prefer web-first assertions |
| Sharing test data | Use independent test data |
| Ignoring failed reports | Review screenshots and traces |
| Committing secrets | Add .env to .gitignore |
| Running too many workers | Configure workers according to available resources |
Best practices
- Follow a consistent folder structure.
- Use TypeScript interfaces and types.
- Keep test data separate from test logic.
- Create reusable fixtures.
- Implement Page Object Model.
- Use meaningful test descriptions.
- Configure screenshots and traces for failures.
- Maintain dependencies using package-lock.json.
- Use Git for team collaboration.
- Execute smoke and regression suites through CI/CD.
13. Frequently Asked Questions (FAQs)
1. What is Playwright TypeScript boilerplate project?
A Playwright TypeScript boilerplate project is a preconfigured automation framework containing reusable components, configurations, fixtures, test data, and reporting capabilities.
2. How do I get started with Playwright TypeScript boilerplate project?
Install Node.js and initialize Playwright using:
npm init playwright@latest
Then create folders for pages, fixtures, test data, utilities, and configuration.
3. What are the benefits of Playwright TypeScript boilerplate project?
It reduces setup time, improves code reusability, supports maintainability, and makes automation frameworks easier to scale.
4. Can beginners use a Playwright starter project?
Yes. Beginners can start with the generated Playwright project and gradually add Page Object Model, fixtures, utilities, and reporting.
5. Does Playwright support Page Object Model?
Yes. You can create TypeScript classes for individual application pages and reuse their methods across test cases.
6. Can I use this boilerplate for enterprise projects?
Yes. You can extend it with authentication, API testing, test data management, environment configurations, reporting integrations, and CI/CD workflows.
7. Can I execute Playwright tests in parallel?
Yes. Playwright Test supports parallel execution using worker processes. Configure workers according to your environment and test data requirements.
