Skip to content

run-code command fails with "page is undefined" in --extension mode #308

@gslvly

Description

@gslvly

Description

When using playwright-cli open --extension, the run-code command fails with page is undefined error.

Reproduction Steps

  1. Run: playwright-cli open --extension
  2. Select a tab in the browser (with existing logged-in session)
  3. Run: playwright-cli run-code "async page => { await page.waitForLoadState('networkidle'); return await page.evaluate(() => document.body.innerText); }"

Expected Behavior

The page parameter should be available in the callback function, allowing Playwright API calls like page.waitForLoadState(), page.evaluate(), etc.

Actual Behavior

Error: page is undefined

Root Cause Analysis

Looking at the source code:

  1. In lib/mcp/browser/tools/runCode.js:56-58:
const context = {
  page: tab.page,
  __end__
};
  1. tab.page comes from Tab class initialization in lib/mcp/browser/tab.js:49

  2. In --extension mode, the browser context is created via ExtensionContextFactory (see lib/mcp/program.js:64-70)

  3. The Tab object may not be properly initialized via _onPageCreated callback when connecting to an existing browser tab through the extension.

Impact

This prevents users from:

  • Using run-code to execute Playwright API calls in extension mode
  • Waiting for page states (e.g., waitForLoadState('networkidle'))
  • Executing complex page evaluations

Workaround

  • Use normal mode without --extension flag
  • Use state-save/state-load to transfer authentication state between sessions

Environment

  • playwright-cli version: 0.1.1
  • playwright version: 1.59.0-alpha
  • Node.js: v22.20.0
  • OS: macOS (darwin)

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions