ReadonlyeventStreams page events after adapting supported payloads to wrapper values.
Details
Event listeners are removed when stream consumption ends. The stream also ends when the page closes.
Example (Reading the first console event)
import { Effect, Stream } from "effect";
import { Playwright } from "effect-playwright";
const firstConsoleMessage = Effect.gen(function* () {
const page = yield* Playwright.Page;
return yield* page.eventStream("console").pipe(Stream.runHead);
});
ReadonlyaddAdds a script which would be evaluated in one of the following scenarios:
ReadonlyaddAdds a <script> tag into the page with the desired url or content.
ReadonlyaddAdds a <link rel="stylesheet"> tag into the page with the desired url or a <style type="text/css"> tag with the content.
ReadonlyariaCaptures the aria snapshot of the page.
ReadonlybringBrings page to front (activates tab).
ReadonlycancelCancels the locator picking mode.
ReadonlyclearClears stored console messages.
ReadonlyclearClears stored page errors.
ReadonlyclickClicks an element matching the given selector.
Use Page.locator to create a locator and then call click on it instead.
ReadonlyclockAccess the clock.
ReadonlycloseCloses the page.
ReadonlyconsoleReturns all messages that have been logged to the console.
ReadonlycontentReturns the full HTML contents of the page, including the doctype.
ReadonlycontextGet the browser context that the page belongs to.
ReadonlydragDrags a source element to a target element and drops it.
ReadonlyemulateThis method changes the CSS media type through the media argument, and/or the 'prefers-colors-scheme' media feature, using the colorScheme argument.
ReadonlyevaluateEvaluates a function in the context of the page. Example (Evaluating browser-side code)
import { Effect } from "effect";
import { Playwright } from "effect-playwright";
const dimensions = Effect.gen(function* () {
const page = yield* Playwright.Page;
return yield* page.evaluate(() => ({
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
}));
});
ReadonlyexposeIdentical to exposeFunction but meant to be used with a static Effect.
This is useful when the exposed function does not need any arguments and just
runs a pre-defined effect in the application context.
import { Console, Effect } from "effect";
import { Playwright } from "effect-playwright";
const program = Effect.gen(function* () {
const browser = yield* Playwright.Browser;
const page = yield* browser.newPage();
yield* page.exposeEffect("ping", Console.log("pong"));
yield* page.evaluate(async () => {
// @ts-expect-error
await window.ping();
});
});
ReadonlyexposeAdds a function called name on the window object of every frame in this page.
The provided function must return an Effect which will be executed using the
current runtime when the function is called from the browser context.
If you don't require your function to have args you can use exposeEffect instead.
import { Console, Effect } from "effect";
import { Playwright } from "effect-playwright";
const program = Effect.gen(function* () {
const browser = yield* Playwright.Browser;
const page = yield* browser.newPage();
// Expose an Effect-based function to the browser
yield* page.exposeFunction("logMessage", (message: string) =>
Console.log(`Message from browser: ${message}`),
);
yield* page.evaluate(() => {
// Call the exposed function from the browser context
// @ts-expect-error
return window.logMessage("Hello from the other side!");
});
});
import { Context, Effect } from "effect";
import { Playwright } from "effect-playwright";
// A custom Database service used in your Effect application
class Database extends Context.Service<
Database,
{ readonly insertProduct: (name: string, price: number) => Effect.Effect<void> }
>()("Database") {}
const program = Effect.gen(function* () {
const browser = yield* Playwright.Browser;
const page = yield* browser.newPage();
// Expose a function that seamlessly accesses Effect Context using Effect.fn
yield* page.exposeFunction(
"saveProduct",
Effect.fn(function* (name: string, price: number) {
const db = yield* Database;
yield* db.insertProduct(name, price);
}),
);
yield* page.evaluate(async () => {
// Extract data from the page and save it
const items = document.querySelectorAll(".product");
for (const item of items) {
const name = item.querySelector(".name")?.textContent || "Unknown";
const price = Number(item.querySelector(".price")?.textContent || 0);
// Call the Effect function directly from the browser
// @ts-expect-error
await window.saveProduct(name, price);
}
});
});
ReadonlyframeReturns a frame matching the specified criteria.
ReadonlyframesReturns all frames attached to the page.
ReadonlygetReturns a locator that matches the given alt text.
ReadonlygetReturns a locator that matches the given label.
ReadonlygetReturns a locator that matches the given placeholder.
ReadonlygetReturns a locator that matches the given role.
ReadonlygetReturns a locator that matches the given test id.
ReadonlygetReturns a locator that matches the given text.
ReadonlygetReturns a locator that matches the given title.
ReadonlygoNavigate to the previous page in history.
ReadonlygoNavigate to the next page in history.
ReadonlygotoNavigates the page to the given URL.
ReadonlyhideClears all highlights.
ReadonlyisIndicates that the page has been closed.
ReadonlykeyboardAccess the keyboard.
ReadonlylocalAccess local storage for the page's current origin.
ReadonlylocatorReturns a locator for the given selector.
NOTE: This method will cause a defect if options.has or options.hasNot are provided and belong to a different frame.
ReadonlymainThe page's main frame. Page is guaranteed to have a main frame which persists during navigations.
ReadonlymouseAccess the mouse.
ReadonlyopenerReturns the opener for popup pages and Option.none for others.
If the opener has been closed already, returns Option.none.
ReadonlypageReturns all errors that have been thrown in the page.
ReadonlypausePauses the script execution.
ReadonlypdfReturns the PDF buffer.
page.pdf() generates a pdf of the page with print css media. To generate a pdf with screen media, call
Page.emulateMedia before calling page.pdf().
ReadonlypickEnters an interactive mode where hovering over elements highlights them and shows the corresponding locator.
ReadonlyreloadReloads the page.
ReadonlyrequestRequest the page to perform garbage collection. Note that there is no guarantee that all unreachable objects will be collected.
ReadonlyrequestsReturns the most recent network requests from the page.
ReadonlyscreencastAccess the screencast.
ReadonlyscreenshotCaptures a screenshot of the page.
ReadonlysessionAccess session storage for the page's current origin.
ReadonlysetThis method internally calls document.write(), inheriting all its specific characteristics and behaviors.
ReadonlysetThis setting will change the default maximum navigation time for the following methods:
ReadonlysetThis setting will change the default maximum time for all the methods accepting timeout option.
ReadonlysetThe extra HTTP headers will be sent with every request the page initiates.
ReadonlysetSets the viewport size for the page.
ReadonlytitleReturns the page title.
ReadonlytouchscreenAccess the touchscreen.
ReadonlyurlReturns the current URL of the page.
ReadonlyuseRuns an asynchronous operation against the underlying Playwright Page.
When to use
Use this escape hatch only when Page does not expose the native Playwright operation you need.
Gotchas
The callback must return a Promise. The native page has the same lifetime
as this wrapper; closing it also closes the wrapped page.
ReadonlyviewportReturns the viewport size.
ReadonlywaitWaits for the page to reach the given load state.
NOTE: Most of the time, this method is not needed because Playwright auto-waits before every action.
ReadonlywaitWaits for the given timeout in milliseconds.
ReadonlywaitWaits for the page to navigate to the given URL.
ReadonlyworkersReturns all workers.
Effect-friendly operations for a Playwright page.
When to use
Use this service for navigation, DOM interaction, evaluation, page state, and page event streams. Operations that can fail return
Effect; safe synchronous observations remain plain functions, and nullable Playwright results are represented withOption.Since
0.1.0