dsh-uitest 2.1.2 → 2.1.4

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.
@@ -1,85 +0,0 @@
1
- import assert from 'node:assert/strict';
2
- import fs from 'node:fs';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import test from 'node:test';
6
-
7
- import { clearProfileLocks, isProfileLockError, prepareProfile, profileOwnerFile } from '../../lib/browser-ab.mjs';
8
- import { summarizeBrowserError } from '../../lib/browser-errors.mjs';
9
-
10
- const SCREENSHOT_ERROR =
11
- 'Auto-launch failed: Chrome exited early (exit code: 21) without writing DevToolsActivePort\n' +
12
- 'Lock file can not be created: 另一个程序正在使用此文件。';
13
-
14
- function tempProfile() {
15
- return fs.mkdtempSync(path.join(os.tmpdir(), 'uitest-profile-'));
16
- }
17
-
18
- test('clearProfileLocks 只删除 Chrome 锁文件,保留 profile 数据', () => {
19
- const dir = tempProfile();
20
- fs.mkdirSync(path.join(dir, 'Default'));
21
- fs.writeFileSync(path.join(dir, 'Default', 'Cookies'), 'cookie');
22
- const locks = ['SingletonLock', 'SingletonCookie', 'SingletonSocket', 'DevToolsActivePort'];
23
- for (const name of locks) fs.writeFileSync(path.join(dir, name), 'lock');
24
-
25
- clearProfileLocks(dir);
26
-
27
- for (const name of locks) assert.equal(fs.existsSync(path.join(dir, name)), false, name + ' 应被删除');
28
- assert.equal(fs.existsSync(path.join(dir, 'Default')), true);
29
- assert.equal(fs.readFileSync(path.join(dir, 'Default', 'Cookies'), 'utf8'), 'cookie');
30
- });
31
-
32
- test('prepareProfile 停掉其它会话并接管 profile', async () => {
33
- const dir = tempProfile();
34
- fs.writeFileSync(profileOwnerFile(dir), JSON.stringify({ session: 'old-session', pid: 1, at: 'x' }));
35
- fs.writeFileSync(path.join(dir, 'SingletonLock'), 'lock');
36
- const stopped = [];
37
-
38
- const owner = await prepareProfile(dir, 'new-session', null, {
39
- stopSessions: (names) => stopped.push(...names),
40
- wait: async () => {},
41
- });
42
-
43
- assert.deepEqual(stopped, ['old-session'], '应先停止占用 profile 的会话');
44
- assert.equal(owner.session, 'new-session');
45
- const written = JSON.parse(fs.readFileSync(profileOwnerFile(dir), 'utf8'));
46
- assert.equal(written.session, 'new-session');
47
- assert.ok(written.pid > 0);
48
- assert.equal(fs.existsSync(path.join(dir, 'SingletonLock')), false, '接管时应清理残留锁');
49
- });
50
-
51
- test('prepareProfile 对同一会话不清锁,保留 attach 复用', async () => {
52
- const dir = tempProfile();
53
- fs.writeFileSync(profileOwnerFile(dir), JSON.stringify({ session: 'same-session', pid: 1, at: 'x' }));
54
- fs.writeFileSync(path.join(dir, 'SingletonLock'), 'lock');
55
-
56
- await prepareProfile(dir, 'same-session', null, {
57
- stopSessions: () => { throw new Error('不应停止同一会话'); },
58
- wait: async () => {},
59
- });
60
-
61
- assert.equal(fs.existsSync(path.join(dir, 'SingletonLock')), true);
62
- });
63
-
64
- test('isProfileLockError 识别截图里的完整错误与 ProcessSingleton', () => {
65
- assert.equal(isProfileLockError(SCREENSHOT_ERROR), true);
66
- assert.equal(isProfileLockError('processsingleton lock held'), true);
67
- assert.equal(isProfileLockError('Lock file can not be created: 另一个程序正在使用此文件'), true);
68
- assert.equal(isProfileLockError('devtools active port missing'), false);
69
- });
70
-
71
- test('summarizeBrowserError 返回中文短句,detail 保留原始错误', () => {
72
- const locked = summarizeBrowserError(SCREENSHOT_ERROR);
73
- assert.equal(locked.code, 'BROWSER_PROFILE_LOCKED');
74
- assert.equal(locked.message, '测试浏览器 profile 被占用,浏览器没有启动。请等上一次执行结束后重试。');
75
- assert.match(locked.detail, /DevToolsActivePort/);
76
-
77
- const failed = summarizeBrowserError('Auto-launch failed: no DevToolsActivePort written');
78
- assert.equal(failed.code, 'BROWSER_LAUNCH_FAILED');
79
- assert.equal(failed.message, 'Chrome 启动失败,没有写入 DevToolsActivePort。请检查浏览器环境后重试。');
80
-
81
- const other = summarizeBrowserError(' boom ');
82
- assert.equal(other.code, 'BROWSER_LAUNCH_FAILED');
83
- assert.equal(other.message, 'boom');
84
- assert.equal(other.detail, 'boom');
85
- });
@@ -1,305 +0,0 @@
1
- import assert from 'node:assert/strict';
2
- import fs from 'node:fs';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import test from 'node:test';
6
-
7
- import ExcelJS from 'exceljs';
8
-
9
- import { loadConfig } from '../../lib/config.mjs';
10
- import { bulkUpdateCases, deleteCases, loadImportedCases, loadProjectCases, restoreCases, saveCase } from '../../lib/cases-import.mjs';
11
- import { importDocument } from '../import.mjs';
12
- import { readDocs } from '../store.mjs';
13
-
14
- async function writeSmallXlsx(file, rows) {
15
- const workbook = new ExcelJS.Workbook();
16
- const sheet = workbook.addWorksheet('用例');
17
- sheet.addRow(['序号', '优先级', '模块', '一级功能', '二级功能', '测试功能点', '前提条件', '操作步骤', '预期结果', '测试结果', '备注', '用例编号']);
18
- for (const [seq, tcId, title] of rows) sheet.addRow([seq, '中', '模块', '一级', '分组', title, '', '', '', '', '', tcId]);
19
- await workbook.xlsx.writeFile(file);
20
- }
21
-
22
- function makeProject() {
23
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-doc-'));
24
- fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
25
- baseUrl: 'http://example.test',
26
- cases: { markdown: 'cases' },
27
- output: { dir: 'reports' },
28
- }, null, 2));
29
- fs.mkdirSync(path.join(dir, 'cases'), { recursive: true });
30
- return { dir, config: loadConfig(dir) };
31
- }
32
-
33
- function renderDoc(cases) {
34
- return '# 文档对象\n\n' + cases.map(([id, title, step]) => [
35
- '## ' + id + ' ' + title,
36
- '',
37
- '| 项 | 内容 |',
38
- '|---|---|',
39
- '| **前置条件** | 系统可访问 |',
40
- '| **操作步骤** | ' + step + ' |',
41
- '| **预期结果** | 页面正常 |',
42
- '',
43
- ].join('\n')).join('\n');
44
- }
45
-
46
- test('import: 文档对象稳定,merge 保留已移除用例,overwrite 删除并写 tombstone', async () => {
47
- const { dir, config } = makeProject();
48
- const source = path.join(dir, 'doc.md');
49
- fs.writeFileSync(source, renderDoc([['TC-DOC-001', '第一条', '打开页面'], ['TC-DOC-002', '第二条', '点击查询']]));
50
-
51
- const first = await importDocument(config, { file: source, name: '文档对象' });
52
- assert.equal(first.doc.docKey, '文档对象');
53
- assert.match(first.doc.id, /^doc-[0-9a-f]{10}$/);
54
- assert.equal(first.diffSummary.added, 2);
55
- assert.equal(loadProjectCases(config).cases.length, 2);
56
-
57
- const copied = path.join(dir, 'doc-copy.md');
58
- fs.copyFileSync(source, copied);
59
- const second = await importDocument(config, { file: copied, name: '文档对象' });
60
- assert.equal(second.doc.id, first.doc.id, '相同 docKey 复用文档 id');
61
- assert.equal(readDocs(config).docs.length, 1, '上传路径变化不产生重复文档对象');
62
- assert.equal(second.doc.version, 2);
63
-
64
- fs.writeFileSync(source, renderDoc([['TC-DOC-001', '第一条(改)', '打开页面并等待']]));
65
- const merged = await importDocument(config, { file: source, name: '文档对象', mode: 'merge' });
66
- assert.equal(merged.diffSummary.missing, 1);
67
- assert.equal(merged.diffSummary.removed, 0);
68
- assert.deepEqual(loadProjectCases(config).cases.map((item) => item.tcId), ['TC-DOC-001', 'TC-DOC-002']);
69
- assert.equal(loadProjectCases(config).cases.find((item) => item.tcId === 'TC-DOC-001').functionPoint, '第一条(改)');
70
-
71
- const overwritten = await importDocument(config, { file: source, name: '文档对象', mode: 'overwrite' });
72
- assert.equal(overwritten.diffSummary.removed, 1);
73
- assert.deepEqual(loadProjectCases(config).cases.map((item) => item.tcId), ['TC-DOC-001']);
74
-
75
- fs.writeFileSync(source, renderDoc([['TC-DOC-001', '第一条(改)', '打开页面并等待'], ['TC-DOC-002', '第二条回归', '点击查询']]));
76
- const restored = await importDocument(config, { file: source, name: '文档对象', mode: 'overwrite' });
77
- assert.equal(restored.diffSummary.added, 1);
78
- assert.deepEqual(loadProjectCases(config).cases.map((item) => item.tcId), ['TC-DOC-001', 'TC-DOC-002']);
79
- });
80
-
81
- test('import: 重新导入时同编号用例以文档内容更新,面板修改不遮蔽', async () => {
82
- const { config } = makeProject();
83
- const source = path.join(config.projectDir, 'doc.md');
84
- fs.writeFileSync(source, renderDoc([['TC-DOC-010', '文档标题', '打开页面']]));
85
- await importDocument(config, { file: source, name: '文档对象' });
86
- const before = loadProjectCases(config).cases[0];
87
- saveCase(config.projectDir, { ...before, functionPoint: '面板标题一' }, { originalTcId: before.tcId, by: 'test', source: before.source });
88
- const edited = loadProjectCases(config).cases[0];
89
- saveCase(config.projectDir, { ...edited, functionPoint: '面板标题二' }, { originalTcId: edited.tcId, by: 'test', source: edited.source });
90
- assert.equal(loadProjectCases(config).cases[0].functionPoint, '面板标题二');
91
- assert.equal(loadProjectCases(config).cases[0].source.original?.docKey, '文档对象');
92
-
93
- await importDocument(config, { file: source, name: '文档对象', mode: 'merge' });
94
- assert.equal(loadProjectCases(config).cases[0].functionPoint, '文档标题');
95
- });
96
-
97
- test('import: 同源文档改名重新导入不复制用例,旧生成文件被清理', async () => {
98
- const { config } = makeProject();
99
- const source = path.join(config.projectDir, 'renamed.md');
100
- fs.writeFileSync(source, [
101
- '# 无编号用例',
102
- '',
103
- '## 登录成功',
104
- '',
105
- '| 项 | 内容 |',
106
- '|---|---|',
107
- '| **前置条件** | 系统可访问 |',
108
- '| **操作步骤** | 打开登录页 |',
109
- '| **预期结果** | 进入首页 |',
110
- '',
111
- '## 退出登录',
112
- '',
113
- '| 项 | 内容 |',
114
- '|---|---|',
115
- '| **前置条件** | 已登录 |',
116
- '| **操作步骤** | 点击退出 |',
117
- '| **预期结果** | 回到登录页 |',
118
- '',
119
- ].join('\n'));
120
- const first = await importDocument(config, { file: source, name: '文档甲' });
121
- const firstIds = first.cases.map((item) => item.tcId);
122
- const firstGenerated = first.doc.generatedPath;
123
- assert.ok(fs.existsSync(firstGenerated));
124
-
125
- const second = await importDocument(config, { file: source, name: '文档乙', mode: 'merge' });
126
- assert.equal(second.doc.id, first.doc.id);
127
- assert.deepEqual(second.cases.map((item) => item.tcId), firstIds);
128
- assert.deepEqual(loadProjectCases(config).cases.map((item) => item.tcId), firstIds);
129
- assert.equal(fs.existsSync(firstGenerated), false, '旧的生成文件被改名导入清理');
130
- assert.equal(readDocs(config).docs.length, 1);
131
- });
132
-
133
- test('import: 不同来源的 xlsx 文档使用相同用例编号时拒绝导入,不抢走其它文档的用例', async () => {
134
- const { config } = makeProject();
135
- const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
136
- await importDocument(config, { file: fixture, name: 'M006甲' });
137
- const copy = path.join(config.projectDir, 'M006副本.xlsx');
138
- fs.copyFileSync(fixture, copy);
139
- await assert.rejects(
140
- () => importDocument(config, { file: copy, name: 'M006乙' }),
141
- (error) => error.code === 'XLSX_TCID_CONFLICT',
142
- );
143
- assert.equal(loadProjectCases(config).cases.length, 249);
144
- assert.equal(loadProjectCases(config).cases[0].source.docKey, 'm006甲');
145
- });
146
-
147
- test('import: 改名时自动编号沿用旧前缀,不产生重复用例', async () => {
148
- const { config } = makeProject();
149
- const source = path.join(config.projectDir, 'auto.md');
150
- fs.writeFileSync(source, [
151
- '# 自动编号',
152
- '',
153
- '## 登录成功',
154
- '',
155
- '| 项 | 内容 |',
156
- '|---|---|',
157
- '| **前置条件** | 系统可访问 |',
158
- '| **操作步骤** | 打开登录页 |',
159
- '| **预期结果** | 进入首页 |',
160
- '',
161
- '## 退出登录',
162
- '',
163
- '| 项 | 内容 |',
164
- '|---|---|',
165
- '| **前置条件** | 已登录 |',
166
- '| **操作步骤** | 点击退出 |',
167
- '| **预期结果** | 回到登录页 |',
168
- '',
169
- ].join('\n'));
170
- const first = await importDocument(config, { file: source, name: 'Login Flow' });
171
- assert.deepEqual(first.cases.map((item) => item.tcId), ['LOGIN-FLOW-001', 'LOGIN-FLOW-002']);
172
- const firstGenerated = first.doc.generatedPath;
173
- const edited = loadProjectCases(config).cases[0];
174
- saveCase(config.projectDir, { ...edited, functionPoint: '面板改标题' }, { originalTcId: edited.tcId, by: 'test', source: edited.source });
175
- const second = await importDocument(config, { file: source, name: '认证登录', mode: 'merge' });
176
- assert.deepEqual(second.cases.map((item) => item.tcId), ['LOGIN-FLOW-001', 'LOGIN-FLOW-002']);
177
- assert.equal(loadProjectCases(config).cases.length, 2, '改名不复制用例');
178
- assert.equal(loadProjectCases(config).cases[0].functionPoint, '登录成功', '面板修改让位给文档');
179
- assert.equal(fs.existsSync(firstGenerated), false);
180
- });
181
-
182
- test('import: xlsx 文档改名后,保留的旧用例与 tombstone 迁到新文档 key', async () => {
183
- const { config } = makeProject();
184
- const original = path.join(config.projectDir, '原文档.xlsx');
185
- await writeSmallXlsx(original, [[1, 'TC-RN-01', '旧一'], [2, 'TC-RN-02', '旧二']]);
186
- await importDocument(config, { file: original, name: '原文档' });
187
- deleteCases(config.projectDir, ['TC-RN-02'], { cases: loadProjectCases(config).cases });
188
-
189
- await writeSmallXlsx(original, [[1, 'TC-RN-01', '旧一']]);
190
- await importDocument(config, { file: original, name: '新文档', mode: 'merge' });
191
- const tombstone = loadImportedCases(config.projectDir).deleted.find((item) => item.tcId === 'TC-RN-02');
192
- assert.equal(tombstone?.case?.source?.docKey, '新文档');
193
- restoreCases(config.projectDir, ['TC-RN-02']);
194
- assert.equal(loadProjectCases(config).cases.find((item) => item.tcId === 'TC-RN-02')?.source?.docKey, '新文档');
195
-
196
- await writeSmallXlsx(original, [[1, 'TC-RN-01', '新一'], [2, 'TC-RN-02', '新二']]);
197
- const updated = await importDocument(config, { file: original, name: '新文档', mode: 'merge' });
198
- assert.equal(updated.diffSummary.updated, 2);
199
- const final = loadProjectCases(config).cases;
200
- assert.equal(final.find((item) => item.tcId === 'TC-RN-01')?.functionPoint, '新一');
201
- assert.equal(final.find((item) => item.tcId === 'TC-RN-02')?.functionPoint, '新二');
202
- });
203
-
204
- test('import: 旧 imported 条目没有 docKey 时按 docs.json 判断归属,不抢走其它文档的编号', async () => {
205
- const { config } = makeProject();
206
- const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
207
- await importDocument(config, { file: fixture, name: 'M006甲' });
208
- const storeFile = path.join(config.projectDir, '.uitest/cases/imported.json');
209
- const store = JSON.parse(fs.readFileSync(storeFile, 'utf8'));
210
- store.cases = store.cases.map((item) => ({
211
- ...item,
212
- source: { kind: item.source.kind, file: item.source.file, sheet: item.source.sheet, importId: item.source.importId },
213
- }));
214
- fs.writeFileSync(storeFile, JSON.stringify(store));
215
- const copy = path.join(config.projectDir, 'M006副本.xlsx');
216
- fs.copyFileSync(fixture, copy);
217
- await assert.rejects(
218
- () => importDocument(config, { file: copy, name: 'M006乙' }),
219
- (error) => error.code === 'XLSX_TCID_CONFLICT',
220
- );
221
- assert.equal(loadProjectCases(config).cases.length, 249);
222
- });
223
-
224
- test('import: 旧 docs 没有 docKey 时不会把其它文档的 importId 归给自己', async () => {
225
- const { config } = makeProject();
226
- const fileA = path.join(config.projectDir, 'a.xlsx');
227
- const fileB = path.join(config.projectDir, 'b.xlsx');
228
- await writeSmallXlsx(fileA, [[1, 'TC-LEGACY-A', '甲用例']]);
229
- await writeSmallXlsx(fileB, [[1, 'TC-LEGACY-B', '乙用例']]);
230
- await importDocument(config, { file: fileA, name: 'A' });
231
- await importDocument(config, { file: fileB, name: 'B' });
232
-
233
- const docsFile = path.join(config.projectDir, '.uitest/studio/docs.json');
234
- const docs = JSON.parse(fs.readFileSync(docsFile, 'utf8'));
235
- docs.docs = docs.docs.map((doc) => ({
236
- id: doc.id, name: doc.name, sourcePath: doc.sourcePath, generatedPath: doc.generatedPath,
237
- kind: doc.kind, importedAt: doc.importedAt, count: doc.count, caseIds: doc.caseIds, version: doc.version, mode: doc.mode,
238
- }));
239
- fs.writeFileSync(docsFile, JSON.stringify(docs));
240
-
241
- const storeFile = path.join(config.projectDir, '.uitest/cases/imported.json');
242
- const store = JSON.parse(fs.readFileSync(storeFile, 'utf8'));
243
- store.sources = store.sources.map((source) => ({
244
- id: source.id, name: source.name, file: source.file, sheets: source.sheets, importedAt: source.importedAt, count: source.count,
245
- }));
246
- fs.writeFileSync(storeFile, JSON.stringify(store));
247
-
248
- await importDocument(config, { file: fileA, name: 'A' });
249
- const after = JSON.parse(fs.readFileSync(docsFile, 'utf8')).docs.find((doc) => doc.name === 'A');
250
- const bSourceId = store.sources.find((source) => source.name === 'B').id;
251
- assert.equal(after.importIds.includes(bSourceId), false, '不接管 B 的导入来源');
252
- assert.ok(after.importIds.length >= 1, '保留 A 自己的导入来源');
253
- const again = await importDocument(config, { file: fileB, name: 'B' });
254
- assert.equal(again.cases.length, 1);
255
- });
256
-
257
- test('bulkUpdateCases: 批量修改保留来源对象,不把整条用例写进 original', async () => {
258
- const { config } = makeProject();
259
- const source = path.join(config.projectDir, 'bulk.md');
260
- fs.writeFileSync(source, renderDoc([['TC-BULK-01', '批量用例', '打开页面']]));
261
- await importDocument(config, { file: source, name: '批量文档' });
262
- const item = loadProjectCases(config).cases[0];
263
- const result = bulkUpdateCases(config.projectDir, [item.tcId], { priority: '低' }, { cases: [item] });
264
- assert.equal(result.updated, 1);
265
- const saved = loadImportedCases(config.projectDir).cases.find((entry) => entry.tcId === item.tcId);
266
- assert.equal(saved.source.kind, 'manual');
267
- assert.equal(saved.source.original?.kind, 'markdown');
268
- assert.equal(saved.source.original?.tcId, undefined);
269
- assert.equal(saved.source.original?.docKey, '批量文档');
270
- });
271
-
272
- test('delete/restore: xlsx 导入用例可从 tombstone 快照恢复', async () => {
273
- const { config } = makeProject();
274
- const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
275
- await importDocument(config, { file: fixture, name: 'M006' });
276
- const before = loadProjectCases(config).cases;
277
- const target = before[0];
278
- const removed = deleteCases(config.projectDir, [target.tcId], { cases: before, by: 'test' });
279
- assert.equal(removed.deleted, 1);
280
- assert.equal(loadProjectCases(config).cases.some((item) => item.tcId === target.tcId), false);
281
- const restored = restoreCases(config.projectDir, [target.tcId]);
282
- assert.equal(restored.restored, 1);
283
- assert.equal(restored.results[0].written, true);
284
- const after = loadProjectCases(config).cases.find((item) => item.tcId === target.tcId);
285
- assert.ok(after, '恢复后用例重新可见');
286
- assert.equal(after.functionPoint, target.functionPoint);
287
- assert.equal(loadProjectCases(config).cases[0].tcId, target.tcId, '恢复后回到原来的顺序');
288
- });
289
-
290
- test('import: 面板新增的用例不属于文档,重新导入不会删除', async () => {
291
- const { config } = makeProject();
292
- const source = path.join(config.projectDir, 'doc.md');
293
- fs.writeFileSync(source, renderDoc([['TC-DOC-020', '文档用例', '打开页面']]));
294
- await importDocument(config, { file: source, name: '文档对象' });
295
- saveCase(config.projectDir, {
296
- tcId: 'TC-MANUAL-001',
297
- functionPoint: '面板新增',
298
- precondition: '无',
299
- steps: '打开页面',
300
- expected: '正常',
301
- }, { by: 'test' });
302
- assert.equal(loadProjectCases(config).cases.length, 2);
303
- await importDocument(config, { file: source, name: '文档对象', mode: 'overwrite' });
304
- assert.deepEqual(loadProjectCases(config).cases.map((item) => item.tcId).sort(), ['TC-DOC-020', 'TC-MANUAL-001']);
305
- });
@@ -1,196 +0,0 @@
1
- import assert from 'node:assert/strict';
2
- import fs from 'node:fs';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import test from 'node:test';
6
-
7
- import ExcelJS from 'exceljs';
8
-
9
- import { loadConfig } from '../../lib/config.mjs';
10
- import { loadProjectCases } from '../../lib/cases-import.mjs';
11
- import { addResult } from '../../lib/results.mjs';
12
- import { loadCases } from '../../lib/xlsx.mjs';
13
- import { importDocument } from '../import.mjs';
14
- import { exportCasesWorkbook, exportResultsWorkbook } from '../export.mjs';
15
- import { createBatch, saveCaseRun } from '../store.mjs';
16
-
17
- const DOC = [
18
- '# 导出用例',
19
- '',
20
- '## TC-EXP-001 登录成功',
21
- '',
22
- '| 项 | 内容 |',
23
- '|---|---|',
24
- '| **优先级** | 高 |',
25
- '| **前置条件** | 系统可访问 |',
26
- '| **操作步骤** | 打开登录页<br>输入账号密码 |',
27
- '| **预期结果** | 进入首页 |',
28
- '',
29
- '## TC-EXP-002 查询列表',
30
- '',
31
- '| 项 | 内容 |',
32
- '|---|---|',
33
- '| **优先级** | 中 |',
34
- '| **前置条件** | 已登录 |',
35
- '| **操作步骤** | 打开列表页 |',
36
- '| **预期结果** | 列表返回数据 |',
37
- '',
38
- ].join('\n');
39
-
40
- function makeProject() {
41
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-export-'));
42
- fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
43
- baseUrl: 'http://example.test',
44
- cases: { markdown: 'cases' },
45
- output: { dir: 'reports' },
46
- }, null, 2));
47
- fs.mkdirSync(path.join(dir, 'cases'), { recursive: true });
48
- return { dir, config: loadConfig(dir) };
49
- }
50
-
51
- async function projectWithCases() {
52
- const { dir, config } = makeProject();
53
- const source = path.join(dir, 'cases.md');
54
- fs.writeFileSync(source, DOC);
55
- await importDocument(config, { file: source, name: '导出用例' });
56
- return { dir, config };
57
- }
58
-
59
- test('export: 用例 xlsx 使用 M006 列模板并可再次导入', async () => {
60
- const { config } = await projectWithCases();
61
- const result = await exportCasesWorkbook(config, {});
62
- assert.ok(fs.existsSync(result.file));
63
- assert.equal(result.count, 2);
64
- const loaded = loadCases({ file: result.file, sheet: '导出用例' });
65
- assert.deepEqual(loaded.headers.map((item) => item.header), [
66
- '序号', '优先级', '模块', '一级功能', '二级功能', '测试功能点', '前提条件', '操作步骤', '预期结果', '测试结果', '备注', '是否可自动化', '用例编号',
67
- ]);
68
- assert.deepEqual(loaded.cases.map((item) => item.tcId), ['TC-EXP-001', 'TC-EXP-002']);
69
- assert.equal(loaded.cases[0].priority, '高');
70
-
71
- const reimport = await importDocument(config, { file: result.file, name: '导出用例', mode: 'overwrite' });
72
- assert.equal(reimport.diffSummary.added, 0);
73
- assert.equal(reimport.diffSummary.removed, 0);
74
- assert.equal(reimport.diffSummary.updated, 0);
75
- assert.equal(reimport.diffSummary.unchanged, 2);
76
- assert.equal(reimport.warnings.some((item) => String(item.sheet || item.message || '').includes('发行控制')), false);
77
- });
78
-
79
- test('export: 表头样式、冻结与用例编号列可被 Excel 识别', async () => {
80
- const { config } = await projectWithCases();
81
- const result = await exportCasesWorkbook(config, {});
82
- const workbook = new ExcelJS.Workbook();
83
- await workbook.xlsx.readFile(result.file);
84
- assert.deepEqual(workbook.worksheets.map((sheet) => sheet.name), ['发行控制', '导出用例']);
85
- const sheet = workbook.getWorksheet('导出用例');
86
- assert.equal(sheet.getCell('A1').value, '序号');
87
- assert.equal(sheet.getCell('L1').value, '是否可自动化');
88
- assert.equal(sheet.getCell('M1').value, '用例编号');
89
- assert.equal(sheet.getCell('A1').fill.fgColor.argb, 'FFFFFF00');
90
- assert.equal(sheet.views[0].state, 'frozen');
91
- assert.equal(sheet.views[0].ySplit, 1);
92
- assert.equal(sheet.getCell('M2').value, 'TC-EXP-001');
93
- });
94
-
95
- test('export: 二级功能两行分组也会纵向合并', async () => {
96
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-export-merge-'));
97
- const xlsx = path.join(dir, 'cases.xlsx');
98
- const source = new ExcelJS.Workbook();
99
- const sheet = source.addWorksheet('用例');
100
- sheet.addRow(['序号', '优先级', '模块', '一级功能', '二级功能', '测试功能点', '前提条件', '操作步骤', '预期结果', '测试结果', '备注']);
101
- sheet.addRow([1, '中', '模块', '一级', 'A', '功能1', '', '', '', '', 'TC-MERGE-01']);
102
- sheet.addRow([2, '中', '模块', '一级', 'A', '功能2', '', '', '', '', 'TC-MERGE-02']);
103
- sheet.addRow([3, '中', '模块', '一级', 'B', '功能3', '', '', '', '', 'TC-MERGE-03']);
104
- sheet.addRow([4, '中', '模块', '一级', 'B', '功能4', '', '', '', '', 'TC-MERGE-04']);
105
- await source.xlsx.writeFile(xlsx);
106
- fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
107
- baseUrl: 'http://example.test',
108
- cases: { xlsx },
109
- output: { dir: 'reports' },
110
- }, null, 2));
111
- const config = loadConfig(dir);
112
- const result = await exportCasesWorkbook(config, {});
113
- const workbook = new ExcelJS.Workbook();
114
- await workbook.xlsx.readFile(result.file);
115
- const exported = workbook.getWorksheet('模块');
116
- assert.deepEqual(exported.model.merges, ['E2:E3', 'E4:E5']);
117
- });
118
-
119
- test('export: 数据表名避开导入器保留的工作表名', async () => {
120
- const { config } = await projectWithCases();
121
- const result = await exportCasesWorkbook(config, { title: '测试计划' });
122
- const workbook = new ExcelJS.Workbook();
123
- await workbook.xlsx.readFile(result.file);
124
- assert.deepEqual(workbook.worksheets.map((sheet) => sheet.name), ['发行控制', '测试计划-用例']);
125
- });
126
-
127
- test('export: 测试结果 xlsx 带结果状态与执行明细', async () => {
128
- const { config } = await projectWithCases();
129
- addResult(config, { caseRef: 'TC-EXP-001', status: '通过', note: '登录正常', evidence: [] });
130
- const result = await exportResultsWorkbook(config, {});
131
- assert.equal(result.summary.passed, 1);
132
- assert.equal(result.summary.untested, 1);
133
- const workbook = new ExcelJS.Workbook();
134
- await workbook.xlsx.readFile(result.file);
135
- const sheet = workbook.getWorksheet('导出用例');
136
- const headers = sheet.getRow(1).values.slice(1);
137
- assert.deepEqual(headers.slice(-4), ['用例编号', '执行批次', '耗时', '执行说明']);
138
- assert.equal(sheet.getCell('J2').value, '通过');
139
- assert.equal(sheet.getCell('P2').value, '登录正常');
140
- assert.equal(sheet.getCell('J3').value, '未执行');
141
- });
142
-
143
- test('export: 覆盖导入会隐藏仍然存在于同一源 xlsx 的旧用例', async () => {
144
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-export-xlsx-'));
145
- const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
146
- fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
147
- baseUrl: 'http://example.test',
148
- cases: { xlsx: fixture, sheet: '履约管理' },
149
- output: { dir: 'reports' },
150
- }, null, 2));
151
- const config = loadConfig(dir);
152
- const first = await importDocument(config, { file: fixture, name: 'M006' });
153
- const subsetIds = first.doc.caseIds.slice(0, 3);
154
- const exported = await exportCasesWorkbook(config, { caseIds: subsetIds, out: path.join(dir, 'subset.xlsx') });
155
- const overwritten = await importDocument(config, { file: exported.file, name: 'M006', mode: 'overwrite' });
156
- assert.equal(overwritten.diffSummary.removed, first.doc.caseIds.length - 3);
157
- assert.equal(overwritten.diffSummary.hidden, first.doc.caseIds.length - 3);
158
- assert.equal(loadProjectCases(config).cases.length, 3);
159
- const full = await importDocument(config, { file: fixture, name: 'M006', mode: 'overwrite' });
160
- assert.equal(full.diffSummary.added, first.doc.caseIds.length - 3);
161
- assert.equal(loadProjectCases(config).cases.length, first.doc.caseIds.length);
162
- });
163
-
164
- test('export: 批次结果优先于结果库', async () => {
165
- const { config } = await projectWithCases();
166
- const batch = createBatch(config, {
167
- name: '导出批次',
168
- caseIds: ['TC-EXP-001', 'TC-EXP-002'],
169
- plan: { items: [{ caseId: 'TC-EXP-001', title: '登录成功', mode: 'flow' }, { caseId: 'TC-EXP-002', title: '查询列表', mode: 'agent' }], summary: { total: 2 } },
170
- options: { writeBack: 'none' },
171
- });
172
- // 队列项入队与结果落在同一毫秒时会被判为上一轮结果;这里把完成时间放到入队之后。
173
- const endedAt = new Date(Date.parse(batch.createdAt) + 2000).toISOString();
174
- const startedAt = new Date(Date.parse(endedAt) - 1234).toISOString();
175
- saveCaseRun(config, batch.id, {
176
- caseId: 'TC-EXP-001',
177
- batchId: batch.id,
178
- title: '登录成功',
179
- mode: 'flow',
180
- status: 'failed',
181
- startedAt,
182
- endedAt,
183
- durationMs: 1234,
184
- note: '登录按钮无响应',
185
- steps: [],
186
- evidence: [],
187
- });
188
- const result = await exportResultsWorkbook(config, { batchId: batch.id });
189
- assert.equal(result.summary.failed, 1);
190
- const workbook = new ExcelJS.Workbook();
191
- await workbook.xlsx.readFile(result.file);
192
- const sheet = workbook.getWorksheet('导出用例');
193
- assert.equal(sheet.getCell('J2').value, '失败');
194
- assert.equal(sheet.getCell('N2').value, batch.id);
195
- assert.equal(sheet.getCell('P2').value, '登录按钮无响应');
196
- });