dsh-uitest 2.1.4 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -13
- package/bin/studio.mjs +117 -17
- package/bin/uitest.mjs +3 -3
- package/client.js +2583 -683
- package/index.js +90 -5
- package/lib/ab.mjs +17 -4
- package/lib/backfill.mjs +134 -61
- package/lib/browser-ab.mjs +3 -2
- package/lib/browser.mjs +1 -1
- package/lib/cases-import.mjs +198 -89
- package/lib/cases-md.mjs +5 -0
- package/lib/config.mjs +2 -2
- package/lib/csv.mjs +207 -0
- package/lib/knowledge-central.mjs +16 -0
- package/lib/xlsx.mjs +2 -1
- package/package.json +1 -1
- package/skills/uitest/SKILL.md +9 -4
- package/studio/documents.mjs +332 -0
- package/studio/host.mjs +141 -17
- package/studio/import.mjs +231 -38
- package/studio/model.mjs +3 -0
- package/studio/runner.mjs +55 -0
- package/studio/snapshot.mjs +28 -30
- package/studio/store.mjs +56 -1
- package/studio/writeback.mjs +299 -33
- package/templates/uitest.config.json +1 -1
package/index.js
CHANGED
|
@@ -16,17 +16,18 @@ import { fileURLToPath } from 'node:url';
|
|
|
16
16
|
|
|
17
17
|
import { loadConfig } from './lib/config.mjs';
|
|
18
18
|
import { readFlows } from './host/state.mjs';
|
|
19
|
-
import { importDocument } from './studio/import.mjs';
|
|
19
|
+
import { importDocument, reReadDocument } from './studio/import.mjs';
|
|
20
|
+
import { deleteDocumentRecord, setDocumentArchived } from './studio/documents.mjs';
|
|
20
21
|
import { accountsPayload, deleteAccount, importAccountsText, saveAccount, setDefaultAccount } from './studio/accounts.mjs';
|
|
21
22
|
import { buildStudioPlan, compactPlan } from './studio/plan.mjs';
|
|
22
23
|
import { buildStudioSnapshot } from './studio/snapshot.mjs';
|
|
23
24
|
import { knowledgeSnapshot } from './studio/knowledge.mjs';
|
|
24
25
|
import { knowledgeContextForCase } from './lib/knowledge-query.mjs';
|
|
25
26
|
import { centralSearch } from './lib/knowledge-central.mjs';
|
|
26
|
-
import { createBatch, mergeBatchRuns, patchBatch, readBatch, readCaseRuns } from './studio/store.mjs';
|
|
27
|
+
import { acquireLock, createBatch, mergeBatchRuns, patchBatch, readBatch, readCaseRuns } from './studio/store.mjs';
|
|
27
28
|
import { summarizeCaseRuns } from './studio/model.mjs';
|
|
28
29
|
import { requestBatchStop, runBatch } from './studio/runner.mjs';
|
|
29
|
-
import { applyBatchWriteback, previewBatchWriteback } from './studio/writeback.mjs';
|
|
30
|
+
import { applyBatchWriteback, applyDocumentWriteback, previewBatchWriteback, previewDocumentWriteback } from './studio/writeback.mjs';
|
|
30
31
|
import { registerTestApprovalAnswerer } from './host/agent-test.mjs';
|
|
31
32
|
import {
|
|
32
33
|
activeBatchFor,
|
|
@@ -158,7 +159,7 @@ function toolDefinitions(ctx, panel) {
|
|
|
158
159
|
return [
|
|
159
160
|
{
|
|
160
161
|
name: 'uitest_import',
|
|
161
|
-
description: 'Import a test-case document into the project and generate natural-language cases. Supports .md/.markdown/.txt/.xlsx/.json or a directory. Markdown/text is parsed into cases with 前置条件 / 测试过程 / 预期结果 and written to cases/studio/*.md; xlsx
|
|
162
|
+
description: 'Import a test-case document into the project and generate natural-language cases. Supports .md/.markdown/.txt/.xlsx/.csv/.json or a directory. Markdown/text is parsed into cases with 前置条件 / 测试过程 / 预期结果 and written to cases/studio/*.md; xlsx and CSV are imported into the project case overlay, and CSV also gets a generated Markdown write-back mirror; the source file is never modified. Returns the document record, generated case ids, warnings, and the refreshed case counts. Use this as the first step of the Test Studio story.',
|
|
162
163
|
parameters: {
|
|
163
164
|
type: 'object',
|
|
164
165
|
additionalProperties: false,
|
|
@@ -167,6 +168,8 @@ function toolDefinitions(ctx, panel) {
|
|
|
167
168
|
project: { type: 'string', description: 'Test project directory containing uitest.config.json. Defaults to the session cwd.' },
|
|
168
169
|
file: { type: 'string', description: 'Document file or directory, absolute or relative to the project.' },
|
|
169
170
|
name: { type: 'string', description: 'Optional document name used for the generated case prefix.' },
|
|
171
|
+
docId: { type: 'string', description: 'Re-associate this existing document instead of matching by name/path. Only a single file is accepted.' },
|
|
172
|
+
mode: { type: 'string', enum: ['merge', 'overwrite'], description: 'merge keeps cases removed from the source; overwrite removes them. Defaults to merge.' },
|
|
170
173
|
},
|
|
171
174
|
},
|
|
172
175
|
output: textOutput(),
|
|
@@ -175,7 +178,12 @@ function toolDefinitions(ctx, panel) {
|
|
|
175
178
|
const { cwd } = projectContext(ctx, exec, input.project);
|
|
176
179
|
const config = loadConfig(cwd);
|
|
177
180
|
const file = isAbsolute(String(input.file)) ? String(input.file) : resolve(config.projectDir, String(input.file));
|
|
178
|
-
const result = await importDocument(config, {
|
|
181
|
+
const result = await importDocument(config, {
|
|
182
|
+
file,
|
|
183
|
+
name: input.name || '',
|
|
184
|
+
docId: input.docId || '',
|
|
185
|
+
mode: input.mode === 'overwrite' ? 'overwrite' : 'merge',
|
|
186
|
+
});
|
|
179
187
|
return {
|
|
180
188
|
ok: true,
|
|
181
189
|
doc: result.doc,
|
|
@@ -185,6 +193,83 @@ function toolDefinitions(ctx, panel) {
|
|
|
185
193
|
};
|
|
186
194
|
},
|
|
187
195
|
},
|
|
196
|
+
{
|
|
197
|
+
name: 'uitest_docs',
|
|
198
|
+
description: 'Manage the imported test-case document layer above natural-language cases. Each document keeps a stable id, its source file, the generated case file, ownership of case ids, archive state, read history and the last write-back summary. action=list returns documents with visible/owned/tombstoned/stale counts, source and case-file state, per-status counts, last run and archive flag. action=writeback previews or applies result write-back for one document (optionally one batch or selected cases); Markdown/JSON/CSV writes into the generated case file, xlsx writes a separate execution copy and never touches the source workbook. action=re-read re-reads the stored source file, keeps the document identity, and best-effort re-applies the latest results. action=archive/restore hide or restore a document and its cases without deleting data. action=delete removes only the document record (confirm=true required); its cases stay in the library as unassigned.',
|
|
199
|
+
parameters: {
|
|
200
|
+
type: 'object',
|
|
201
|
+
additionalProperties: false,
|
|
202
|
+
required: ['action'],
|
|
203
|
+
properties: {
|
|
204
|
+
action: { type: 'string', enum: ['list', 'writeback', 're-read', 'archive', 'restore', 'delete'] },
|
|
205
|
+
project: { type: 'string', description: 'Test project directory. Defaults to the session cwd.' },
|
|
206
|
+
docId: { type: 'string', description: 'Document id. Required for every action except list.' },
|
|
207
|
+
includeArchived: { type: 'boolean', description: 'For list: include archived documents. Defaults to false.' },
|
|
208
|
+
batchId: { type: 'string', description: 'For writeback: use this batch instead of the latest result per case.' },
|
|
209
|
+
caseIds: { type: 'array', items: { type: 'string' }, description: 'For writeback: restrict to these case ids.' },
|
|
210
|
+
dryRun: { type: 'boolean', description: 'For writeback: preview only. Defaults to true unless apply=true or dryRun=false.' },
|
|
211
|
+
apply: { type: 'boolean', description: 'For writeback: actually write. Defaults to false (preview).' },
|
|
212
|
+
mode: { type: 'string', enum: ['merge', 'overwrite'], description: 'For re-read: merge keeps cases removed from the source; overwrite removes them. Defaults to the document mode.' },
|
|
213
|
+
confirm: { type: 'boolean', description: 'For delete: must be true; deletion cannot be undone from the tool.' },
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
output: textOutput(),
|
|
217
|
+
async execute(args, exec) {
|
|
218
|
+
const input = asRecord(args);
|
|
219
|
+
const { cwd } = projectContext(ctx, exec, input.project);
|
|
220
|
+
const config = loadConfig(cwd);
|
|
221
|
+
if (input.action === 'list') {
|
|
222
|
+
const snapshot = buildStudioSnapshot(config, { caseLimit: 1 });
|
|
223
|
+
if (snapshot.docsError) throw toolError(snapshot.docsError.message);
|
|
224
|
+
const docs = input.includeArchived === true
|
|
225
|
+
? snapshot.docs
|
|
226
|
+
: snapshot.docs.filter((doc) => !doc.archived);
|
|
227
|
+
return { ok: true, docs, total: snapshot.docs.length, archived: snapshot.counts.archivedDocs };
|
|
228
|
+
}
|
|
229
|
+
const docId = String(input.docId || '');
|
|
230
|
+
if (!docId) throw toolError('uitest_docs ' + input.action + ' requires docId');
|
|
231
|
+
if (input.action === 'writeback') {
|
|
232
|
+
const active = activeBatchFor(config);
|
|
233
|
+
if (active) throw toolError('已有批次在执行(' + (active.name || active.id) + ');请先停止或等待完成');
|
|
234
|
+
const options = {
|
|
235
|
+
batchId: input.batchId || '',
|
|
236
|
+
caseIds: Array.isArray(input.caseIds) ? input.caseIds.map(String) : [],
|
|
237
|
+
};
|
|
238
|
+
if (input.apply === true || input.dryRun === false) {
|
|
239
|
+
return { ok: true, writeback: await applyDocumentWriteback(config, docId, options) };
|
|
240
|
+
}
|
|
241
|
+
return { ok: true, preview: await previewDocumentWriteback(config, docId, options) };
|
|
242
|
+
}
|
|
243
|
+
if (input.action === 're-read') {
|
|
244
|
+
const active = activeBatchFor(config);
|
|
245
|
+
if (active) throw toolError('已有批次在执行(' + (active.name || active.id) + ');请先停止或等待完成');
|
|
246
|
+
const result = await reReadDocument(config, { docId, mode: input.mode || '' });
|
|
247
|
+
return {
|
|
248
|
+
ok: true,
|
|
249
|
+
doc: result.doc,
|
|
250
|
+
caseCount: (result.cases || []).length,
|
|
251
|
+
diff: result.diffSummary || null,
|
|
252
|
+
warnings: result.warnings || [],
|
|
253
|
+
resultReapplied: result.resultReapplied === true,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (input.action === 'archive' || input.action === 'restore') {
|
|
257
|
+
return { ok: true, doc: await setDocumentArchived(config, docId, input.action === 'archive') };
|
|
258
|
+
}
|
|
259
|
+
if (input.action === 'delete') {
|
|
260
|
+
if (input.confirm !== true) throw toolError('uitest_docs delete requires confirm=true; cases stay in the library but the document record and its case ownership are lost');
|
|
261
|
+
const active = activeBatchFor(config);
|
|
262
|
+
if (active) throw toolError('已有批次在执行(' + (active.name || active.id) + ');请先停止或等待完成');
|
|
263
|
+
const lock = acquireLock(config, 'writeback');
|
|
264
|
+
try {
|
|
265
|
+
return { ok: true, doc: await deleteDocumentRecord(config, docId) };
|
|
266
|
+
} finally {
|
|
267
|
+
lock.release();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
throw toolError('Unknown uitest_docs action: ' + String(input.action));
|
|
271
|
+
},
|
|
272
|
+
},
|
|
188
273
|
{
|
|
189
274
|
name: 'uitest_accounts',
|
|
190
275
|
description: 'Read or configure Test Studio test accounts. Accounts store username, name, roles, department, area, part-time department, password environment variable, storage-state file and notes. action=import parses an OMS-test-account Markdown table from a server path and merges by username; action=list returns configured accounts; action=save creates/updates one account; action=default sets the default account; action=delete removes one. Plaintext passwords in the source document are never stored or returned; only passwordEnv is kept.',
|
package/lib/ab.mjs
CHANGED
|
@@ -160,12 +160,13 @@ function parseJson(text) {
|
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
export class Ab {
|
|
163
|
-
constructor(session, { headed = false, state = null, profile = null } = {}) {
|
|
163
|
+
constructor(session, { headed = false, state = null, profile = null, viewport = null } = {}) {
|
|
164
164
|
if (!/^[\p{L}\p{N}_-]+$/u.test(session)) throw new Error('非法 session 名:' + session);
|
|
165
165
|
this.session = session;
|
|
166
166
|
this.headed = headed;
|
|
167
167
|
this.state = state;
|
|
168
168
|
this.profile = profile;
|
|
169
|
+
this.viewport = viewport;
|
|
169
170
|
this._launched = false;
|
|
170
171
|
this.closed = true;
|
|
171
172
|
}
|
|
@@ -187,17 +188,29 @@ export class Ab {
|
|
|
187
188
|
return g;
|
|
188
189
|
}
|
|
189
190
|
|
|
191
|
+
/** 会话内设置视口;CLI 不支持时静默保留默认视口。 */
|
|
192
|
+
async applyViewport() {
|
|
193
|
+
const width = Number(this.viewport?.width);
|
|
194
|
+
const height = Number(this.viewport?.height);
|
|
195
|
+
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) return;
|
|
196
|
+
try {
|
|
197
|
+
await this.cmd(['set', 'viewport', String(width), String(height)], { timeout: 30000 });
|
|
198
|
+
} catch { /* 旧版 CLI 没有 set viewport */ }
|
|
199
|
+
}
|
|
200
|
+
|
|
190
201
|
async open(url) {
|
|
191
202
|
const args = this.launchArgs();
|
|
192
203
|
this._launched = true;
|
|
193
204
|
this.closed = false;
|
|
205
|
+
let result = null;
|
|
194
206
|
try {
|
|
195
|
-
|
|
207
|
+
result = await this.cmd([...args, 'open', url], { timeout: 120000 });
|
|
196
208
|
} catch (e) {
|
|
197
209
|
// 泛微首屏常超过 open 的 25s load 等待;导航已发出,交给调用方轮询 DOM
|
|
198
|
-
if (
|
|
199
|
-
throw e;
|
|
210
|
+
if (!/waiting for load state|Timed out/i.test(e.message)) throw e;
|
|
200
211
|
}
|
|
212
|
+
await this.applyViewport();
|
|
213
|
+
return result;
|
|
201
214
|
}
|
|
202
215
|
|
|
203
216
|
async close() {
|
package/lib/backfill.mjs
CHANGED
|
@@ -230,9 +230,9 @@ function compactDate(value) {
|
|
|
230
230
|
return String(value || '').replace(/-/g, '');
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
function outputXlsxPath({ config, date, outputPath }) {
|
|
233
|
+
function outputXlsxPath({ config, date, outputPath, sourceFile }) {
|
|
234
234
|
if (outputPath) return path.isAbsolute(outputPath) ? outputPath : path.resolve(config.projectDir, outputPath);
|
|
235
|
-
const source = config.casesFile;
|
|
235
|
+
const source = sourceFile || config.casesFile;
|
|
236
236
|
const suffix = config.backfill?.xlsxSuffix || '-执行结果';
|
|
237
237
|
const ext = path.extname(source) || '.xlsx';
|
|
238
238
|
const base = path.basename(source, ext);
|
|
@@ -286,43 +286,19 @@ async function prepareWorkbookSource(source) {
|
|
|
286
286
|
return { file, cleanup: () => fs.rmSync(dir, { recursive: true, force: true }) };
|
|
287
287
|
}
|
|
288
288
|
|
|
289
|
-
/**
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
outputPath = '',
|
|
296
|
-
dryRun = false,
|
|
297
|
-
embedShots = true,
|
|
298
|
-
maxShotsPerCase = 4,
|
|
299
|
-
} = {}) {
|
|
300
|
-
const xlsxCases = cases.filter((item) => item.source?.kind === 'xlsx' && item.source.file && path.resolve(item.source.file) === path.resolve(config.casesFile));
|
|
301
|
-
if (!xlsxCases.length) return { path: null, written: false, reason: '没有来自 xlsx 的用例' };
|
|
302
|
-
const target = outputXlsxPath({ config, date, outputPath });
|
|
303
|
-
if (dryRun) {
|
|
304
|
-
const count = xlsxCases.filter((item) => resultForCase(resultsByCase, item)).length;
|
|
305
|
-
return { path: target, written: false, dryRun: true, cases: count, shots: 0 };
|
|
289
|
+
/** 面板编辑把来源包成 manual + original 链;xlsx 归属要沿链找到最初的工作簿。 */
|
|
290
|
+
function xlsxOriginOf(caseItem) {
|
|
291
|
+
let current = caseItem?.source || null;
|
|
292
|
+
for (let depth = 0; depth < 4 && current; depth += 1) {
|
|
293
|
+
if (['xlsx', 'import'].includes(current.kind) && current.file) return current;
|
|
294
|
+
current = current.original || null;
|
|
306
295
|
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
307
298
|
|
|
308
|
-
|
|
309
|
-
const ExcelJS = mod.default || mod;
|
|
310
|
-
const workbook = new ExcelJS.Workbook();
|
|
311
|
-
const prepared = await prepareWorkbookSource(config.casesFile);
|
|
312
|
-
try {
|
|
313
|
-
await workbook.xlsx.readFile(prepared.file);
|
|
314
|
-
} finally {
|
|
315
|
-
prepared.cleanup();
|
|
316
|
-
}
|
|
317
|
-
const sheetName = config.cases?.sheet || '';
|
|
318
|
-
const worksheet =
|
|
319
|
-
(sheetName && workbook.getWorksheet(sheetName)) ||
|
|
320
|
-
workbook.worksheets.find((item) => item.name.includes('履约')) ||
|
|
321
|
-
workbook.worksheets[0];
|
|
322
|
-
if (!worksheet) throw new ConfigError(`xlsx 中没有可回填的工作表:${config.casesFile}`);
|
|
299
|
+
function applySheetCases(config, worksheet, sheetCases, resultsByCase, { maxShotsPerCase = 4, dryRun = false } = {}) {
|
|
323
300
|
const header = findHeaderRow(worksheet);
|
|
324
|
-
if (!header)
|
|
325
|
-
|
|
301
|
+
if (!header) return { changed: 0, evidenceByName: new Map(), writtenIds: [], sheet: worksheet.name, headers: null };
|
|
326
302
|
const resultCol = columnOf(header.texts, [/^测试结果$/, /^执行结果$/, /^结果$/]) || header.texts.length + 1;
|
|
327
303
|
const timeCol = columnOf(header.texts, [/^执行时间$/]) || Math.max(resultCol, header.texts.length) + 1;
|
|
328
304
|
const noteCol = columnOf(header.texts, [/^执行备注$/, /^实测观察$/]) || Math.max(timeCol, header.texts.length) + 1;
|
|
@@ -337,27 +313,27 @@ export async function applyXlsxResults({
|
|
|
337
313
|
[keyShotCol, '关键步骤截图'],
|
|
338
314
|
[errorShotCol, '错误截图'],
|
|
339
315
|
];
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
316
|
+
if (!dryRun) {
|
|
317
|
+
for (const [col, title] of extraHeaders) {
|
|
318
|
+
if (!header.texts[col - 1]) {
|
|
319
|
+
const cell = setCell(worksheet, header.rowNumber, col, title);
|
|
320
|
+
cell.font = { bold: true };
|
|
321
|
+
cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFE7E6E6' } };
|
|
322
|
+
worksheet.getColumn(col).width = title.includes('截图') ? 36 : title === '执行备注' ? 60 : 20;
|
|
323
|
+
}
|
|
346
324
|
}
|
|
347
325
|
}
|
|
348
326
|
|
|
349
327
|
const seqCol = columnOf(header.texts, [/^序号$/]);
|
|
350
328
|
const tcCol = columnOf(header.texts, [/^用例编号$/, /^TC编号$/]);
|
|
351
329
|
const remarkCol = columnOf(header.texts, [/^备注$/]);
|
|
352
|
-
const functionCol = columnOf(header.texts, [/测试功能点|用例名称|测试点/]);
|
|
353
|
-
const priorityCol = columnOf(header.texts, [/^优先级$/]);
|
|
354
330
|
const rowCache = new Map();
|
|
355
331
|
const maxRow = worksheet.rowCount;
|
|
356
332
|
for (let rowNumber = header.rowNumber + 1; rowNumber <= maxRow; rowNumber += 1) {
|
|
357
333
|
const seq = seqCol ? String(worksheet.getCell(rowNumber, seqCol).value ?? '').trim() : '';
|
|
358
334
|
const tc = tcCol ? String(worksheet.getCell(rowNumber, tcCol).value ?? '').trim() : '';
|
|
359
335
|
const remark = remarkCol ? String(worksheet.getCell(rowNumber, remarkCol).value ?? '').trim() : '';
|
|
360
|
-
for (const caseItem of
|
|
336
|
+
for (const caseItem of sheetCases) {
|
|
361
337
|
const id = String(caseItem.tcId);
|
|
362
338
|
if (!resultForCase(resultsByCase, caseItem)) continue;
|
|
363
339
|
if ((tc && tc === id) || (remark && remark.includes(id)) || (seq && seq === String(caseItem.seq))) {
|
|
@@ -365,29 +341,123 @@ export async function applyXlsxResults({
|
|
|
365
341
|
}
|
|
366
342
|
}
|
|
367
343
|
}
|
|
368
|
-
for (const caseItem of
|
|
369
|
-
|
|
344
|
+
for (const caseItem of sheetCases) {
|
|
345
|
+
const origin = xlsxOriginOf(caseItem);
|
|
346
|
+
const rowNumber = origin?.rowNumber || caseItem.source?.rowNumber;
|
|
347
|
+
if (rowNumber && !rowCache.has(String(caseItem.tcId))) rowCache.set(String(caseItem.tcId), rowNumber);
|
|
370
348
|
}
|
|
371
349
|
|
|
372
350
|
const evidenceByName = new Map();
|
|
351
|
+
const writtenIds = [];
|
|
373
352
|
let changed = 0;
|
|
374
|
-
for (const caseItem of
|
|
353
|
+
for (const caseItem of sheetCases) {
|
|
375
354
|
const id = String(caseItem.tcId);
|
|
376
355
|
const result = resultForCase(resultsByCase, caseItem);
|
|
377
356
|
if (!result) continue;
|
|
378
357
|
const rowNumber = rowCache.get(id);
|
|
379
358
|
if (!rowNumber) continue;
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
359
|
+
if (!dryRun) {
|
|
360
|
+
const evidence = evidenceListFor(config, result, { maxKey: maxShotsPerCase, maxError: maxShotsPerCase });
|
|
361
|
+
evidenceByName.set(id, evidence);
|
|
362
|
+
const statusCell = setCell(worksheet, rowNumber, resultCol, result.status || '');
|
|
363
|
+
styleStatus(statusCell, result.status);
|
|
364
|
+
setCell(worksheet, rowNumber, timeCol, result.at ? formatDateTime(new Date(result.at)) : '');
|
|
365
|
+
setCell(worksheet, rowNumber, noteCol, truncate(result.note || '', 800));
|
|
366
|
+
setCell(worksheet, rowNumber, batchCol, result.batchId || '');
|
|
367
|
+
setCell(worksheet, rowNumber, keyShotCol, evidence.key.map((item) => path.basename(item.absolute)).join('; '));
|
|
368
|
+
setCell(worksheet, rowNumber, errorShotCol, evidence.error.map((item) => path.basename(item.absolute)).join('; '));
|
|
369
|
+
}
|
|
370
|
+
writtenIds.push(id);
|
|
389
371
|
changed += 1;
|
|
390
372
|
}
|
|
373
|
+
return {
|
|
374
|
+
changed,
|
|
375
|
+
writtenIds,
|
|
376
|
+
evidenceByName,
|
|
377
|
+
sheet: worksheet.name,
|
|
378
|
+
headers: { resultCol, timeCol, noteCol, batchCol, keyShotCol, errorShotCol },
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 生成回填后的 xlsx(默认写到源文件旁的副本,不覆盖原文档)。
|
|
384
|
+
* sourceFile/outputPath 让导入文档从自己的源工作簿生成独立执行副本;cases 按 source.sheet 分表写回。
|
|
385
|
+
*/
|
|
386
|
+
export async function applyXlsxResults({
|
|
387
|
+
config,
|
|
388
|
+
cases = [],
|
|
389
|
+
resultsByCase,
|
|
390
|
+
sourceFile = '',
|
|
391
|
+
date = formatDate(new Date()),
|
|
392
|
+
outputPath = '',
|
|
393
|
+
dryRun = false,
|
|
394
|
+
embedShots = true,
|
|
395
|
+
maxShotsPerCase = 4,
|
|
396
|
+
} = {}) {
|
|
397
|
+
const source = sourceFile
|
|
398
|
+
? (path.isAbsolute(sourceFile) ? sourceFile : path.resolve(config.projectDir, String(sourceFile)))
|
|
399
|
+
: config.casesFile;
|
|
400
|
+
const xlsxCases = [];
|
|
401
|
+
for (const item of cases) {
|
|
402
|
+
const origin = xlsxOriginOf(item);
|
|
403
|
+
if (!origin) continue;
|
|
404
|
+
if (path.resolve(origin.file) !== path.resolve(source)) continue;
|
|
405
|
+
xlsxCases.push(item);
|
|
406
|
+
}
|
|
407
|
+
if (!xlsxCases.length) return { path: null, written: false, reason: '没有来自 xlsx 的用例' };
|
|
408
|
+
const target = outputXlsxPath({ config, date, outputPath, sourceFile: source });
|
|
409
|
+
|
|
410
|
+
const mod = await import('exceljs');
|
|
411
|
+
const ExcelJS = mod.default || mod;
|
|
412
|
+
const workbook = new ExcelJS.Workbook();
|
|
413
|
+
const prepared = await prepareWorkbookSource(source);
|
|
414
|
+
try {
|
|
415
|
+
await workbook.xlsx.readFile(prepared.file);
|
|
416
|
+
} finally {
|
|
417
|
+
prepared.cleanup();
|
|
418
|
+
}
|
|
419
|
+
const fallbackSheet = () => (config.cases?.sheet && workbook.getWorksheet(config.cases.sheet))
|
|
420
|
+
|| workbook.worksheets.find((item) => item.name.includes('履约'))
|
|
421
|
+
|| workbook.worksheets[0];
|
|
422
|
+
const bySheet = new Map();
|
|
423
|
+
for (const caseItem of xlsxCases) {
|
|
424
|
+
const sheetName = String(xlsxOriginOf(caseItem)?.sheet || '');
|
|
425
|
+
if (!bySheet.has(sheetName)) bySheet.set(sheetName, []);
|
|
426
|
+
bySheet.get(sheetName).push(caseItem);
|
|
427
|
+
}
|
|
428
|
+
const sheets = [];
|
|
429
|
+
const written = new Set();
|
|
430
|
+
const evidenceByName = new Map();
|
|
431
|
+
let changed = 0;
|
|
432
|
+
let firstHeaders = null;
|
|
433
|
+
for (const [sheetName, sheetCases] of bySheet) {
|
|
434
|
+
const worksheet = sheetName ? workbook.getWorksheet(sheetName) : fallbackSheet();
|
|
435
|
+
if (!worksheet) continue;
|
|
436
|
+
const applied = applySheetCases(config, worksheet, sheetCases, resultsByCase, { maxShotsPerCase, dryRun });
|
|
437
|
+
if (!applied.headers) continue;
|
|
438
|
+
firstHeaders = firstHeaders || applied.headers;
|
|
439
|
+
changed += applied.changed;
|
|
440
|
+
for (const id of applied.writtenIds) written.add(String(id));
|
|
441
|
+
for (const [id, evidence] of applied.evidenceByName) evidenceByName.set(id, evidence);
|
|
442
|
+
sheets.push({ sheet: applied.sheet, cases: applied.changed });
|
|
443
|
+
}
|
|
444
|
+
// 预览也按真实工作表定位行:找不到工作表/表头的结果不能算进可回写条数。
|
|
445
|
+
if (dryRun) {
|
|
446
|
+
return {
|
|
447
|
+
path: target,
|
|
448
|
+
written: false,
|
|
449
|
+
dryRun: true,
|
|
450
|
+
cases: written.size,
|
|
451
|
+
writtenIds: [...written],
|
|
452
|
+
shots: 0,
|
|
453
|
+
sheet: sheets[0]?.sheet,
|
|
454
|
+
sheets,
|
|
455
|
+
sourceFile: source,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
if (!sheets.length) throw new ConfigError(`xlsx 中没有可回填的工作表:${source}`);
|
|
459
|
+
// 摘要和截图只列真正写进用例表的行;找不到工作表或表头的用例不能出现在执行副本里。
|
|
460
|
+
const summaryCases = xlsxCases.filter((item) => written.has(String(item.tcId)));
|
|
391
461
|
|
|
392
462
|
// 摘要表
|
|
393
463
|
const oldSummary = workbook.getWorksheet('执行摘要');
|
|
@@ -407,7 +477,7 @@ export async function applyXlsxResults({
|
|
|
407
477
|
];
|
|
408
478
|
summary.getRow(1).font = { bold: true };
|
|
409
479
|
summary.getRow(1).fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFE7E6E6' } };
|
|
410
|
-
const summaryRows =
|
|
480
|
+
const summaryRows = summaryCases
|
|
411
481
|
.map((caseItem) => ({ caseItem, result: resultForCase(resultsByCase, caseItem) }))
|
|
412
482
|
.filter((item) => item.result)
|
|
413
483
|
.map(({ caseItem, result }) => {
|
|
@@ -438,7 +508,7 @@ export async function applyXlsxResults({
|
|
|
438
508
|
{ width: 70 },
|
|
439
509
|
];
|
|
440
510
|
let cursor = 1;
|
|
441
|
-
for (const caseItem of
|
|
511
|
+
for (const caseItem of summaryCases) {
|
|
442
512
|
const id = String(caseItem.tcId);
|
|
443
513
|
const result = resultForCase(resultsByCase, caseItem);
|
|
444
514
|
if (!result) continue;
|
|
@@ -477,9 +547,12 @@ export async function applyXlsxResults({
|
|
|
477
547
|
path: target,
|
|
478
548
|
written: true,
|
|
479
549
|
cases: changed,
|
|
550
|
+
writtenIds: [...written],
|
|
480
551
|
shots: shotsAdded,
|
|
481
|
-
sheet:
|
|
482
|
-
|
|
552
|
+
sheet: sheets[0].sheet,
|
|
553
|
+
sheets,
|
|
554
|
+
sourceFile: source,
|
|
555
|
+
headers: firstHeaders,
|
|
483
556
|
};
|
|
484
557
|
}
|
|
485
558
|
|
package/lib/browser-ab.mjs
CHANGED
|
@@ -181,7 +181,7 @@ export async function launchSession(config, { headless, logger, storageState = t
|
|
|
181
181
|
if (profileDir) ensureDir(profileDir);
|
|
182
182
|
const { needsSeed, useState } = profileSeedPlan({ storageState, stateFile, profileDir });
|
|
183
183
|
const name = sessionName || `uitest-${timeStamp()}-${Math.random().toString(36).slice(2, 6)}`;
|
|
184
|
-
const abFactory = () => new Ab(name, { state: useState ? stateFile : null, headed: !headless, profile: profileDir || null });
|
|
184
|
+
const abFactory = () => new Ab(name, { state: useState ? stateFile : null, headed: !headless, profile: profileDir || null, viewport: config.browser?.viewport });
|
|
185
185
|
await prepareProfile(profileDir, name, logger);
|
|
186
186
|
const base = config.baseUrl ? String(config.baseUrl).replace(/\/+$/, '') + '/' : 'about:blank';
|
|
187
187
|
const ab = await startAb({ ab: abFactory(), abFactory, name, profileDir, logger, url: base });
|
|
@@ -211,7 +211,7 @@ export async function attachSession(config, { session = 'uitest-do', headless, l
|
|
|
211
211
|
const profileDir = profileOf(config, profile);
|
|
212
212
|
if (profileDir) ensureDir(profileDir);
|
|
213
213
|
const { needsSeed, useState } = profileSeedPlan({ storageState, stateFile, profileDir });
|
|
214
|
-
const abFactory = () => new Ab(session, { state: useState ? stateFile : null, headed: !headless, profile: profileDir || null });
|
|
214
|
+
const abFactory = () => new Ab(session, { state: useState ? stateFile : null, headed: !headless, profile: profileDir || null, viewport: config.browser?.viewport });
|
|
215
215
|
let ab = abFactory();
|
|
216
216
|
let url = null;
|
|
217
217
|
try { url = await ab.url(); } catch { url = null; }
|
|
@@ -224,6 +224,7 @@ export async function attachSession(config, { session = 'uitest-do', headless, l
|
|
|
224
224
|
} else {
|
|
225
225
|
// Ab 构造时默认 closed=true;url() 能返回有效地址才说明复用的会话还活着,页面观察靠这个标记判断是否继续。
|
|
226
226
|
ab.closed = false;
|
|
227
|
+
await ab.applyViewport();
|
|
227
228
|
}
|
|
228
229
|
const s = { ab, config, pageErrors: [], _url: url, sessionName: session };
|
|
229
230
|
s.page = pageShim(s);
|
package/lib/browser.mjs
CHANGED
|
@@ -57,7 +57,7 @@ export async function launchSession(config, { headless, logger, storageState = t
|
|
|
57
57
|
const statePath = config.paths.stateFile;
|
|
58
58
|
const stateUsed = Boolean(storageState && statePath && fs.existsSync(statePath));
|
|
59
59
|
const contextOptions = {
|
|
60
|
-
viewport: config.browser.viewport || { width:
|
|
60
|
+
viewport: config.browser.viewport || { width: 1920, height: 1280 },
|
|
61
61
|
locale: config.browser.locale || 'zh-CN',
|
|
62
62
|
timezoneId: config.browser.timezoneId || 'Asia/Shanghai',
|
|
63
63
|
ignoreHTTPSErrors: true,
|