allwright

API ReferenceJavaHooks

Hooks

A hook coordinates a browser-native event that fires asynchronously outside any direct call — a new tab, a native file picker, a download. registerHook before the triggering action, then waitFor it, so the event can't be missed between the two.

Hooks.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.

public static final HookType<Page> NEW_PAGE

Example

Hook<Page> hook = page.registerHook(Hooks.NEW_PAGE);
page.click("a[target=_blank]");
Page newPage = hook.waitFor();

Hooks.FILE_CHOOSER

PropertySince v0.1.7

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

public static final HookType<FileChooser> FILE_CHOOSER

Example

Hook<FileChooser> hook = page.registerHook(Hooks.FILE_CHOOSER);
page.click("button.open-upload");
FileChooser chooser = hook.waitFor();
chooser.setFiles(Path.of("fixtures/document.pdf"));

Hooks.DOWNLOAD

PropertySince v0.1.7

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

public static final HookType<Download> DOWNLOAD

Example

Hook<Download> hook = page.registerHook(Hooks.DOWNLOAD);
page.click("a.download-report");
Download download = hook.waitFor();
download.saveAs(Path.of("artifacts", download.suggestedFilename()));