Skip to content

Commit 4251efe

Browse files
authored
Merge pull request #37 from OpenTestSolar/dev/support_symbol
feat: fix attachments
2 parents a389b4e + c7fa91b commit 4251efe

4 files changed

Lines changed: 25 additions & 20 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.18",
3+
"version": "0.2.21",
44
"description": "TestSolar support for playwright tool",
55
"main": "main.js",
66
"scripts": {

‎playwright/src/playwrightx/executor.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export async function runTestCase(runParamFile: string): Promise<void> {
2222
let projPath = data.ProjectPath;
2323
const taskId = data.TaskId;
2424

25+
// 创建附件目录
26+
const attachmentsPath = path.join(projPath, "attachments");
27+
if (!fs.existsSync(attachmentsPath)) {
28+
fs.mkdirSync(attachmentsPath, { recursive: true });
29+
}
30+
2531
const relPath = process.env.TESTSOLAR_TTP_RELPATH || "";
2632
if (relPath !== "") {
2733
log.info(`Relative path: ${relPath}`);
@@ -33,12 +39,7 @@ export async function runTestCase(runParamFile: string): Promise<void> {
3339

3440
const testcasePrefix = getTestcasePrefix();
3541
const reporter = new Reporter(taskId, data.FileReportPath);
36-
37-
// 创建附件目录
38-
const attachmentsPath = path.join(projPath, "attachments");
39-
if (!fs.existsSync(attachmentsPath)) {
40-
fs.mkdirSync(attachmentsPath, { recursive: true });
41-
}
42+
4243

4344
// 检查是否运行所有测试用例
4445
const runAllCases = process.env.TESTSOLAR_TTP_RUN_ALL_CASES;
@@ -66,7 +67,8 @@ export async function runTestCase(runParamFile: string): Promise<void> {
6667
projPath,
6768
command,
6869
testIdentifiers.length > 0 ? testIdentifiers : testSelectors,
69-
jsonName
70+
jsonName,
71+
attachmentsPath,
7072
);
7173

7274
// 创建并上报测试结果
@@ -108,6 +110,7 @@ export async function runTestCase(runParamFile: string): Promise<void> {
108110
command,
109111
testIdentifiers,
110112
jsonName,
113+
attachmentsPath,
111114
);
112115

113116
const results = createTestResults(testResults, testIdentifiers);
@@ -117,6 +120,3 @@ export async function runTestCase(runParamFile: string): Promise<void> {
117120
}
118121
}
119122
}
120-
121-
122-
runTestCase(process.argv[2])

‎playwright/src/playwrightx/utils.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as util from "util";
44
import * as fs from "fs";
55
import * as os from "os";
66
import * as path from "path";
7+
import { createHash } from 'crypto';
78
import { parseISO, addMilliseconds } from "date-fns";
89
import { zonedTimeToUtc } from "date-fns-tz";
910
import { TestCase } from "testsolar-oss-sdk/src/testsolar_sdk/model/test";
@@ -358,8 +359,13 @@ export function generateCommands(
358359

359360
// 检查是否应该启用 trace
360361
const enableTrace = process.env.TESTSOLAR_TTP_TRACE === "1";
361-
const traceOption = enableTrace ? "--trace on" : "";
362+
const traceOption = enableTrace ? "--trace on" : "--trace off";
362363

364+
// 创建基于测试路径和测试用例的哈希值
365+
const input = `${casePath}-${testCases.join('-')}-${Date.now()}-${Math.random()}`;
366+
const hash = createHash('md5').update(input).digest('hex').substring(0, 10);
367+
const outputOption = `--output=test-results-${hash}`;
368+
363369
// 获取 grep 模式
364370
let grepPattern = "";
365371
if (testCases.length > 0) {
@@ -371,16 +377,16 @@ export function generateCommands(
371377
if (useEnvJsonFile) {
372378
// 使用环境变量设置 JSON 输出
373379
if (testCases.length === 0) {
374-
command = `export PLAYWRIGHT_JSON_OUTPUT_NAME=${jsonName} && npx playwright test --reporter=json ${traceOption} ${extraArgs}`;
380+
command = `export PLAYWRIGHT_JSON_OUTPUT_NAME=${jsonName} && npx playwright test --reporter=json ${traceOption} ${outputOption} ${extraArgs}`;
375381
} else {
376-
command = `export PLAYWRIGHT_JSON_OUTPUT_NAME=${jsonName} && npx playwright test ${casePath} ${grepPattern} --reporter=json ${traceOption} ${extraArgs}`;
382+
command = `export PLAYWRIGHT_JSON_OUTPUT_NAME=${jsonName} && npx playwright test ${casePath} ${grepPattern} --reporter=json ${traceOption} ${outputOption} ${extraArgs}`;
377383
}
378384
} else {
379385
// 使用原始的重定向方式
380386
if (testCases.length === 0) {
381-
command = `npx playwright test --reporter=json ${traceOption} ${extraArgs} > ${jsonName}`;
387+
command = `npx playwright test --reporter=json ${traceOption} ${outputOption} ${extraArgs} > ${jsonName}`;
382388
} else {
383-
command = `npx playwright test ${casePath} ${grepPattern} --reporter=json ${traceOption} ${extraArgs} > ${jsonName}`;
389+
command = `npx playwright test ${casePath} ${grepPattern} --reporter=json ${traceOption} ${outputOption} ${extraArgs} > ${jsonName}`;
384390
}
385391
}
386392

@@ -629,6 +635,7 @@ export async function executeCommands(
629635
command: string,
630636
cases: string[],
631637
jsonFile: string, // 接收jsonFile作为参数
638+
attachmentsPath: string,
632639
): Promise<Record<string, SpecResult[]>> {
633640
const results: Record<string, SpecResult[]> = {};
634641

@@ -637,15 +644,13 @@ export async function executeCommands(
637644
`Run cmdline: ${command} \n Run stdout: ${stdout}\nRun stderr: ${stderr}`,
638645
);
639646

640-
// 复制文件到附件目录,加载用例已创建该目录,此处不做检查
641-
const attachmentPath = path.join(projPath, 'attachments'); // 附件目录
642647

643648
// 检查 JSON 文件是否存在
644649
if (!fs.existsSync(jsonFile)) {
645650
console.error(`用例json文件不存在: ${jsonFile}`);
646651
} else {
647652
// 定义目标文件路径
648-
const targetFilePath = path.join(attachmentPath, path.basename(jsonFile));
653+
const targetFilePath = path.join(attachmentsPath, path.basename(jsonFile));
649654

650655
// 复制文件
651656
fs.copyFileSync(jsonFile, targetFilePath);

‎playwright/testtool.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schemaVersion: 1.0
22
name: playwright
33
nameZh: Playwright自动化测试
44
lang: javascript
5-
version: '0.2.18'
5+
version: '0.2.21'
66
defaultBaseImage: node:18
77
langType: INTERPRETED
88
description: |-

0 commit comments

Comments
 (0)