POMWright


POMWright is a TypeScript library for Playwright Test that gives a suite’s locators one
place to live. You register each locator once, under a dot-delimited path, with Playwright’s own methods; POMWright
builds the chain from the path, and what you get back is a plain Playwright Locator. Page objects, session storage,
report logging and steps build on that, and each part works on its own.
Example
An orders page lists the orders the app keeps in session storage, in a table. The rows are registered once; two methods
narrow them to one order, and each reaches a different element in that row:
// orders.page.ts
import { expect, type Page } from "@playwright/test";
import { json, PageObject, step } from "pomwright";
// the known statuses are suggested; any other string is accepted too
type OrderStatus = "Open" | "Approved" | "Completed" | "Cancelled" | (string & {});
export type Order = { id: string; status: OrderStatus };
const ordersStorage = { orders: json<Order[]>() };
type Paths =
| "main"
| "main.table@orders"
| "[email protected]"
| "[email protected]@cancel"
| "[email protected]@status";
export class OrdersPage extends PageObject<Paths, { storage: typeof ordersStorage }> {
constructor(page: Page) {
super(page, "https://shop.example", "/orders", { sessionStorage: { schema: ordersStorage } });
}
protected defineLocators(): void {
this.add("main").getByRole("main");
this.add("main.table@orders").getByRole("table", { name: "Orders" });
this.add("[email protected]").getByRole("row");
this.add("[email protected]@cancel").getByRole("button", { name: "Cancel" });
this.add("[email protected]@status").getByTestId("order-status");
}
@step()
async cancel(options: { order: string }) {
const { order } = options;
await this.getLocatorSchema("[email protected]@cancel")
.filter("[email protected]", { hasText: order })
.getNestedLocator()
.click();
}
@step()
async expectStatus(options: { order: string; status: OrderStatus }) {
const { order, status } = options;
const cell = this.getLocatorSchema("[email protected]@status")
.filter("[email protected]", { hasText: order })
.getNestedLocator();
await expect(cell).toHaveText(status);
}
}
The test receives the page object as a Playwright fixture, seeds the orders and logs them to the report, then opens the
page:
// orders.spec.ts
import { expect } from "@playwright/test";
import { test as base } from "pomwright";
import { type Order, OrdersPage } from "./orders.page";
const test = base.extend<{ ordersPage: OrdersPage }>({
ordersPage: async ({ page }, use) => {
await use(new OrdersPage(page));
},
});
const orders: [Order, Order] = [
{ id: "ORD-1042", status: "Open" },
{ id: "ORD-1043", status: "Open" },
];
test("a customer cancels an open order", async ({ ordersPage, log }) => {
await ordersPage.sessionStorage.seed({ orders });
log.info("seeded sessionStorage", { orders });
await ordersPage.navigation.goto();
await expect(ordersPage.getNestedLocator("main.table@orders")).toBeVisible();
await ordersPage.expectStatus({ order: orders[0].id, status: "Open" });
await ordersPage.cancel({ order: orders[0].id });
await ordersPage.expectStatus({ order: orders[0].id, status: "Cancelled" });
});
Canceling ORD-1042 clicks this locator, which the report and the trace viewer show by its path,
[email protected]@cancel:
getByRole('main').getByRole('table', { name: 'Orders' }).getByRole('row').filter({ hasText: 'ORD-1042' }).getByRole('button', { name: 'Cancel' })
getNestedLocator(path) resolves a path’s registered chain as it is: the shorthand for the locators you use most, as
the test does with the table. getLocatorSchema(path) returns a copy of the chain to narrow, index, patch or replace
for one use, as both methods do; the registered locators never change.
seed stores entries before the app’s first script reads them, typed by the schema the page object declares, and the
log fixture of pomwright’s test attaches each entry it records to the report.
A path that is not in Paths does not compile, and a mistake the compiler cannot see fails the fixture before the test
body runs. Say Paths also declared
[email protected], and the table were registered with filter({ has: "[email protected]" }), but the
caption itself never was:
OrdersPage: Locator registry verification failed for 1 of 5 paths:
"main.table@orders": No locator schema registered for path "[email protected]".
The parts on their own
POMWright does not replace Playwright’s API, runner or assertions, and @playwright/test is a peer dependency. Use as
much of it as you need:
| part |
on its own |
| the locator registry |
const { add, getNestedLocator } = createRegistryWithAccessors<Paths>(page); |
| page objects |
class OrdersPage extends PageObject<Paths>, with goto, expectThisPage and expectAnotherPage from its URL |
| session storage |
await new SessionStorage(page, { origin }).seed({ token }); stores entries before the app’s first script reads them |
| report logging |
test from pomwright, whose log fixture attaches its entries to the test’s report |
| report steps |
@step() on any async method makes it a step of the report |
A suite with a few pages and little repetition may need none of it: plain Playwright locators in a class are enough
there. The registry pays off as a suite grows:
- The same structure, located again and again, with a variation for one use, while the registered locator stays
as it is.
- Locators shared across page objects, or across applications, through locator modules and seeds.
- Many similar elements on a page. Every path resolves as a chain scoped by its ancestors, so a Cancel button is
found in its row, in the orders table, in
main. The scope is written once, in the registry, not at every use.
Installation
npm install --save-dev pomwright
POMWright 3 needs @playwright/test 1.64 or later (below 2) and Node 20 or later.
Documentation
- Getting started: installation, a first page object, page
objects as fixtures, organizing a project
- API reference: every export and member, and every error message
- Upgrading from 2.x: every change in 3.x a 2.x project
meets, and what to write instead
- Internal: how POMWright works
Using POMWright 2.x? Its documentation is in this repository at the
v2.1.0 tag. Still on 1.x? The
1.x to 2.x migration guide is at the same tag.
Agent skills
Working through a coding agent? POMWright’s agent skills teach it
POMWright as the documentation describes: pomwright writes and reviews tests and page code, pomwright-map-site
generates page code for a running site, and pomwright-teach teaches POMWright over several sessions. They live in
skills/, not in the npm package; npx skills add DyHex/POMWright installs them.
Support
If you run into issues or have questions, please use the
GitHub issues page. Report a security vulnerability privately instead, as
SECURITY.md describes.
Contributing
Pull requests are welcome. AGENTS.md is the contributor guide: the commands, the rules every change
keeps, and an index of the files that cover each part of the repository (an AGENTS.md in src/ and its
locators/ and helpers/, test/, website/, skills/ and .github/, and the files in docs/).
License
POMWright is open-source software licensed under Apache-2.0.
评论