This guide will help you set up and run your first test with the Playwright TypeScript framework.
Before you begin, ensure you have the following installed:
-
Node.js (version 18 or higher)
node --version # Should be v18.0.0 or higher -
npm (comes with Node.js)
npm --version # Should be 9.0.0 or higher -
Git (for cloning the repository)
git --version
# Clone the repository
git clone <repository-url>
cd playwright-typescript-example
# Install all dependencies
npm install
# Install Playwright browsers (Chromium, Firefox, WebKit)
npx playwright install# Run a quick test to verify everything works
npx playwright test --project=local --grep="API Tests"If successful, you should see test results in your terminal.
# Run all tests in headed mode (you'll see the browser)
npx playwright test --project=local# Run all tests in headless mode (no browser UI)
npx playwright test --project=pipeline# Run Wikipedia navigation tests
npx playwright test --grep="Wikipedia Navigation Test"
# Run API tests only
npx playwright test --grep="API Tests"After running tests, you'll see:
- Console Output - Real-time test execution logs
- Reports Directory - Generated at
reports/playwright-report/- Interactive HTML reporttest-results/- Test artifacts, screenshots, videosallure-results/- Allure test resultssmart-reports/- Smart reporter HTML
# Open the Playwright HTML report
npx playwright show-report reports/playwright-reportsrc/
├── fixtures/ # Test fixtures (reusable setup)
├── pages/ # Page Objects (UI element abstractions)
├── tests/ # Your test files
└── utils/ # Helper utilities (Logger, Allure, etc.)
// src/pages/MyNewPage.ts
import { Page, Locator } from '@playwright/test';
import { BasePage } from './BasePage';
export class MyNewPage extends BasePage {
readonly searchInput: Locator;
readonly searchButton: Locator;
constructor(page: Page) {
super(page);
this.searchInput = this.page.locator('[name="search"]');
this.searchButton = this.page.locator('button[type="submit"]');
}
async search(query: string): Promise<void> {
this.logger.info(`Searching for: ${query}`);
await this.searchInput.fill(query);
await this.searchButton.click();
await this.waitForPageLoad();
}
}// src/fixtures/PageFixtures.ts
import { MyNewPage } from '../pages/MyNewPage';
type PageFixtures = {
// ... existing fixtures
myNewPage: MyNewPage;
};
export const test = base.extend<PageFixtures>({
// ... existing fixtures
myNewPage: async ({ page }, use) => {
const myNewPage = new MyNewPage(page);
await use(myNewPage);
},
});// src/tests/MyNewTest.ts
import { expect } from '@playwright/test';
import { test } from '../fixtures/PageFixtures';
test.describe('My New Test Suite', () => {
test('should search successfully', async ({ myNewPage, page }) => {
await test.step('Navigate and search', async () => {
await page.goto('https://example.com');
await myNewPage.search('playwright');
});
await test.step('Verify results', async () => {
await expect(page).toHaveURL(/search/);
});
});
});npx playwright test --grep="My New Test Suite" --project=local# Run tests in debug mode
npx playwright test --debug
# Run tests with UI mode (interactive)
npx playwright test --ui
# Run a specific test file
npx playwright test src/tests/MyNewTest.ts
# Run tests and update snapshots
npx playwright test --update-snapshots
# Show test report
npx playwright show-report reports/playwright-report
# Generate Allure report
npm run allure:serve- Playwright Test for VSCode - Run and debug tests from IDE
- ESLint - Code linting
- Prettier - Code formatter - Code formatting
# Install VS Code Playwright extension
code --install-extension ms-playwright.playwright- Open test file
- Click the green play button next to test name
- Or use
F5to start debugging
- Read the Architecture Overview to understand the framework design
- Learn about Page Objects for better test organization
- Explore Custom Utilities like Logger and AllureHelper
- Check the Reporting Guide for advanced reporting features
# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install
# Reinstall browsers
npx playwright install --force# Install browser dependencies (Linux)
npx playwright install-deps
# Use specific browser
npx playwright test --project=local --browser=chromiumIf you see port conflicts, check for running Node processes:
# Windows PowerShell
Get-Process node | Stop-Process
# Linux/Mac
pkill -f nodeReady to dive deeper? Continue to the Architecture Overview