test(browser): add regression coverage
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildRoleSnapshotFromAriaSnapshot } from "./pw-role-snapshot.js";
|
||||
import {
|
||||
buildRoleSnapshotFromAriaSnapshot,
|
||||
getRoleSnapshotStats,
|
||||
} from "./pw-role-snapshot.js";
|
||||
|
||||
describe("pw-role-snapshot", () => {
|
||||
it("adds refs for interactive elements", () => {
|
||||
@@ -42,4 +45,14 @@ describe("pw-role-snapshot", () => {
|
||||
expect(res.snapshot).toContain(" - group");
|
||||
expect(res.snapshot).not.toContain("button");
|
||||
});
|
||||
|
||||
it("computes stats", () => {
|
||||
const aria = ['- button "OK"', '- button "Cancel"'].join("\n");
|
||||
const res = buildRoleSnapshotFromAriaSnapshot(aria);
|
||||
const stats = getRoleSnapshotStats(res.snapshot, res.refs);
|
||||
expect(stats.refs).toBe(2);
|
||||
expect(stats.interactive).toBe(2);
|
||||
expect(stats.lines).toBeGreaterThan(0);
|
||||
expect(stats.chars).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
53
src/browser/pw-session.test.ts
Normal file
53
src/browser/pw-session.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Page } from "playwright-core";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { ensurePageState, refLocator } from "./pw-session.js";
|
||||
|
||||
function fakePage(): Page {
|
||||
const handlers = new Map<string, Array<(...args: unknown[]) => void>>();
|
||||
const on = vi.fn((event: string, cb: (...args: unknown[]) => void) => {
|
||||
const list = handlers.get(event) ?? [];
|
||||
list.push(cb);
|
||||
handlers.set(event, list);
|
||||
return undefined as unknown;
|
||||
});
|
||||
const getByRole = vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) }));
|
||||
const frameLocator = vi.fn(() => ({
|
||||
getByRole: vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) })),
|
||||
}));
|
||||
const locator = vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) }));
|
||||
|
||||
return {
|
||||
on,
|
||||
getByRole,
|
||||
frameLocator,
|
||||
locator,
|
||||
} as unknown as Page;
|
||||
}
|
||||
|
||||
describe("pw-session refLocator", () => {
|
||||
it("uses frameLocator for role refs when snapshot was scoped to a frame", () => {
|
||||
const page = fakePage();
|
||||
const state = ensurePageState(page);
|
||||
state.roleRefs = { e1: { role: "button", name: "OK" } };
|
||||
state.roleRefsFrameSelector = "iframe#main";
|
||||
|
||||
refLocator(page, "e1");
|
||||
|
||||
expect(
|
||||
page.frameLocator as unknown as ReturnType<typeof vi.fn>,
|
||||
).toHaveBeenCalledWith("iframe#main");
|
||||
});
|
||||
|
||||
it("uses page getByRole for role refs by default", () => {
|
||||
const page = fakePage();
|
||||
const state = ensurePageState(page);
|
||||
state.roleRefs = { e1: { role: "button", name: "OK" } };
|
||||
|
||||
refLocator(page, "e1");
|
||||
|
||||
expect(
|
||||
page.getByRole as unknown as ReturnType<typeof vi.fn>,
|
||||
).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -232,4 +232,69 @@ describe("pw-tools-core", () => {
|
||||
expect(dismiss).toHaveBeenCalled();
|
||||
expect(accept).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("waits for selector, url, load state, and function", async () => {
|
||||
const waitForSelector = vi.fn(async () => {});
|
||||
const waitForURL = vi.fn(async () => {});
|
||||
const waitForLoadState = vi.fn(async () => {});
|
||||
const waitForFunction = vi.fn(async () => {});
|
||||
const waitForTimeout = vi.fn(async () => {});
|
||||
|
||||
currentPage = {
|
||||
locator: vi.fn(() => ({
|
||||
first: () => ({ waitFor: waitForSelector }),
|
||||
})),
|
||||
waitForURL,
|
||||
waitForLoadState,
|
||||
waitForFunction,
|
||||
waitForTimeout,
|
||||
getByText: vi.fn(() => ({ first: () => ({ waitFor: vi.fn() }) })),
|
||||
};
|
||||
|
||||
const mod = await importModule();
|
||||
await mod.waitForViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
selector: "#main",
|
||||
url: "**/dash",
|
||||
loadState: "networkidle",
|
||||
fn: "window.ready===true",
|
||||
timeoutMs: 1234,
|
||||
timeMs: 50,
|
||||
});
|
||||
|
||||
expect(waitForTimeout).toHaveBeenCalledWith(50);
|
||||
expect(
|
||||
currentPage.locator as ReturnType<typeof vi.fn>,
|
||||
).toHaveBeenCalledWith("#main");
|
||||
expect(waitForSelector).toHaveBeenCalledWith({
|
||||
state: "visible",
|
||||
timeout: 1234,
|
||||
});
|
||||
expect(waitForURL).toHaveBeenCalledWith("**/dash", { timeout: 1234 });
|
||||
expect(waitForLoadState).toHaveBeenCalledWith("networkidle", {
|
||||
timeout: 1234,
|
||||
});
|
||||
expect(waitForFunction).toHaveBeenCalledWith("window.ready===true", {
|
||||
timeout: 1234,
|
||||
});
|
||||
});
|
||||
|
||||
it("rewrites strict mode violations into snapshot hints", async () => {
|
||||
const click = vi.fn(async () => {
|
||||
throw new Error(
|
||||
'Error: strict mode violation: locator("aria-ref=1") resolved to 2 elements',
|
||||
);
|
||||
});
|
||||
currentRefLocator = { click };
|
||||
currentPage = {};
|
||||
|
||||
const mod = await importModule();
|
||||
await expect(
|
||||
mod.clickViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
targetId: "T1",
|
||||
ref: "1",
|
||||
}),
|
||||
).rejects.toThrow(/Run a new snapshot/i);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user