BrowserManager
TheBrowserManager class manages the Playwright browser lifecycle with support for multiple tabs/windows, element refs, snapshots, and advanced browser automation features.
Constructor
Launch & Connection
launch()
Launches a browser instance or connects to an existing one.Launch configuration options
Run browser in headless mode (default: true)
Browser engine to launch (default: ‘chromium’)
Initial viewport size. Set to null to disable viewport emulation
Connect to Chrome DevTools Protocol on this port
Connect to CDP via WebSocket URL (ws:// or wss://)
Auto-discover and connect to running Chrome instance
Cloud browser provider
Chrome extension paths to load (Chromium only)
Path to persistent browser profile directory
Path to storage state JSON file for session persistence
Extra HTTP headers to send with every request
Custom user agent string
Persistent color scheme preference
Directory for browser downloads
Domain allowlist for navigation (blocks other domains)
Enable file:// URL access (Chromium only)
Ignore HTTPS certificate errors
Additional browser launch arguments
isLaunched()
Check if the browser is currently launched.True if browser is launched
close()
Close the browser and cleanup resources.Page & Frame Management
getPage()
Get the current active page. Throws if browser is not launched.The active Playwright Page instance
getPages()
Get all open pages.Array of all Page instances
hasPages()
Check if the browser has any usable pages.True if pages exist
ensurePage()
Ensure at least one page exists. Creates a new page if all were closed.getFrame()
Get the current frame (or page’s main frame if no frame is selected).Current Frame instance
switchToFrame()
Switch to a frame by selector, name, or URL.switchToMainFrame()
Switch back to the main frame.Snapshots & Element Refs
getSnapshot()
Get an enhanced accessibility snapshot with element refs.Only include interactive elements (buttons, links, inputs)
Include cursor-interactive elements (cursor:pointer, onclick)
Maximum depth of tree to include
Remove structural elements without meaningful content
CSS selector to scope the snapshot
getRefMap()
Get the cached ref map from the last snapshot.Ref map with element locator information
getLocatorFromRef()
Get a Playwright locator from a ref (e.g., “e1”, “@e1”, “ref=e1”).Element ref string (e1, @e1, or ref=e1)
Playwright Locator or null if ref doesn’t exist
getLocator()
Get a locator - supports both refs and regular selectors.Element ref or CSS selector
Playwright Locator instance
isRef()
Check if a selector looks like a ref.Selector string to check
True if string is a ref format
getLastSnapshot()
Get the last snapshot tree text (empty string if no snapshot has been taken).Last snapshot tree text
setLastSnapshot()
Update the stored snapshot (used by diff to keep the baseline current).Snapshot tree text to store
Browser Context & Settings
getContext()
Get the current browser context.Playwright BrowserContext or null
getBrowser()
Get the current browser instance.Playwright Browser instance or null
getActiveIndex()
Get the current active page index.Zero-based index of active page
setColorScheme()
Set the persistent color scheme preference. Applied to all new pages and contexts.Color scheme to apply
setViewport()
Set the viewport size.Viewport width in pixels
Viewport height in pixels
setDeviceScaleFactor()
Set device scale factor (devicePixelRatio) via CDP.Device pixel ratio (e.g., 2 for Retina)
Viewport width
Viewport height
Enable mobile emulation (default: false)
clearDeviceMetricsOverride()
Clear device metrics override to restore default devicePixelRatio.getDevice()
Get device descriptor by name.Device name (e.g., “iPhone 13 Pro”)
Playwright device descriptor or undefined
listDevices()
List all available device names.Array of device names
Dialogs & Handlers
setDialogHandler()
Set up automatic dialog (alert/confirm/prompt) handler.How to respond to dialogs
Text to enter for prompt dialogs (when accepting)
clearDialogHandler()
Remove the dialog handler.Request Tracking & Routing
startRequestTracking()
Start tracking all network requests.getRequests()
Get tracked requests, optionally filtered.Optional URL substring filter
Array of tracked request data
clearRequests()
Clear all tracked requests.addRoute()
Add a route to intercept and mock requests.URL pattern to intercept
removeRoute()
Remove a route by URL pattern (or all routes if no URL provided).Optional URL pattern to remove (omit to remove all)
Headers
setExtraHeaders()
Set extra HTTP headers for all requests.Headers to set
setScopedHeaders()
Set headers only for requests matching an origin.Origin hostname or URL
Headers to add for matching requests
clearScopedHeaders()
Clear scoped headers for an origin (or all if no origin specified).Optional origin to clear (omit to clear all)
Geolocation & Permissions
setGeolocation()
Set geolocation coordinates.Latitude coordinate
Longitude coordinate
Optional accuracy in meters
setPermissions()
Grant or deny browser permissions.Permission names to grant/deny
True to grant, false to revoke
setOffline()
Set offline mode.Enable/disable offline mode
Console & Error Tracking
startConsoleTracking()
Start tracking console messages.getConsoleMessages()
Get all tracked console messages.Array of console messages with type, text, and timestamp
clearConsoleMessages()
Clear all tracked console messages.startErrorTracking()
Start tracking page errors.getPageErrors()
Get all tracked page errors.Array of page errors with message and timestamp
clearPageErrors()
Clear all tracked page errors.Recording & Tracing
startHarRecording()
Start HAR (HTTP Archive) recording.isHarRecording()
Check if HAR recording is active.True if recording
startTracing()
Start Playwright tracing.stopTracing()
Stop tracing and save to file.Optional output path for trace file
Storage State
saveStorageState()
Save storage state (cookies, localStorage) to file.Output file path
getAndClearWarnings()
Get and clear launch warnings (e.g., decryption failures).Array of warning messages
Domain Filtering
checkDomainAllowed()
Check if a URL is allowed by the domain allowlist. Throws if blocked.URL to check
Utility Functions
getDefaultTimeout()
Get the default Playwright timeout in milliseconds.Timeout in milliseconds (configurable via AGENT_BROWSER_DEFAULT_TIMEOUT env var)