Files
fn_registry/frontend/functions/browser/pw_launch_browser.ts
T
egutierrez 626d06327c feat(registry): add playwright capability group (6 TS browser fns)
New domain `browser` under frontend/functions/ with 6 Playwright helpers:
- pw_launch_browser: chromium + context + page bootstrap with storageState
  support and baseUrl navigation.
- pw_kanban_login: authenticates a Page against /api/auth/login; sets the
  kanban_session cookie via shared storageState; verifies login page no
  longer visible after navigation.
- pw_drag_drop: human-like pointer drag (mousedown + activateOffset +
  stepped move + mouseup) compatible with @dnd-kit/core's 8px activation
  threshold; supports hoverMs for time-based dropzones.
- pw_keyboard_sequence: ordered focus/type/press/wait steps for scripting
  realistic input flows (typing then arrow-key navigating autocompletes).
- pw_wait_predicate: thin wrapper over page.waitForFunction with friendlier
  defaults and custom error messages.
- pw_assert_class: poll-based assertion that a Locator has/lacks a CSS
  class within a timeout; useful for visual-state checks.

Each function ships with vitest tests (5-8 cases each) covering both happy
and error paths, plus self-documenting .md (Ejemplo + Cuando usarla +
Gotchas + frontmatter with params/output schema).

Adds frontend/functions/package.json with `"type": "module"` so consumers
can ESM-import the .ts files from anywhere in the registry (Playwright's
tsx loader respects nearest package.json).

Capability page docs/capabilities/playwright.md documents the group with
a canonical end-to-end example, frontiers, prerequisites, and gotchas.
Index updated.

First consumer (issue 0088): apps/kanban requester-input.spec.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 12:57:30 +02:00

59 lines
2.0 KiB
TypeScript

import { existsSync } from "fs";
import type { Browser, BrowserContext, Page } from "playwright";
/** Options for launching a Playwright browser session. */
export interface PwLaunchOptions {
/** Use headless mode. Default: true */
headless?: boolean;
/** Base URL to navigate to after opening the page. Default: "http://localhost:5180" */
baseUrl?: string;
/** Viewport size. Default: 1280x800 */
viewport?: { width: number; height: number };
/** Optional path to a Playwright storageState JSON for pre-auth. Loaded only if the file exists. */
storageStatePath?: string;
/** Slow down operations by the given ms. Default: 0 */
slowMo?: number;
}
/** Playwright handles returned after launching the browser. */
export interface PwHandles {
browser: Browser;
context: BrowserContext;
page: Page;
/** Closes context then browser. */
close: () => Promise<void>;
}
/**
* pw_launch_browser — launches chromium and returns a Page navigated to baseUrl.
*
* Optionally pre-authenticates via storageState (loaded only if the file exists).
* Returns handles including a convenience close() that tears down context and browser.
*/
export async function pw_launch_browser(opts: PwLaunchOptions = {}): Promise<PwHandles> {
const { chromium } = await import("playwright");
const headless = opts.headless ?? true;
const slowMo = opts.slowMo ?? 0;
const baseUrl = opts.baseUrl ?? "http://localhost:5180";
const viewport = opts.viewport ?? { width: 1280, height: 800 };
const browser = await chromium.launch({ headless, slowMo });
const contextOptions: Parameters<Browser["newContext"]>[0] = { viewport };
if (opts.storageStatePath && existsSync(opts.storageStatePath)) {
contextOptions.storageState = opts.storageStatePath;
}
const context = await browser.newContext(contextOptions);
const page = await context.newPage();
await page.goto(baseUrl);
const close = async (): Promise<void> => {
await context.close();
await browser.close();
};
return { browser, context, page, close };
}