Browser Types
Playwright supports three browser engines out of the box:Chromium
Google Chrome and Microsoft Edge
Firefox
Mozilla Firefox
WebKit
Apple Safari
Launching Browsers
Basic Launch
Launch Options
From server/browserType.ts:66, the
launch() method validates options and manages the browser process lifecycle.Browser Class
TheBrowser class (client/browser.ts:31) represents a running browser instance:
Browser Lifecycle
1
Launch
Browser process starts
2
Create Contexts
Multiple isolated sessions
3
Use Browser
Perform automation tasks
4
Close
Clean up resources
Browser Connection State
Browser Properties
Version Information
Browser Type
Multiple Contexts Pattern
Browsers can host multiple independent contexts, which is much more efficient than launching multiple browsers:Persistent Context
Launch a browser with persistent storage (user data directory):Remote Browser Connection
Connect to a browser running on another machine:Browser Server
Start a browser server for remote connections:CDP Session (Chromium Only)
Access Chrome DevTools Protocol directly:Error Handling
Browser Process Management
Playwright automatically manages browser processes:- Process Launch - Starts browser with appropriate flags
- Connection Setup - Establishes WebSocket or pipe communication
- Protocol Initialization - Sets up CDP/Juggler/Inspector protocol
- Context Management - Tracks all browser contexts
- Cleanup - Terminates browser process on close
- Option validation
- Executable path resolution
- Process spawning
- Connection establishment
- Protocol handshake
Best Practices
Reuse Browsers Across Tests
Reuse Browsers Across Tests
Launch once, create multiple contexts. Contexts are lightweight.
Always Close Resources
Always Close Resources
Use try-finally or async disposal to ensure cleanup.
Handle Browser Crashes
Handle Browser Crashes
Listen for disconnection events.
Browser Arguments
Customize browser behavior with command-line arguments:Next Steps
Browser Contexts
Learn about isolated browser sessions
Pages and Frames
Working with tabs and iframes
Test Isolation
Ensuring test independence
