Automated end-to-end testing suites using Playwright.
This guide will help you:
- Understand the Framework - Learn the framework architecture and file organization
- Setup the Framework - Install and configure the testing environment
- Run Tests - Execute tests in various modes
- Write Tests - Create new test cases using best practices
- Generate Reports - View and analyze test results
- Troubleshoot Issues - Fix common problems
- Node.js (v18 or higher)
- npm package manager
- Clone the repository
git clone [email protected]:fleks-dev/saucedemo.git
- Install dependencies
npm install
This will automatically install Playwright and all browser engines.
- Verify installation
npx playwright --version
saucedemo-playwright/
โโโ .github/ # GitHub Actions workflows
โ โโโ workflows/
โ โโโ manual-trigger.yml # Trigger any test suite manually
โ โโโ smoke.yml # Run pre-defined set of smoke tests
โ โโโ test-orchestration.yml # Workflow orchestrator
โโโ configs/ # Playwright configuration files
โ โโโ [config-name].config.ts # Team-specific test configs
โโโ fixtures/ # Test data and files
โ โโโ users.json
โโโ src/ # Source code
โ โโโ pages/ # Page Object Model classes
โ โโโ helpers/ # Utility functions
โ โโโ constants.ts
โโโ tests/ # Test files
โ โโโ base-test.ts # Base test configuration
โ โโโ global-setup.ts # Global test setup
โ โโโ global-teardown.ts # Global test cleanup
โ โโโ feature-[name]/ # Feature-specific test suites
โ โโโ [test-suite]/
โโโ playwright.config.ts # Main Playwright configuration
โโโ package.json
| Command | Description |
|---|---|
npm run test |
Run all tests in headless mode |
npm run test:headed |
Run all tests with browser UI visible |
npm run test:debug |
Run tests in debug mode |
npm run test:ui |
Run tests with Playwright's interactive UI |
Run tests using specific configurations or test paths:
# Run specific config file CONFIG_NAME=configName npm run test:config CONFIG_NAME=configName npm run test:config:headed # Run specific test suite SUITE_PATH=feature-name/test-suite npm run test:suite SUITE_PATH=feature-name/test-suite npm run test:suite:headed
By default, all tests run only on desktop browsers (chromium) to ensure fast test execution. Mobile device testing is opt-in.
To run tests on mobile device emulators:
# Enable all mobile devices for test run INCLUDE_MOBILE=true npx playwright test # Run all tests on a specific mobile device (headless mode) npx playwright test --project="Mobile Safari" npx playwright test --project="iPhone 13 Pro" npx playwright test --project="Mobile Chrome" npx playwright test --project="Pixel 7" # Run specific test file on mobile device with visible browser (headed mode) npx playwright test [relative path to test file] --project="Mobile Safari" --headed # Run tests on multiple mobile devices simultaneously npx playwright test --project="Mobile Safari" --project="Mobile Chrome" # Run specific test in debug mode on mobile device npx playwright test [relative path to the test file] --project="Mobile Safari" --debug # Run with slower execution speed for easier viewing (1000ms delay between actions) npx playwright test [relative path to the test file] --project="Mobile Safari" --headed --slow-mo=1000
| Device Name | Platform | Viewport Size | Description |
|---|---|---|---|
| Mobile Safari | iOS (iPhone) | 390ร844 | iPhone 13 |
| iPhone 13 Pro | iOS (iPhone) | 390ร844 | iPhone 13 Pro |
| Mobile Chrome | Android | 393ร851 | Pixel 5 |
| Pixel 7 | Android | 412ร915 | Pixel 7 |
Mobile device testing includes:
- Correct viewport dimensions
- Touch event support
- Mobile-specific user agents
- Device pixel ratio emulation
- Mobile Safari/Chrome rendering engines
For local development, create a .env file
cp .env.template .env
Set these environment variables to customize test behavior:
INCLUDE_MOBILE=true # Include mobile browsers in test runs (default: false) SUITE_PATH=feature-name/suite-name # Specify test suite path for test:suite commands
The playwright.config.ts automatically loads environment variables from .env using dotenv.
Desktop-First Approach: By default, all tests run only on desktop browsers (chromium) for faster test execution. This applies globally to all test files without any per-file configuration.
- Default behavior: Tests run on
chromium(desktop) only - Enable mobile: Set
INCLUDE_MOBILE=trueto include all mobile browser projects - Specific device: Use
--projectflag to run on a specific mobile device
Example:
# Default: Desktop only (fast) npx playwright test # Include all mobile devices INCLUDE_MOBILE=true npx playwright test # Specific mobile device npx playwright test --project="iPhone 13 Pro"
Reports are automatically generated after running tests. When you execute any test command (npm run test, npm run test:headed, etc.), Playwright generates multiple report formats:
- HTML Report:
playwright-report/- Interactive visual report - JUnit XML:
playwright-reports/junit-results.xml- For CI/CD integration - JSON:
playwright-reports/test-results.json- Machine-readable results - GitHub Actions: Automatic annotations in pull requests when running on CI
After running tests, view the interactive HTML report:
npm run show-report
This opens a browser with detailed test results, including:
- Test execution timeline
- Screenshots of failures
- Video recordings (if enabled)
- Trace viewer for debugging
npm run clean
Removes:
test-results/- Screenshots, videos, tracesplaywright-report/- HTML reportsplaywright-reports/- JUnit XML and JSON result filesplaywright-test-results/- Additional test result files
This project uses GitHub Actions for Continuous Integration and Continuous Deployment. The workflows are defined in .github/workflows/.
Allows you to manually trigger any test suite from the GitHub Actions UI.
- Trigger: Manual (
workflow_dispatch) - Inputs:
test_configs: Test suites to run (e.g.,"product-page","cart"). Default:"checkout".baseURL: Base URL to test against. Default:https://www.saucedemo.com.runner: GitHub runner to use. Default:ubuntu-latest.
Runs a critical set of tests to ensure the platform's health.
- Trigger:
- Scheduled: Every weekday at 8:00 AM.
- Manual: Can be triggered manually.
- Tests: Runs
checkout,authentication, andcartsuites.
A reusable workflow that handles the actual test execution logic. It is called by other workflows (like Manual Trigger and Smoke Tests).
Key Features:
- Setup: Installs Node.js and dependencies.
- Caching: Caches Playwright browsers to speed up execution.
- Execution: Runs tests based on inputs (suites, patterns, grep).
- Reporting: Checks for blob reports, combines them, and deploys the final HTML report.
The workflows utilize custom composite actions located in .github/actions/:
combine-reports: Merges blob reports from sharded test runs into a single report.deploy-reports: Deploys the generated HTML report to the saucedemo-reports repository and updates its README with the 10 latest test reports.
The reports are available at the github page saucedemo-reports.
Follow these steps to create a new test:
Create your test file in the appropriate feature directory:
tests/feature-[name]/[test-suite]/yourTest.spec.ts
import { test, expect } from '@playwright/test'; test.describe('Feature Name', () => { test('should perform specific action', async ({ page }) => { // Navigate to page await page.goto('/your-page'); // Perform actions await page.click('button#submit'); // Verify results await expect(page.locator('.success-message')).toBeVisible(); }); });
For better maintainability, create page objects in src/pages/:
Create Page Object (src/pages/LoginPage.ts):
import { Page } from '@playwright/test'; export class LoginPage { constructor(private page: Page) {} async goto() { await this.page.goto('/login'); } async login(email: string, password: string) { await this.page.fill('input[name="email"]', email); await this.page.fill('input[name="password"]', password); await this.page.click('button[type="submit"]'); } }
- Page Objects: Create reusable page classes in
src/pages/ - Test Data: Store test fixtures in
fixtures/ - Utilities: Add helper functions in
src/helpers/ - Naming: Use descriptive names:
featureName.spec.ts
When running tests in UI mode (npx playwright test --ui), you can change the baseURL by setting the BASE_URL environment variable before running the command:
BASE_URL=http://localhost:5173 npx playwright test --uiThis overrides the default baseURL defined in playwright.config.ts.
Use debug mode to step through tests:
npm run test:debug
This opens a browser with developer tools where you can:
- Step through test actions
- Inspect elements
- View console logs
- Take screenshots
You can run tests in a Docker container to ensure a consistent environment.
- Docker installed on your machine.
Build the Docker image using the provided Dockerfile. This will install all dependencies and browsers required by Playwright.
docker build -t saucedemo-playwright .Run the tests inside the container. To access the test reports generated inside the container, you need to mount a volume mapping the container's report directory to a local directory.
PowerShell (Windows):
docker run --rm -v ${PWD}/playwright-report:/app/playwright-report saucedemo-playwright
Bash (Linux/macOS):
docker run --rm -v $(pwd)/playwright-report:/app/playwright-report saucedemo-playwrightCommand Prompt (Windows):
docker run --rm -v %cd%/playwright-report:/app/playwright-report saucedemo-playwrightAfter the tests complete, you can find the HTML report in your local playwright-report directory.
The Dockerfile performs the following steps:
- Base Image: Uses
mcr.microsoft.com/playwright:v1.40.0-jammywhich includes Node.js and Playwright browsers. - Dependencies: Copies
package.jsonand runsnpm cito clean install dependencies. - Source Code: Copies the project files into the
/appdirectory. - Execution: Runs
npm testby default.
To reference specific files in the container or debug, you can override the default command:
docker run --rm -it saucedemo-playwright /bin/bash