Playwright Test Runner
The Playwright Test Runner is a powerful tool for managing and executing your end-to-end tests.
The Playwright Test Runner is a built-in framework that helps you write, organize, and execute automated tests efficiently. It provides features such as test organization, assertions, parallel execution, retries, reporting, and test hooks.
Unlike older tools where you need multiple libraries, Playwright already includes a powerful testing framework.
Key Features:
- Built-in test runner
- Automatic waiting
- Parallel test execution
- Test retries
- HTML reporting
- Screenshots and traces on failure
Getting Started
To get started with the Playwright Test Runner, you'll need to install the necessary dependencies and configure your test environment.
npm install @playwright/testRunning Tests
Once you have your tests set up, you can run them using the Playwright Test Runner:
npx playwright testTest Structure
A Playwright test is written using the test() function. Each test represents a scenario or functionality being validated.
import { test, expect } from '@playwright/test';
test('Verify homepage title', async ({ page }) => {
await page.goto('https://example.com');
const title = await page.title();
expect(title).toBe('Example Domain');
});Explanation:
- import { test, expect } → Import Playwright testing utilities
- test() → Defines a test case
- async ({ page }) → Provides browser page fixture
- page.goto() → Navigates to a URL
- expect() → Used to validate results
Each test should focus on one functionality only to keep tests clean and maintainable.
test and expect Basics:
1. test()
The test() function defines a test case.
Syntax:
test('test name', async ({ page }) => {
// Test code here
});Example:
test('Login page should open successfully', async ({ page }) => {
await page.goto('https://example.com/login');
});It takes a test name and an asynchronous function that contains the test logic.
1. expect()
The expect() function is used for assertions. It verifies whether the application behaves as expected.
test('Check page title', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle('Example Domain');
});If the expected result does not match the actual result, the test will fail.
Hooks in Playwright:
Hooks are used to run specific code before or after tests. Playwright provides hooks to set up and tear down test environments.
Common hooks include:
beforeAll()afterAll()beforeEach()afterEach()
1. beforeAll()
The beforeAll() hook runs once before all tests in the file.
test.beforeAll(async () => {
console.log('Starting test suite');
});Use cases:
- Initialize test data
- Setup environment
- Launch services
2. afterAll()
The afterAll() hook runs once after all tests in the file have completed.
test.afterAll(async () => {
console.log('Test suite completed');
});Use cases:
- Cleanup test data
- Teardown environment
- Stop services
3. beforeEach()
The beforeEach() hook runs before each test in the file.
test.beforeEach(async ({ page }) => {
await page.goto('https://example.com/login');
});Use cases:
- Opening application
- Login steps
- Reset state before each test
4. afterEach()
The afterEach() hook runs after each test in the file.
test.afterEach(async () => {
console.log('Test completed');
});Use cases:
- Logging results
- Cleanup tasks
- Closing resources
Running Single or Multiple Tests:
Playwright provides several ways to execute tests.
1. Run All Tests
npx playwright testThis command runs all tests inside the tests folder.
2. Run Specific File
npx playwright test login.spec.tsThis runs tests only from that file.
3. Run Specific Test
test.only('Run this test only', async ({ page }) => {
// Test code here
});This runs a specific test within a file using .only().
4. Skip Tests
test.skip('Skip this test', async ({ page }) => {
// Test code here
});This allows you to skip tests based on a condition.
5. Run in Debug Mode
npx playwright test --debugThis runs tests in debug mode, allowing you to inspect the browser and test execution.
Parallel Test Execution:
One of the biggest advantages of Playwright is parallel test execution.
Tests can run simultaneously across multiple browsers and workers, which significantly reduces execution time.
Example Configuration
In playwright.config.ts:
export default defineConfig({
workers: 4
});This configuration allows up to 4 tests to run in parallel or simultaneously.
Playwright automatically manages test distribution across workers and browsers.
Benefits
- Faster test execution
- Efficient CI/CD pipelines
- Scalable testing for large suites