@a11y-pulse/browser-adaptor 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -3
- package/dist/adaptor.d.ts +7 -2
- package/dist/adaptors/playwright.d.ts +23 -0
- package/dist/adaptors/playwright.js +103 -0
- package/dist/adaptors/playwright.js.map +1 -0
- package/dist/adaptors/puppeteer.d.ts +13 -2
- package/dist/adaptors/puppeteer.js +12 -3
- package/dist/adaptors/puppeteer.js.map +1 -1
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Released under the [MIT License](#license).
|
|
|
14
14
|
npm install @a11y-pulse/browser-adaptor
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
`puppeteer`
|
|
17
|
+
`puppeteer` and `playwright-core` are optional peer dependencies. Each is only required if you use the matching bundled adaptor ([Puppeteer](#puppeteer-adaptor), [Playwright](#playwright-adaptor)).
|
|
18
18
|
|
|
19
19
|
## Exports
|
|
20
20
|
|
|
@@ -22,6 +22,7 @@ npm install @a11y-pulse/browser-adaptor
|
|
|
22
22
|
| --- | --- |
|
|
23
23
|
| `@a11y-pulse/browser-adaptor` | `BrowserAdaptor`, `ElementRef`, `Rect` |
|
|
24
24
|
| `@a11y-pulse/browser-adaptor/puppeteer` | `PuppeteerAdaptor` |
|
|
25
|
+
| `@a11y-pulse/browser-adaptor/playwright` | `PlaywrightAdaptor` |
|
|
25
26
|
| `@a11y-pulse/browser-adaptor/dom` | `getSelector`, `truncateHtml` |
|
|
26
27
|
|
|
27
28
|
## Quickstart
|
|
@@ -52,7 +53,8 @@ interface BrowserAdaptor {
|
|
|
52
53
|
disposeRef(ref: ElementRef): Promise<void>;
|
|
53
54
|
pressTab(): Promise<void>;
|
|
54
55
|
pressEnter(): Promise<void>;
|
|
55
|
-
screenshotClip(clip: Rect): Promise<Uint8Array>;
|
|
56
|
+
screenshotClip(clip: Rect, scale?: number): Promise<Uint8Array>;
|
|
57
|
+
readonly screenshotClipScale?: number;
|
|
56
58
|
ensureFocusReporting(): Promise<void>; // must not throw
|
|
57
59
|
}
|
|
58
60
|
```
|
|
@@ -61,7 +63,36 @@ Consumers may ignore methods they do not need. `ensureFocusReporting` is best-ef
|
|
|
61
63
|
|
|
62
64
|
## Puppeteer adaptor
|
|
63
65
|
|
|
64
|
-
`PuppeteerAdaptor` wraps a Puppeteer `Page`.
|
|
66
|
+
`PuppeteerAdaptor` wraps a Puppeteer `Page`. Its CDP session is cached per page, so reusing a page across audit runs does not open extra sessions.
|
|
67
|
+
|
|
68
|
+
## Playwright adaptor
|
|
69
|
+
|
|
70
|
+
`PlaywrightAdaptor` wraps a Playwright `Page`, and works with either `playwright` or `playwright-core`.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
import { PlaywrightAdaptor } from "@a11y-pulse/browser-adaptor/playwright";
|
|
74
|
+
import { chromium } from "playwright";
|
|
75
|
+
|
|
76
|
+
const browser = await chromium.launch();
|
|
77
|
+
const page = await browser.newPage({ deviceScaleFactor: 2 });
|
|
78
|
+
await page.goto("https://example.com/");
|
|
79
|
+
|
|
80
|
+
const adaptor = new PlaywrightAdaptor(page);
|
|
81
|
+
await adaptor.ensureFocusReporting();
|
|
82
|
+
await adaptor.pressTab();
|
|
83
|
+
|
|
84
|
+
await browser.close();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Two differences from the Puppeteer adaptor are worth knowing about.
|
|
88
|
+
|
|
89
|
+
**`evaluate` needs `unsafe-eval`.** Playwright's `evaluate` takes a single argument, so the adaptor packs the page function's source and its arguments into one tuple and rebuilds the function in the page with `new Function`. A page whose Content-Security-Policy omits `unsafe-eval` from `script-src` will reject that; the adaptor throws a message naming CSP as the cause rather than letting it read as a page failure. Puppeteer is unaffected, because CDP serialises the function itself.
|
|
90
|
+
|
|
91
|
+
**Screenshot scale depends on the engine.** On Chromium the adaptor screenshots over CDP, which honours `screenshotClipScale` per capture and captures clips that lie outside the viewport, so evidence images match Puppeteer's. Firefox and WebKit expose no CDP, so the adaptor falls back to a full-page capture at the context's own `deviceScaleFactor`. There, create the context with a `deviceScaleFactor` matching `screenshotClipScale` (2 by default), or pass the scale you want:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
new PlaywrightAdaptor(page, { screenshotClipScale: 1 });
|
|
95
|
+
```
|
|
65
96
|
|
|
66
97
|
## DOM helpers
|
|
67
98
|
|
package/dist/adaptor.d.ts
CHANGED
|
@@ -35,9 +35,14 @@ export interface BrowserAdaptor {
|
|
|
35
35
|
*/
|
|
36
36
|
pressEnter(): Promise<void>;
|
|
37
37
|
/**
|
|
38
|
-
* Screenshot a clipped region of the page defined by `clip`.
|
|
38
|
+
* Screenshot a clipped region of the page defined by `clip`. `scale` is the device scale factor
|
|
39
|
+
* (bitmap pixels per CSS pixel), multiplied against whatever deviceScaleFactor the page itself
|
|
40
|
+
* currently has. The caller is responsible for the page being at a known deviceScaleFactor before
|
|
41
|
+
* invoking this. This function must return PNG bytes.
|
|
39
42
|
*/
|
|
40
|
-
screenshotClip(clip: Rect): Promise<Uint8Array>;
|
|
43
|
+
screenshotClip(clip: Rect, scale?: number): Promise<Uint8Array>;
|
|
44
|
+
/** Device scale factor for evidence screenshots stored on failure. */
|
|
45
|
+
readonly screenshotClipScale?: number;
|
|
41
46
|
/**
|
|
42
47
|
* Ensure the page reports focus for its lifetime. In particular, `document.hasFocus()` must
|
|
43
48
|
* function correctly and :focus styles must be applied, even when the page is not the foreground.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
|
+
import type { BrowserAdaptor, ElementRef, Rect } from "../adaptor";
|
|
3
|
+
export type PlaywrightAdaptorOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Device scale factor for evidence screenshots. Defaults to 2. On Chromium any value is honoured
|
|
6
|
+
* per capture. Elsewhere this must match the `deviceScaleFactor` the browser context was created
|
|
7
|
+
* with, because Playwright's public screenshot API cannot rescale a single capture.
|
|
8
|
+
*/
|
|
9
|
+
screenshotClipScale?: number;
|
|
10
|
+
};
|
|
11
|
+
/** A BrowserAdaptor backed by a Playwright Page. */
|
|
12
|
+
export declare class PlaywrightAdaptor implements BrowserAdaptor {
|
|
13
|
+
private readonly page;
|
|
14
|
+
readonly screenshotClipScale: number;
|
|
15
|
+
constructor(page: Page, options?: PlaywrightAdaptorOptions);
|
|
16
|
+
evaluate<T>(fn: (...args: any[]) => T | Promise<T>, ...args: unknown[]): Promise<T>;
|
|
17
|
+
evaluateHandle(fn: () => Element | null): Promise<ElementRef>;
|
|
18
|
+
disposeRef(ref: ElementRef): Promise<void>;
|
|
19
|
+
pressTab(): Promise<void>;
|
|
20
|
+
pressEnter(): Promise<void>;
|
|
21
|
+
screenshotClip(clip: Rect, scale?: number): Promise<Uint8Array>;
|
|
22
|
+
ensureFocusReporting(): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/adaptors/playwright.ts
|
|
2
|
+
var cdpSessions = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
var focusEmulatedPages = /* @__PURE__ */ new WeakSet();
|
|
4
|
+
var DEFAULT_SCREENSHOT_CLIP_SCALE = 2;
|
|
5
|
+
var EVAL_BLOCKED = /EvalError|unsafe-eval|Content Security Policy/i;
|
|
6
|
+
function cdpSession(page) {
|
|
7
|
+
const existing = cdpSessions.get(page);
|
|
8
|
+
if (existing) {
|
|
9
|
+
return existing;
|
|
10
|
+
}
|
|
11
|
+
const session = page.context().newCDPSession(page).catch(() => null);
|
|
12
|
+
cdpSessions.set(page, session);
|
|
13
|
+
return session;
|
|
14
|
+
}
|
|
15
|
+
function isChromium(page) {
|
|
16
|
+
try {
|
|
17
|
+
return page.context().browser()?.browserType().name() === "chromium";
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
var PlaywrightAdaptor = class {
|
|
23
|
+
constructor(page, options = {}) {
|
|
24
|
+
this.page = page;
|
|
25
|
+
this.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;
|
|
26
|
+
}
|
|
27
|
+
page;
|
|
28
|
+
screenshotClipScale;
|
|
29
|
+
async evaluate(fn, ...args) {
|
|
30
|
+
try {
|
|
31
|
+
return await this.page.evaluate(
|
|
32
|
+
([source, packed]) => new Function(`return (${source})`)()(...packed),
|
|
33
|
+
[fn.toString(), args]
|
|
34
|
+
);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
37
|
+
if (EVAL_BLOCKED.test(message)) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"The Playwright adaptor rebuilds page functions with new Function(), which this page's Content-Security-Policy blocks. Allow 'unsafe-eval' in script-src, or use the Puppeteer adaptor.",
|
|
40
|
+
{ cause: error }
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
evaluateHandle(fn) {
|
|
47
|
+
return this.page.evaluateHandle(fn);
|
|
48
|
+
}
|
|
49
|
+
async disposeRef(ref) {
|
|
50
|
+
await ref.dispose();
|
|
51
|
+
}
|
|
52
|
+
async pressTab() {
|
|
53
|
+
await this.page.keyboard.press("Tab");
|
|
54
|
+
}
|
|
55
|
+
async pressEnter() {
|
|
56
|
+
await this.page.keyboard.press("Enter");
|
|
57
|
+
}
|
|
58
|
+
async screenshotClip(clip, scale = 1) {
|
|
59
|
+
const session = await cdpSession(this.page);
|
|
60
|
+
if (session) {
|
|
61
|
+
const { data } = await session.send("Page.captureScreenshot", {
|
|
62
|
+
format: "png",
|
|
63
|
+
captureBeyondViewport: true,
|
|
64
|
+
clip: { ...clip, scale }
|
|
65
|
+
});
|
|
66
|
+
return Buffer.from(data, "base64");
|
|
67
|
+
}
|
|
68
|
+
return await this.page.screenshot({
|
|
69
|
+
type: "png",
|
|
70
|
+
fullPage: true,
|
|
71
|
+
clip,
|
|
72
|
+
scale: "device"
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
async ensureFocusReporting() {
|
|
76
|
+
const session = await cdpSession(this.page);
|
|
77
|
+
if (!session) {
|
|
78
|
+
if (isChromium(this.page)) {
|
|
79
|
+
console.warn("Could not open a CDP session for the browser adaptor");
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
if (focusEmulatedPages.has(this.page)) {
|
|
85
|
+
await session.send("Emulation.setFocusEmulationEnabled", {
|
|
86
|
+
enabled: false
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
await session.send("Emulation.setFocusEmulationEnabled", {
|
|
90
|
+
enabled: true
|
|
91
|
+
});
|
|
92
|
+
focusEmulatedPages.add(this.page);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.warn("Could not enable focus emulation for the browser adaptor", {
|
|
95
|
+
error
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
export {
|
|
101
|
+
PlaywrightAdaptor
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=playwright.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adaptors/playwright.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"playwright-core\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\n\n// The CDP session for each page, shared by focus emulation and clipped screenshots. A fresh adaptor\n// is created per audit run, but a page often outlives one run, so without this each run would open\n// another session that is never detached. The session is kept attached for the page's lifetime:\n// Chrome clears focus emulation when the CDP client disconnects. Resolves to null on Firefox and\n// WebKit, where Playwright offers no CDP.\nconst cdpSessions = new WeakMap<Page, Promise<CDPSession | null>>();\n\n// Pages that have had focus emulation enabled at least once.\nconst focusEmulatedPages = new WeakSet<Page>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nconst EVAL_BLOCKED = /EvalError|unsafe-eval|Content Security Policy/i;\n\nfunction cdpSession(page: Page): Promise<CDPSession | null> {\n\tconst existing = cdpSessions.get(page);\n\n\tif (existing) {\n\t\treturn existing;\n\t}\n\n\tconst session = page\n\t\t.context()\n\t\t.newCDPSession(page)\n\t\t.catch(() => null);\n\tcdpSessions.set(page, session);\n\n\treturn session;\n}\n\nfunction isChromium(page: Page): boolean {\n\ttry {\n\t\treturn page.context().browser()?.browserType().name() === \"chromium\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nexport type PlaywrightAdaptorOptions = {\n\t/**\n\t * Device scale factor for evidence screenshots. Defaults to 2. On Chromium any value is honoured\n\t * per capture. Elsewhere this must match the `deviceScaleFactor` the browser context was created\n\t * with, because Playwright's public screenshot API cannot rescale a single capture.\n\t */\n\tscreenshotClipScale?: number;\n};\n\n/** A BrowserAdaptor backed by a Playwright Page. */\nexport class PlaywrightAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PlaywrightAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t}\n\n\tasync evaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\ttry {\n\t\t\t// Playwright takes a single argument, so the function source and its args travel packed\n\t\t\t// into one tuple. Handles nested in that tuple are still resolved to live elements.\n\t\t\treturn (await this.page.evaluate(\n\t\t\t\t([source, packed]) => new Function(`return (${source})`)()(...(packed as unknown[])),\n\t\t\t\t[fn.toString(), args] as [string, unknown[]],\n\t\t\t)) as T;\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\n\t\t\tif (EVAL_BLOCKED.test(message)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"The Playwright adaptor rebuilds page functions with new Function(), which this page's Content-Security-Policy blocks. Allow 'unsafe-eval' in script-src, or use the Puppeteer adaptor.\",\n\t\t\t\t\t{ cause: error },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (session) {\n\t\t\t// The public API cannot scale one capture, and its `clip` is only honoured inside the\n\t\t\t// viewport. CDP is what Puppeteer drives too, so evidence images stay pixel-comparable.\n\t\t\tconst { data } = await session.send(\"Page.captureScreenshot\", {\n\t\t\t\tformat: \"png\",\n\t\t\t\tcaptureBeyondViewport: true,\n\t\t\t\tclip: { ...clip, scale },\n\t\t\t});\n\n\t\t\treturn Buffer.from(data, \"base64\");\n\t\t}\n\n\t\t// Clips are document coordinates, and Playwright renders anything outside the viewport blank\n\t\t// unless the capture is full-page.\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\tfullPage: true,\n\t\t\tclip,\n\t\t\tscale: \"device\",\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\tconst session = await cdpSession(this.page);\n\n\t\tif (!session) {\n\t\t\t// Playwright emulates focus on Firefox and WebKit itself, so there is nothing to assert\n\t\t\t// and nothing worth warning about.\n\t\t\tif (isChromium(this.page)) {\n\t\t\t\tconsole.warn(\"Could not open a CDP session for the browser adaptor\");\n\t\t\t}\n\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tif (focusEmulatedPages.has(this.page)) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tawait session.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulatedPages.add(this.page);\n\t\t} catch (error) {\n\t\t\tconsole.warn(\"Could not enable focus emulation for the browser adaptor\", {\n\t\t\t\terror,\n\t\t\t});\n\t\t}\n\t}\n}\n"],"mappings":";AAQA,IAAM,cAAc,oBAAI,QAA0C;AAGlE,IAAM,qBAAqB,oBAAI,QAAc;AAE7C,IAAM,gCAAgC;AAEtC,IAAM,eAAe;AAErB,SAAS,WAAW,MAAwC;AAC3D,QAAM,WAAW,YAAY,IAAI,IAAI;AAErC,MAAI,UAAU;AACb,WAAO;AAAA,EACR;AAEA,QAAM,UAAU,KACd,QAAQ,EACR,cAAc,IAAI,EAClB,MAAM,MAAM,IAAI;AAClB,cAAY,IAAI,MAAM,OAAO;AAE7B,SAAO;AACR;AAEA,SAAS,WAAW,MAAqB;AACxC,MAAI;AACH,WAAO,KAAK,QAAQ,EAAE,QAAQ,GAAG,YAAY,EAAE,KAAK,MAAM;AAAA,EAC3D,QAAQ;AACP,WAAO;AAAA,EACR;AACD;AAYO,IAAM,oBAAN,MAAkD;AAAA,EAGxD,YACkB,MACjB,UAAoC,CAAC,GACpC;AAFgB;AAGjB,SAAK,sBAAsB,QAAQ,uBAAuB;AAAA,EAC3D;AAAA,EAJkB;AAAA,EAHT;AAAA,EAST,MAAM,SAEL,OACG,MACU;AACb,QAAI;AAGH,aAAQ,MAAM,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AS;AAGb,UAAI,WAAW,KAAK,IAAI,GAAG;AAC1B,gBAAQ,KAAK,sDAAsD;AAAA,MACpE;AAEA;AAAA,IACD;AAEA,QAAI;AACH,UAAI,mBAAmB,IAAI,KAAK,IAAI,GAAG;AAEtC,cAAM,QAAQ,KAAK,sCAAsC;AAAA,UACxD,SAAS;AAAA,QACV,CAAC;AAAA,MACF;AAEA,YAAM,QAAQ,KAAK,sCAAsC;AAAA,QACxD,SAAS;AAAA,MACV,CAAC;AACD,yBAAmB,IAAI,KAAK,IAAI;AAAA,IACjC,SAAS,OAAO;AACf,cAAQ,KAAK,4DAA4D;AAAA,QACxE;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;","names":[]}
|
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
import type { Page } from "puppeteer";
|
|
2
2
|
import type { BrowserAdaptor, ElementRef, Rect } from "../adaptor";
|
|
3
|
+
export type PuppeteerAdaptorOptions = {
|
|
4
|
+
/** Device scale factor for evidence screenshots. Defaults to 2, applied per capture. */
|
|
5
|
+
screenshotClipScale?: number;
|
|
6
|
+
/**
|
|
7
|
+
* Trade PNG compression for capture speed. Defaults to true, which roughly doubles the encoded
|
|
8
|
+
* size of each capture without changing a single pixel.
|
|
9
|
+
*/
|
|
10
|
+
optimizeForSpeed?: boolean;
|
|
11
|
+
};
|
|
3
12
|
/** A BrowserAdaptor backed by a Puppeteer Page. */
|
|
4
13
|
export declare class PuppeteerAdaptor implements BrowserAdaptor {
|
|
5
14
|
private readonly page;
|
|
6
|
-
|
|
15
|
+
readonly screenshotClipScale: number;
|
|
16
|
+
private readonly optimizeForSpeed;
|
|
17
|
+
constructor(page: Page, options?: PuppeteerAdaptorOptions);
|
|
7
18
|
evaluate<T>(fn: (...args: any[]) => T | Promise<T>, ...args: unknown[]): Promise<T>;
|
|
8
19
|
evaluateHandle(fn: () => Element | null): Promise<ElementRef>;
|
|
9
20
|
disposeRef(ref: ElementRef): Promise<void>;
|
|
10
21
|
pressTab(): Promise<void>;
|
|
11
22
|
pressEnter(): Promise<void>;
|
|
12
|
-
screenshotClip(clip: Rect): Promise<Uint8Array>;
|
|
23
|
+
screenshotClip(clip: Rect, scale?: number): Promise<Uint8Array>;
|
|
13
24
|
ensureFocusReporting(): Promise<void>;
|
|
14
25
|
}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
// src/adaptors/puppeteer.ts
|
|
2
2
|
var focusEmulationSessions = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
var DEFAULT_SCREENSHOT_CLIP_SCALE = 2;
|
|
3
4
|
var PuppeteerAdaptor = class {
|
|
4
|
-
constructor(page) {
|
|
5
|
+
constructor(page, options = {}) {
|
|
5
6
|
this.page = page;
|
|
7
|
+
this.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;
|
|
8
|
+
this.optimizeForSpeed = options.optimizeForSpeed ?? true;
|
|
6
9
|
}
|
|
7
10
|
page;
|
|
11
|
+
screenshotClipScale;
|
|
12
|
+
optimizeForSpeed;
|
|
8
13
|
evaluate(fn, ...args) {
|
|
9
14
|
return this.page.evaluate(fn, ...args);
|
|
10
15
|
}
|
|
@@ -20,8 +25,12 @@ var PuppeteerAdaptor = class {
|
|
|
20
25
|
async pressEnter() {
|
|
21
26
|
await this.page.keyboard.press("Enter");
|
|
22
27
|
}
|
|
23
|
-
async screenshotClip(clip) {
|
|
24
|
-
return await this.page.screenshot({
|
|
28
|
+
async screenshotClip(clip, scale = 1) {
|
|
29
|
+
return await this.page.screenshot({
|
|
30
|
+
type: "png",
|
|
31
|
+
optimizeForSpeed: this.optimizeForSpeed,
|
|
32
|
+
clip: { ...clip, scale }
|
|
33
|
+
});
|
|
25
34
|
}
|
|
26
35
|
async ensureFocusReporting() {
|
|
27
36
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adaptors/puppeteer.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"puppeteer\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\n\n// The CDP session holding focus emulation for each page. A fresh adaptor is created per audit run,\n// but a page often outlives one run, so without this each run would open another CDP session that\n// is never detached. The session is kept attached for the page's lifetime: Chrome clears the\n// emulation when the CDP client disconnects.\nconst focusEmulationSessions = new WeakMap<Page, CDPSession>();\n\n/** A BrowserAdaptor backed by a Puppeteer Page. */\nexport class PuppeteerAdaptor implements BrowserAdaptor {\n\tconstructor(
|
|
1
|
+
{"version":3,"sources":["../../src/adaptors/puppeteer.ts"],"sourcesContent":["import type { CDPSession, JSHandle, Page } from \"puppeteer\";\nimport type { BrowserAdaptor, ElementRef, Rect } from \"../adaptor\";\n\n// The CDP session holding focus emulation for each page. A fresh adaptor is created per audit run,\n// but a page often outlives one run, so without this each run would open another CDP session that\n// is never detached. The session is kept attached for the page's lifetime: Chrome clears the\n// emulation when the CDP client disconnects.\nconst focusEmulationSessions = new WeakMap<Page, CDPSession>();\n\nconst DEFAULT_SCREENSHOT_CLIP_SCALE = 2;\n\nexport type PuppeteerAdaptorOptions = {\n\t/** Device scale factor for evidence screenshots. Defaults to 2, applied per capture. */\n\tscreenshotClipScale?: number;\n\n\t/**\n\t * Trade PNG compression for capture speed. Defaults to true, which roughly doubles the encoded\n\t * size of each capture without changing a single pixel.\n\t */\n\toptimizeForSpeed?: boolean;\n};\n\n/** A BrowserAdaptor backed by a Puppeteer Page. */\nexport class PuppeteerAdaptor implements BrowserAdaptor {\n\treadonly screenshotClipScale: number;\n\n\tprivate readonly optimizeForSpeed: boolean;\n\n\tconstructor(\n\t\tprivate readonly page: Page,\n\t\toptions: PuppeteerAdaptorOptions = {},\n\t) {\n\t\tthis.screenshotClipScale = options.screenshotClipScale ?? DEFAULT_SCREENSHOT_CLIP_SCALE;\n\t\tthis.optimizeForSpeed = options.optimizeForSpeed ?? true;\n\t}\n\n\tevaluate<T>(\n\t\t// biome-ignore lint/suspicious/noExplicitAny: the page-evaluated fn accepts arbitrary serialised args and element handles.\n\t\tfn: (...args: any[]) => T | Promise<T>,\n\t\t...args: unknown[]\n\t): Promise<T> {\n\t\t// Puppeteer resolves any JSHandle passed as an arg to the live element.\n\t\treturn this.page.evaluate(fn as never, ...(args as never[])) as Promise<T>;\n\t}\n\n\tevaluateHandle(fn: () => Element | null): Promise<ElementRef> {\n\t\treturn this.page.evaluateHandle(fn);\n\t}\n\n\tasync disposeRef(ref: ElementRef): Promise<void> {\n\t\tawait (ref as JSHandle).dispose();\n\t}\n\n\tasync pressTab(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Tab\");\n\t}\n\n\tasync pressEnter(): Promise<void> {\n\t\tawait this.page.keyboard.press(\"Enter\");\n\t}\n\n\tasync screenshotClip(clip: Rect, scale = 1): Promise<Uint8Array> {\n\t\treturn (await this.page.screenshot({\n\t\t\ttype: \"png\",\n\t\t\toptimizeForSpeed: this.optimizeForSpeed,\n\t\t\tclip: { ...clip, scale },\n\t\t})) as Uint8Array;\n\t}\n\n\tasync ensureFocusReporting(): Promise<void> {\n\t\t// Pages only report focus while they are the foreground tab. Since this browser instance is\n\t\t// likely running in the background, we need to enable focus emulation to get accurate results.\n\t\t//\n\t\t// Note this function does not throw if enabling emulation fails; it is a best-effort.\n\t\ttry {\n\t\t\tconst existing = focusEmulationSessions.get(this.page);\n\n\t\t\tif (existing) {\n\t\t\t\t// Chromium ignores a redundant enable, so re-asserting has to toggle.\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: false,\n\t\t\t\t});\n\t\t\t\tawait existing.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\t\tenabled: true,\n\t\t\t\t});\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst client = await this.page.createCDPSession();\n\t\t\tawait client.send(\"Emulation.setFocusEmulationEnabled\", {\n\t\t\t\tenabled: true,\n\t\t\t});\n\t\t\tfocusEmulationSessions.set(this.page, client);\n\t\t} catch (error) {\n\t\t\tconsole.warn(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D,YAAM,OAAO,KAAK,sCAAsC;AAAA,QACvD,SAAS;AAAA,MACV,CAAC;AACD,6BAAuB,IAAI,KAAK,MAAM,MAAM;AAAA,IAC7C,SAAS,OAAO;AACf,cAAQ,KAAK,4DAA4D;AAAA,QACxE;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a11y-pulse/browser-adaptor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Shared browser adaptor primitives and DOM helpers for A11y Pulse accessibility audits.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"accessibility",
|
|
19
19
|
"a11y",
|
|
20
20
|
"puppeteer",
|
|
21
|
+
"playwright",
|
|
21
22
|
"browser-adaptor",
|
|
22
23
|
"audit"
|
|
23
24
|
],
|
|
@@ -35,6 +36,11 @@
|
|
|
35
36
|
"types": "./dist/adaptors/puppeteer.d.ts",
|
|
36
37
|
"import": "./dist/adaptors/puppeteer.js"
|
|
37
38
|
},
|
|
39
|
+
"./playwright": {
|
|
40
|
+
"source": "./src/adaptors/playwright.ts",
|
|
41
|
+
"types": "./dist/adaptors/playwright.d.ts",
|
|
42
|
+
"import": "./dist/adaptors/playwright.js"
|
|
43
|
+
},
|
|
38
44
|
"./dom": {
|
|
39
45
|
"source": "./src/dom.ts",
|
|
40
46
|
"types": "./dist/dom.d.ts",
|
|
@@ -59,15 +65,20 @@
|
|
|
59
65
|
"access": "public"
|
|
60
66
|
},
|
|
61
67
|
"peerDependencies": {
|
|
62
|
-
"
|
|
68
|
+
"playwright-core": ">=1.40",
|
|
69
|
+
"puppeteer": "^25.10.0"
|
|
63
70
|
},
|
|
64
71
|
"peerDependenciesMeta": {
|
|
72
|
+
"playwright-core": {
|
|
73
|
+
"optional": true
|
|
74
|
+
},
|
|
65
75
|
"puppeteer": {
|
|
66
76
|
"optional": true
|
|
67
77
|
}
|
|
68
78
|
},
|
|
69
79
|
"devDependencies": {
|
|
70
80
|
"happy-dom": "^20.12.0",
|
|
71
|
-
"
|
|
81
|
+
"playwright-core": "^1.63.0",
|
|
82
|
+
"puppeteer": "^25.10.0"
|
|
72
83
|
}
|
|
73
84
|
}
|