Workflow:Microsoft Playwright Browser automation CLI
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, CLI_Tooling, Web_Scraping |
| Last Updated | 2026-02-11 22:00 GMT |
Overview
End-to-end process for performing one-off browser automation tasks (screenshots, PDFs, interactive browsing) using Playwright's command-line interface without writing test files.
Description
This workflow covers Playwright's CLI commands for ad-hoc browser automation tasks. The CLI provides commands to open URLs in specific browsers, capture screenshots, generate PDFs, and launch interactive browsing sessions with device emulation. These commands operate without requiring a test project, making them suitable for quick automation tasks, visual verification, and scripting workflows. The CLI supports all three browser engines (Chromium, Firefox, WebKit) and extensive configuration options for viewport, geolocation, color scheme, and authentication state.
Usage
Execute this workflow when you need to perform a quick browser task without setting up a full test project: capturing a screenshot of a URL, generating a PDF, testing a page under specific device emulation, or interactively debugging a web application in a specific browser engine.
Execution Steps
Step 1: Install browsers
Ensure browser binaries are installed using npx playwright install. This downloads the required browser executables (Chromium, Firefox, WebKit) that the CLI commands will use. Optionally install only specific browsers or install system-level OS dependencies.
Key considerations:
- Use npx playwright install chromium to install only Chromium
- Use npx playwright install-deps to install OS-level dependencies on Linux
- Browser binaries are cached and shared across projects
- Use --with-deps to combine browser and dependency installation
Step 2: Select browser and configure context
Choose the target browser engine and configure the browser context options via CLI flags. Options include device emulation, viewport size, color scheme, geolocation, locale, timezone, and user agent.
Key considerations:
- Use --browser=chromium|firefox|webkit or shorthand cr|ff|wk
- Use --device="iPhone 13 Pro" for mobile emulation
- Use --color-scheme=dark for dark mode testing
- Use --viewport-size=1280,720 for custom viewport
- Use --timezone and --lang for locale testing
Step 3: Execute automation command
Run the desired automation command. Playwright provides several built-in commands for common tasks:
Open a URL interactively:
- npx playwright open [url] opens a browser with the Playwright Inspector attached
Capture a screenshot:
- npx playwright screenshot [url] [output-file] navigates to the URL and saves a screenshot
- Use --full-page for a full-page screenshot
Generate a PDF:
- npx playwright pdf [url] [output-file] navigates to the URL and saves as PDF (Chromium only)
Key considerations:
- Screenshots support PNG and JPEG formats
- PDF generation works only with Chromium
- The --wait-for-selector and --wait-for-timeout flags control when the capture occurs
- Use --save-storage and --load-storage to persist and restore authentication state
Step 4: Save and load authentication state
Optionally save the browser's authentication state (cookies, local storage) after an interactive session for reuse in subsequent automation tasks. This avoids repeating login flows across multiple CLI invocations.
Key considerations:
- Use --save-storage=auth.json to save state after an interactive session
- Use --load-storage=auth.json to restore state in subsequent commands
- Storage state includes cookies and local storage for all origins
- This is useful for scripting authenticated screenshot or PDF capture workflows