allwright

API ReferenceRustHooks

Hooks

A hook coordinates a browser-native event that fires asynchronously outside any direct call — a new tab, a native file picker, a download. register_hook before the triggering action, then wait for it, so the event can't be missed between the two. HookType is a sealed trait — NEW_PAGE, FILE_CHOOSER, and DOWNLOAD are the only implementations.

NEW_PAGE

PropertySince v0.1.5

Waits for a tab opened by the triggering action. Registers on the page that triggers the event (not the browser) as of v0.1.6, which fixed misattribution when more than one tab could plausibly open around the same time.

pub const NEW_PAGE: NewPage; // HookType::Output = Tab

Example

let hook = page.register_hook(allwright::NEW_PAGE).await?;
page.click("a[target=_blank]").await?;
let new_page = hook.wait().await?;

FILE_CHOOSER

PropertySince v0.1.7

Waits for the native file-picker dialog; no OS dialog actually renders. The resulting FileChooser's set_file(...)/set_files(...) supplies one or more paths.

pub const FILE_CHOOSER: FileChooserHook; // HookType::Output = FileChooser

Example

let hook = page.register_hook(allwright::FILE_CHOOSER).await?;
page.click("button.open-upload").await?;
let chooser = hook.wait().await?;
chooser.set_file("fixtures/document.pdf").await?;

DOWNLOAD

PropertySince v0.1.7

Resolves as soon as a download starts, exposing url() and suggested_filename() immediately. The resulting Download's save_as(...) waits for it to finish and copies it to path.

pub const DOWNLOAD: DownloadHook; // HookType::Output = Download

Example

let hook = page.register_hook(allwright::DOWNLOAD).await?;
page.click("a.download-report").await?;
let download = hook.wait().await?;
download.save_as(format!("artifacts/{}", download.suggested_filename())).await?;