browser
def browser(
path:str=None
):A headless browser for check; one at a time in this process, closed at the end.
validate reads the spec. check runs it. It writes the static page, opens it in a headless browser with playwright, and plays the demo as a learner would. The browser is always headless, so no window opens and nothing takes the focus.
These checks run in light and in dark mode:
data-rk-ready="1". Any console error, page error, or request that is not file:, data: or about: fails.after is hidden before its gate.rk-predict event fires, its answer matches the Python twin, and the blocks that wait show.The check never uses the Chrome that you use every day: a headless run of that app shares its crash reporter, its updater and its app registration, and many runs can crash it. It uses, in this order: a path that you give, the path in $FASTCDP_CHROME, or the Chromium that playwright install chromium gives. One process runs one browser at a time, and each browser closes in a finally block.
A headless browser for check; one at a time in this process, closed at the end.
The browser to use: path, else $FASTCDP_CHROME, else None for playwright’s own Chromium. Never the everyday Chrome.
The default is playwright’s own Chromium, not the everyday Chrome, and a path into the everyday Chrome is refused.
can_check says if a browser starts here. The notebook runs its browser tests only when one does.
Call f; in a thread when an asyncio loop runs here (as in a notebook), because playwright’s sync API cannot run in a loop.
This script runs in the page. For each element with its own text, it finds the colour behind the text: the backgrounds of the element and its parents, and for SVG text, a shape behind it in the same group (the dot of a picked wheel item, the cell of a grid). It mixes in opacity, then gives the WCAG ratio. Disabled controls and hidden text are skipped.
The texts of the page whose contrast is under the WCAG need, as (text, ratio, need).
_state reads the state of the demo from the page. _parity compares each derived value with the Python twin of its fn.
_boxes checks that each visible block has a box, and that each block with after is hidden before its gate. Chrome’s checkVisibility() does not see display: none on an SVG element, so VIS also looks at the element and its parents.
_play plays each gate: it picks the first option with the key 1, and locks in with ⌘↩︎.
_keys sends keys to each control and checks that the state moves. A control at one end of its range gets the opposite key too.
check puts the parts together. It returns AttrDict(ok, errors, info, shots). With shots, it saves screenshots at 2× before and after the play, in light and dark.
check runs in a thread when an asyncio loop runs, as in a notebook.
Draw spec in a headless browser, play it, and return AttrDict(ok, errors, info, shots); b reuses an open browser.
These tests need a browser, so they run only where one starts.
A good demo passes. The info has the options of the gate and the verdict.
A spec with a broken fn name fails with the error text of the runtime.
Each block example passes the whole check, in light and dark.