@jieay/dsh-tester 0.1.4 → 0.1.7
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 +18 -7
- package/cordis.patch.yml +2 -2
- package/docs/DETAIL.md +18 -3
- package/index.js +3 -60
- package/lib/flow.js +60 -12
- package/lib/report.js +173 -0
- package/lib/run.js +11 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -68,19 +68,30 @@ HTTP ≥ 400 的响应、截图路径,以及被截断的证据条数。
|
|
|
68
68
|
> 审计会在未登录状态下跑完,把登录页当业务页报「通过」。用 `expectUrl` 指向登录后的地址最稳妥;
|
|
69
69
|
> 一旦报告里出现「跳到登录页」,说明这次审计无效。`web_flow` 返回每一步的通过/失败,加上同样的页面证据和失败截图。
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
插件在**项目根目录**下只占用一个 `.dsh-tester/` 文件夹,所有产物按类型归类,不会把项目目录搅乱:
|
|
72
72
|
|
|
73
73
|
```text
|
|
74
|
-
|
|
75
|
-
└──
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
<项目目录>/
|
|
75
|
+
└── .dsh-tester/
|
|
76
|
+
└── test-reports/
|
|
77
|
+
├── 2026-12-04_13-44-20_audit/
|
|
78
|
+
│ ├── report.md ← 可直接阅读的报告
|
|
79
|
+
│ ├── audit.json ← 完整结构化证据
|
|
80
|
+
│ ├── 192.168.192.25_index.php_m_my_f_index_98db71.png
|
|
81
|
+
│ └── 192.168.192.25_index.php_m_qa_f_index_505bfd.png
|
|
82
|
+
└── 2026-12-04_13-45-01_flow/
|
|
83
|
+
├── report.md
|
|
84
|
+
├── flow.json
|
|
85
|
+
└── final.png
|
|
79
86
|
```
|
|
80
87
|
|
|
81
88
|
文件夹名是「本地开始时间 + 测试类型」,截图文件名包含该页面的完整路由(含 query),所以
|
|
82
89
|
`index.php?m=my&f=index` 和 `index.php?m=bug&f=browse` 不会互相覆盖。
|
|
83
90
|
|
|
91
|
+
**每次运行都会写一份 `report.md`**:结论表格、失败页面证据、以及内嵌的截图(相对路径,
|
|
92
|
+
在 VSCode、Typora 或代码托管页面上可以直接预览)。不打开 DSH 也能翻历史报告,
|
|
93
|
+
需要归档时把某个文件夹整个提交或附到工单里即可。
|
|
94
|
+
|
|
84
95
|
## 浏览器依赖
|
|
85
96
|
|
|
86
97
|
唯一依赖是 `playwright-core`,所以安装这个插件**不会下载任何浏览器**。浏览器在调用时才确定,顺序是:
|
|
@@ -98,7 +109,7 @@ test-reports/
|
|
|
98
109
|
|
|
99
110
|
| 配置 | 默认 | 含义 |
|
|
100
111
|
| --- | --- | --- |
|
|
101
|
-
| `artifactsDir` |
|
|
112
|
+
| `artifactsDir` | `.dsh-tester/test-reports` | 证据根目录,相对会话工作目录;每次运行在其下建时间戳子目录 |
|
|
102
113
|
| `channel` | 自动 | 优先使用的浏览器通道:`chrome`、`chrome-beta`、`msedge`、`chromium` |
|
|
103
114
|
| `headless` | `true` | 无头运行;设为 `false` 可以看它真的在点页面 |
|
|
104
115
|
| `settleMs` | `1200` | 每次导航后等待客户端渲染的静默期 |
|
package/cordis.patch.yml
CHANGED
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
也禁止根据 HTTP 状态码推断「前端正常」。服务端返回 200 但页面白屏、JS 报错、按钮无响应的情况非常常见。
|
|
60
60
|
例外:只读取证可以用 HTTP 请求,但不能据此下前端结论。
|
|
61
61
|
2. 前端测试必须留截图。每次测试都在项目目录下生成一个带时间戳的文件夹
|
|
62
|
-
(默认
|
|
62
|
+
(默认 `.dsh-tester/test-reports/<日期>_<时间>_<audit|flow>/`),里面必须包含每个页面的截图和一份 JSON 证据。
|
|
63
63
|
报告里给出该文件夹的绝对路径,供人工复核。
|
|
64
64
|
|
|
65
65
|
## 工具纪律
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
- id: tester-tools
|
|
111
111
|
name: '@jieay/dsh-tester'
|
|
112
112
|
config:
|
|
113
|
-
artifactsDir: test-reports
|
|
113
|
+
artifactsDir: .dsh-tester/test-reports
|
|
114
114
|
# 默认无头运行,行为可预测、不弹窗;想亲眼看它点页面时,
|
|
115
115
|
# 让模型在调用里传 headless=false,或把这里改成 false。
|
|
116
116
|
headless: true
|
package/docs/DETAIL.md
CHANGED
|
@@ -52,6 +52,9 @@ web_flow({
|
|
|
52
52
|
|
|
53
53
|
失败时自动截图并保留当时的页面证据,不会因为一步失败就丢掉整个报告。
|
|
54
54
|
|
|
55
|
+
iframe 说明:服务端渲染应用常把业务界面装在 iframe 里。`web_flow` 的选择器会跨 frame 查找,
|
|
56
|
+
`expectText` 不带 target 时汇总所有 frame 的文本。但**跨域 iframe** 仍不可操作(浏览器安全限制)。
|
|
57
|
+
|
|
55
58
|
选择器说明:`target` 就是 Playwright 选择器,`#id`、`.class`、`role=button[name="登录"]`、
|
|
56
59
|
`button:has-text("登录")` 都可以。注意 `text=登录` 可能命中标题而不是按钮,点击类操作优先用
|
|
57
60
|
`role=` 或 `:has-text()`。
|
|
@@ -72,8 +75,20 @@ web_flow({
|
|
|
72
75
|
|
|
73
76
|
## 产物位置
|
|
74
77
|
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
插件在项目根目录下只创建一个 `.dsh-tester/` 目录,按用途分类存放:
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
.dsh-tester/
|
|
82
|
+
└── test-reports/
|
|
83
|
+
└── <日期>_<时间>_<audit|flow>/
|
|
84
|
+
├── report.md 可直接阅读的报告(内嵌截图)
|
|
85
|
+
├── audit.json 完整结构化证据
|
|
86
|
+
└── <host>_<path>_<query>_<hash>.png 每页一张截图
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
每次运行一个独立文件夹,不会覆盖历史结果。`report.md` 里的截图使用相对路径,
|
|
90
|
+
所以整个文件夹可以单独移动、归档或提交进工单,不会失效。`.dsh-tester/` 下后续还会放文档类报告,
|
|
91
|
+
所以建议把它整体加入 `.gitignore`(本插件的 `.gitignore` 模板已默认包含)。截图文件名包含完整路由(含 query),
|
|
77
92
|
同名路径的页面不会互相覆盖。
|
|
78
93
|
每份报告末尾都会给出证据目录的绝对路径。
|
|
79
94
|
|
|
@@ -89,7 +104,7 @@ web_flow({
|
|
|
89
104
|
|
|
90
105
|
| 配置 | 默认 | 说明 |
|
|
91
106
|
| --- | --- | --- |
|
|
92
|
-
| `artifactsDir` |
|
|
107
|
+
| `artifactsDir` | `.dsh-tester/test-reports` | 证据根目录,相对会话工作目录 |
|
|
93
108
|
| `channel` | 自动 | 优先使用的浏览器通道,如 `chrome`、`msedge`、`chromium` |
|
|
94
109
|
| `headless` | `true` | 无头运行;想看它真的在点页面就设为 `false` |
|
|
95
110
|
| `settleMs` | `1200` | 每次导航后等待客户端渲染的静默期 |
|
package/index.js
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
import { readFileSync } from 'node:fs'
|
|
22
22
|
import { artifactDir, auditPages, resolveUrl, runFlow } from './lib/run.js'
|
|
23
|
+
import { auditReportMarkdown, flowReportMarkdown } from './lib/report.js'
|
|
23
24
|
|
|
24
25
|
export const name = 'tester-tools'
|
|
25
26
|
|
|
@@ -52,45 +53,7 @@ function workingDirectoryOf(ctx, agent) {
|
|
|
52
53
|
* @returns {Array<{type: string, text: string}>} one text block.
|
|
53
54
|
*/
|
|
54
55
|
function renderAudit(value) {
|
|
55
|
-
|
|
56
|
-
lines.push(`浏览器 ${value.browser} · 页面 ${value.summary.pages} 个 · 失败 ${value.summary.failed} 个`)
|
|
57
|
-
if (!value.setup.ok) lines.push(`\n前置步骤失败:${value.setup.detail}`)
|
|
58
|
-
lines.push('', '| 页面 | 状态 | 结果 | 关键证据 |', '| --- | --- | --- | --- |')
|
|
59
|
-
for (const page of value.pages) {
|
|
60
|
-
const problems = []
|
|
61
|
-
if (page.whiteScreen) problems.push('白屏')
|
|
62
|
-
if (page.errorOverlay) problems.push(`错误覆盖层 ${page.errorOverlay}`)
|
|
63
|
-
if (page.pageErrors.length) problems.push(`未捕获异常 ${page.pageErrors.length}`)
|
|
64
|
-
if (page.httpErrors.length) problems.push(`HTTP 错误 ${page.httpErrors.length}`)
|
|
65
|
-
if (page.navigationError) problems.push('导航失败')
|
|
66
|
-
if (page.redirectedToLogin) problems.push('跳到登录页(会话未登录)')
|
|
67
|
-
if (page.consoleErrors.length) problems.push(`console.error ${page.consoleErrors.length}`)
|
|
68
|
-
lines.push(`| ${page.url} | ${page.status} | ${page.ok ? '通过' : '失败'} | ${problems.join(';') || '—'} |`)
|
|
69
|
-
}
|
|
70
|
-
const broken = value.pages.filter((page) => !page.ok)
|
|
71
|
-
if (broken.length > 0) {
|
|
72
|
-
lines.push('', '## 失败页面证据')
|
|
73
|
-
for (const page of broken) {
|
|
74
|
-
lines.push('', `### ${page.url}`, `- 标题:${page.title || '(空)'};正文长度 ${page.textLength}`
|
|
75
|
-
+ (page.frameCount > 0 ? `(顶层 ${page.topTextLength} 字 + ${page.frameCount} 个 iframe 内 ${page.textLength - page.topTextLength} 字)` : ''))
|
|
76
|
-
if (page.whiteScreen) lines.push('- 判定白屏:页面没有可见内容,通常是脚本报错或根组件未挂载')
|
|
77
|
-
if (page.errorOverlay) lines.push(`- 框架错误覆盖层:${page.errorOverlay}`)
|
|
78
|
-
if (page.navigationError) lines.push(`- 导航失败:${page.navigationError}`)
|
|
79
|
-
if (page.redirectedToLogin) {
|
|
80
|
-
lines.push(`- **落在登录页**:实际 URL 是 ${page.finalUrl}`)
|
|
81
|
-
lines.push(' → 这页的检查结果不可信:审计会话没有登录态,测到的是登录页而不是业务页。请检查 setupSteps 是否真的等到了登录完成。')
|
|
82
|
-
}
|
|
83
|
-
for (const error of page.pageErrors) lines.push(`- 未捕获异常:${error}`)
|
|
84
|
-
for (const error of page.httpErrors) lines.push(`- HTTP:${error}`)
|
|
85
|
-
for (const error of page.failedRequests) lines.push(`- 请求失败:${error}`)
|
|
86
|
-
for (const error of page.consoleErrors) lines.push(`- console.error:${error}`)
|
|
87
|
-
if (page.screenshot) lines.push(`- 截图:${page.screenshot}`)
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const dropped = value.pages.reduce((total, page) => total + page.omitted, 0)
|
|
91
|
-
if (dropped) lines.push('', `(另有 ${dropped} 条证据因长度上限未展示)`)
|
|
92
|
-
lines.push('', `证据目录:${value.artifactsDir}`)
|
|
93
|
-
return [{ type: 'text', text: lines.join('\n') }]
|
|
56
|
+
return [{ type: 'text', text: auditReportMarkdown(value) }]
|
|
94
57
|
}
|
|
95
58
|
|
|
96
59
|
/**
|
|
@@ -99,27 +62,7 @@ function renderAudit(value) {
|
|
|
99
62
|
* @returns {Array<{type: string, text: string}>} one text block.
|
|
100
63
|
*/
|
|
101
64
|
function renderFlow(value) {
|
|
102
|
-
|
|
103
|
-
lines.push(`起点 ${value.url} · 浏览器 ${value.browser} · 通过 ${value.passed} 步 / 失败 ${value.failed} 步`)
|
|
104
|
-
lines.push('', '| # | 动作 | 结果 | 说明 |', '| --- | --- | --- | --- |')
|
|
105
|
-
for (const step of value.steps) {
|
|
106
|
-
lines.push(`| ${step.index} | ${step.action} | ${step.ok ? '通过' : '失败'} | ${step.detail.replace(/\|/g, '\\|')} |`)
|
|
107
|
-
}
|
|
108
|
-
if (value.whiteScreen) lines.push('', '页面当前处于白屏状态。')
|
|
109
|
-
const problems = [
|
|
110
|
-
...value.pageErrors.map((error) => `未捕获异常:${error}`),
|
|
111
|
-
...value.httpErrors.map((error) => `HTTP:${error}`),
|
|
112
|
-
...value.failedRequests.map((error) => `请求失败:${error}`),
|
|
113
|
-
...value.consoleErrors.map((error) => `console.error:${error}`),
|
|
114
|
-
]
|
|
115
|
-
if (problems.length > 0) {
|
|
116
|
-
lines.push('', '## 页面证据')
|
|
117
|
-
for (const problem of problems) lines.push(`- ${problem}`)
|
|
118
|
-
}
|
|
119
|
-
if (value.screenshot) lines.push('', `截图:${value.screenshot}`)
|
|
120
|
-
if (value.omitted) lines.push(`(另有 ${value.omitted} 条证据因长度上限未展示)`)
|
|
121
|
-
lines.push('', `证据目录:${value.artifactsDir}`)
|
|
122
|
-
return [{ type: 'text', text: lines.join('\n') }]
|
|
65
|
+
return [{ type: 'text', text: flowReportMarkdown(value) }]
|
|
123
66
|
}
|
|
124
67
|
|
|
125
68
|
/**
|
package/lib/flow.js
CHANGED
|
@@ -45,6 +45,53 @@ export function isSupportedAction(action) {
|
|
|
45
45
|
return ACTIONS.has(action)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Resolve a target to the first locator that matches it, searching the main
|
|
51
|
+
* frame first and then every child frame.
|
|
52
|
+
*
|
|
53
|
+
* Server-rendered shells (ZenTao and friends) put the whole application body
|
|
54
|
+
* inside an `<iframe>`; Playwright's `page.locator` never looks into frames, so
|
|
55
|
+
* without this fallback every interaction inside the app body times out while
|
|
56
|
+
* the page looks perfectly rendered to a human.
|
|
57
|
+
* @param {import('playwright-core').Page} page the page under test.
|
|
58
|
+
* @param {string} target a Playwright selector.
|
|
59
|
+
* @returns {Promise<import('playwright-core').Locator>} a locator in the frame that matched.
|
|
60
|
+
*/
|
|
61
|
+
async function locateAcrossFrames(page, target) {
|
|
62
|
+
const main = page.locator(target)
|
|
63
|
+
if (await main.count().catch(() => 0) > 0) return main.first()
|
|
64
|
+
for (const frame of page.frames()) {
|
|
65
|
+
if (frame === page.mainFrame()) continue
|
|
66
|
+
try {
|
|
67
|
+
const locator = frame.locator(target)
|
|
68
|
+
if (await locator.count() > 0) return locator.first()
|
|
69
|
+
} catch {
|
|
70
|
+
// A detached or cross-origin frame cannot be searched; try the next one.
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Nothing matched anywhere: keep the main-frame locator so the error message
|
|
74
|
+
// names the selector the caller wrote.
|
|
75
|
+
return main.first()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Collect the visible text of the page and all readable child frames.
|
|
80
|
+
* @param {import('playwright-core').Page} page the page under test.
|
|
81
|
+
* @returns {Promise<string>} concatenated text.
|
|
82
|
+
*/
|
|
83
|
+
async function textAcrossFrames(page) {
|
|
84
|
+
const parts = []
|
|
85
|
+
for (const frame of page.frames()) {
|
|
86
|
+
try {
|
|
87
|
+
parts.push(await frame.evaluate(() => (document.body ? document.body.innerText : '')))
|
|
88
|
+
} catch {
|
|
89
|
+
// Cross-origin or detached frames are skipped.
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return parts.join('\n')
|
|
93
|
+
}
|
|
94
|
+
|
|
48
95
|
/**
|
|
49
96
|
* Run one assertion or action against a page.
|
|
50
97
|
* @param {import('playwright-core').Page} page the page under test.
|
|
@@ -81,34 +128,34 @@ export async function runStep(page, step, index, context) {
|
|
|
81
128
|
break
|
|
82
129
|
}
|
|
83
130
|
case 'click': {
|
|
84
|
-
await page
|
|
131
|
+
await (await locateAcrossFrames(page, target)).click({ timeout })
|
|
85
132
|
record.detail = `clicked ${target}`
|
|
86
133
|
break
|
|
87
134
|
}
|
|
88
135
|
case 'dblclick': {
|
|
89
|
-
await page
|
|
136
|
+
await (await locateAcrossFrames(page, target)).dblclick({ timeout })
|
|
90
137
|
record.detail = `double-clicked ${target}`
|
|
91
138
|
break
|
|
92
139
|
}
|
|
93
140
|
case 'fill': {
|
|
94
|
-
await page
|
|
141
|
+
await (await locateAcrossFrames(page, target)).fill(String(step.value ?? ''), { timeout })
|
|
95
142
|
record.detail = `filled ${target}`
|
|
96
143
|
break
|
|
97
144
|
}
|
|
98
145
|
case 'press': {
|
|
99
146
|
const key = String(step.key ?? step.value ?? '')
|
|
100
|
-
if (target) await page
|
|
147
|
+
if (target) await (await locateAcrossFrames(page, target)).press(key, { timeout })
|
|
101
148
|
else await page.keyboard.press(key)
|
|
102
149
|
record.detail = `pressed ${key}`
|
|
103
150
|
break
|
|
104
151
|
}
|
|
105
152
|
case 'select': {
|
|
106
|
-
await page
|
|
153
|
+
await (await locateAcrossFrames(page, target)).selectOption(String(step.value ?? ''), { timeout })
|
|
107
154
|
record.detail = `selected ${step.value} in ${target}`
|
|
108
155
|
break
|
|
109
156
|
}
|
|
110
157
|
case 'hover': {
|
|
111
|
-
await page
|
|
158
|
+
await (await locateAcrossFrames(page, target)).hover({ timeout })
|
|
112
159
|
record.detail = `hovered ${target}`
|
|
113
160
|
break
|
|
114
161
|
}
|
|
@@ -119,28 +166,29 @@ export async function runStep(page, step, index, context) {
|
|
|
119
166
|
break
|
|
120
167
|
}
|
|
121
168
|
case 'waitFor': {
|
|
122
|
-
await page
|
|
169
|
+
await (await locateAcrossFrames(page, target)).waitFor({ state: step.state === 'hidden' ? 'hidden' : 'visible', timeout })
|
|
123
170
|
record.detail = `waited for ${target} to become ${step.state === 'hidden' ? 'hidden' : 'visible'}`
|
|
124
171
|
break
|
|
125
172
|
}
|
|
126
173
|
case 'expectVisible': {
|
|
127
|
-
await page
|
|
174
|
+
await (await locateAcrossFrames(page, target)).waitFor({ state: 'visible', timeout })
|
|
128
175
|
record.detail = `${target} is visible`
|
|
129
176
|
break
|
|
130
177
|
}
|
|
131
178
|
case 'expectHidden': {
|
|
132
|
-
await page
|
|
179
|
+
await (await locateAcrossFrames(page, target)).waitFor({ state: 'hidden', timeout })
|
|
133
180
|
record.detail = `${target} is hidden`
|
|
134
181
|
break
|
|
135
182
|
}
|
|
136
183
|
case 'expectText': {
|
|
137
184
|
const expected = String(step.text ?? step.value ?? '')
|
|
138
|
-
const scope = target ? page.locator(target).first() : page.locator('body')
|
|
139
185
|
const deadline = Date.now() + timeout
|
|
140
186
|
let actual = ''
|
|
141
187
|
for (;;) {
|
|
142
188
|
try {
|
|
143
|
-
actual =
|
|
189
|
+
actual = target
|
|
190
|
+
? ((await (await locateAcrossFrames(page, target)).innerText({ timeout: 1000 })) ?? '')
|
|
191
|
+
: await textAcrossFrames(page)
|
|
144
192
|
} catch {
|
|
145
193
|
actual = ''
|
|
146
194
|
}
|
|
@@ -159,7 +207,7 @@ export async function runStep(page, step, index, context) {
|
|
|
159
207
|
let actual = ''
|
|
160
208
|
for (;;) {
|
|
161
209
|
try {
|
|
162
|
-
actual = await page
|
|
210
|
+
actual = await (await locateAcrossFrames(page, target)).inputValue({ timeout: 1000 })
|
|
163
211
|
} catch {
|
|
164
212
|
actual = ''
|
|
165
213
|
}
|
package/lib/report.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown reports for one test run.
|
|
3
|
+
*
|
|
4
|
+
* The same formatter feeds two consumers:
|
|
5
|
+
* - the tool result, where screenshot paths must stay absolute so the model and
|
|
6
|
+
* the UI can act on them;
|
|
7
|
+
* - the `report.md` written next to the evidence, where paths must be relative
|
|
8
|
+
* to that file so the document renders in an editor or on a code host.
|
|
9
|
+
*
|
|
10
|
+
* Keeping one formatter prevents the two from drifting apart.
|
|
11
|
+
* @module @jieay/dsh-tester/report
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { basename } from 'node:path'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Format a timestamp for the report header.
|
|
18
|
+
* @param {Date} date the moment of the run.
|
|
19
|
+
* @returns {string} `YYYY-MM-DD HH:mm:ss` in local time.
|
|
20
|
+
*/
|
|
21
|
+
export function reportTime(date = new Date()) {
|
|
22
|
+
const pad = (value) => String(value).padStart(2, '0')
|
|
23
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
|
24
|
+
+ ` ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Build the screenshot reference for one mode.
|
|
29
|
+
* @param {boolean} inFolder whether the output is a file inside the evidence folder.
|
|
30
|
+
* @param {string} path the absolute screenshot path.
|
|
31
|
+
* @param {string} label a human label for the image.
|
|
32
|
+
* @returns {string} a markdown line referencing the screenshot.
|
|
33
|
+
*/
|
|
34
|
+
function screenshotRef(inFolder, path, label) {
|
|
35
|
+
if (!path) return ''
|
|
36
|
+
const target = inFolder ? basename(path) : path
|
|
37
|
+
return inFolder ? `` : `- 截图:${target}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Format an audit report.
|
|
42
|
+
* @param {object} report the audit result.
|
|
43
|
+
* @param {object} [options] rendering options.
|
|
44
|
+
* @param {boolean} [options.inFolder] emit relative screenshot paths and image embeds.
|
|
45
|
+
* @param {Date} [options.generatedAt] the moment the report was produced.
|
|
46
|
+
* @returns {string} markdown text.
|
|
47
|
+
*/
|
|
48
|
+
export function auditReportMarkdown(report, options = {}) {
|
|
49
|
+
const inFolder = options.inFolder === true
|
|
50
|
+
const lines = [`# 页面审计:${report.ok ? '通过' : '发现问题'}`, '']
|
|
51
|
+
|
|
52
|
+
if (inFolder) {
|
|
53
|
+
lines.push(`- 时间:${reportTime(options.generatedAt)}`)
|
|
54
|
+
lines.push(`- 浏览器:${report.browser}`)
|
|
55
|
+
lines.push(`- 页面:${report.summary.pages} 个,失败 ${report.summary.failed} 个`)
|
|
56
|
+
if (report.setup && !report.setup.ok) lines.push(`- 前置步骤:失败(${report.setup.detail})`)
|
|
57
|
+
lines.push('')
|
|
58
|
+
} else {
|
|
59
|
+
lines.push(`浏览器 ${report.browser} · 页面 ${report.summary.pages} 个 · 失败 ${report.summary.failed} 个`)
|
|
60
|
+
if (!report.setup.ok) lines.push(`\n前置步骤失败:${report.setup.detail}`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
lines.push('', inFolder
|
|
64
|
+
? '| 页面 | 状态 | 结果 | 关键证据 | 截图 |'
|
|
65
|
+
: '| 页面 | 状态 | 结果 | 关键证据 |')
|
|
66
|
+
lines.push(inFolder ? '| --- | --- | --- | --- | --- |' : '| --- | --- | --- | --- |')
|
|
67
|
+
|
|
68
|
+
for (const page of report.pages) {
|
|
69
|
+
const problems = []
|
|
70
|
+
if (page.whiteScreen) problems.push('白屏')
|
|
71
|
+
if (page.errorOverlay) problems.push(`错误覆盖层 ${page.errorOverlay}`)
|
|
72
|
+
if (page.pageErrors.length) problems.push(`未捕获异常 ${page.pageErrors.length}`)
|
|
73
|
+
if (page.httpErrors.length) problems.push(`HTTP 错误 ${page.httpErrors.length}`)
|
|
74
|
+
if (page.navigationError) problems.push('导航失败')
|
|
75
|
+
if (page.redirectedToLogin) problems.push('跳到登录页(会话未登录)')
|
|
76
|
+
if (page.consoleErrors.length) problems.push(`console.error ${page.consoleErrors.length}`)
|
|
77
|
+
const cells = [page.url, page.status, page.ok ? '通过' : '失败', problems.join(';') || '—']
|
|
78
|
+
if (inFolder) cells.push(page.screenshot ? `[查看](${basename(page.screenshot)})` : '—')
|
|
79
|
+
lines.push(`| ${cells.join(' | ')} |`)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const broken = report.pages.filter((page) => !page.ok)
|
|
83
|
+
if (broken.length > 0) {
|
|
84
|
+
lines.push('', '## 失败页面证据')
|
|
85
|
+
for (const page of broken) {
|
|
86
|
+
lines.push('', `### ${page.url}`)
|
|
87
|
+
lines.push(`- 标题:${page.title || '(空)'};正文长度 ${page.textLength}`
|
|
88
|
+
+ (page.frameCount > 0
|
|
89
|
+
? `(顶层 ${page.topTextLength} 字 + ${page.frameCount} 个 iframe 内 ${page.textLength - page.topTextLength} 字)`
|
|
90
|
+
: ''))
|
|
91
|
+
if (page.whiteScreen) lines.push('- 判定白屏:页面没有可见内容,通常是脚本报错或根组件未挂载')
|
|
92
|
+
if (page.errorOverlay) lines.push(`- 框架错误覆盖层:${page.errorOverlay}`)
|
|
93
|
+
if (page.navigationError) lines.push(`- 导航失败:${page.navigationError}`)
|
|
94
|
+
if (page.redirectedToLogin) {
|
|
95
|
+
lines.push(`- **落在登录页**:实际 URL 是 ${page.finalUrl}`)
|
|
96
|
+
lines.push(' → 这页的检查结果不可信:审计会话没有登录态,测到的是登录页而不是业务页。请检查 setupSteps 是否真的等到了登录完成。')
|
|
97
|
+
}
|
|
98
|
+
for (const error of page.pageErrors) lines.push(`- 未捕获异常:${error}`)
|
|
99
|
+
for (const error of page.httpErrors) lines.push(`- HTTP:${error}`)
|
|
100
|
+
for (const error of page.failedRequests) lines.push(`- 请求失败:${error}`)
|
|
101
|
+
for (const error of page.consoleErrors) lines.push(`- console.error:${error}`)
|
|
102
|
+
if (page.screenshot) {
|
|
103
|
+
const ref = screenshotRef(inFolder, page.screenshot, `${page.url} 截图`)
|
|
104
|
+
lines.push(`- 截图:${basename(page.screenshot)}`)
|
|
105
|
+
if (inFolder) lines.push('', ref)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const dropped = report.pages.reduce((total, page) => total + page.omitted, 0)
|
|
111
|
+
if (dropped) lines.push('', `(另有 ${dropped} 条证据因长度上限未展示)`)
|
|
112
|
+
if (inFolder) {
|
|
113
|
+
lines.push('', '---', '', '同目录下的 `audit.json` 是这次运行的完整结构化证据。')
|
|
114
|
+
} else {
|
|
115
|
+
lines.push('', `证据目录:${report.artifactsDir}`)
|
|
116
|
+
}
|
|
117
|
+
return lines.join('\n')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Format a journey report.
|
|
122
|
+
* @param {object} report the flow result.
|
|
123
|
+
* @param {object} [options] rendering options; see {@link auditReportMarkdown}.
|
|
124
|
+
* @returns {string} markdown text.
|
|
125
|
+
*/
|
|
126
|
+
export function flowReportMarkdown(report, options = {}) {
|
|
127
|
+
const inFolder = options.inFolder === true
|
|
128
|
+
const lines = [`# 流程测试:${report.ok ? '通过' : '失败'}`, '']
|
|
129
|
+
|
|
130
|
+
if (inFolder) {
|
|
131
|
+
lines.push(`- 时间:${reportTime(options.generatedAt)}`)
|
|
132
|
+
lines.push(`- 起点:${report.url}`)
|
|
133
|
+
lines.push(`- 浏览器:${report.browser}`)
|
|
134
|
+
lines.push(`- 步骤:通过 ${report.passed} 步 / 失败 ${report.failed} 步`)
|
|
135
|
+
lines.push('')
|
|
136
|
+
} else {
|
|
137
|
+
lines.push(`起点 ${report.url} · 浏览器 ${report.browser} · 通过 ${report.passed} 步 / 失败 ${report.failed} 步`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
lines.push('', '| # | 动作 | 结果 | 说明 |', '| --- | --- | --- | --- |')
|
|
141
|
+
for (const step of report.steps) {
|
|
142
|
+
lines.push(`| ${step.index} | ${step.action} | ${step.ok ? '通过' : '失败'} | ${step.detail.replace(/\|/g, '\\|')} |`)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (report.whiteScreen) lines.push('', '页面当前处于白屏状态。')
|
|
146
|
+
|
|
147
|
+
const problems = [
|
|
148
|
+
...report.pageErrors.map((error) => `未捕获异常:${error}`),
|
|
149
|
+
...report.httpErrors.map((error) => `HTTP:${error}`),
|
|
150
|
+
...report.failedRequests.map((error) => `请求失败:${error}`),
|
|
151
|
+
...report.consoleErrors.map((error) => `console.error:${error}`),
|
|
152
|
+
]
|
|
153
|
+
if (problems.length > 0) {
|
|
154
|
+
lines.push('', '## 页面证据')
|
|
155
|
+
for (const problem of problems) lines.push(`- ${problem}`)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (report.screenshot) {
|
|
159
|
+
if (inFolder) {
|
|
160
|
+
lines.push('', `截图:${basename(report.screenshot)}`, '', screenshotRef(true, report.screenshot, '最终截图'))
|
|
161
|
+
} else {
|
|
162
|
+
lines.push('', `截图:${report.screenshot}`)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (report.omitted) lines.push(`(另有 ${report.omitted} 条证据因长度上限未展示)`)
|
|
166
|
+
|
|
167
|
+
if (inFolder) {
|
|
168
|
+
lines.push('', '---', '', '同目录下的 `flow.json` 是这次运行的完整结构化证据。')
|
|
169
|
+
} else {
|
|
170
|
+
lines.push('', `证据目录:${report.artifactsDir}`)
|
|
171
|
+
}
|
|
172
|
+
return lines.join('\n')
|
|
173
|
+
}
|
package/lib/run.js
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* @module @jieay/dsh-tester/run
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { writeFile } from 'node:fs/promises'
|
|
11
|
+
import { join, resolve } from 'node:path'
|
|
11
12
|
import {
|
|
12
13
|
PAGE_CAP,
|
|
13
14
|
UNKNOWN_STATUS,
|
|
@@ -22,8 +23,12 @@ import {
|
|
|
22
23
|
writeSidecar,
|
|
23
24
|
} from './audit.js'
|
|
24
25
|
import { launchBrowser } from './browser.js'
|
|
26
|
+
import { auditReportMarkdown, flowReportMarkdown } from './report.js'
|
|
25
27
|
import { runSteps } from './flow.js'
|
|
26
28
|
|
|
29
|
+
/** Default evidence root, relative to the session working directory. */
|
|
30
|
+
export const DEFAULT_ARTIFACTS_DIR = '.dsh-tester/test-reports'
|
|
31
|
+
|
|
27
32
|
/** Default settle delay after a navigation, for client-rendered apps. */
|
|
28
33
|
export const SETTLE_MS = 1200
|
|
29
34
|
|
|
@@ -185,6 +190,8 @@ export async function auditPages(request) {
|
|
|
185
190
|
summary: { pages: pages.length, failed: pages.filter((entry) => !entry.ok).length },
|
|
186
191
|
}
|
|
187
192
|
await writeSidecar(dir, 'audit', report)
|
|
193
|
+
await writeFile(join(dir, 'report.md'),
|
|
194
|
+
`${auditReportMarkdown(report, { inFolder: true })}\n`, 'utf8')
|
|
188
195
|
return report
|
|
189
196
|
} finally {
|
|
190
197
|
request.signal?.removeEventListener('abort', onAbort)
|
|
@@ -261,6 +268,8 @@ export async function runFlow(request) {
|
|
|
261
268
|
screenshot: finalShot,
|
|
262
269
|
}
|
|
263
270
|
await writeSidecar(dir, 'flow', report)
|
|
271
|
+
await writeFile(join(dir, 'report.md'),
|
|
272
|
+
`${flowReportMarkdown(report, { inFolder: true })}\n`, 'utf8')
|
|
264
273
|
return report
|
|
265
274
|
} finally {
|
|
266
275
|
request.signal?.removeEventListener('abort', onAbort)
|
|
@@ -275,6 +284,6 @@ export async function runFlow(request) {
|
|
|
275
284
|
* @returns {string} an absolute artifact parent directory.
|
|
276
285
|
*/
|
|
277
286
|
export function artifactDir(configured, workingDirectory) {
|
|
278
|
-
const base = configured && configured.trim() ? configured.trim() :
|
|
287
|
+
const base = configured && configured.trim() ? configured.trim() : DEFAULT_ARTIFACTS_DIR
|
|
279
288
|
return resolve(workingDirectory, base)
|
|
280
289
|
}
|