dsh-plugin-ui-actions 0.0.0-stage → 0.2.10
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/LICENSE +21 -0
- package/README.md +129 -2
- package/README.zh-CN.md +130 -0
- package/package.json +36 -4
- package/src/index.mjs +184 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsh-plugin-e2e contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,130 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-plugin-ui-actions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Plugin-agnostic Playwright actions for driving the DeepSeek Harness (DSH) web UI in tests: launch a browser, get past the onboarding dialog, flip localStorage settings, mock a POST, type a draft, click/wait, measure element geometry, and compare pixel and ARIA goldens.
|
|
6
|
+
|
|
7
|
+
This is a **pure library** — no `bin`, no CLI. Every action is `async` and takes a Playwright `page` as its **first argument**, except `launchBrowser`, which creates one.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -D dsh-plugin-ui-actions
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- Node >= 22.15.0.
|
|
16
|
+
- Dependencies: `playwright-core`, `pngjs`, `pixelmatch`.
|
|
17
|
+
- No browsers are bundled. Pass `channel: 'msedge'` to drive system Edge, or install Playwright's bundled Chromium with `npx dsh-plugin-e2e install-browser`.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import {
|
|
23
|
+
launchBrowser,
|
|
24
|
+
dismissOnboarding,
|
|
25
|
+
enableSetting,
|
|
26
|
+
typeDraft,
|
|
27
|
+
clickAttr,
|
|
28
|
+
smoke,
|
|
29
|
+
shot,
|
|
30
|
+
} from 'dsh-plugin-ui-actions'
|
|
31
|
+
|
|
32
|
+
const { browser, context, page } = await launchBrowser({ channel: 'msedge' })
|
|
33
|
+
await page.goto('http://127.0.0.1:5173/')
|
|
34
|
+
|
|
35
|
+
await dismissOnboarding(page) // first-run "preview build" notice, if present
|
|
36
|
+
await enableSetting(page, 'my-plugin.enabled', '1') // plugins often store flags in localStorage
|
|
37
|
+
await page.reload()
|
|
38
|
+
|
|
39
|
+
if (!(await smoke('data-my-plugin'))) throw new Error('plugin did not mount')
|
|
40
|
+
|
|
41
|
+
await typeDraft(page, 'hello from a test')
|
|
42
|
+
await clickAttr(page, 'data-my-plugin')
|
|
43
|
+
await shot(page, 'artifacts/after-click.png')
|
|
44
|
+
|
|
45
|
+
await browser.close()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
All 14 exports. `page` is a Playwright `Page` unless stated otherwise.
|
|
51
|
+
|
|
52
|
+
| Export | Signature | Returns | Behavior |
|
|
53
|
+
|---|---|---|---|
|
|
54
|
+
| `launchBrowser` | `launchBrowser(opts = {})` | `Promise<{ browser, context, page }>` | Launches Chromium (or `opts.channel`), opens a context and one page. |
|
|
55
|
+
| `dismissOnboarding` | `dismissOnboarding(page)` | `Promise<void>` | Clicks the first button matching `继续` / `Continue` / `Got it` / `知道了` (4 s timeout), then waits 800 ms. No-op when absent. |
|
|
56
|
+
| `enableSetting` | `enableSetting(page, key, value)` | `Promise<void>` | `localStorage.setItem(key, String(value))`. |
|
|
57
|
+
| `mockPost` | `mockPost(page, pattern, body, status = 200)` | `Promise<void>` | `page.route(pattern)` fulfilling with `JSON.stringify(body)` as `application/json`. |
|
|
58
|
+
| `typeDraft` | `typeDraft(page, text)` | `Promise<void>` | Clicks the first `[contenteditable="true"]`, types `text` via the keyboard, waits 600 ms. |
|
|
59
|
+
| `clickAttr` | `clickAttr(page, attr)` | `Promise<void>` | Force-clicks the first `[attr]` element, waits 1200 ms. |
|
|
60
|
+
| `click` | `click(page, selector, opts = {})` | `Promise<void>` | Clicks the first match, then waits `opts.settleMs` (default 300 ms). |
|
|
61
|
+
| `waitFor` | `waitFor(page, selector, timeout = 15000)` | `Promise<void>` | Waits for the first match to reach `state: 'visible'`. |
|
|
62
|
+
| `smoke` | `smoke(page, attr)` | `Promise<boolean>` | `true` if `[attr]` matches at least one element — pass the full attribute name (e.g. `'data-my-plugin'`), i.e. proof the plugin actually mounted. |
|
|
63
|
+
| `rects` | `rects(page, prevSel, cardSel)` | `Promise<object>` | Geometry of two elements plus their left/width delta and vertical gap. |
|
|
64
|
+
| `shot` | `shot(page, filePath)` | `Promise<string>` | Screenshots to `filePath` and returns it. |
|
|
65
|
+
| `golden` | `golden(page, name, opts)` | `Promise<object>` | Pixel golden compare with tolerance; `opts.goldenDir` is required. |
|
|
66
|
+
| `ariaSnapshot` | `ariaSnapshot(page, sel = 'body')` | `Promise<string>` | `Locator.ariaSnapshot()` for `sel`. |
|
|
67
|
+
| `goldenAria` | `goldenAria(page, name, opts)` | `Promise<object>` | Normalized ARIA golden compare with line tolerance; `opts.dir` is required. |
|
|
68
|
+
|
|
69
|
+
### `launchBrowser` options
|
|
70
|
+
|
|
71
|
+
| Option | Default | Meaning |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `headless` | `true` | Anything other than `false` keeps it headless. |
|
|
74
|
+
| `channel` | none | `'msedge'` (system Edge) or any Playwright channel. `'chromium'` / `'bundled'` are treated as "no channel". |
|
|
75
|
+
| `viewport` | `{ width: 1440, height: 900 }` | Context viewport. |
|
|
76
|
+
|
|
77
|
+
`chromiumSandbox` is always `false`.
|
|
78
|
+
|
|
79
|
+
### `golden(page, name, opts)`
|
|
80
|
+
|
|
81
|
+
| Option | Default | Meaning |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `goldenDir` | — | **Required.** Directory for `<name>.png`; created if missing. |
|
|
84
|
+
| `sel` | full page | Screenshot only this element. |
|
|
85
|
+
| `stabilize` | `true` | Inject `animation/transition: none` and await `document.fonts.ready`. |
|
|
86
|
+
| `pixelThreshold` | `0.1` | `pixelmatch` per-pixel color threshold. |
|
|
87
|
+
| `maxDiffRatio` | `0.002` | Allowed differing-pixel **ratio** — 0.2% of the image. |
|
|
88
|
+
|
|
89
|
+
Returns `{ status: 'created', file }` on first run, `{ status: 'match', file, diffPixels, diffRatio }` within tolerance, or `{ status: 'diff', file, diffFile?, diffPixels?, diffRatio?, reason? }` — a `.diff.png` is written next to the golden, and a size mismatch short-circuits to `status: 'diff'` with a `reason`. The tolerance is **inclusive**: at 100×100 (10,000 px) with the default `0.002`, 20 differing pixels is `match` (ratio `0.002`) and 21 is `diff` (ratio `0.0021`) — both verified.
|
|
90
|
+
|
|
91
|
+
### `goldenAria(page, name, opts)`
|
|
92
|
+
|
|
93
|
+
| Option | Default | Meaning |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `dir` | — | **Required.** Directory for `<name>.aria.txt`. |
|
|
96
|
+
| `sel` | `'body'` | Element to snapshot. |
|
|
97
|
+
| `maxDiffLines` | `max(3, ceil(2% of the larger snapshot's line count))` | Allowed differing lines. |
|
|
98
|
+
|
|
99
|
+
Snapshots are normalized first (trim each line, collapse whitespace, drop empty lines). Returns `{ status: 'created' | 'updated' | 'match' | 'diff', file, diffLines? }`; on `diff` the new snapshot is written to `<name>.aria.diff.txt`. Set `GOLDEN_UPDATE=1` to rewrite the baseline (status `updated`).
|
|
100
|
+
|
|
101
|
+
`diffLines` is a **multiset difference**, so one changed line counts as **2** (one line missing from the baseline, one extra in the new snapshot). With the default floor of 3, that single edit still passes as `match`; verified alongside a 200-line rewrite, which reported `diffLines: 202` and `status: 'diff'`.
|
|
102
|
+
|
|
103
|
+
### `rects` return value
|
|
104
|
+
|
|
105
|
+
`{ found: false, hasPreview, hasCard }` when either selector is missing; otherwise:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
{
|
|
109
|
+
found: true,
|
|
110
|
+
preview: { left, top, bottom, width }, // rounded px
|
|
111
|
+
card: { left, top, width },
|
|
112
|
+
leftDiff, // preview.left - card.left
|
|
113
|
+
widthDiff, // preview.width - card.width
|
|
114
|
+
gapAboveCard, // card.top - preview.bottom
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Notes
|
|
119
|
+
|
|
120
|
+
- **`page` is always the first argument** — including `click`, `waitFor` and `smoke`. `dsh-plugin-e2e` spec helpers look inconsistent only because they pre-bind `page`: there `click(sel)` / `waitFor(sel)` / `smoke(attr)` / `dismissOnboarding()` are page-bound wrappers, while `enableSetting` / `mockPost` / `typeDraft` / `clickAttr` / `rects` still take `page` from the spec. This package itself never hides `page`.
|
|
121
|
+
- **Waits are baked in**: `typeDraft` 600 ms, `clickAttr` 1200 ms, `click` 300 ms (`settleMs`), `dismissOnboarding` 800 ms. Do not assert on DOM state immediately after a click; raise `settleMs` if your plugin debounces.
|
|
122
|
+
- **`goldenDir` / `dir` are effectively required**: `mkdir(undefined)` throws `TypeError: The "path" argument must be of type string...` before any comparison happens, so you get a confusing error rather than a golden file. `dsh-plugin-e2e` always supplies them.
|
|
123
|
+
- `golden` compares pixels, not bytes — a 0.2% tolerance absorbs cross-machine font and anti-aliasing jitter. `goldenAria` compares text lines, so it survives layout shifts but not content changes.
|
|
124
|
+
- `ariaSnapshot` throws `ariaSnapshot 不可用(需较新 Playwright)` when the installed Playwright lacks `Locator.ariaSnapshot`.
|
|
125
|
+
- `enableSetting` writes a string; call it before the reload/`goto` that your plugin reads the flag on.
|
|
126
|
+
- `dsh-plugin-e2e/browser` re-exports this package verbatim, so existing `dsh-plugin-e2e/browser` imports keep working.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# dsh-plugin-ui-actions
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
驱动 DeepSeek Harness(DSH)Web UI 的**插件无关** Playwright 动作库:起浏览器、关首启引导弹窗、写 localStorage 开关、拦 POST 请求做 mock、键入草稿、点击/等待、量元素几何、像素金标准与 aria 金标准比对。
|
|
6
|
+
|
|
7
|
+
这是**纯库**——没有 `bin`,没有 CLI。除 `launchBrowser` 自己造 `page` 外,其余每个动作都是 `async`,且**首参固定是 Playwright 的 `page`**。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -D dsh-plugin-ui-actions
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- Node >= 22.15.0。
|
|
16
|
+
- 依赖:`playwright-core`、`pngjs`、`pixelmatch`。
|
|
17
|
+
- 不带浏览器。传 `channel: 'msedge'` 用系统 Edge,或用 `npx dsh-plugin-e2e install-browser` 装 Playwright 自带的 Chromium。
|
|
18
|
+
|
|
19
|
+
## 快速开始
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import {
|
|
23
|
+
launchBrowser,
|
|
24
|
+
dismissOnboarding,
|
|
25
|
+
enableSetting,
|
|
26
|
+
typeDraft,
|
|
27
|
+
clickAttr,
|
|
28
|
+
smoke,
|
|
29
|
+
shot,
|
|
30
|
+
} from 'dsh-plugin-ui-actions'
|
|
31
|
+
|
|
32
|
+
const { browser, context, page } = await launchBrowser({ channel: 'msedge' })
|
|
33
|
+
await page.goto('http://127.0.0.1:5173/')
|
|
34
|
+
|
|
35
|
+
await dismissOnboarding(page) // 首启的「预览版说明」弹窗,没有就跳过
|
|
36
|
+
await enableSetting(page, 'my-plugin.enabled', '1') // 插件开关多以 localStorage 存
|
|
37
|
+
await page.reload()
|
|
38
|
+
|
|
39
|
+
if (!(await smoke('data-my-plugin'))) throw new Error('插件没挂上')
|
|
40
|
+
|
|
41
|
+
await typeDraft(page, 'hello from a test')
|
|
42
|
+
await clickAttr(page, 'data-my-plugin')
|
|
43
|
+
await shot(page, 'artifacts/after-click.png')
|
|
44
|
+
|
|
45
|
+
await browser.close()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
共 14 个导出。除特别说明外,`page` 都是 Playwright 的 `Page`。
|
|
51
|
+
|
|
52
|
+
| 导出 | 签名 | 返回 | 行为 |
|
|
53
|
+
|---|---|---|---|
|
|
54
|
+
| `launchBrowser` | `launchBrowser(opts = {})` | `Promise<{ browser, context, page }>` | 启动 Chromium(或 `opts.channel`),建一个 context 和一个 page。 |
|
|
55
|
+
| `dismissOnboarding` | `dismissOnboarding(page)` | `Promise<void>` | 点第一个文案匹配 `继续` / `Continue` / `Got it` / `知道了` 的按钮(4 秒超时),再等 800 ms。没有就跳过。 |
|
|
56
|
+
| `enableSetting` | `enableSetting(page, key, value)` | `Promise<void>` | `localStorage.setItem(key, String(value))`。 |
|
|
57
|
+
| `mockPost` | `mockPost(page, pattern, body, status = 200)` | `Promise<void>` | 用 `page.route(pattern)` 拦截,返回 `JSON.stringify(body)`,`content-type` 为 `application/json`。 |
|
|
58
|
+
| `typeDraft` | `typeDraft(page, text)` | `Promise<void>` | 点第一个 `[contenteditable="true"]`,键盘输入 `text`,等 600 ms。 |
|
|
59
|
+
| `clickAttr` | `clickAttr(page, attr)` | `Promise<void>` | 强制点第一个带 `[attr]` 的元素,等 1200 ms。 |
|
|
60
|
+
| `click` | `click(page, selector, opts = {})` | `Promise<void>` | 点第一个匹配项,再等 `opts.settleMs`(默认 300 ms)。 |
|
|
61
|
+
| `waitFor` | `waitFor(page, selector, timeout = 15000)` | `Promise<void>` | 等第一个匹配项到 `state: 'visible'`。 |
|
|
62
|
+
| `smoke` | `smoke(page, attr)` | `Promise<boolean>` | `[attr]` 至少匹配到一个元素时为 `true`——`attr` 要传完整属性名(如 `'data-my-plugin'`),即插件真的挂上了的证据。 |
|
|
63
|
+
| `rects` | `rects(page, prevSel, cardSel)` | `Promise<object>` | 两个元素的几何,外加左右/宽度差与垂直间距。 |
|
|
64
|
+
| `shot` | `shot(page, filePath)` | `Promise<string>` | 截图到 `filePath` 并原样返回该路径。 |
|
|
65
|
+
| `golden` | `golden(page, name, opts)` | `Promise<object>` | 像素金标准比对(带容差);`opts.goldenDir` 必填。 |
|
|
66
|
+
| `ariaSnapshot` | `ariaSnapshot(page, sel = 'body')` | `Promise<string>` | 对 `sel` 调 `Locator.ariaSnapshot()`。 |
|
|
67
|
+
| `goldenAria` | `goldenAria(page, name, opts)` | `Promise<object>` | 归一化后的 aria 金标准比对(带行级容差);`opts.dir` 必填。 |
|
|
68
|
+
|
|
69
|
+
### `launchBrowser` 选项
|
|
70
|
+
|
|
71
|
+
| 选项 | 默认 | 含义 |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `headless` | `true` | 只有显式 `false` 才开有头。 |
|
|
74
|
+
| `channel` | 无 | `'msedge'`(系统 Edge)或任意 Playwright channel。`'chromium'` / `'bundled'` 视为「不指定 channel」。 |
|
|
75
|
+
| `viewport` | `{ width: 1440, height: 900 }` | context 视口。 |
|
|
76
|
+
|
|
77
|
+
`chromiumSandbox` 恒为 `false`。
|
|
78
|
+
|
|
79
|
+
### `golden(page, name, opts)`
|
|
80
|
+
|
|
81
|
+
| 选项 | 默认 | 含义 |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `goldenDir` | — | **必填。** `<name>.png` 所在目录,不存在会自动创建。 |
|
|
84
|
+
| `sel` | 整页 | 只截这个元素。 |
|
|
85
|
+
| `stabilize` | `true` | 注入 `animation/transition: none` 并等 `document.fonts.ready`。 |
|
|
86
|
+
| `pixelThreshold` | `0.1` | `pixelmatch` 的单像素色差阈值。 |
|
|
87
|
+
| `maxDiffRatio` | `0.002` | 允许的差异像素**占比**——即 0.2%。 |
|
|
88
|
+
|
|
89
|
+
首次返回 `{ status: 'created', file }`;在容差内返回 `{ status: 'match', file, diffPixels, diffRatio }`;超出则返回 `{ status: 'diff', file, diffFile?, diffPixels?, diffRatio?, reason? }`——会在金标准旁写一个 `.diff.png`;尺寸不同则直接短路成 `status: 'diff'` 并带 `reason`。容差是**含等号**的:100×100(10000 px)配默认 `0.002` 时,20 个差异像素是 `match`(ratio `0.002`),21 个就是 `diff`(ratio `0.0021`)——两者均已实测。
|
|
90
|
+
|
|
91
|
+
### `goldenAria(page, name, opts)`
|
|
92
|
+
|
|
93
|
+
| 选项 | 默认 | 含义 |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `dir` | — | **必填。** `<name>.aria.txt` 所在目录。 |
|
|
96
|
+
| `sel` | `'body'` | 取快照的元素。 |
|
|
97
|
+
| `maxDiffLines` | `max(3, ceil(较大快照的行数 × 2%))` | 允许的差异行数。 |
|
|
98
|
+
|
|
99
|
+
快照先归一化(每行 trim、压缩空白、丢空行)。返回 `{ status: 'created' | 'updated' | 'match' | 'diff', file, diffLines? }`;`diff` 时把新快照写到 `<name>.aria.diff.txt`。设 `GOLDEN_UPDATE=1` 重建基线(状态为 `updated`)。
|
|
100
|
+
|
|
101
|
+
`diffLines` 是**多重集差异**,所以改一行会算成 **2**(基线少一行、新快照多一行)。默认下限是 3,因此这种单行改动仍判 `match`;已与「重写 200 行」一起实测,后者报 `diffLines: 202`、`status: 'diff'`。
|
|
102
|
+
|
|
103
|
+
### `rects` 返回值
|
|
104
|
+
|
|
105
|
+
任一选择器没找到时返回 `{ found: false, hasPreview, hasCard }`;否则:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
{
|
|
109
|
+
found: true,
|
|
110
|
+
preview: { left, top, bottom, width }, // 四舍五入到 px
|
|
111
|
+
card: { left, top, width },
|
|
112
|
+
leftDiff, // preview.left - card.left
|
|
113
|
+
widthDiff, // preview.width - card.width
|
|
114
|
+
gapAboveCard, // card.top - preview.bottom
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 注意事项
|
|
119
|
+
|
|
120
|
+
- **`page` 永远是首参**——`click`、`waitFor`、`smoke` 也不例外。`dsh-plugin-e2e` 的 spec helper 看着不一致,只是因为它们把 `page` 预绑了:那里 `click(sel)` / `waitFor(sel)` / `smoke(attr)` / `dismissOnboarding()` 是已绑 `page` 的包装,而 `enableSetting` / `mockPost` / `typeDraft` / `clickAttr` / `rects` 仍由 spec 自己传 `page`。本包自身从不隐藏 `page`。
|
|
121
|
+
- **等待是写死的**:`typeDraft` 600 ms、`clickAttr` 1200 ms、`click` 300 ms(`settleMs`)、`dismissOnboarding` 800 ms。点完别立刻断言 DOM;插件若有防抖,调大 `settleMs`。
|
|
122
|
+
- **`goldenDir` / `dir` 实际是必填**:`mkdir(undefined)` 会在任何比对发生之前抛 `TypeError: The "path" argument must be of type string...`,所以拿到的是一个莫名其妙的报错而不是金标准文件。`dsh-plugin-e2e` 总会传这两个值。
|
|
123
|
+
- `golden` 比的是像素不是字节——0.2% 容差用来吸收跨机器字体与抗锯齿抖动。`goldenAria` 比的是文本行,所以能容忍布局位移,但容忍不了内容变化。
|
|
124
|
+
- 装到的 Playwright 没有 `Locator.ariaSnapshot` 时,`ariaSnapshot` 抛 `ariaSnapshot 不可用(需较新 Playwright)`。
|
|
125
|
+
- `enableSetting` 写入的是字符串;要在插件读取该开关的那次 reload / `goto` **之前**调用。
|
|
126
|
+
- `dsh-plugin-e2e/browser` 原样转发本包,原有的 `dsh-plugin-e2e/browser` 导入继续可用。
|
|
127
|
+
|
|
128
|
+
## 许可证
|
|
129
|
+
|
|
130
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-ui-actions",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.10",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"description": "DSH UI automation kit (Playwright): launch, dismiss onboarding, set localStorage switches, mock POST, type draft, click/attr/waitFor, geometry, screenshot/golden pixel, aria snapshot — plugin-agnostic helpers for DSH UI testing.",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src/",
|
|
13
|
+
"README.md",
|
|
14
|
+
"README.zh-CN.md"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"playwright-core": "^1.49.0",
|
|
18
|
+
"pngjs": "^7.0.0",
|
|
19
|
+
"pixelmatch": "^6.0.0"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/metal-huang/dsh-devtools.git",
|
|
24
|
+
"directory": "packages/dsh-plugin-ui-actions"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org/"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"dsh": ">=0.2.2-rc.5",
|
|
32
|
+
"node": ">=22.15.0"
|
|
33
|
+
},
|
|
34
|
+
"dshTestedOn": [
|
|
35
|
+
"0.2.2-rc.5",
|
|
36
|
+
"0.2.3-alpha.2"
|
|
37
|
+
]
|
|
38
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// Playwright 薄封装:只做"插件无关"的通用动作,供各插件 spec 复用。
|
|
2
|
+
import { chromium } from 'playwright-core'
|
|
3
|
+
import { readFile, writeFile, mkdir } from 'node:fs/promises'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
export async function launchBrowser(opts = {}) {
|
|
7
|
+
const launch = { headless: opts.headless !== false, chromiumSandbox: false }
|
|
8
|
+
// channel 未给 / 'chromium' / 'bundled' → 用 Playwright 自带 chromium(Linux/CI);'msedge' → 系统 Edge。
|
|
9
|
+
if (opts.channel && opts.channel !== 'chromium' && opts.channel !== 'bundled') launch.channel = opts.channel
|
|
10
|
+
const browser = await chromium.launch(launch)
|
|
11
|
+
const context = await browser.newContext({ viewport: opts.viewport || { width: 1440, height: 900 } })
|
|
12
|
+
const page = await context.newPage()
|
|
13
|
+
return { browser, context, page }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 关掉首启的「预览版说明」引导弹窗(背景会 inert);没有就跳过。 */
|
|
17
|
+
export async function dismissOnboarding(page) {
|
|
18
|
+
const cont = page.locator('button', { hasText: /继续|Continue|Got it|知道了/i })
|
|
19
|
+
if (await cont.count()) {
|
|
20
|
+
try {
|
|
21
|
+
await cont.first().click({ timeout: 4000 })
|
|
22
|
+
await page.waitForTimeout(800)
|
|
23
|
+
} catch {}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 写 localStorage 开关(插件设置多以 localStorage 存)。 */
|
|
28
|
+
export async function enableSetting(page, key, value) {
|
|
29
|
+
await page.evaluate(([k, v]) => localStorage.setItem(k, v), [key, String(value)])
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 拦某个 POST 请求并返回固定 JSON(浏览器层 mock,避免真模型)。 */
|
|
33
|
+
export async function mockPost(page, pattern, body, status = 200) {
|
|
34
|
+
await page.route(pattern, (route) =>
|
|
35
|
+
route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body) }),
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 在主输入框键入草稿(Lexical contenteditable)。 */
|
|
40
|
+
export async function typeDraft(page, text) {
|
|
41
|
+
const ed = page.locator('[contenteditable="true"]').first()
|
|
42
|
+
await ed.click()
|
|
43
|
+
await page.keyboard.type(text)
|
|
44
|
+
await page.waitForTimeout(600)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 点带某 data-* 属性的按钮(如 data-dsh-prompt-wit)。 */
|
|
48
|
+
export async function clickAttr(page, attr) {
|
|
49
|
+
await page.locator(`[${attr}]`).first().click({ force: true })
|
|
50
|
+
await page.waitForTimeout(1200)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 取两个元素相对几何(left/width 差、垂直间距)。 */
|
|
54
|
+
export async function rects(page, prevSel, cardSel) {
|
|
55
|
+
return page.evaluate(
|
|
56
|
+
([ps, cs]) => {
|
|
57
|
+
const p = document.querySelector(ps)
|
|
58
|
+
const c = document.querySelector(cs)
|
|
59
|
+
const pr = p ? p.getBoundingClientRect() : null
|
|
60
|
+
const cr = c ? c.getBoundingClientRect() : null
|
|
61
|
+
if (!pr || !cr) return { found: false, hasPreview: !!p, hasCard: !!c }
|
|
62
|
+
return {
|
|
63
|
+
found: true,
|
|
64
|
+
preview: { left: Math.round(pr.left), top: Math.round(pr.top), bottom: Math.round(pr.bottom), width: Math.round(pr.width) },
|
|
65
|
+
card: { left: Math.round(cr.left), top: Math.round(cr.top), width: Math.round(cr.width) },
|
|
66
|
+
leftDiff: Math.round(pr.left) - Math.round(cr.left),
|
|
67
|
+
widthDiff: Math.round(pr.width) - Math.round(cr.width),
|
|
68
|
+
gapAboveCard: Math.round(cr.top) - Math.round(pr.bottom),
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
[prevSel, cardSel],
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 像素金标准:截图(可限定元素)与 golden/name.png 比较。
|
|
77
|
+
* 首次=创建(created),之后=精确 sha 比较(match/diff)。返回 {status, file, hash}。
|
|
78
|
+
*/
|
|
79
|
+
export async function golden(page, name, opts = {}) {
|
|
80
|
+
const dir = opts.goldenDir
|
|
81
|
+
await mkdir(dir, { recursive: true })
|
|
82
|
+
const file = join(dir, name + '.png')
|
|
83
|
+
// 稳定化:关动画/过渡、等字体加载,降低跨机器/跨次抖动
|
|
84
|
+
if (opts.stabilize !== false) {
|
|
85
|
+
try {
|
|
86
|
+
await page.addStyleTag({ content: '*,*::before,*::after{animation:none!important;transition:none!important;caret-color:transparent!important}' })
|
|
87
|
+
} catch {}
|
|
88
|
+
try {
|
|
89
|
+
await page.evaluate(() => document.fonts && document.fonts.ready)
|
|
90
|
+
} catch {}
|
|
91
|
+
}
|
|
92
|
+
const buf = opts.sel
|
|
93
|
+
? await page.locator(opts.sel).first().screenshot({ animations: 'disabled' })
|
|
94
|
+
: await page.screenshot({ animations: 'disabled' })
|
|
95
|
+
const prev = await readFile(file).catch(() => null)
|
|
96
|
+
if (prev === null) {
|
|
97
|
+
await writeFile(file, buf)
|
|
98
|
+
return { status: 'created', file }
|
|
99
|
+
}
|
|
100
|
+
// 容忍度像素比对(默认允许 0.2% 像素差),避免跨机器字体/抗锯齿导致误报
|
|
101
|
+
const pkgPNG = await import('pngjs')
|
|
102
|
+
const PNG = pkgPNG.PNG || pkgPNG.default?.PNG
|
|
103
|
+
const pmMod = await import('pixelmatch')
|
|
104
|
+
const pixelmatch = pmMod.default || pmMod
|
|
105
|
+
const a = PNG.sync.read(prev)
|
|
106
|
+
const b = PNG.sync.read(buf)
|
|
107
|
+
if (a.width !== b.width || a.height !== b.height) {
|
|
108
|
+
return { status: 'diff', file, reason: `尺寸不同 ${a.width}x${a.height} vs ${b.width}x${b.height}` }
|
|
109
|
+
}
|
|
110
|
+
const diff = new PNG({ width: a.width, height: a.height })
|
|
111
|
+
const diffPixels = pixelmatch(a.data, b.data, diff.data, a.width, a.height, { threshold: opts.pixelThreshold ?? 0.1 })
|
|
112
|
+
const diffRatio = diffPixels / (a.width * a.height)
|
|
113
|
+
const maxDiffRatio = opts.maxDiffRatio ?? 0.002
|
|
114
|
+
if (diffRatio <= maxDiffRatio) return { status: 'match', file, diffPixels, diffRatio }
|
|
115
|
+
const diffFile = join(dir, name + '.diff.png')
|
|
116
|
+
await writeFile(diffFile, PNG.sync.write(diff))
|
|
117
|
+
return { status: 'diff', file, diffFile, diffPixels, diffRatio }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 取 aria 快照(Playwright Locator.ariaSnapshot)。 */
|
|
121
|
+
export async function ariaSnapshot(page, sel = 'body') {
|
|
122
|
+
const loc = sel === 'body' ? page.locator('body') : page.locator(sel).first()
|
|
123
|
+
if (typeof loc.ariaSnapshot !== 'function') throw new Error('ariaSnapshot 不可用(需较新 Playwright)')
|
|
124
|
+
return loc.ariaSnapshot()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeAria(s) {
|
|
128
|
+
return String(s)
|
|
129
|
+
.split('\n')
|
|
130
|
+
.map((l) => l.trim().replace(/\s+/g, ' '))
|
|
131
|
+
.filter(Boolean)
|
|
132
|
+
.join('\n')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** aria 金标准:归一化 + 行级容差(默认允许 2% 或 ≥3 行差异),跨机器/小改动不误报;`GOLDEN_UPDATE=1` 重建基线。 */
|
|
136
|
+
export async function goldenAria(page, name, opts = {}) {
|
|
137
|
+
const dir = opts.dir
|
|
138
|
+
await mkdir(dir, { recursive: true })
|
|
139
|
+
const file = join(dir, name + '.aria.txt')
|
|
140
|
+
const snap = normalizeAria(await ariaSnapshot(page, opts.sel || 'body'))
|
|
141
|
+
const prevRaw = await readFile(file, 'utf8').catch(() => null)
|
|
142
|
+
if (prevRaw === null || process.env.GOLDEN_UPDATE === '1') {
|
|
143
|
+
await writeFile(file, snap)
|
|
144
|
+
return { status: prevRaw === null ? 'created' : 'updated', file }
|
|
145
|
+
}
|
|
146
|
+
const a = normalizeAria(prevRaw).split('\n')
|
|
147
|
+
const b = snap.split('\n')
|
|
148
|
+
const tally = new Map()
|
|
149
|
+
for (const l of a) tally.set(l, (tally.get(l) || 0) + 1)
|
|
150
|
+
let diffLines = 0
|
|
151
|
+
for (const l of b) {
|
|
152
|
+
const n = tally.get(l) || 0
|
|
153
|
+
if (n > 0) tally.set(l, n - 1)
|
|
154
|
+
else diffLines++
|
|
155
|
+
}
|
|
156
|
+
for (const v of tally.values()) diffLines += v
|
|
157
|
+
const maxDiffLines = opts.maxDiffLines ?? Math.max(3, Math.ceil(Math.max(a.length, b.length) * 0.02))
|
|
158
|
+
if (diffLines <= maxDiffLines) return { status: 'match', file, diffLines }
|
|
159
|
+
const diffFile = join(dir, name + '.aria.diff.txt')
|
|
160
|
+
await writeFile(diffFile, snap)
|
|
161
|
+
return { status: 'diff', file, diffFile, diffLines }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 截图到指定文件(spec 可直接用;CLI 的 h.shot 绑定到感知目录,签名 h.shot(name))。 */
|
|
165
|
+
export async function shot(page, filePath) {
|
|
166
|
+
await page.screenshot({ path: filePath })
|
|
167
|
+
return filePath
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 点任意选择器(不只 data-*)。 */
|
|
171
|
+
export async function click(page, selector, opts = {}) {
|
|
172
|
+
await page.locator(selector).first().click(opts)
|
|
173
|
+
await page.waitForTimeout(opts.settleMs ?? 300)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 等元素可见。 */
|
|
177
|
+
export async function waitFor(page, selector, timeout = 15000) {
|
|
178
|
+
await page.locator(selector).first().waitFor({ state: 'visible', timeout })
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 插件已装载的 DOM 证据:返回带该 data-* 属性的元素是否存在。 */
|
|
182
|
+
export async function smoke(page, attr) {
|
|
183
|
+
return (await page.locator(`[${attr}]`).count()) > 0
|
|
184
|
+
}
|