Skip to main content
When you execute code with Playwriter, the following variables are available in the execution context:

state

Persistent object for storing data between execute calls within your session. Each session has its own isolated state. Type: Record<string, any> Availability: All sessions Example:
// Store page in state
state.page = await context.newPage()
await state.page.goto('https://example.com')

// Access in next execute call
await state.page.click('button')

// Store any data
state.userData = { name: 'John', email: '[email protected]' }
state.requestData = []
Important: state is session-isolated but pages are shared across all sessions. Multiple sessions can see the same browser tabs. To avoid interference, always use your own page.

page

Default page (may be shared with other agents). Prefer creating your own page and storing it in state. Type: Page Availability: All sessions (shared) Example:
// Get or create your own page (recommended)
state.page = context.pages().find(p => p.url() === 'about:blank') ?? await context.newPage()
await state.page.goto('https://example.com')

// Using default page (not recommended - may be shared)
await page.goto('https://example.com')

context

Browser context - access to all pages, create new pages, set default timeouts. Type: BrowserContext Availability: All sessions Example:
// Create new page
const newPage = await context.newPage()

// List all available pages
const pages = context.pages()
console.log(pages.map(p => p.url()))

// Set timeouts
context.setDefaultTimeout(5000)
context.setDefaultNavigationTimeout(30000)

require

Load Node.js built-in modules. ESM import is not available in the sandbox. Type: NodeRequire Availability: All sessions Allowed modules:
  • File system: fs, node:fs
  • Path utilities: path, node:path
  • URL parsing: url, node:url, querystring, node:querystring
  • Crypto: crypto, node:crypto
  • Data: buffer, node:buffer, string_decoder, node:string_decoder
  • Utilities: util, node:util, assert, node:assert
  • Events: events, node:events, timers, node:timers
  • Streams: stream, node:stream, zlib, node:zlib
  • HTTP: http, node:http, https, node:https, http2, node:http2
  • System: os, node:os
Example:
// Read/write files
const fs = require('node:fs')
const content = fs.readFileSync('./data.json', 'utf-8')
fs.writeFileSync('./output.txt', 'result')

// Path operations
const path = require('node:path')
const filePath = path.join(__dirname, 'file.txt')

// Crypto
const crypto = require('node:crypto')
const hash = crypto.createHash('sha256').update('data').digest('hex')

Other Globals

Standard Node.js globals are also available:
// Timers
setTimeout(() => console.log('delayed'), 1000)
setInterval(() => console.log('tick'), 1000)
clearTimeout(timerId)
clearInterval(intervalId)

// Fetch API
const response = await fetch('https://api.example.com/data')
const data = await response.json()

// URL
const url = new URL('/path', 'https://example.com')
const params = new URLSearchParams({ key: 'value' })

// Buffers and encoding
const buffer = Buffer.from('text')
const encoder = new TextEncoder()
const decoder = new TextDecoder()

// Other
crypto.randomUUID()
structuredClone(obj)
AbortController
AbortSignal

Build docs developers (and LLMs) love