Blocks
A Block is one step: it starts on a Checkpoint, does one thing, and lands on a Checkpoint.
Checkpoints: a page is its own
A Checkpoint is a page: a place the app can be, with its own address. A Page Block makes its own, from a name:
export const HomePage = definePageBlock({ name: "page-home", checkpoint: "Home", url: "/home", ... });
HomePage.checkpoint; // the value, for what must name it: { lands: HomePage.checkpoint }
page.method({ to: "Docs", async act() {} }) // another page, by its name: no import, no cycle (`waygraph check` catches a typo)There is no states file and no checkpoint() plus export type pair per page. (Older projects declare them in src/states/ and pass the value as checkpoint: Home; both work.) Where every flow begins is Start.
What changes on a page is not another Checkpoint. Adding an item to the cart leaves you on Home; what changed is what Home shows, its read's home.cartCount and each item's inCart, which the Block's verify checks. A link sent, an alert shown, a banner up: the same. waygraph check names a Checkpoint that is a state of a page (only Methods land on it and it has no address of its own, or it has another page's).
One Checkpoint comes with waygraph: Start, where every flow begins, before its first step. A Block that leads off a flow starts from: Start; one that sends the run back to the beginning (signing out) goes to: Start.
import { Start } from "waygraph";
defineMethodBlock({ name: "sign-out", from: Dashboard, to: Start, async act(page) { … } });The kinds
| Kind | Use it to | Made with | Moves? |
|---|---|---|---|
| Nav | open a page by URL or by a link | defineNavBlock (url) · defineNavClickBlock (click) | to its checkpoint |
| Page | say "you are here": what you can do (do), what it shows (read), what a scenario can expect (expects) | definePageBlock | no |
| Method | do one thing: fill, click, submit (or to one row of many: rows) | defineMethodBlock | from → to |
| Expect | what a scenario expects on a page: reads and judges, changes nothing | defineExpectBlock | no (an expectation, not a step) |
| API | make an HTTP call, no page | defineApiBlock | from → to |
| Given | a precondition: seed what the scenario needs with any code (factory, SQL, script), undo it after | defineGivenBlock | no (played before step 1) |
All of them are plain Blocks when the flow runs; the kind is what map() and waygraph check hold you to.
Nav: getting to a page
A page opens itself: give its Page Block a url (or a click, an element already on the page), and gotoPage(Page) in a flow goes there (the page). A Nav Block is for a move that isn't a page's own address: a link one page has to another, a menu entry.
export const NavContinueShoppingBlock = defineNavBlock({
name: "nav-continue-shopping",
checkpoint: Home,
click: CartSel.continueShopping, // or url: (mem) => `/orders/${mem.get(OrderKeys.id)}`
from: [Cart],
verify: [Trait.url({ pathname: "/home" })],
});Only Nav Blocks and Page Blocks with a url or click navigate. In any other Block, page.goto / reload / goBack show as deprecated in the editor, and waygraph check warns about them.
A page can be opened from anywhere by default. One you can only reach after signing in says so with from, so route finding (auto, pilot reach, traverse) signs in first:
export const SIGNED_IN = [Dashboard, Settings] as const;
definePageBlock({ name: "page-equipment", checkpoint: Equipment, url: "/equipment", from: SIGNED_IN });Method: one action
export const ClearCartBlock = defineMethodBlock({
name: "clear-cart",
description: "Clears every item via the Clear cart control.",
from: Home,
to: Home,
async act(page) {
await page.locator(DemoSel.clear).click();
},
verify: (): Trait[] => [HomePage.read.cartCount.is(0)], // what the page shows now: no item in the cart
});One action per Block. A login form is fill-username, fill-password and submit-login: three Blocks, never one. waygraph check warns on a Method that fills more than once or fills and clicks.
No method pollution. A page's do is its menu of actions a user can take: waygraph auto, Pilot and any agent driving the page list it. A Method written for one scenario (fill-email-for-lockout) shows up there for everyone, so write the general Method and vary the scenario with mem and the step's expect. A Method only one flow uses is a smell.
When a Block can land in more than one place, to is a list and lands says which one it reached:
defineMethodBlock({
name: "submit-login",
from: LoginPage,
to: [InventoryPage, LoginPage], // a wrong password stays here
async act(page) { await page.locator(LoginSel.submit).click(); },
async lands(page) { return page.url().includes("/inventory") ? InventoryPage : LoginPage; },
});| Part | Does | May touch |
|---|---|---|
precondition | checks the page is where the Method assumes, before it acts | the page |
act | the action | the page, mem |
lands | looks at what happened and names the Checkpoint (only when to is a list) | the page, mem |
verify | proves it took effect | the page |
verify | checks the page is what resolve said (Checks) | reads only |
Page: the hub
export const HomePage = definePageBlock({
name: "page-home",
checkpoint: Home,
url: "/home", // how it opens; `from` lists where it opens from, none = anywhere
verify: [Trait.visible(DemoSel.catalog)],
read: (ctx) => ({ cartCount: ctx.page.number(DemoSel.cartCount), items: ctx.page.list(DemoSel.item, (row) => ({ name: row.text(DemoSel.name), inCart: row.visible(DemoSel.remove) })) }),
do: () => ({ addItem: AddItemBlock, clearCart: ClearCartBlock }),
expects: (page) => ({ empty: page.read.cartCount.is(0) }),
});A Page Block is the page's namespace: HomePage opens it, HomePage.read.cartCount is a typed key of what it shows (What a page shows), HomePage.do.addItem is the Add Item Block, typed as itself, and HomePage.expects.empty a check a scenario can expect there. do and expects are functions of the page, called the first time they're used, so the page and its Methods can import each other (Lifecycle). A page whose address has a param (url: "/requests/:id", or [id] as in Next and Nuxt) has go, typed by the params: .gotoPage(Request.go({ id: 42 })) opens /requests/42; leaving one out doesn't compile.
A Method lives in its own file in the page's _methods/, one action per file, so a page stays small as the app grows and a person or an agent writes every Method the same way:
// _methods/fill-email.method.block.ts: a function of its page, importing only the page's type (so no import cycle)
export const fillEmail = (self: PageHandle<"Login">) =>
self.method({ async act(page) { await page.locator(LoginSel.email).fill("…"); } });
// _page.block.ts: one line to list it
do: (self) => ({ fillEmail: fillEmail(self), submit: submit(self) }),The page is its from and to, the key is its name (login-fill-email), to: "Docs" lands on another page by name, and lands picks one when it can land on several. A one-line Method can be written in the page's do instead (fillEmail: self.method({ … })); a Method shared by several pages is a plain defineMethodBlock with its from and to. waygraph gen page login --do fillEmail,submit writes the page, its _sel.ts and a _methods/ file for each, and waygraph gen method login fillPassword adds one.
A Method can take params, for what the scenario chooses and no mem key fits (a drag from here to there): do: (self) => ({ drag: self.method((from: Point, to: Point) => ({ async act(p) { … } })) }), used as .method(Board.do.drag({ x: 40, y: 80 }, { x: 320, y: 80 })). The arguments are typed, and the Block's name carries the values (board-drag({"x":40…})) in the run log. A Method with params isn't listed in waygraph auto or Pilot menus, which can't run it without values: they list the page's actions that need none.
A page that lives in an iframe on another page says frame: "#embed": its Blocks, checks and read see every locator inside the frame, so selectors carry no prefix and the page's twin outside a frame shares the same _sel.ts.
A page's read can ask the app's own API for what the page only shows after a reload (a status the backend already changed): ctx.page.api((url) => /requests/${id}, (json) => json.status), with new Engine({ api: { baseUrl, headers } }) saying where the API is and how a request is signed.
A tab or a filter kept in the address (?tab=files) is state of the page, not a page of its own: query: ["tab"] on the page, then Page.go({ id: 42, tab: "files" }) opens with it, Page.setQuery({ tab: "signers" }) is a step that changes it, and both check the address says so.
A flow imports the page, not each Block:
engine.map().gotoPage(HomePage).method(HomePage.do.addItem, { expect: [HomePage.read.cartCount.is(1)] });waygraph auto groups a page's methods under it. A page's methods and expectations never count as orphans, though no flow plays them; nor does a Block a flow names in an expect: [...] (a step's, or a Block's own). Name the Page Block after the page (HomePage): the Checkpoint already has the page's own name (Home).
Screenshots
screenshot: true (or { fullPage: true }) on a Block takes a screenshot of the page as the last thing in its step: after its action, its checks, its expectations and the page's read. On a Page Block it's the page's: every step that lands on that page takes one, whichever Block it was.
export const CartPage = definePageBlock({ name: "page-cart", checkpoint: CartPage, screenshot: true, ... });Each is kept with its run (.waygraph/runs/<runId>/<step>-<block>.png, a run:artifact in the run log naming the step and the page), and the newest per page as .waygraph/screenshots/<Checkpoint>.png. Waygraph Studio shows a page's newest on its map, and each step's on the run. waygraph's own overlay (the demo's panel, rings, banner) is left out of the picture; a screenshot that can't be taken never fails the step.
A Method on one row of many
A Method that acts on one row of several (add this item) names the row in mem (requires) and lists the rows on the page (instanceOptions), so waygraph auto shows "Add Alpha", "Add Beta"…:
defineMethodBlock({
name: "add-item",
from: Home,
to: Home,
requires: [DemoKeys.selectedItem],
async act(page, _input, mem) {
await page.locator(DemoSel.addBtn(mem.get(DemoKeys.selectedItem).id)).click();
},
verify: (): Trait[] => [
HomePage.check("the item in the cart", ({ items }, mem) => items.some((i) => i.name === mem.get(DemoKeys.selectedItem).name && i.inCart)),
],
async instanceOptions(page) {
// one { id, label, key: DemoKeys.selectedItem, value, highlight } per Add button on the page
},
});Expect and API
export const ExpectSubmitGatedBlock = defineExpectBlock({
name: "expect-submit-gated",
description: "Continue stays disabled until the form is filled.",
checkpoint: CheckoutInfoPage,
verify: [Trait.disabled(CheckoutSel.submit)],
});
export const CheckOrderCreatedBlock = defineApiBlock({
name: "check-order-created",
from: InventoryPage,
to: [OrderConfirmed, OrderFailed],
method: "GET", // shown in the demo's network tab
call: ({ request }) => request.get("/api/orders/latest"), // shares the browser's cookies
resolve: (r) => (r.ok ? OrderConfirmed : OrderFailed), // r = { status, ok, body }
});An Expect Block goes in an expect: [...] list, on a Block or on a step, never as a step of its own; it only reads (Checks).
Selectors live in _sel.ts
export const DemoSel = {
title: "h1",
cartCount: "[data-test=cart-count]",
addBtn: (id: string) => `[data-test=add-${id}]`,
} as const;A selector written inline in a Block, or a regex / XPath selector, is a waygraph check warning. Mem holds values (which item); _sel.ts holds where things are on the page.
Plain strings, or typed builders
A plain string is always fine: a selector is a string everywhere (page.locator(…), a Trait, click, a ring). For an application where a wrong selector costs a failed run, sel builds the same strings Playwright's getBy… methods make, typed from Playwright's own types, so a wrong role, option or argument is a compile error instead of "element not found":
import { sel } from "waygraph";
export const LoginSel = {
username: sel.placeholder("Username"),
submit: sel.role("button", { name: "Login" }), // the role is Playwright's union; the options are getByRole's
error: sel.testId("error", { attribute: "data-test" }), // the app's own test-id attribute
field: (name: string) => sel.label(name), // a factory, as with plain strings
inForm: sel.within(sel.css("form#login"), sel.role("button", { name: "Login" })),
};sel.css · sel.role · sel.testId · sel.text · sel.label · sel.placeholder · sel.altText · sel.title · sel.within. Each picks what its page.getBy… picks (exact means the same), and the manifest, Pilot's coverage and Studio read an entry written with fixed arguments as its string, as they read a plain one. Mix the two freely in one _sel.ts.
Several Blocks as one
| Helper | Gives |
|---|---|
composeBlock(name, steps) | one named Block made of several, each keeping its own verify |
fastForwardComposeBlock(name, steps) | the same, played at full speed in the demo (sign-in you don't need to watch) |
withVerify(block, verify) | the same Block with other checks |
withDialog(block, { accept, into }) | answers the confirm() / alert() the Block's action opens |
Lint it
npx waygraph check # navigation outside Nav Blocks, inline selectors, empty verify, orphans, pages with no `read`,
# Checkpoints that are a state of a page (no address of their own, or another page's)
npx waygraph typecheck # tsc --noEmit, then the same checkscheck also points out code written for something waygraph already does, in any file (helpers and specs too):
| Warning | Found | Use instead |
|---|---|---|
cast-to-outcome | Block as MethodBlock<A, B> | .method(Block, { lands: B }) (upgrade --codemod lands) |
ff-run | { ff: true } on three or more steps in a row | .ffStart() … .ffEnd() (upgrade --codemod ff) |
hand-rolled-retry | a polling loop in a Trait file | nothing: Traits retry, and say what they wanted and saw |
seeded-spec | seedMemStub in a spec | memStub: true: the flow fills keys when it runs |
builder-helper | a function over MapBuilder | a part, steps(Checkpoint, (m) => …), or an account for signing in |
overlay-workaround | a dispatched click, a native value setter | click() and fill(): they work in a demo |
expect-one-read | an Expect Block that is one read check | Key.is(value) in the step's expect |
mutating-expectation | an Expect Block that clicks, types, navigates or sets mem | a Method step before it; the expectation only reads |
arrival-in-expect | an Expect Block that only checks the URL or that something is visible | the verify of the Block that lands there |
outcome-in-verify | a Method's verify that reads a message (a toast, an alert, an error) | expect: [...] on the step (or the Block's expect when every use expects it) |
start-tag | "__start__" written by hand | import { Start } from "waygraph" |
Silence one warning on purpose with // waygraph-ignore: empty-verify (or any other kind: multi-input, combined-action, hand-written-ui, ff-run, …) in that file.