UI Mode
Relevant source files
The following files were used as context for generating this wiki page:
- packages/trace-viewer/uiMode.html
- CONTRIBUTING.md
- tests/playwright-test/ui-mode-test-output.spec.ts
- tests/playwright-test/ui-mode-test-source.spec.ts
- tests/playwright-test/ui-mode-test-setup.spec.ts
- tests/playwright-test/ui-mode-test-ct.spec.ts
- tests/playwright-test/test-server.spec.ts
- packages/web/src/components/codeMirrorWrapper.spec.tsx
UI Mode provides an interactive, browser-based experience for running and debugging Playwright tests. It is designed to improve the developer feedback loop by offering features like watch mode, test filtering, and detailed inspection of test execution. It can be launched via the command line using the --ui flag.
An experimental development mode with Hot Module Replacement (HMR) is also available for the UI Mode, which can be enabled by setting the PW_HMR=1 environment variable. This enhances the development experience by allowing live updates to the UI without a full reload.
Sources: CONTRIBUTING.md
Architecture
UI Mode operates on a client-server model. When initiated, the Playwright test runner starts a TestServer which communicates with a web-based frontend over WebSockets. The frontend is a single-page application that renders the test suite, execution results, and debugging tools.
Sources: tests/playwright-test/test-server.spec.ts, packages/trace-viewer/uiMode.html
This diagram illustrates the high-level architecture of UI Mode.
Backend (TestServer)
The backend is managed by the TestServerConnection class, which handles the WebSocket communication between the test runner process and the UI client. It exposes an API for the client to list, run, and watch tests.
Sources: tests/playwright-test/test-server.spec.ts:25-43
The following table summarizes the key methods exposed by the TestServerConnection API, which the UI frontend uses to interact with the test runner.
| Method | Description |
|---|---|
initialize | Initializes the connection, with options like interceptStdio. |
listTests | Requests the list of all available tests from the runner. |
runTests | Executes a specified set of tests. |
watch | Watches specified files for changes and notifies the client. |
startDevServer | Starts the dev server, primarily for component testing. |
stopDevServer | Stops the dev server. |
runGlobalSetup | Executes the global setup defined in the configuration. |
runGlobalTeardown | Executes the global teardown defined in the configuration. |
findRelatedTestFiles | Finds test files that are related to a given set of source files. |
clearCache | Instructs the test runner to clear its internal cache. |
Sources: tests/playwright-test/test-server.spec.ts
The sequence diagram below shows the interaction flow when a user initiates a test run from the UI.
Frontend (Web UI)
The UI is a web application loaded from uiMode.html. It presents a rich interface for test interaction, composed of several key components.
Sources: packages/trace-viewer/uiMode.html
| Component | Description |
|---|---|
| Test Explorer | A tree view that displays all test files, describes blocks, and individual tests. Users can select tests to run, view their source, or watch for changes. |
| Source Viewer | Displays the source code of the selected test file. It highlights the currently running or selected test and can display inline errors for syntax or runtime issues. It uses CodeMirror for rendering. |
| Console/Output | A tabbed view that shows stdout and stderr from the test runner process, as well as console messages from the browser context. |
| Toolbar | Contains controls to run all tests, stop execution, reload the test suite, and toggle watch mode. |
Sources: tests/playwright-test/ui-mode-test-source.spec.ts, tests/playwright-test/ui-mode-test-output.spec.ts, packages/web/src/components/codeMirrorWrapper.spec.tsx
Key Features
Test Execution and Watch Mode
UI Mode allows for running all tests or a filtered subset. The "Watch all" functionality automatically re-runs tests when a test file or a related component file is modified, providing a fast feedback loop during development. This is particularly powerful for component testing, where changes to a component can trigger a re-run of its associated tests.
Sources: tests/playwright-test/ui-mode-test-ct.spec.ts:121-140, CONTRIBUTING.md
Source Code Inspection
Users can click on any test in the test explorer to view its source code. The corresponding test(...) call is highlighted in the source viewer. If there are load-time errors, such as syntax errors or duplicate test titles, they are displayed inline next to the problematic line of code.
Sources: tests/playwright-test/ui-mode-test-source.spec.ts:28-60, 111-127
Console and Output Aggregation
The "Console" tab aggregates output from multiple sources into a single, streamlined view for a selected test run. This includes:
console.logandconsole.errorfrom the Node.js test process.console.logandconsole.errorfrom the browser's page context.- Output from a configured
webServer.
The console supports ANSI color codes for better readability and collapses repeated messages to reduce noise. It also formats objects, dates, and other complex types.
Sources: tests/playwright-test/ui-mode-test-output.spec.ts:60-154, 203-216
Setup and Teardown Support
UI Mode correctly handles and displays globalSetup, globalTeardown, and project-level dependencies (setup/teardown projects). These special tests are visible in the test explorer and can be run and debugged just like regular tests. The UI also provides project filters, allowing users to selectively run or exclude setup/teardown projects from a test run.
Sources: tests/playwright-test/ui-mode-test-setup.spec.ts:20-44, 110-142
Component Testing Integration
UI Mode is fully integrated with Playwright's experimental component testing capabilities. It can start the necessary development server and watch for changes in both test files and the components themselves, providing a seamless experience for developing and testing UI components in isolation.
Sources: tests/playwright-test/ui-mode-test-ct.spec.ts:25-45