# Playwright Testing Environment

> Modern browser testing with Playwright and trace viewer.

- Category: Development Tools
- Difficulty: intermediate
- Components: playwright, nginx
- Tags: playwright, testing, browser, e2e, microsoft
- Source: https://docker.recipes/development/playwright-testing

## Overview

Playwright is Microsoft's modern end-to-end testing framework that enables reliable testing across Chromium, Firefox, and WebKit browsers with a single API. Released in 2020, it addresses common browser automation challenges with features like auto-wait, network interception, and comprehensive debugging tools including trace generation for test analysis. This deployment creates a complete testing environment with two specialized services: a Playwright test runner container with all browser engines pre-installed, and a dedicated trace viewer service using NGINX to serve test results and debugging traces through a web interface. The configuration is designed for development teams and QA engineers who need a containerized testing environment with visual debugging capabilities. The trace-viewer service provides immediate access to test reports at http://localhost:8080/report and trace files for debugging at http://localhost:8080/traces, making it valuable for teams that need to analyze test failures, share debugging information, or integrate browser testing into CI/CD pipelines with persistent result storage.

## Features

- Multi-browser support with Chromium, Firefox, and WebKit engines pre-installed in a single container
- Web-based trace viewer accessible at port 8080 for visual test debugging and analysis
- Persistent test results and reports storage using Docker volumes for result retention
- Network isolation between Playwright runner and trace viewer for secure test execution
- Ready-to-mount test directory structure with ./tests mapped to container workspace
- NGINX-powered static file serving for fast access to generated HTML reports and trace files
- Keep-alive Playwright container for interactive test development and debugging sessions
- Separate read-only volume mounts for traces and reports to prevent accidental data modification

## Use cases

- Development teams running cross-browser compatibility tests with visual debugging capabilities
- CI/CD pipelines requiring persistent test result storage and web-accessible reporting
- QA engineers analyzing test failures through Playwright's trace viewer interface
- Remote teams sharing test execution traces and reports through centralized web access
- Educational environments teaching browser automation with hands-on trace analysis
- Automated testing workflows that need to archive and review historical test results
- Development workflows requiring isolated browser testing without local browser installations

## Prerequisites

- Docker Engine 20.10+ with support for multi-platform images (Playwright image is Linux-based)
- Minimum 4GB RAM available for Docker (Playwright browsers are memory-intensive during execution)
- Port 8080 available on host system for accessing the trace viewer web interface
- Test files prepared in ./tests directory structure compatible with Playwright test discovery
- Basic understanding of Playwright test syntax and trace file generation commands
- Network connectivity for initial image pulls (Playwright image is approximately 2GB)

## docker-compose.yml

```yaml
services:
  playwright:
    image: mcr.microsoft.com/playwright:latest
    working_dir: /app
    volumes:
      - ./tests:/app
      - playwright_results:/app/test-results
      - playwright_reports:/app/playwright-report
    command: tail -f /dev/null
    networks:
      - playwright_net

  trace-viewer:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - playwright_results:/usr/share/nginx/html/traces:ro
      - playwright_reports:/usr/share/nginx/html/report:ro
    networks:
      - playwright_net

volumes:
  playwright_results:
  playwright_reports:

networks:
  playwright_net:
```

## .env

```bash
# Playwright
# Run tests: docker compose exec playwright npx playwright test
# Reports at http://localhost:8080/report
# Traces at http://localhost:8080/traces
```

## Usage notes

- Run tests in playwright container
- Reports at http://localhost:8080/report
- Trace files at http://localhost:8080/traces
- Chromium, Firefox, WebKit included
- Use npx playwright show-trace for viewer

## Troubleshooting

- Browser crashes with 'GPU process launch failed': Add --disable-gpu and --no-sandbox flags to browser launch options in test configuration
- Trace viewer shows 404 for report files: Ensure tests generate reports with correct output directory matching the mounted volume paths
- Playwright container exits immediately: The tail -f /dev/null command keeps container running - use docker exec to access for test execution
- Cannot access trace viewer at localhost:8080: Check if port 8080 is already in use and verify trace-viewer service started successfully
- Test results not persisting between container restarts: Verify playwright_results and playwright_reports volumes are properly mounted and accessible
- Fonts rendering incorrectly in screenshots: Install additional font packages in Playwright container or use headless mode with font fallbacks

