stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

Allow Stagehand to Attach to an Existing Playwright Page for Proper Video Recording in e2e testing

Open jonathanphilippou opened this issue 10 months ago • 5 comments

Title:

Allow Stagehand to Attach to an Existing Playwright Page for Proper Video Recording

Summary:

When integrating Stagehand with Playwright tests, video recording fails to work as expected. The root cause is that Stagehand’s current initialization (via init() or the deprecated initFromPage()) creates its own browser context and page rather than attaching to the Playwright test runner’s page. Since Playwright’s video recording is configured at the context level (the one that the test runner creates), any actions performed in a separate Stagehand‑created context are not recorded.

What’s Happening:

  • Playwright’s Test Runner:

    • Creates a browser context and page with video recording enabled (per settings like video: "retain-on-failure").
    • Expects to manage the lifecycle (creation/closure) of that context to trigger video finalization.
  • Stagehand’s Current Behavior:

    • When calling init(), Stagehand creates its own browser context and page, independent of the Playwright-managed one.
    • This new context isn’t set up with video recording, so even if it’s closed later, Playwright never records the test actions.
  • Result:

    • Tests that use Stagehand’s methods (like observe(), act(), etc.) run on a page that isn’t being recorded, causing missing or blank video artifacts.

Proposed Solution:

Modify Stagehand to allow its initialization to accept an existing Playwright page. Specifically:

  • Extend the Initialization API:

    • Add an optional parameter (e.g., init({ page: existingPage }) or an attachToPage option) so that if a Playwright page is provided, Stagehand attaches to that page and its context.
  • Behavior in "Attached" Mode:

    • Use the provided Playwright page instead of creating a new one.
    • Set the internal context to providedPage.context().
    • Avoid closing the page or context on teardown—leaving that responsibility to the test runner.
  • Benefits:

    • Ensures that all Stagehand operations occur in the same context that Playwright manages.
    • Preserves Playwright’s video recording, tracing, and other artifact mechanisms.
    • Provides a consistent and reliable test environment for users leveraging Stagehand’s AI features.

Impact:

  • For Users:

    • Tests will now properly record videos when using Stagehand’s methods, making debugging easier.
    • A cleaner integration between Stagehand and the Playwright test runner.
  • For Maintenance:

    • Aligns Stagehand’s lifecycle management with Playwright’s, reducing confusion and potential side effects.
    • Allows continued use of AI-driven automation without sacrificing the rich artifact collection provided by Playwright.

Next Steps:

  • Implement the API change in Stagehand’s initialization code.
  • Add tests to ensure that when a page is attached, video recording works as expected.
  • Document the new usage pattern for contributors and users.

jonathanphilippou avatar Feb 20 '25 20:02 jonathanphilippou

I just wanted to +1 this issue. I was hoping to utilize stagehand to write/replace normal E2E playwright tests, but it appears the integration with the test runner context is a little awkward.

meddy avatar Feb 24 '25 23:02 meddy

+1 to this

hailee0710 avatar Mar 07 '25 11:03 hailee0710

+1 on this. We are exploring Stagehand and this is exactly whats turning us off currently

sannajammeh avatar Mar 07 '25 14:03 sannajammeh

+1 to this

cadeParade avatar Apr 01 '25 20:04 cadeParade

Looks like it previously had this feature, but was removed in v2 in favor of requiring a browserbase session id... Link

cadeParade avatar Apr 01 '25 20:04 cadeParade

+1 on this. may websites opens things in popups.

valllabh avatar Jul 02 '25 08:07 valllabh

Just bumping this yet again, this would be insanely useful

harrismcc avatar Jul 15 '25 21:07 harrismcc

+1

z0gSh1u avatar Jul 22 '25 11:07 z0gSh1u

Is there anyone working on this now?

4t145 avatar Jul 28 '25 03:07 4t145

+1

eraserx99 avatar Sep 03 '25 04:09 eraserx99

+1

PavelGlebovAndritz avatar Sep 16 '25 15:09 PavelGlebovAndritz