test(browser): extend automation coverage
This commit is contained in:
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildRoleSnapshotFromAriaSnapshot,
|
||||
getRoleSnapshotStats,
|
||||
parseRoleRef,
|
||||
} from "./pw-role-snapshot.js";
|
||||
|
||||
describe("pw-role-snapshot", () => {
|
||||
@@ -55,4 +56,19 @@ describe("pw-role-snapshot", () => {
|
||||
expect(stats.lines).toBeGreaterThan(0);
|
||||
expect(stats.chars).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("returns a helpful message when no interactive elements exist", () => {
|
||||
const aria = ['- heading "Hello"', "- paragraph: world"].join("\n");
|
||||
const res = buildRoleSnapshotFromAriaSnapshot(aria, { interactive: true });
|
||||
expect(res.snapshot).toBe("(no interactive elements)");
|
||||
expect(Object.keys(res.refs)).toEqual([]);
|
||||
});
|
||||
|
||||
it("parses role refs", () => {
|
||||
expect(parseRoleRef("e12")).toBe("e12");
|
||||
expect(parseRoleRef("@e12")).toBe("e12");
|
||||
expect(parseRoleRef("ref=e12")).toBe("e12");
|
||||
expect(parseRoleRef("12")).toBeNull();
|
||||
expect(parseRoleRef("")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,16 @@ import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { ensurePageState, refLocator } from "./pw-session.js";
|
||||
|
||||
function fakePage(): Page {
|
||||
function fakePage(): {
|
||||
page: Page;
|
||||
handlers: Map<string, Array<(...args: unknown[]) => void>>;
|
||||
mocks: {
|
||||
on: ReturnType<typeof vi.fn>;
|
||||
getByRole: ReturnType<typeof vi.fn>;
|
||||
frameLocator: ReturnType<typeof vi.fn>;
|
||||
locator: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
} {
|
||||
const handlers = new Map<string, Array<(...args: unknown[]) => void>>();
|
||||
const on = vi.fn((event: string, cb: (...args: unknown[]) => void) => {
|
||||
const list = handlers.get(event) ?? [];
|
||||
@@ -17,37 +26,82 @@ function fakePage(): Page {
|
||||
}));
|
||||
const locator = vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) }));
|
||||
|
||||
return {
|
||||
const page = {
|
||||
on,
|
||||
getByRole,
|
||||
frameLocator,
|
||||
locator,
|
||||
} as unknown as Page;
|
||||
|
||||
return { page, handlers, mocks: { on, getByRole, frameLocator, locator } };
|
||||
}
|
||||
|
||||
describe("pw-session refLocator", () => {
|
||||
it("uses frameLocator for role refs when snapshot was scoped to a frame", () => {
|
||||
const page = fakePage();
|
||||
const { page, mocks } = 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");
|
||||
expect(mocks.frameLocator).toHaveBeenCalledWith("iframe#main");
|
||||
});
|
||||
|
||||
it("uses page getByRole for role refs by default", () => {
|
||||
const page = fakePage();
|
||||
const { page, mocks } = 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();
|
||||
expect(mocks.getByRole).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("pw-session ensurePageState", () => {
|
||||
it("tracks page errors and network requests (best-effort)", () => {
|
||||
const { page, handlers } = fakePage();
|
||||
const state = ensurePageState(page);
|
||||
|
||||
const req = {
|
||||
method: () => "GET",
|
||||
url: () => "https://example.com/api",
|
||||
resourceType: () => "xhr",
|
||||
failure: () => ({ errorText: "net::ERR_FAILED" }),
|
||||
} as unknown as import("playwright-core").Request;
|
||||
|
||||
const resp = {
|
||||
request: () => req,
|
||||
status: () => 500,
|
||||
ok: () => false,
|
||||
} as unknown as import("playwright-core").Response;
|
||||
|
||||
handlers.get("request")?.[0]?.(req);
|
||||
handlers.get("response")?.[0]?.(resp);
|
||||
handlers.get("requestfailed")?.[0]?.(req);
|
||||
handlers.get("pageerror")?.[0]?.(new Error("boom"));
|
||||
|
||||
expect(state.errors.at(-1)?.message).toBe("boom");
|
||||
expect(state.requests.at(-1)).toMatchObject({
|
||||
method: "GET",
|
||||
url: "https://example.com/api",
|
||||
resourceType: "xhr",
|
||||
status: 500,
|
||||
ok: false,
|
||||
failureText: "net::ERR_FAILED",
|
||||
});
|
||||
});
|
||||
|
||||
it("drops state on page close", () => {
|
||||
const { page, handlers } = fakePage();
|
||||
const state1 = ensurePageState(page);
|
||||
handlers.get("close")?.[0]?.();
|
||||
|
||||
const state2 = ensurePageState(page);
|
||||
expect(state2).not.toBe(state1);
|
||||
expect(state2.console).toEqual([]);
|
||||
expect(state2.errors).toEqual([]);
|
||||
expect(state2.requests).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -297,4 +297,42 @@ describe("pw-tools-core", () => {
|
||||
}),
|
||||
).rejects.toThrow(/Run a new snapshot/i);
|
||||
});
|
||||
|
||||
it("rewrites not-visible timeouts into snapshot hints", async () => {
|
||||
const click = vi.fn(async () => {
|
||||
throw new Error(
|
||||
'Timeout 5000ms exceeded. waiting for locator("aria-ref=1") to be visible',
|
||||
);
|
||||
});
|
||||
currentRefLocator = { click };
|
||||
currentPage = {};
|
||||
|
||||
const mod = await importModule();
|
||||
await expect(
|
||||
mod.clickViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
targetId: "T1",
|
||||
ref: "1",
|
||||
}),
|
||||
).rejects.toThrow(/not found or not visible/i);
|
||||
});
|
||||
|
||||
it("rewrites covered/hidden errors into interactable hints", async () => {
|
||||
const click = vi.fn(async () => {
|
||||
throw new Error(
|
||||
"Element is not receiving pointer events because another element intercepts pointer events",
|
||||
);
|
||||
});
|
||||
currentRefLocator = { click };
|
||||
currentPage = {};
|
||||
|
||||
const mod = await importModule();
|
||||
await expect(
|
||||
mod.clickViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
targetId: "T1",
|
||||
ref: "1",
|
||||
}),
|
||||
).rejects.toThrow(/not interactable/i);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user