Flows
A flow is a path through your Blocks, written with engine.map(). TypeScript checks that each step starts where the last one ended, so a flow can't skip from one page to an unrelated one. The chain is the flow: there is no closing call, and whatever the chain holds when you export it, runs.
import { engine } from "#config/waygraph.config";
import { HomePage } from "#map/(app_base)/home/_page.block";
import { HomeRead } from "#map/(app_base)/home/_reads";
export const homeCartFlow = engine
.map()
.gotoPage(HomePage)
.method(HomePage.do.addItem, { expect: [HomeRead.cartCount.is(1)] })
.method(HomePage.do.clearCart);A flow imports one thing per page it visits: the page's Page Block, whose nav opens it, whose do holds what you can do there and whose expects holds what you can check there (Blocks). #config/ and #map/ are the project's own short paths (Project).
The steps
Each step takes the kind of Block its name says. Hand it anything else, or an object that only looks like a Block, and it throws, naming the factory to use.
| Step | Takes |
|---|---|
.gotoPage(b) | a Nav or Page Block on your app |
.gotoExternal(b) | a Nav Block on another site (a mail catcher, a REST client) |
.method(b) | a Method or API Block |
.set(key, value) | sets a mem key for the steps after it: a value, or one worked out from mem |
.steps(part) | a part made with steps(): the same steps in several flows |
.flow(sub, { reset, expect }) | another flow's steps, in the same page and mem; expect is checked once it's done |
.branch({ Tag: (m) => … }) | a different continuation for each place the last step can land |
map({ given: [b] }) | preconditions: seed what the scenario needs before step 1; undo puts it back |
.finally(b) | a cleanup step: runs after the others, pass or fail |
Step options
The second argument tunes one step:
| Option | Does | Example |
|---|---|---|
expect | what the scenario expects after this step (below) | { expect: [ExpectSavedToastBlock, Machine.status.is("Saved")] } |
lands | the outcome this flow is about, when a Block can land in several | .method(SubmitLoginBlock, { lands: InventoryPage }) |
settle | waits for a page that keeps changing (a debounced search) | { settle: true } or { settle: { quietMs: 600, maxMs: 8000 } } |
when | plays the step only if it returns true (an optional field) | { when: (mem) => mem.get(Case.body) !== undefined } |
pace | demo speed for this step | { pace: "blitz" } |
ff | fast-forwards this one step in the demo; for several, .ffStart() … .ffEnd() | { ff: true } |
A run where the Block lands somewhere other than lands fails at that step: "submit-login" landed on LoginPage, but this flow continues from InventoryPage.
Several Methods of one page: .on()
.on(Page, (p) => …) writes the Methods of the page the flow is on as one call. It is the same flat flow: each call is its own step, with its own name in the run log, and says its own options and narration right where it is written:
engine.map()
.gotoPage(Lines)
.on(Lines, (p) => p
.typeSearch("<b>x</b>").with({ settle: true, expect: [Lines.check("only marked", ({ rows }) => rows.every((r) => r.name.includes("<b>x</b>")))] })
.stub((ctx) => ctx.ui.caption("The markup is shown as text"))
.submit())
.gotoPage(Detail);The chain offers the page's Methods that start and end on it (autocomplete lists exactly those; a Method with params takes them in the call). .with({ expect, settle, pace, when, ff }) and .stubBefore/.stub/.stubOnError say something about the Method just before them. A Method that goes to another page is a .method(...) after the chain. .flow(sub) episodes, .in(pane) and every other step are unchanged.
What the scenario expects: expect
After a step, the flow says what the scenario expects: Expect Blocks, Traits, and what the page shows (Key.is(value), Page.check(why, test), read through the page's read):
engine.map({ as: Planner })
.gotoPage(Machine, { expect: [Machine.expects.header] })
.method(Machine.do.confirmArchive, {
expect: [
ExpectArchivedToastBlock, // named, reusable
Machine.status.is("Archived"), // equal (deep, for lists and objects)
Machine.check("the machine we archived", ({ code }, mem) => code === mem.get(Keys.machine).code),
],
});They're added to what the Block itself expects, run together right after its verify, never change the page, and a failing step lists every one that didn't hold (Checks). { expect: false } drops the Block's own for one step; map({ expect: false }) or --no-expect runs only verify.
One Method, several inputs: params, or .set()
A Method takes what the scenario chooses as a param, written on the step: typed, inline, and no mem key to declare:
// in the page: do: (self) => ({ fillSearch: self.method((text: string) => ({ async act(p) { await p.locator(Sel.search).fill(text); } })) })
engine.map()
.gotoPage(RegistryPage)
.method(RegistryPage.do.fillSearch("<b>x</b>"))
.method(RegistryPage.do.fillSearch("MCH-0111"));When the value has to be worked out from mem as the run goes (an id an earlier step made), the Block reads it from mem and the flow says what it is right before the step with .set():
engine.map()
.gotoPage(RegistryPage)
.set(Keys.search, (mem) => mem.get(Keys.downMachine).code) // worked out when the step runs
.method(RegistryPage.do.fillSearch);A key a .set() gives is no longer one the run must supply: --data and mem stubs aren't asked for it.
What earlier steps saw: mem.trail
Every step's read is kept with the run's mem, so a later step, on another page, can ask what an earlier one showed: no mem key to declare, typed by the page's own read keys (autocomplete is Page.read.x):
engine.map()
.gotoPage(Machines)
.method(Machines.do.create("Mixer 2")) // step 4: something is made
.gotoPage(Report)
.method(Report.do.export, {
expect: [Report.check("lists what we made", ({ rows }, mem) => rows.includes(mem.trail.latest(Machines.read.lastCreated) ?? ""))],
});| Ask | Gives |
|---|---|
mem.trail.latest(key) / first(key) | the newest / the first value any step saw for that key (undefined when none did) |
mem.trail.at(step, key) | what it showed right after that step (its index in the run log) |
mem.trail.all(key) | every sighting: { step, block, page, pane?, value } |
mem.trail.changed(key) | { from, to } between the last two sightings |
mem reaches every hook that has one: a Method's act(page, input, mem), a Trait's check(page, mem), Page.check(why, (read, mem) => …), a stub's ctx.mem. In a flow with panes, { pane: "standard" } as the last argument picks one pane's sightings.
The same steps in several flows: steps()
export const logDisruption = steps(DisruptionLog, (m) =>
m.method(LogPage.do.selectMachine).method(LogPage.do.fillShiftStart).method(LogPage.do.submit),
);
engine.map({ as: Planner })
.gotoPage(LogPage)
.steps(logDisruption)
.gotoPage(LogPage)
.steps(logDisruption);A part is typed from the Checkpoint it starts on, like any step: played from anywhere else, it doesn't compile. It isn't a flow on its own; the flow that plays it is. For signing in, use an account instead.
Before and after: setup and cleanup
Data a flow needs goes in first, as steps (an API Block, or a whole flow), so the flow doesn't depend on what the database happens to hold; what it made goes away in .finally(), pass or fail:
engine.map({ as: Planner })
.flow(seedDownMachineFlow) // API Blocks: create the machine, keep its code in mem
.gotoPage(RegistryPage, { expect: [Registry.check("the down machine is listed", ({ rows }, mem) => rows.some((r) => r.code === mem.get(Keys.downMachine).code))] })
.finally(DeleteMachineApiBlock);A precondition is the same idea with a name: list the seeding Blocks (defineGivenBlock: a factory, SQL, a script, a request) in given and the flow's steps stay the scenario. A Given Block's undo is its way back, played after the flow in reverse order, pass or fail; a given that can't be made blocks the flow (WG_GIVEN_FAILED) instead of failing it. Shared lists are named groups: engine.given("signed-up-tenant", [...]). See How a run works.
engine.map({ as: Planner, given: [SeedDownMachine] }).gotoPage(RegistryPage, { expect: [...] });Two outcomes: lands or .branch()
engine.map()
.gotoPage(NavLoginBlock)
.method(SubmitLoginBlock, { expect: { LoginPage: [ExpectLoginErrorBlock] } }) // per outcome
.branch({
InventoryPage: (m) => m.gotoPage(NavCartBlock),
LoginPage: null,
});.branch() must name every outcome (null ends that one there). Use lands when the flow is only about one.
What a flow says about itself
export const loginFlow = engine.map({
title: "Sign in",
description: "A standard user signs in and lands on the inventory.",
labels: ["auth", "smoke"],
ticket: "QA-12", // optional
owner: "web", // optional
video: "auth/{label}/{title}", // where `--video` saves it
});The title names the test and the demo's first slide, the description is its caption, and the labels group the flow (they join the config's labels). waygraph list, the manifest and Waygraph Studio show all of them; waygraph check names the flows still without a description.
video is one key, like an object key in S3: where a recording of the flow (--video) is saved under the video folder, instead of a random name beside every other run. The tokens are {flow}, {title}, {label} (its first), {date}, {time} and {pane}; the last part is the file's name, the extension is added, and a name already taken gets -2. A flow with panes (--video-panes) adds -<pane> to the name unless the key has {pane}, and a voice-over's subtitles move with the video. A run of several flows uses the first that says.
Flow settings
map({ … }) sets the flow up before its first step:
| Setting | Does |
|---|---|
memStub: true | fills keys nothing else supplies from their mem stubs (Data); new Engine({ memStub: true }) does it for every flow |
as: Account | starts signed in as that account (below) |
input: Key | the flow takes this key as its input, once per case (Data) |
device: "mobile", { preset: "tablet", orientation: "landscape" }, { width, height } | the device it plays on, over the config's device (Config) |
timezone / network | the world it runs in (Testing) |
exclusive: true | test --flows runs it alone, after the others |
sessionReset: true | starts with fresh cookies and storage when it follows another flow |
expectedFailure: "why" | the flow is meant to fail (a locked-out user); passes when it does |
demoPace, highlightStyle | demo speed and ring style (Demo) |
homeOrigin | checks each Nav's URL is on this origin (gotoExternal checks it isn't) |
Accounts
A flow that starts signed in says as whom. An account is a name and the flow that signs it in:
// src/accounts/accounts.ts
export const StandardUser = account("standard-user", { signIn: loginFlow });
// src/flows/cart/cart-as-account.flow.ts
export const cartAsAccountFlow = engine
.map({ as: StandardUser })
.gotoPage(NavInventoryBlock)
.method(AddToCartBlock);So a test run signs each account in once. forgetSessions() drops the kept sessions.
How long a session lasts: ttl
An app signs people out after a while. Say how long, and the account signs in again once its session is older:
export const StandardUser = account("standard-user", { signIn: loginFlow, ttl: "15m" }); // "30s", "2h", "1d", or msAn account with a ttl also saves its session (.waygraph/sessions/), so the next run and every test worker start signed in too, until it runs out, instead of each signing in again (a sign-in by emailed link is slow, and often rate-limited). Without a ttl the session lasts as long as the process and is never saved: nothing says how long the app keeps it.
- A saved session is the account's for one site and one person: a different base url, or a different person in its
mem, signs in again (and saves its own). - The files hold cookies:
.waygraph/is in the scaffold's.gitignore, and each file is readable only by you. --fresh-sessions(run, demo, test) signs every account in again and saves the new sessions;WAYGRAPH_SESSIONS=0turns saving off, or names another folder.- Count from when the app signs the person in, a little under its real timeout: a session the app ended early fails the flow's first step (which says what the page showed).
When one sign-in flow serves several people (a magic link, by email), the account says who it is with mem: set right before it signs in, so a flow as it needs nothing more, and whoever an earlier flow signed in as doesn't matter.
const person = (email: string) => (mem: MemPage) => mem.set(AuthKeys.recipient, { email });
export const PlanningOfficer = account("planning-officer", { signIn: loginPlanningOfficerFlow, mem: person("kat@jbtec.example") });
export const Leadership = account("leadership", { signIn: loginLeadershipFlow, mem: person("cesar@jbtec.example") });Waygraph loads src/accounts/ itself (as it does mem stubs), so a sign-in flow that is one account's knows whose it is: waygraph demo loginLeadershipFlow, or Run in Waygraph Studio, signs in as Cesar with no --data. A value given with --data still wins.
Two people in one flow: panes
A scenario with two sides (one person requests, another approves) is one flow with panes, each signed in as its own account, side by side in a demo: map({ panes: [PlannerPane, AdminPane] }) (each pane("admin", { as: Admin })), then .in(AdminPane) before the admin's steps. Not two flows, and not a sign-out in the middle. See Split view.
Who is given what: for
for: [accounts] on a Page Block (who can open the page), a Method (who has its button) or a layout's link says who is given it; without it, everyone. waygraph check flags a flow as an account that uses something it isn't given, and waygraph test --flows checks each account's access. See Layouts and access.
Episodes: nesting flows
export const showcase = engine
.map()
.flow(ownerFlow) // episode 1
.flow(viewerFlow, { reset: true }) // episode 2: a second user, fresh cookies;Running a flow
| How | Command |
|---|---|
| headless | npx waygraph run homeCartFlow (or the file: src/flows/home/cart.flow.ts) |
| step by step, narrated | npx waygraph demo homeCartFlow (Demo) |
| as a test | npx waygraph test --flows (Testing) |
| two in a row | npx waygraph run "loginFlow then shopFlow" |
| list them | npx waygraph list |
| in code | await flow.run(new MemPage()) (own browser) · await flow.run(context, new MemPage().set(Keys.user, marco)) (a Playwright test's context) |
flow.run(context, mem, { page, closeOnFinish: false }) drives a page you already have and hands it back: recipes.