gz-laya-pilot 0.1.0 → 0.1.2
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/NOTICE.md +20 -2
- package/README.md +54 -2
- package/dist/agent/bridge.d.ts +21 -0
- package/dist/agent/bridge.js +158 -0
- package/dist/agent/bridge.js.map +1 -0
- package/dist/agent/report.d.ts +54 -0
- package/dist/agent/report.js +100 -0
- package/dist/agent/report.js.map +1 -0
- package/dist/agent/run.d.ts +19 -0
- package/dist/agent/run.js +121 -0
- package/dist/agent/run.js.map +1 -0
- package/dist/agent/schema.d.ts +42 -0
- package/dist/agent/schema.js +9 -0
- package/dist/agent/schema.js.map +1 -0
- package/dist/agent/worker.d.ts +1 -0
- package/dist/agent/worker.js +102 -0
- package/dist/agent/worker.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +7 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +14 -6
- package/src/agent/bridge.ts +180 -0
- package/src/agent/report.ts +139 -0
- package/src/agent/run.ts +136 -0
- package/src/agent/schema.ts +38 -0
- package/src/agent/worker.ts +114 -0
- package/src/cli.ts +5 -0
- package/src/index.ts +3 -0
- package/vendor/.gitattributes +5 -0
- package/vendor/UPSTREAM.json +168 -0
- package/vendor/laya-pilot/LICENSE +21 -0
- package/vendor/laya-pilot/README.en.md +116 -0
- package/vendor/laya-pilot/README.md +116 -0
- package/vendor/laya-pilot/ROADMAP.md +21 -0
- package/vendor/laya-pilot/config.example.json +14 -0
- package/vendor/laya-pilot/docs/architecture.md +21 -0
- package/vendor/laya-pilot/docs/browser-use-evaluation.md +32 -0
- package/vendor/laya-pilot/docs/demo.png +0 -0
- package/vendor/laya-pilot/docs/local-runtime.md +88 -0
- package/vendor/laya-pilot/docs/pr1-selection.md +33 -0
- package/vendor/laya-pilot/docs/retry-policy.md +14 -0
- package/vendor/laya-pilot/examples/demo-cases.xlsx +0 -0
- package/vendor/laya-pilot/examples/demo-server.mjs +19 -0
- package/vendor/laya-pilot/examples/demo.html +733 -0
- package/vendor/laya-pilot/lib/auth-state.mjs +141 -0
- package/vendor/laya-pilot/lib/browser-provider.mjs +43 -0
- package/vendor/laya-pilot/lib/diagnostics.mjs +210 -0
- package/vendor/laya-pilot/lib/discovery.mjs +474 -0
- package/vendor/laya-pilot/lib/dom.mjs +539 -0
- package/vendor/laya-pilot/lib/engine.mjs +879 -0
- package/vendor/laya-pilot/lib/form.mjs +377 -0
- package/vendor/laya-pilot/lib/language.mjs +273 -0
- package/vendor/laya-pilot/lib/model.mjs +271 -0
- package/vendor/laya-pilot/lib/page-capabilities.mjs +356 -0
- package/vendor/laya-pilot/lib/page-exploration.mjs +372 -0
- package/vendor/laya-pilot/lib/page-session.mjs +69 -0
- package/vendor/laya-pilot/lib/pending-writes.mjs +50 -0
- package/vendor/laya-pilot/lib/planner.mjs +127 -0
- package/vendor/laya-pilot/lib/project-config.mjs +95 -0
- package/vendor/laya-pilot/lib/readiness.mjs +179 -0
- package/vendor/laya-pilot/lib/related-records.mjs +328 -0
- package/vendor/laya-pilot/lib/replay-model.mjs +44 -0
- package/vendor/laya-pilot/lib/run-state.mjs +133 -0
- package/vendor/laya-pilot/lib/test-data.mjs +128 -0
- package/vendor/laya-pilot/lib/workflow-policy.mjs +68 -0
- package/vendor/laya-pilot/lib/workflow.mjs +2321 -0
- package/vendor/laya-pilot/lib/workflow_excel.py +462 -0
- package/vendor/laya-pilot/requirements-local.txt +2 -0
- package/vendor/laya-pilot/requirements.txt +1 -0
- package/vendor/laya-pilot/runner.mjs +452 -0
- package/vendor/laya-pilot/tests/api.test.mjs +98 -0
- package/vendor/laya-pilot/tests/browser/runtime.test.mjs +847 -0
- package/vendor/laya-pilot/tests/config.test.mjs +109 -0
- package/vendor/laya-pilot/tests/diagnostics.test.mjs +91 -0
- package/vendor/laya-pilot/tests/discovery.test.mjs +76 -0
- package/vendor/laya-pilot/tests/fixtures/local-model/laya.py +28 -0
- package/vendor/laya-pilot/tests/fixtures/local-model/torch.py +5 -0
- package/vendor/laya-pilot/tests/guards.test.mjs +43 -0
- package/vendor/laya-pilot/tests/language.test.mjs +39 -0
- package/vendor/laya-pilot/tests/planner.test.mjs +129 -0
- package/vendor/laya-pilot/tests/readiness.test.mjs +118 -0
- package/vendor/laya-pilot/tests/real-model.smoke.mjs +74 -0
- package/vendor/laya-pilot/tests/replay-model.test.mjs +90 -0
- package/vendor/laya-pilot/tests/retries.test.mjs +270 -0
- package/vendor/laya-pilot/tests/run-state.test.mjs +162 -0
- package/vendor/laya-pilot/tests/selected-pr1.test.mjs +309 -0
- package/vendor/laya-pilot/tests/test-data.test.mjs +83 -0
- package/vendor/laya-pilot/tests/test_workflow_excel.py +162 -0
- package/vendor/laya-pilot/tests/worker_test.py +33 -0
- package/vendor/laya-pilot/tests/workflow.test.mjs +98 -0
- package/vendor/laya-pilot/worker.py +135 -0
- package/vendor/patches/0001-native-field-recipes.patch +3009 -0
|
@@ -0,0 +1,879 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { Halt, expectation, lines, variables, parseData, norm, quoted } from './language.mjs';
|
|
4
|
+
import { observe, target, settle, assertionTarget, discoverRowLabels } from './dom.mjs';
|
|
5
|
+
import { waitForReady, readTables } from './readiness.mjs';
|
|
6
|
+
import { expandStep, semanticStep, scopeNames, approvalOperation } from './planner.mjs';
|
|
7
|
+
const writeName =
|
|
8
|
+
/(?:删除|移除|保存|提交|新增|创建|修改|编辑|上线|下线|授权|发布|导入|发送|支付|购买|确定|确认|应用|delete|remove|save|submit|create|edit|publish|send|pay|buy|import|confirm|apply)/i;
|
|
9
|
+
export class Engine {
|
|
10
|
+
constructor(page, laya, config) {
|
|
11
|
+
this.page = page;
|
|
12
|
+
this.laya = laya;
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.events = [];
|
|
15
|
+
this.actions = 0;
|
|
16
|
+
this.current = null;
|
|
17
|
+
this.failures = [];
|
|
18
|
+
this.navigationCount = 0;
|
|
19
|
+
this.baselines = new Map();
|
|
20
|
+
this.phase = 'setup';
|
|
21
|
+
this.navFailureStart = 0;
|
|
22
|
+
this.dirtyAreas = new Map();
|
|
23
|
+
page.on('response', (r) => {
|
|
24
|
+
if (r.status() >= 400) {
|
|
25
|
+
const u = new URL(r.url());
|
|
26
|
+
this.failures.push({
|
|
27
|
+
case: this.current?.key,
|
|
28
|
+
path: u.pathname,
|
|
29
|
+
status: r.status(),
|
|
30
|
+
resourceType: r.request().resourceType(),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async chooseTarget(name, kind = 'click', extras = {}) {
|
|
36
|
+
const actual = this.config.labels?.[name] || name;
|
|
37
|
+
if (actual !== name) console.log(' 站点名称映射:' + name + ' → ' + actual);
|
|
38
|
+
return target(this.page, this.laya, actual, {
|
|
39
|
+
kind,
|
|
40
|
+
meta: { case: this.current?.key, step: this.actions, original_target: name },
|
|
41
|
+
minProbability: this.config.minProbability,
|
|
42
|
+
minMargin: this.config.minMargin,
|
|
43
|
+
...extras,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
guard(control, step) {
|
|
47
|
+
if (control.role === 'tab') return;
|
|
48
|
+
if (!writeName.test(control.name)) return;
|
|
49
|
+
if (!this.config.allowWrite)
|
|
50
|
+
throw new Halt(
|
|
51
|
+
'写入未启用',
|
|
52
|
+
`当前为只读执行,步骤涉及“${control.name}”;在专用测试环境使用--allow-write才能执行。`,
|
|
53
|
+
);
|
|
54
|
+
if (!writeName.test(step))
|
|
55
|
+
throw new Halt('动作与用例不符', '模型选择写入操作,但原始步骤没有对应写入意图。');
|
|
56
|
+
}
|
|
57
|
+
async directClick(name, step) {
|
|
58
|
+
await this.revealRowMenu(name);
|
|
59
|
+
const rowKey = await this.targetRow(name);
|
|
60
|
+
const found = await this.chooseTarget(name, 'click', { rowKey });
|
|
61
|
+
this.guard(found.control, step);
|
|
62
|
+
if (
|
|
63
|
+
this.pendingPreconditions?.length &&
|
|
64
|
+
writeName.test(found.control.name) &&
|
|
65
|
+
found.control.role !== 'tab'
|
|
66
|
+
)
|
|
67
|
+
throw new Halt(
|
|
68
|
+
'前置条件未验证',
|
|
69
|
+
'业务写入前仍有未证明的前提:' + this.pendingPreconditions.join(';'),
|
|
70
|
+
);
|
|
71
|
+
if (found.control.href) {
|
|
72
|
+
const u = new URL(found.control.href, this.page.url());
|
|
73
|
+
if (!this.config.origins.includes(u.origin))
|
|
74
|
+
throw new Halt('目标地址超出测试范围', '链接不属于本次配置的测试站点。');
|
|
75
|
+
}
|
|
76
|
+
console.log(' Laya → ' + found.control.name + ' [' + found.control.role + ']');
|
|
77
|
+
this.navFailureStart = this.failures.length;
|
|
78
|
+
await found.locator.click();
|
|
79
|
+
await settle(this.page, this.config.delay);
|
|
80
|
+
await this.ready();
|
|
81
|
+
return found.control;
|
|
82
|
+
}
|
|
83
|
+
async revealRowMenu(name) {
|
|
84
|
+
if (!this.rowKey) return;
|
|
85
|
+
const snap = await observe(this.page);
|
|
86
|
+
if (
|
|
87
|
+
snap.controls.some(
|
|
88
|
+
(c) =>
|
|
89
|
+
norm(c.name) === norm(name) &&
|
|
90
|
+
(c.rowKey || !['menuitem', 'option'].includes(c.role)),
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
return;
|
|
94
|
+
const more = snap.controls.filter(
|
|
95
|
+
(c) => c.rowKey === this.rowKey && /^(?:图标:\s*more|更多|more)$/i.test(c.name),
|
|
96
|
+
);
|
|
97
|
+
if (more.length !== 1) return;
|
|
98
|
+
// This menu is click-triggered. Merely hovering revealed transient content
|
|
99
|
+
// that disappeared during model inference, causing repeated 9-second waits.
|
|
100
|
+
const menuItem = snap.controls.find(
|
|
101
|
+
(c) => !c.rowKey && norm(c.name) === norm(name) && c.role === 'menuitem',
|
|
102
|
+
);
|
|
103
|
+
if (menuItem) {
|
|
104
|
+
const item = await assertionTarget(this.page, name, 'visible');
|
|
105
|
+
await item.locator.hover();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const found = await this.chooseTarget(more[0].name, 'click', { rowKey: this.rowKey });
|
|
109
|
+
await found.locator.click();
|
|
110
|
+
await this.page.waitForTimeout(180);
|
|
111
|
+
this.events.push({
|
|
112
|
+
phase: this.phase,
|
|
113
|
+
action: 'open-menu',
|
|
114
|
+
text: '展开当前记录的更多操作',
|
|
115
|
+
rowKey: this.rowKey,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async targetRow(name) {
|
|
119
|
+
if (!this.rowKey) return undefined;
|
|
120
|
+
const snap = await observe(this.page);
|
|
121
|
+
// Dialog controls supersede the list-row binding. Exact global navigation
|
|
122
|
+
// remains global; other operation buttons stay in the chosen record.
|
|
123
|
+
if (!snap.controls.some((c) => c.rowKey === this.rowKey)) return undefined;
|
|
124
|
+
if (snap.controls.some((c) => !c.rowKey && norm(c.name) === norm(name))) return undefined;
|
|
125
|
+
return this.rowKey;
|
|
126
|
+
}
|
|
127
|
+
async bindRow(text) {
|
|
128
|
+
const snap = await observe(this.page),
|
|
129
|
+
all = [
|
|
130
|
+
...new Map(
|
|
131
|
+
snap.controls.filter((c) => c.rowKey && c.context).map((c) => [c.rowKey, c]),
|
|
132
|
+
).values(),
|
|
133
|
+
];
|
|
134
|
+
const state = text.match(/(?:找到|存在)(.+?)状态/)?.[1];
|
|
135
|
+
const quotedName = text.match(/名称(?:为|含)[「“"]([^」”"]+)[」”"]/)?.[1];
|
|
136
|
+
const permission = text.includes('只读') ? '只读' : text.includes('读写') ? '读写' : null;
|
|
137
|
+
const eligible = all.filter(
|
|
138
|
+
(c) =>
|
|
139
|
+
(!state || c.context.includes(state)) &&
|
|
140
|
+
(!quotedName || c.context.includes(quotedName)) &&
|
|
141
|
+
(!permission || c.context.includes(permission)),
|
|
142
|
+
);
|
|
143
|
+
if (!eligible.length)
|
|
144
|
+
throw new Halt('缺少测试数据', '当前列表没有符合步骤条件的可定位记录:' + text);
|
|
145
|
+
// Arbitrary-record UI checks may use the first eligible row; mutations may
|
|
146
|
+
// only select a specifically named record or explicitly supplied fixture.
|
|
147
|
+
const writes = /(?:点击|执行|确认|提交).{0,12}(?:删除|保存|上线|下线|发布)/.test(
|
|
148
|
+
this.current?.steps || '',
|
|
149
|
+
);
|
|
150
|
+
const fixture = this.config.data?.recordName;
|
|
151
|
+
const chosen = fixture ? eligible.find((c) => c.context.includes(fixture)) : eligible[0];
|
|
152
|
+
if (!chosen) throw new Halt('缺少测试数据', '未找到配置的独立测试记录:' + fixture);
|
|
153
|
+
if (writes && !fixture && !quotedName)
|
|
154
|
+
throw new Halt(
|
|
155
|
+
'缺少独立测试数据',
|
|
156
|
+
'该用例会修改记录;需先准备独立测试数据并通过--data的recordName指定,不能对现有任意记录操作',
|
|
157
|
+
);
|
|
158
|
+
this.rowKey = chosen.rowKey;
|
|
159
|
+
this.events.push({
|
|
160
|
+
phase: this.phase,
|
|
161
|
+
action: 'bind-row',
|
|
162
|
+
text,
|
|
163
|
+
rowKey: chosen.rowKey,
|
|
164
|
+
record: chosen.context,
|
|
165
|
+
selection: fixture ? 'configured-fixture' : 'first-eligible-live-row',
|
|
166
|
+
});
|
|
167
|
+
console.log(' 绑定记录:' + chosen.context.slice(0, 100));
|
|
168
|
+
await discoverRowLabels(this.page, this.rowKey);
|
|
169
|
+
}
|
|
170
|
+
async ready() {
|
|
171
|
+
return waitForReady(this.page, {
|
|
172
|
+
timeout: this.config.pageTimeout || 18000,
|
|
173
|
+
failures: () => this.failures.slice(this.navFailureStart),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
async navigate(url, { force = false } = {}) {
|
|
177
|
+
const u = new URL(url, this.config.url);
|
|
178
|
+
if (!['http:', 'https:'].includes(u.protocol) || !this.config.origins.includes(u.origin))
|
|
179
|
+
throw new Halt('目标地址超出测试范围', '步骤网址必须属于本次测试站点');
|
|
180
|
+
const changed = force || this.page.url() !== u.href;
|
|
181
|
+
if (changed) {
|
|
182
|
+
this.navFailureStart = this.failures.length;
|
|
183
|
+
this.navigationCount++;
|
|
184
|
+
await this.page.goto(u.href, { waitUntil: 'domcontentloaded' });
|
|
185
|
+
}
|
|
186
|
+
await this.ready();
|
|
187
|
+
return changed;
|
|
188
|
+
}
|
|
189
|
+
async showArea(name) {
|
|
190
|
+
const snap = await observe(this.page),
|
|
191
|
+
tabs = snap.controls.filter((c) => c.role === 'tab' && norm(c.name) === norm(name));
|
|
192
|
+
if (tabs.length !== 1) throw new Halt('页签无法确认', '找不到唯一页签:' + name);
|
|
193
|
+
if (!tabs[0].selected) {
|
|
194
|
+
const c = await this.directClick(name, '切换到' + name + 'Tab');
|
|
195
|
+
this.events.push({
|
|
196
|
+
phase: this.phase,
|
|
197
|
+
action: 'click',
|
|
198
|
+
text: '切换到' + name + 'Tab',
|
|
199
|
+
control: { name: c.name, role: c.role },
|
|
200
|
+
url: this.page.url(),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async closeOverlay() {
|
|
205
|
+
const modal = this.page
|
|
206
|
+
.locator('dialog[open],[role="dialog"]:visible,.ant-drawer-open .ant-drawer-content')
|
|
207
|
+
.last();
|
|
208
|
+
if (!(await modal.isVisible().catch(() => false))) return;
|
|
209
|
+
// A header Close and a footer 关闭 can coexist. Prefer the explicit header
|
|
210
|
+
// close label, then exact alternatives; never select a generic confirmation.
|
|
211
|
+
for (const name of ['Close', '关闭', 'Cancel', '取消']) {
|
|
212
|
+
const close = modal
|
|
213
|
+
.getByRole('button', { name, exact: true })
|
|
214
|
+
.filter({ visible: true });
|
|
215
|
+
if ((await close.count()) !== 1) continue;
|
|
216
|
+
await close.click();
|
|
217
|
+
await modal.waitFor({ state: 'hidden', timeout: this.config.assertTimeout || 4000 });
|
|
218
|
+
await this.ready();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
throw new Halt('页面复位未完成', '存在未关闭弹窗,无法唯一确定取消/关闭按钮');
|
|
222
|
+
}
|
|
223
|
+
async resetCase(url) {
|
|
224
|
+
this.phase = 'setup';
|
|
225
|
+
const desired = new URL(url, this.config.url),
|
|
226
|
+
current = new URL(this.page.url());
|
|
227
|
+
const sameModule =
|
|
228
|
+
desired.origin === current.origin &&
|
|
229
|
+
desired.pathname === current.pathname &&
|
|
230
|
+
!desired.search &&
|
|
231
|
+
!desired.hash;
|
|
232
|
+
const changed =
|
|
233
|
+
sameModule && !this.config.reloadEachCase
|
|
234
|
+
? (await this.ready(), false)
|
|
235
|
+
: await this.navigate(url, { force: !!this.config.reloadEachCase });
|
|
236
|
+
const key = new URL(url, this.config.url).href;
|
|
237
|
+
if (!this.baselines.has(key)) {
|
|
238
|
+
const snap = await observe(this.page);
|
|
239
|
+
this.baselines.set(
|
|
240
|
+
key,
|
|
241
|
+
snap.controls.filter((c) => c.role === 'tab' && c.selected).map((c) => c.name),
|
|
242
|
+
);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (!changed) {
|
|
246
|
+
await this.closeOverlay();
|
|
247
|
+
for (const area of this.dirtyAreas.values()) {
|
|
248
|
+
for (const name of area) await this.showArea(name);
|
|
249
|
+
const snap = await observe(this.page),
|
|
250
|
+
resets = snap.controls.filter(
|
|
251
|
+
(c) => c.role === 'button' && /^(重置|Reset)$/i.test(c.name),
|
|
252
|
+
);
|
|
253
|
+
if (resets.length === 1) {
|
|
254
|
+
const c = await this.directClick(resets[0].name, '重置查询条件');
|
|
255
|
+
this.events.push({
|
|
256
|
+
phase: 'setup',
|
|
257
|
+
action: 'reset',
|
|
258
|
+
scope: area,
|
|
259
|
+
control: { name: c.name, role: c.role },
|
|
260
|
+
});
|
|
261
|
+
} else {
|
|
262
|
+
await this.navigate(url, { force: true });
|
|
263
|
+
console.log(' 上条改变了表单且页面没有重置入口,刷新一次恢复初始状态');
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
for (const name of this.baselines.get(key)) await this.showArea(name);
|
|
268
|
+
}
|
|
269
|
+
this.dirtyAreas.clear();
|
|
270
|
+
}
|
|
271
|
+
async checkPrecondition(p, c) {
|
|
272
|
+
if (/^(?:无|无特殊要求|none|n\/a)$/i.test(p)) return;
|
|
273
|
+
if (/^(?:用户)?已登录(?:系统)?[,,]?\s*$/i.test(p)) {
|
|
274
|
+
if (
|
|
275
|
+
!this.config.authenticated ||
|
|
276
|
+
(await this.page.locator('input[type="password"]:visible').count())
|
|
277
|
+
)
|
|
278
|
+
throw new Halt('登录前提不足', '本轮没有完成登录流程,或页面仍显示密码框');
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const loginWith = p.match(/^(?:用户)?已登录(?:系统)?[,,](.+)$/);
|
|
282
|
+
if (loginWith) {
|
|
283
|
+
await this.checkPrecondition('已登录系统', c);
|
|
284
|
+
return this.checkPrecondition(loginWith[1], c);
|
|
285
|
+
}
|
|
286
|
+
const role = p.match(/^(?:仅)?拥有(.+?)(只读|读写)权限$/);
|
|
287
|
+
if (role) {
|
|
288
|
+
const t = await readTables(this.page),
|
|
289
|
+
i = t.headers.findIndex((h) => h.includes('权限'));
|
|
290
|
+
if (i >= 0 && t.rows.some((r) => norm(r[i]) === norm(role[2]))) return;
|
|
291
|
+
throw new Halt(
|
|
292
|
+
'缺少权限场景',
|
|
293
|
+
'当前列表没有证明所需的' + role[2] + '权限记录;需要对应授权或测试身份',
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
if (/^进入/.test(p)) {
|
|
297
|
+
await this.step(p);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const permission = p.match(/^已拥有(.+)菜单权限$/);
|
|
301
|
+
if (permission) {
|
|
302
|
+
const leaf = permission[1].split(/[_>>→]/).at(-1),
|
|
303
|
+
snap = await observe(this.page);
|
|
304
|
+
if (snap.controls.some((x) => norm(x.name) === norm(leaf))) return;
|
|
305
|
+
throw new Halt(
|
|
306
|
+
'前置条件未验证',
|
|
307
|
+
'未找到菜单“' +
|
|
308
|
+
leaf +
|
|
309
|
+
'”;先确认页面已加载且页面语言与Excel一致,不能据此判定账号无权限',
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
const exists = p.match(/^系统存在(.+)数据$/);
|
|
313
|
+
if (exists) {
|
|
314
|
+
const moduleName = c.title.split(/\s+-\s+/)[0];
|
|
315
|
+
let constraint = exists[1].replaceAll(moduleName, '').replace(/的/g, '');
|
|
316
|
+
const snap = await observe(this.page),
|
|
317
|
+
tabs = snap.controls.filter((x) => x.role === 'tab'),
|
|
318
|
+
areas = tabs.filter((x) => constraint.includes(x.name)).map((x) => x.name);
|
|
319
|
+
for (const area of areas) constraint = constraint.replaceAll(area, '');
|
|
320
|
+
constraint = constraint.replace(/和|、/g, '');
|
|
321
|
+
const state = constraint.match(/^(.+)状态$/),
|
|
322
|
+
permissionValue = constraint.match(/^(.+)权限$/),
|
|
323
|
+
named = constraint.match(/^名称含[「“"](.+)[」”"]$/);
|
|
324
|
+
if (state && /^(?:各种|所有|多种|不同)$/.test(state[1]))
|
|
325
|
+
throw new Halt(
|
|
326
|
+
'前置条件未验证',
|
|
327
|
+
'尚不能证明状态覆盖完整:' + p + ';需逐状态准备样本,不能把“各种”当成状态名称',
|
|
328
|
+
);
|
|
329
|
+
if (constraint && !state && !permissionValue && !named)
|
|
330
|
+
throw new Halt(
|
|
331
|
+
'前置条件未验证',
|
|
332
|
+
'尚不能证明这个业务前提:' + p + ';请提供明确的数据条件,未将它当作通过',
|
|
333
|
+
);
|
|
334
|
+
const column = state ? '状态' : permissionValue ? '权限' : named ? '名称' : null,
|
|
335
|
+
value = state?.[1] || permissionValue?.[1] || named?.[1];
|
|
336
|
+
const check = async () => {
|
|
337
|
+
const t = await readTables(this.page);
|
|
338
|
+
if (!t.rows.length) return false;
|
|
339
|
+
if (!column) return true;
|
|
340
|
+
const index = t.headers.findIndex((h) => h.includes(column));
|
|
341
|
+
return (
|
|
342
|
+
index >= 0 &&
|
|
343
|
+
t.rows.some((r) =>
|
|
344
|
+
named ? r[index]?.includes(value) : norm(r[index]) === norm(value),
|
|
345
|
+
)
|
|
346
|
+
);
|
|
347
|
+
};
|
|
348
|
+
if (areas.length) {
|
|
349
|
+
for (const area of areas) {
|
|
350
|
+
await this.showArea(area);
|
|
351
|
+
if (!(await check()))
|
|
352
|
+
throw new Halt('前置条件未验证', area + '当前可见记录未证明:' + p);
|
|
353
|
+
}
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (await check()) return;
|
|
357
|
+
// Data may live behind another visible tab. Probe at most three tabs, never all pages.
|
|
358
|
+
for (const area of tabs.filter((t) => !t.selected).slice(0, 3)) {
|
|
359
|
+
await this.showArea(area.name);
|
|
360
|
+
if (await check()) return;
|
|
361
|
+
}
|
|
362
|
+
throw new Halt(
|
|
363
|
+
'前置条件未验证',
|
|
364
|
+
'当前可见记录未证明:' + p + ';没有查询全量后台数据,不能断言系统不存在该数据',
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
const check = await this.verify(p);
|
|
368
|
+
if (check.status !== '通过')
|
|
369
|
+
throw new Halt('前置条件未验证', '未能验证前提:' + p + ';' + check.reason);
|
|
370
|
+
}
|
|
371
|
+
async step(text) {
|
|
372
|
+
const snap = await observe(this.page);
|
|
373
|
+
let specs = expandStep(text, { controls: snap.controls, area: this.area });
|
|
374
|
+
if (!specs.length)
|
|
375
|
+
specs = await semanticStep(text, snap, this.laya, {
|
|
376
|
+
meta: { case: this.current?.key },
|
|
377
|
+
minProbability: this.config.minProbability,
|
|
378
|
+
minMargin: this.config.minMargin,
|
|
379
|
+
});
|
|
380
|
+
for (const spec of specs) await this.executeStep(text, spec);
|
|
381
|
+
}
|
|
382
|
+
async executeStep(text, spec) {
|
|
383
|
+
if (++this.actions > this.config.maxSteps)
|
|
384
|
+
throw new Halt('达到步骤上限', '本用例达到最大操作次数,停止继续尝试');
|
|
385
|
+
console.log(' 步骤:' + text);
|
|
386
|
+
if (spec.verb === 'bindRow') {
|
|
387
|
+
await this.bindRow(text);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (spec.verb === 'assertStep') {
|
|
391
|
+
const actual = await this.verify(spec.target);
|
|
392
|
+
this.events.push({ phase: this.phase, action: 'step-assertion', text, ...actual });
|
|
393
|
+
if (actual.status !== '通过')
|
|
394
|
+
throw new Halt(
|
|
395
|
+
actual.status === '失败' ? '步骤预期不符' : '执行能力不足',
|
|
396
|
+
actual.reason,
|
|
397
|
+
);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (spec.verb === 'emptyRequired') {
|
|
401
|
+
const snap = await observe(this.page),
|
|
402
|
+
fields = snap.controls.filter(
|
|
403
|
+
(c) =>
|
|
404
|
+
['textbox', 'spinbutton', 'combobox'].includes(c.role) &&
|
|
405
|
+
!c.disabled &&
|
|
406
|
+
!c.readonly,
|
|
407
|
+
);
|
|
408
|
+
if (!fields.length || fields.some((c) => c.value))
|
|
409
|
+
throw new Halt(
|
|
410
|
+
'前置条件未验证',
|
|
411
|
+
'当前必填字段为空尚未证明,不提交可能带有默认值的表单',
|
|
412
|
+
);
|
|
413
|
+
this.events.push({
|
|
414
|
+
phase: this.phase,
|
|
415
|
+
action: 'observe-empty-fields',
|
|
416
|
+
text,
|
|
417
|
+
fields: fields.map((c) => c.name),
|
|
418
|
+
});
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (spec.verb === 'path') {
|
|
422
|
+
const snap = await observe(this.page),
|
|
423
|
+
leaf = spec.path.at(-1),
|
|
424
|
+
current = new URL(this.page.url());
|
|
425
|
+
const here = snap.controls.find((c) => {
|
|
426
|
+
if (norm(c.name) !== norm(leaf) || !c.href) return false;
|
|
427
|
+
const u = new URL(c.href, current);
|
|
428
|
+
return (
|
|
429
|
+
u.href === current.href ||
|
|
430
|
+
(!u.search &&
|
|
431
|
+
!u.hash &&
|
|
432
|
+
u.origin === current.origin &&
|
|
433
|
+
u.pathname === current.pathname)
|
|
434
|
+
);
|
|
435
|
+
});
|
|
436
|
+
if (here) {
|
|
437
|
+
const event = {
|
|
438
|
+
phase: this.phase,
|
|
439
|
+
text,
|
|
440
|
+
action: 'already-on-page',
|
|
441
|
+
control: { name: here.name, role: here.role },
|
|
442
|
+
url: this.page.url(),
|
|
443
|
+
};
|
|
444
|
+
this.events.push(event);
|
|
445
|
+
console.log(' 已在目标页面,复用当前DOM');
|
|
446
|
+
return event;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
// Explicit verbs need no second model vote ("点击查看" was being confused
|
|
450
|
+
// with the observation verb). Laya still selects the real DOM target below.
|
|
451
|
+
const verb = spec.verb;
|
|
452
|
+
let control;
|
|
453
|
+
if (verb === 'navigate') {
|
|
454
|
+
if (!spec.target) throw new Halt('缺少测试数据', '没有明确网址');
|
|
455
|
+
await this.navigate(spec.target);
|
|
456
|
+
} else if (verb === 'path') {
|
|
457
|
+
// If a path's final destination is already visible, choose it directly.
|
|
458
|
+
const snap = await observe(this.page),
|
|
459
|
+
leaf = spec.path.at(-1);
|
|
460
|
+
const candidates = snap.controls.filter((c) => norm(c.name) === norm(leaf));
|
|
461
|
+
if (candidates.length === 1) control = await this.directClick(leaf, text);
|
|
462
|
+
else for (const part of spec.path) control = await this.directClick(part, text);
|
|
463
|
+
} else if (verb === 'observe') {
|
|
464
|
+
const names = quoted(text);
|
|
465
|
+
if (names.length === 1 && /按钮/.test(text)) {
|
|
466
|
+
await this.revealRowMenu(names[0]);
|
|
467
|
+
const found = await assertionTarget(this.page, names[0], 'visible', {
|
|
468
|
+
rowKey: await this.targetRow(names[0]),
|
|
469
|
+
});
|
|
470
|
+
control = found.control;
|
|
471
|
+
this.observedName = names[0];
|
|
472
|
+
}
|
|
473
|
+
await settle(this.page, this.config.delay);
|
|
474
|
+
} else if (verb === 'press') {
|
|
475
|
+
if (spec.value.toLowerCase() === 'enter' && !this.config.allowWrite)
|
|
476
|
+
throw new Halt('写入未启用', 'Enter可能提交当前表单;未启用写入时不猜测其行为');
|
|
477
|
+
await this.page.keyboard.press(spec.value);
|
|
478
|
+
await settle(this.page, this.config.delay);
|
|
479
|
+
} else if (verb === 'click') {
|
|
480
|
+
if (!this.rowKey && /列表(?:中)?点击/.test(text)) await this.bindRow(text);
|
|
481
|
+
control = await this.directClick(spec.target, text);
|
|
482
|
+
} else {
|
|
483
|
+
if (['fill', 'clear', 'select', 'check', 'uncheck'].includes(verb)) {
|
|
484
|
+
const snap = await observe(this.page),
|
|
485
|
+
area = snap.controls
|
|
486
|
+
.filter((c) => c.role === 'tab' && c.selected)
|
|
487
|
+
.map((c) => c.name);
|
|
488
|
+
this.dirtyAreas.set(JSON.stringify(area), area);
|
|
489
|
+
}
|
|
490
|
+
const found = await this.chooseTarget(spec.target, verb, {
|
|
491
|
+
value: spec.value,
|
|
492
|
+
rowKey: await this.targetRow(spec.target),
|
|
493
|
+
allowDisabled: verb === 'hover',
|
|
494
|
+
});
|
|
495
|
+
control = found.control;
|
|
496
|
+
if (verb === 'fill' || verb === 'clear') {
|
|
497
|
+
await found.locator.fill(verb === 'clear' ? '' : spec.value);
|
|
498
|
+
if ((await found.locator.inputValue()) !== (verb === 'clear' ? '' : spec.value))
|
|
499
|
+
throw new Halt('操作未生效', '填写后的实际输入值不一致');
|
|
500
|
+
} else if (verb === 'select') {
|
|
501
|
+
if (control.tag === 'select')
|
|
502
|
+
await found.locator.selectOption({ label: spec.value });
|
|
503
|
+
else {
|
|
504
|
+
await found.locator.click();
|
|
505
|
+
await settle(this.page, this.config.delay);
|
|
506
|
+
const option = await this.chooseTarget(spec.value, 'option');
|
|
507
|
+
await option.locator.click();
|
|
508
|
+
}
|
|
509
|
+
} else if (verb === 'check' || verb === 'uncheck') {
|
|
510
|
+
if (['input'].includes(control.tag))
|
|
511
|
+
await found.locator.setChecked(verb === 'check');
|
|
512
|
+
else if (control.checked !== (verb === 'check')) await found.locator.click();
|
|
513
|
+
} else if (verb === 'hover') await found.locator.hover();
|
|
514
|
+
await settle(this.page, this.config.delay);
|
|
515
|
+
}
|
|
516
|
+
const event = {
|
|
517
|
+
phase: this.phase,
|
|
518
|
+
text,
|
|
519
|
+
action: verb,
|
|
520
|
+
...(control
|
|
521
|
+
? { control: { name: control.name, role: control.role, context: control.context } }
|
|
522
|
+
: {}),
|
|
523
|
+
url: this.page.url(),
|
|
524
|
+
};
|
|
525
|
+
this.events.push(event);
|
|
526
|
+
return event;
|
|
527
|
+
}
|
|
528
|
+
async verify(text) {
|
|
529
|
+
const spec = expectation(text),
|
|
530
|
+
page = this.page;
|
|
531
|
+
if (spec.kind === 'unsupported')
|
|
532
|
+
return {
|
|
533
|
+
text,
|
|
534
|
+
status: '未验证',
|
|
535
|
+
reason: '当前断言语法无法完整解释;未用Laya评分代替结果证明',
|
|
536
|
+
};
|
|
537
|
+
if (spec.scope) await this.showArea(spec.scope);
|
|
538
|
+
let actual,
|
|
539
|
+
passed = false;
|
|
540
|
+
if (spec.kind === 'tooltip') {
|
|
541
|
+
if (!this.observedName)
|
|
542
|
+
return {
|
|
543
|
+
text,
|
|
544
|
+
status: '未验证',
|
|
545
|
+
reason: '没有明确的悬停目标,不能把任意提示作为本条证据',
|
|
546
|
+
};
|
|
547
|
+
await this.revealRowMenu(this.observedName);
|
|
548
|
+
const found = await assertionTarget(page, this.observedName, 'visible', {
|
|
549
|
+
rowKey: await this.targetRow(this.observedName),
|
|
550
|
+
});
|
|
551
|
+
await found.locator.hover();
|
|
552
|
+
await page.waitForTimeout(400);
|
|
553
|
+
actual = [
|
|
554
|
+
...new Set(
|
|
555
|
+
(
|
|
556
|
+
await page
|
|
557
|
+
.locator(
|
|
558
|
+
'[role="tooltip"]:visible,.ant-tooltip:visible,.el-tooltip__popper:visible',
|
|
559
|
+
)
|
|
560
|
+
.allTextContents()
|
|
561
|
+
).map((s) => s.trim()),
|
|
562
|
+
),
|
|
563
|
+
];
|
|
564
|
+
passed = actual.some((s) => s.includes(spec.target));
|
|
565
|
+
} else if (spec.kind === 'controlVisible') {
|
|
566
|
+
const snap = await observe(page);
|
|
567
|
+
actual = snap.controls
|
|
568
|
+
.filter((c) => norm(c.name) === norm(spec.target))
|
|
569
|
+
.map((c) => ({ name: c.name, role: c.role }));
|
|
570
|
+
passed = actual.length === 1;
|
|
571
|
+
} else if (spec.kind === 'dialog') {
|
|
572
|
+
const dialogs = page.locator(
|
|
573
|
+
'dialog[open]:visible,[role="dialog"]:visible,.ant-drawer-open .ant-drawer-content:visible',
|
|
574
|
+
);
|
|
575
|
+
actual = await dialogs.allTextContents();
|
|
576
|
+
passed = actual.some((s) => s.includes(spec.target));
|
|
577
|
+
} else if (spec.kind === 'fixedColumns') {
|
|
578
|
+
actual = await page
|
|
579
|
+
.getByRole('columnheader')
|
|
580
|
+
.filter({ visible: true })
|
|
581
|
+
.evaluateAll(
|
|
582
|
+
(els, names) =>
|
|
583
|
+
Object.fromEntries(
|
|
584
|
+
names.map((name) => [
|
|
585
|
+
name,
|
|
586
|
+
els
|
|
587
|
+
.filter((e) => e.innerText.trim() === name)
|
|
588
|
+
.map((e) => ({
|
|
589
|
+
position: getComputedStyle(e).position,
|
|
590
|
+
left: getComputedStyle(e).left,
|
|
591
|
+
right: getComputedStyle(e).right,
|
|
592
|
+
fixedLeft: !!e.closest('.ant-table-fixed-left'),
|
|
593
|
+
fixedRight: !!e.closest('.ant-table-fixed-right'),
|
|
594
|
+
})),
|
|
595
|
+
]),
|
|
596
|
+
),
|
|
597
|
+
[spec.left, spec.right],
|
|
598
|
+
);
|
|
599
|
+
passed =
|
|
600
|
+
actual[spec.left]?.some(
|
|
601
|
+
(c) => c.fixedLeft || (c.position === 'sticky' && c.left !== 'auto'),
|
|
602
|
+
) &&
|
|
603
|
+
actual[spec.right]?.some(
|
|
604
|
+
(c) => c.fixedRight || (c.position === 'sticky' && c.right !== 'auto'),
|
|
605
|
+
);
|
|
606
|
+
} else if (spec.kind === 'columnValues') {
|
|
607
|
+
const t = await readTables(page),
|
|
608
|
+
i = t.headers.findIndex((h) => h.includes(spec.column));
|
|
609
|
+
actual = i < 0 ? [] : t.rows.map((r) => r[i]);
|
|
610
|
+
passed =
|
|
611
|
+
actual.length > 0 &&
|
|
612
|
+
actual.every((v) => spec.values.some((w) => norm(w) === norm(v)));
|
|
613
|
+
} else if (spec.kind === 'fieldAbsent' || spec.kind === 'fields') {
|
|
614
|
+
const snap = await observe(page),
|
|
615
|
+
fields = snap.controls.filter(
|
|
616
|
+
(c) => ['textbox', 'combobox'].includes(c.role) && c.inPanel,
|
|
617
|
+
);
|
|
618
|
+
actual = fields.map((c) => ({ name: c.name, role: c.role }));
|
|
619
|
+
passed =
|
|
620
|
+
spec.kind === 'fieldAbsent'
|
|
621
|
+
? !fields.some((c) => c.name.includes(spec.target))
|
|
622
|
+
: spec.targets.every((t) =>
|
|
623
|
+
fields.some((c) => c.role === t.role && c.name.includes(t.name)),
|
|
624
|
+
);
|
|
625
|
+
} else if (spec.kind === 'visible' || spec.kind === 'absent') {
|
|
626
|
+
// Check visible rendered text, not hidden DOM or a model's opinion.
|
|
627
|
+
const locator = page.getByText(spec.target, { exact: false }).filter({ visible: true });
|
|
628
|
+
if (spec.kind === 'visible')
|
|
629
|
+
await locator
|
|
630
|
+
.first()
|
|
631
|
+
.waitFor({ timeout: this.config.assertTimeout })
|
|
632
|
+
.catch(() => {});
|
|
633
|
+
else
|
|
634
|
+
await locator
|
|
635
|
+
.first()
|
|
636
|
+
.waitFor({ state: 'hidden', timeout: this.config.assertTimeout })
|
|
637
|
+
.catch(() => {});
|
|
638
|
+
actual = await locator.count();
|
|
639
|
+
passed = spec.kind === 'visible' ? actual > 0 : actual === 0;
|
|
640
|
+
} else if (spec.kind === 'tabs') {
|
|
641
|
+
const snap = await observe(page),
|
|
642
|
+
tabs = snap.controls.filter((c) => c.role === 'tab');
|
|
643
|
+
actual = tabs.map((c) => c.name);
|
|
644
|
+
passed = actual.length === spec.count && spec.targets.every((t) => actual.includes(t));
|
|
645
|
+
} else if (spec.kind === 'columns') {
|
|
646
|
+
actual = await page
|
|
647
|
+
.getByRole('columnheader')
|
|
648
|
+
.filter({ visible: true })
|
|
649
|
+
.allTextContents();
|
|
650
|
+
passed = spec.targets.every((t) => actual.some((a) => a.trim() === t));
|
|
651
|
+
} else if (spec.kind === 'rowcount') {
|
|
652
|
+
const tables = page.getByRole('table').filter({ visible: true });
|
|
653
|
+
if ((await tables.count()) !== 1)
|
|
654
|
+
return {
|
|
655
|
+
text,
|
|
656
|
+
status: '未验证',
|
|
657
|
+
reason: '存在多个表格,需要写明目标表格,不能猜测行数',
|
|
658
|
+
};
|
|
659
|
+
await page
|
|
660
|
+
.waitForFunction(
|
|
661
|
+
(expected) => {
|
|
662
|
+
const visible = (e) => {
|
|
663
|
+
const r = e.getBoundingClientRect(),
|
|
664
|
+
s = getComputedStyle(e);
|
|
665
|
+
return (
|
|
666
|
+
r.width > 0 &&
|
|
667
|
+
r.height > 0 &&
|
|
668
|
+
s.visibility !== 'hidden' &&
|
|
669
|
+
s.display !== 'none'
|
|
670
|
+
);
|
|
671
|
+
};
|
|
672
|
+
const all = [...document.querySelectorAll('table,[role="table"]')].filter(
|
|
673
|
+
visible,
|
|
674
|
+
);
|
|
675
|
+
return (
|
|
676
|
+
all.length === 1 &&
|
|
677
|
+
[...all[0].querySelectorAll('tbody tr')].filter(visible).length ===
|
|
678
|
+
expected
|
|
679
|
+
);
|
|
680
|
+
},
|
|
681
|
+
spec.value,
|
|
682
|
+
{ timeout: this.config.assertTimeout },
|
|
683
|
+
)
|
|
684
|
+
.catch(() => {});
|
|
685
|
+
actual = await tables.locator('tbody tr').filter({ visible: true }).count();
|
|
686
|
+
passed = actual === spec.value;
|
|
687
|
+
} else if (spec.kind === 'url') {
|
|
688
|
+
actual = page.url();
|
|
689
|
+
passed = actual.includes(spec.target);
|
|
690
|
+
} else {
|
|
691
|
+
const found = await assertionTarget(page, spec.target, spec.kind, {
|
|
692
|
+
rowKey: await this.targetRow(spec.target),
|
|
693
|
+
});
|
|
694
|
+
if (['value', 'empty'].includes(spec.kind)) {
|
|
695
|
+
actual = await found.locator.inputValue();
|
|
696
|
+
passed = actual === (spec.kind === 'empty' ? '' : spec.value);
|
|
697
|
+
} else if (['disabled', 'enabled'].includes(spec.kind)) {
|
|
698
|
+
actual = found.control.disabled;
|
|
699
|
+
passed = spec.kind === 'disabled' ? actual : !actual;
|
|
700
|
+
} else if (spec.kind === 'checked') {
|
|
701
|
+
actual = found.control.checked;
|
|
702
|
+
passed = actual === true;
|
|
703
|
+
} else if (spec.kind === 'selected') {
|
|
704
|
+
actual = found.control.selected;
|
|
705
|
+
passed = actual === true;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return {
|
|
709
|
+
text,
|
|
710
|
+
status: passed ? '通过' : '失败',
|
|
711
|
+
kind: spec.kind,
|
|
712
|
+
actual,
|
|
713
|
+
reason: passed ? '明确断言满足' : '实际页面状态与该预期不符,需核对需求或定位证据',
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
async runCase(c) {
|
|
717
|
+
this.current = c;
|
|
718
|
+
this.actions = 0;
|
|
719
|
+
this.events = [];
|
|
720
|
+
this.rowKey = null;
|
|
721
|
+
this.area = null;
|
|
722
|
+
this.observedName = null;
|
|
723
|
+
this.pendingPreconditions = [];
|
|
724
|
+
const started = Date.now();
|
|
725
|
+
const failuresAt = this.failures.length,
|
|
726
|
+
navAt = this.navigationCount;
|
|
727
|
+
let touchedPage = false;
|
|
728
|
+
const result = {
|
|
729
|
+
...c,
|
|
730
|
+
status: '未执行',
|
|
731
|
+
reason: '',
|
|
732
|
+
category: '',
|
|
733
|
+
actions: [],
|
|
734
|
+
assertions: [],
|
|
735
|
+
started_at: new Date().toISOString(),
|
|
736
|
+
};
|
|
737
|
+
try {
|
|
738
|
+
if (c.import_error) throw new Halt('Excel内容不明确', c.import_error);
|
|
739
|
+
if (!this.config.includeApproval && approvalOperation(c))
|
|
740
|
+
throw new Halt(
|
|
741
|
+
'按要求跳过审批',
|
|
742
|
+
'用例包含审批流程操作或审批开关,不执行;仅展示审批状态的用例不按关键词排除',
|
|
743
|
+
);
|
|
744
|
+
const data = { ...parseData(c.data), ...this.config.data, runId: this.config.runId };
|
|
745
|
+
const steps = lines(variables(c.steps, data)),
|
|
746
|
+
expected = lines(variables(c.expected, data)),
|
|
747
|
+
preconditions = lines(variables(c.preconditions, data));
|
|
748
|
+
if (!steps.length || !expected.length)
|
|
749
|
+
throw new Halt('用例内容缺失', '需要明确的操作步骤和预期结果');
|
|
750
|
+
touchedPage = true;
|
|
751
|
+
await this.resetCase(c.url || this.config.url);
|
|
752
|
+
this.phase = 'precondition';
|
|
753
|
+
const scopeSnapshot = await observe(this.page);
|
|
754
|
+
const titleAreas = scopeNames(
|
|
755
|
+
c.title + '\n' + steps.filter((s) => /^切换/.test(s) && s.includes('/')).join('\n'),
|
|
756
|
+
scopeSnapshot.controls,
|
|
757
|
+
);
|
|
758
|
+
if (titleAreas.length === 1) await this.showArea(titleAreas[0]);
|
|
759
|
+
result.precondition_checks = [];
|
|
760
|
+
for (const p of preconditions) {
|
|
761
|
+
try {
|
|
762
|
+
await this.checkPrecondition(p, c);
|
|
763
|
+
result.precondition_checks.push({ text: p, status: '通过' });
|
|
764
|
+
} catch (e) {
|
|
765
|
+
if (
|
|
766
|
+
e.category !== '前置条件未验证' ||
|
|
767
|
+
!/(?:尚不能证明|语法无法完整解释)/.test(e.message)
|
|
768
|
+
)
|
|
769
|
+
throw e;
|
|
770
|
+
this.pendingPreconditions.push(p);
|
|
771
|
+
result.precondition_checks.push({
|
|
772
|
+
text: p,
|
|
773
|
+
status: '待补证',
|
|
774
|
+
reason: e.message,
|
|
775
|
+
});
|
|
776
|
+
console.log(' 前提待补证,继续可观察步骤:' + p);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
// Repeat read-only scenarios explicitly titled for multiple live tabs.
|
|
780
|
+
// Never multiply potentially persistent writes across scopes automatically.
|
|
781
|
+
if (titleAreas.length > 1 && this.config.allowWrite)
|
|
782
|
+
throw new Halt(
|
|
783
|
+
'多页签写入范围不明确',
|
|
784
|
+
'用例标题涉及多个页签且允许写入,请拆成独立用例后执行',
|
|
785
|
+
);
|
|
786
|
+
for (const area of titleAreas.length ? titleAreas : [null]) {
|
|
787
|
+
this.phase = 'step';
|
|
788
|
+
this.area = area;
|
|
789
|
+
this.rowKey = null;
|
|
790
|
+
if (area) {
|
|
791
|
+
await this.closeOverlay();
|
|
792
|
+
await this.showArea(area);
|
|
793
|
+
console.log(' 用例范围:' + area);
|
|
794
|
+
}
|
|
795
|
+
for (const text of steps) await this.step(text);
|
|
796
|
+
this.phase = 'assertion';
|
|
797
|
+
for (const text of expected) {
|
|
798
|
+
try {
|
|
799
|
+
result.assertions.push({ ...(await this.verify(text)), scope: area });
|
|
800
|
+
} catch (e) {
|
|
801
|
+
if (e.category === '模型API错误') throw e;
|
|
802
|
+
result.assertions.push({
|
|
803
|
+
text,
|
|
804
|
+
scope: area,
|
|
805
|
+
status: '未验证',
|
|
806
|
+
reason: e.message,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
for (const p of this.pendingPreconditions)
|
|
812
|
+
result.assertions.push({
|
|
813
|
+
text: '前提:' + p,
|
|
814
|
+
status: '未验证',
|
|
815
|
+
reason: '已继续执行可观察步骤,但该前提仍缺少证据,不能计为通过',
|
|
816
|
+
});
|
|
817
|
+
result.status = result.assertions.some((a) => a.status === '失败')
|
|
818
|
+
? '失败'
|
|
819
|
+
: result.assertions.some((a) => a.status === '未验证')
|
|
820
|
+
? '部分验证'
|
|
821
|
+
: '通过';
|
|
822
|
+
result.category =
|
|
823
|
+
result.status === '失败'
|
|
824
|
+
? '断言不符合预期,需复核'
|
|
825
|
+
: result.status === '部分验证'
|
|
826
|
+
? '部分预期尚不能自动验证'
|
|
827
|
+
: '无';
|
|
828
|
+
result.reason =
|
|
829
|
+
result.assertions
|
|
830
|
+
.filter((a) => a.status !== '通过')
|
|
831
|
+
.map((a) => a.text + ':' + a.reason)
|
|
832
|
+
.join(';') || '所有步骤已执行,所有预期断言通过';
|
|
833
|
+
} catch (e) {
|
|
834
|
+
result.status = this.events.some(
|
|
835
|
+
(x) => x.phase === 'step' && x.action !== 'already-on-page',
|
|
836
|
+
)
|
|
837
|
+
? '部分执行(未完成)'
|
|
838
|
+
: '跳过(未完成)';
|
|
839
|
+
result.category = e.category || '执行器异常';
|
|
840
|
+
result.reason = e.message;
|
|
841
|
+
}
|
|
842
|
+
result.actions = this.events;
|
|
843
|
+
result.duration_ms = Date.now() - started;
|
|
844
|
+
result.network_errors = this.failures.slice(failuresAt);
|
|
845
|
+
result.page_navigations = this.navigationCount - navAt;
|
|
846
|
+
this.lastCaseUiChanged = this.events.some(
|
|
847
|
+
(x) =>
|
|
848
|
+
x.phase === 'step' &&
|
|
849
|
+
['fill', 'select', 'check', 'uncheck', 'clear'].includes(x.action),
|
|
850
|
+
);
|
|
851
|
+
result.evidence_scope = result.actions.length
|
|
852
|
+
? '操作后的页面快照'
|
|
853
|
+
: '本条没有完成动作;截图仅表示停止时浏览器页面';
|
|
854
|
+
if (result.status !== '通过' && result.network_errors.some((x) => x.status === 401)) {
|
|
855
|
+
result.category = '登录会话失效';
|
|
856
|
+
result.reason += ';捕获HTTP401,需重新登录';
|
|
857
|
+
}
|
|
858
|
+
if (
|
|
859
|
+
['页面资源加载失败', '页面尚未就绪'].includes(result.category) &&
|
|
860
|
+
result.network_errors.some((x) => x.status >= 500)
|
|
861
|
+
) {
|
|
862
|
+
result.category = '环境接口故障';
|
|
863
|
+
result.reason += ';页面未就绪且捕获HTTP5xx,不能据此判定产品功能错误';
|
|
864
|
+
}
|
|
865
|
+
const stem = `${String(c.id).replace(/[^\w\u4e00-\u9fff-]/g, '_')}-${c.row}`;
|
|
866
|
+
if (touchedPage) {
|
|
867
|
+
result.screenshot = 'evidence/' + stem + '.png';
|
|
868
|
+
await this.page
|
|
869
|
+
.screenshot({ path: path.join(this.config.out, result.screenshot) })
|
|
870
|
+
.catch(() => {});
|
|
871
|
+
const snap = await observe(this.page);
|
|
872
|
+
await fs.writeFile(
|
|
873
|
+
path.join(this.config.out, 'evidence', stem + '-controls.json'),
|
|
874
|
+
JSON.stringify({ url: snap.url, controls: snap.controls }, null, 2),
|
|
875
|
+
);
|
|
876
|
+
}
|
|
877
|
+
return result;
|
|
878
|
+
}
|
|
879
|
+
}
|