Skip to content

Commit 36c7935

Browse files
authored
fix:增加解析运行日志附件, (#19)
* chore(deps): 更新 Playwright 版本至 0.2.5 修改执行命令并解析用例生成的 JSON 文件逻辑 优化创建临时目录并移动到指定目录下 * chore: 优化创建临时目录并移动 JSON 文件 * chore(utils): 优化parseJsonFile函数,将解析后的JSON文件移动到指定目录 * chore(code): 移除executor.ts中未使用的process模块导入 * refactor(utils): 优化generateCommands函数,添加json文件名参数并修改生成命令逻辑 * test(utils): 添加json文件名参数并修改generateCommands函数的测试用例 * 直接生成到附件目录内 * format
1 parent 5058c62 commit 36c7935

5 files changed

Lines changed: 29 additions & 25 deletions

File tree

‎playwright/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "testsolar-oss-playwright",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "TestSolar support for playwright tool",
55
"main": "main.js",
66
"scripts": {

‎playwright/src/playwrightx/executor.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
2-
import * as process from "process";
31
import * as fs from "fs";
2+
import * as path from "path";
43

54
import Reporter from "testsolar-oss-sdk/src/testsolar_sdk/reporter";
65
import log from 'testsolar-oss-sdk/src/testsolar_sdk/logger';
@@ -24,20 +23,25 @@ export async function runTestCase(runParamFile: string): Promise<void> {
2423
// 按照文件对用例进行分组
2524
const caseLists = groupTestCasesByPath(testSelectors);
2625

26+
// 创建附件目录
27+
const attachmentsPath = path.join(projPath, "attachments");
28+
if (!fs.existsSync(attachmentsPath)) {
29+
fs.mkdirSync(attachmentsPath, { recursive: true });
30+
}
31+
2732
// 对每个文件生成命令行
28-
for (const [path, testcases] of Object.entries(caseLists)) {
29-
const { command, testIdentifiers } = generateCommands(path, testcases);
30-
// 执行命令,解析用例生成的 JSON 文件,上报结果
31-
32-
const jsonName = path.replace(/\//g, "_") + ".json";
33-
process.env.PLAYWRIGHT_JSON_OUTPUT_NAME = jsonName;
34-
33+
for (const [casePath, testcases] of Object.entries(caseLists)) {
34+
// 执行命令并解析用例生成的 JSON 文件
35+
const jsonName = casePath.replace(/\//g, "_") + ".json";
36+
const caseJsonFile = path.join(attachmentsPath, jsonName);
37+
const { command, testIdentifiers } = generateCommands(casePath, testcases, caseJsonFile);
3538
const testResults = await executeCommands(
3639
projPath,
3740
command,
3841
testIdentifiers,
42+
jsonName, // 将jsonName传递给executeCommands函数
3943
);
40-
// log.info("Parse json results:\n", testResults);
44+
4145
const results = createTestResults(testResults);
4246
const reporter = new Reporter(taskId, data.FileReportPath);
4347
for (const result of results) {

‎playwright/src/playwrightx/parser.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as process from "process";
22
import * as fs from "fs";
33
import * as path from "path";
44
import {
5-
createTempDirectory,
65
executeCommand,
76
parseTestcase,
87
filterTestcases,
@@ -31,12 +30,15 @@ export async function collectTestCases(
3130
process.chdir(projPath);
3231
log.info(`Current directory: ${process.cwd()}`);
3332

33+
const newDirPath = path.join(projPath, "attachments");
34+
if (!fs.existsSync(newDirPath)) {
35+
fs.mkdirSync(newDirPath, { recursive: true });
36+
}
37+
const filePath = path.join(newDirPath, "load.json");
3438

35-
const tempDirectory = createTempDirectory();
36-
const filePath = path.join(tempDirectory, "testSolarOutput.json");
3739

3840
// 执行命令获取output.json文件内容
39-
const command = `npx playwright test --list --reporter=json | tee ${filePath}`;
41+
const command = `npx playwright test --list --reporter=json > ${filePath}`;
4042
log.info("Run Command: ", command);
4143
const { stdout, stderr } = await executeCommand(command);
4244
log.info("stdout:", stdout);

‎playwright/src/playwrightx/utils.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ export const parseTestcase = (
269269

270270
/// 生成运行测试用例的命令
271271
export function generateCommands(
272-
path: string,
272+
casePath: string,
273273
testCases: string[],
274+
jsonName: string,
274275
): { command: string; testIdentifiers: string[] } {
275276
const testIdentifiers: string[] = [];
276277

@@ -279,7 +280,7 @@ export function generateCommands(
279280

280281
// 检查 testCases 是否为空
281282
if (testCases.length === 0) {
282-
const defaultCommand = `npx playwright test --reporter=json ${extraArgs}`;
283+
const defaultCommand = `npx playwright test --reporter=json ${extraArgs} > ${jsonName}`;
283284
log.info(`Generated default command for test cases: ${defaultCommand}`);
284285
return { command: defaultCommand, testIdentifiers: [] };
285286
}
@@ -288,10 +289,10 @@ export function generateCommands(
288289
if (grepPattern) {
289290
grepPattern = `--grep="${grepPattern}"`;
290291
}
291-
const command = `npx playwright test ${path} ${grepPattern} --reporter=json ${extraArgs}`;
292+
const command = `npx playwright test ${casePath} ${grepPattern} --reporter=json ${extraArgs} > ${jsonName}`;
292293

293294
for (const testcase of testCases) {
294-
testIdentifiers.push(`${path}?${testcase}`);
295+
testIdentifiers.push(`${casePath}?${testcase}`);
295296
}
296297

297298
log.info(`Generated command for test cases: ${command}`);
@@ -416,9 +417,6 @@ export function parseJsonFile(
416417
`function parseJsonFile: ${process.env.PLAYWRIGHT_JSON_OUTPUT_NAME}`,
417418
);
418419
const data = JSON.parse(fs.readFileSync(jsonFile, "utf-8"));
419-
log.info("--------json data:---------");
420-
log.info(JSON.stringify(data, null, 2));
421-
log.info("---------------------------");
422420
const result = parseJsonContent(projPath, data);
423421

424422
log.info(`Parse result from json: ${JSON.stringify(result, null, 2)}`);
@@ -453,6 +451,7 @@ export async function executeCommands(
453451
projPath: string,
454452
command: string,
455453
cases: string[],
454+
jsonFile: string, // 接收jsonFile作为参数
456455
): Promise<Record<string, SpecResult[]>> {
457456
const results: Record<string, SpecResult[]> = {};
458457

@@ -461,7 +460,6 @@ export async function executeCommands(
461460
`Run cmdline: ${command} \n Run stdout: ${stdout}\nRun stderr: ${stderr}`,
462461
);
463462
// 解析 JSON 文件并处理结果
464-
const jsonFile = process.env.PLAYWRIGHT_JSON_OUTPUT_NAME || "result.json";
465463
const testResults = parseJsonFile(projPath, jsonFile, cases);
466464
Object.assign(results, testResults);
467465
return testResults;

‎playwright/tests/utils.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ describe("generateCommands", () => {
186186
test("should generate test execution commands", () => {
187187
const path = "path/to/tests";
188188
const testCases = ["test1", "test2"];
189-
const { command } = generateCommands(path, testCases);
189+
const { command } = generateCommands(path, testCases, "1.json");
190190
expect(command).toContain("npx playwright test");
191191
});
192192

193193
test("should generate zero test execution commands", () => {
194194
const path = "path/to/tests";
195195
const testCases: string[] = [];
196-
const { command } = generateCommands(path, testCases);
196+
const { command } = generateCommands(path, testCases, "1.json");
197197
expect(command).toContain("npx playwright test");
198198
});
199199
});

0 commit comments

Comments
 (0)