brawsrdocsv0.6
Examples

Recover from a bad step · Python

Run the rewind and reconnection story with the synchronous Python SDK and Playwright.

This is the same recovery story as the TypeScript example: preserve a known good browser state, make a later change, rewind, and prove the captured value returned. The Python SDK uses snake-case method names and the synchronous Playwright API, but the lifecycle is identical.

1. Prepare, then checkpoint

The application chooses a safe point between actions. Here, the marker stands in for a loaded page, filled form, authenticated session, or other browser state your workflow does not want to rebuild.

# A checkpoint is an application-chosen safe point. Prepare the
# browser state first, then capture it between actions.
page.goto(url)
page.evaluate(
    "([key, value]) => localStorage.setItem(key, value)",
    [MARKER_KEY, "checkpointed"],
)
checkpoint = brawsr.create_checkpoint(session.id, label="before-edit")

2. Discard a later edit

rewind accepts the returned checkpoint directly. The operation ID is retained because the workflow will later close the same session.

# This later state is intentionally discarded by rewind.
page.evaluate(
    "([key, value]) => localStorage.setItem(key, value)",
    [MARKER_KEY, "edited-after-checkpoint"],
)

restored = brawsr.rewind(session.id, checkpoint)
rewind_operation_id = restored.operation_id

3. Treat old handles as stale

The restored browser is a new process. Connect from the result, rediscover its page, and verify the value from the safe point.

# Rewind replaces the browser process. Every old Playwright handle
# is stale, so reconnect and rediscover the page.
restored_connection = brawsr.connect_cdp(restored)
restored_browser = playwright.chromium.connect_over_cdp(
    restored_connection.endpoint_url,
    headers=dict(restored_connection.headers),
)
restored_page = restored_browser.contexts[0].pages[0]
value = restored_page.evaluate("key => localStorage.getItem(key)", MARKER_KEY)
if value != "checkpointed":
    raise RuntimeError("rewind lost browser state")

4. Wait before close

CDP work can resume from the rewind result immediately. Close is a lifecycle mutation, so the example waits for terminal rewind completion first.

# The early rewind result is already CDP-usable. Wait before close,
# which is another lifecycle mutation on the same session.
try:
    if rewind_operation_id is not None:
        brawsr.wait_rewind(rewind_operation_id)
finally:
    brawsr.close_session(session.id)

Successful output contains "rewound": true and "value": "checkpointed".

View the complete Python example (opens in a new tab)

On this page