Browser lifecycle¶
playwright-page wires the browser lifecycle into a describe block. It launches
one browser for the group, creates a fresh context and page for each example, and
closes them afterward so examples stay isolated.
Setup¶
Call playwright-page at the top of a describe, passing the fixture to load:
1 2 3 4 5 6 7 8 9 10 | |
The page term¶
use BDD::Behave::Playwright brings a page term into scope that returns the
current page:
1 2 3 4 | |
The same page is also reachable through the example's topic parameter as .page,
for blocks written -> $_ { ... }:
1 2 3 | |
Each example gets its own context and page, so state set in one example does not leak into the next.
Scope¶
- One browser is launched per
describegroup, in abefore-allhook. - Each example gets a fresh context and page, created in
before-eachand published as thepageterm. - The context is closed in
after-each; the browser is closed inafter-all.
Watching the browser¶
The browser runs headless by default. Set SHOW_CHROME to launch it headed, so
the window is visible while the examples drive it:
1 | |
headless-from-env reads the variable and is what playwright-page passes to
launch. An unset variable, or the value 0, stays headless; any other value
runs headed. Headed runs suit watching a suite locally. Leave it unset in CI.
Fixtures¶
fixture-url turns a filesystem path into an absolute file:// URL:
1 | |
playwright-page(fixture => ...) calls it for you. Pass an absolute or
working-directory-relative path to a local HTML file. Browser tests run against
local file:// fixtures, never the network.
A base URL and visit¶
To drive a running application instead of a file fixture, give playwright-page
a base-url. visit then navigates the current page to a path beneath it:
1 2 3 4 5 6 7 8 | |
visit('/path') joins the path onto the base URL. An absolute URL
(visit('http://...')) is used unchanged. goto($url) navigates to a URL
directly, without the base.
The base URL may be a callable, which is resolved at navigation time. That suits a server whose address is known only once it has started, such as a test server bound to an ephemeral port:
1 | |
Context options¶
context passes options through to the browser context that each example runs
in, so a describe can emulate a device, such as a mobile viewport, a color
scheme, or a locale:
1 2 3 4 5 6 7 8 9 10 | |
The keys are the options that WWW::Playwright's
new-context accepts, such as
viewport, is-mobile, has-touch, color-scheme, and locale.