Skip to content
YosoiDocs

Python browser workflows

The shared Flow guide explains recipes and invocation. This existing URL collects complete Python programs for the same supported tasks.

This program owns its temporary Session and Work. Configure stop behavior so a failed prerequisite prevents dependent actions.

import asyncio
import yosoi as ys

async def main():
    recipe = ys.flow([
        ys.navigate("https://example.org/"),
        ys.assert_that(ys.exists("h1")),
    ], on_failure=ys.policy.Stop(scope="invocation"))
    recipe.to_json()
    outcome = await recipe.invoke()
    outcome.require_completed()

asyncio.run(main())

The outer context owns the Session. The inner context owns tab control. Invocation borrows that control, and context exit attempts cleanup on success or failure.

import asyncio
import yosoi as ys

async def main():
    recipe = ys.flow([ys.navigate("https://example.org/")])
    async with ys.session() as session:
        async with session.work() as work:
            outcome = await recipe.invoke(work)
            outcome.require_completed()

asyncio.run(main())

Managed JavaScript evaluation requires explicit permission. A borrowed invocation retains its existing Session’s authority.

import asyncio
import yosoi as ys

async def main():
    recipe = ys.flow([
        ys.navigate("https://example.org/"),
        # The expression runs in the browser, where document is available.
        ys.evaluate("(() => { document.body.dataset.reviewed = 'yes'; return { reviewed: true }; })()"),
        ys.assert_that(ys.exists("body[data-reviewed='yes']")),
    ], on_failure=ys.policy.Stop(scope="invocation"))

    # Permit JavaScript explicitly for the managed Session.
    outcome = await recipe.allow_javascript().invoke()
    outcome.require_completed()

asyncio.run(main())

See Actions and Flows for how recipes are authored and invoked.

Use the typed assertion programs to check a current observation. Use bounded Until when the task needs to wait for supported evidence. Predicates are synchronous and must return promptly.

Pass a caller-owned cancellation token when another task decides when invocation should stop. Cancellation preserves execution facts and does not undo dispatched effects. Retain unknown receipts and inspect domain state before repeating a potentially irreversible operation.

The Python bridge exposes the initial tab and borrowed Work. Rust’s explicit tab creation and Watcher stream remain unavailable. Archive, AutomationRecord and bounded offline View inspection are available; see Keeping the evidence.