Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playwright/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testsolar-oss-playwright",
"version": "0.2.26",
"version": "0.2.27",
"description": "TestSolar support for playwright tool",
"main": "main.js",
"scripts": {
Expand Down
54 changes: 28 additions & 26 deletions playwright/src/playwrightx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,22 +531,21 @@ export function parseJsonContent(
for (const error of result.errors) {
specErrorCtx += error.message + "\n";
}
}


// 处理附件
const testcaseAttachments: Attachment[] = [];
if (result.attachments && result.attachments.length > 0) {
console.log(`发现 attachments 数量: ${result.attachments.length}`);

// 直接使用系统临时目录
const targetDir = os.tmpdir();
console.log(`使用系统临时目录: ${targetDir}`);

for (const attachment of result.attachments) {
const attachmentName = attachment.name;
const attachmentPath = attachment.path;
const contentType = attachment.contentType || "";

if (["screenshot", "video", "trace"].includes(attachmentName) && attachmentPath) {
if ((["screenshot", "video", "trace"].includes(attachmentName) || contentType.startsWith("image/")) && attachmentPath) {
try {
// 获取原始文件名和扩展名
const originalName = path.basename(attachmentPath);
Expand All @@ -560,15 +559,19 @@ export function parseJsonContent(
// 构建新的文件路径
const newPath = path.join(targetDir, fileName);

// 复制文件到临时目录
fs.copyFileSync(attachmentPath, newPath);

// 使用新的文件路径创建 Attachment 对象
testcaseAttachments.push(
new Attachment(fileName, newPath, AttachmentType.FILE)
);

console.log(`成功复制文件 ${fileName} 到 ${newPath}`);
// 检查源文件是否存在
if (fs.existsSync(attachmentPath)) {
// 复制文件到临时目录
fs.copyFileSync(attachmentPath, newPath);
testcaseAttachments.push(
new Attachment(fileName, newPath, AttachmentType.FILE)
);
} else {
// 源文件不存在,使用原路径
testcaseAttachments.push(
new Attachment(originalName, attachmentPath, AttachmentType.FILE)
);
}
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error";
log.error(`复制文件 ${attachmentPath} 失败: ${message}`);
Expand All @@ -577,19 +580,18 @@ export function parseJsonContent(
}
}

specResult = {
projectID: specProjectId,
result: result.status,
duration: duration,
startTime: specStartTime,
endTime: specEndTime,
message: specErrorMsg,
content: specErrorCtx, // 现在包含错误、stdout和stderr
owner: owner,
description: description,
attachments: testcaseAttachments,
};
}
specResult = {
projectID: specProjectId,
result: result.status,
duration: duration,
startTime: specStartTime,
endTime: specEndTime,
message: specErrorMsg,
content: specErrorCtx, // 现在包含错误、stdout和stderr
owner: owner,
description: description,
attachments: testcaseAttachments,
};
}
}

Expand Down
20 changes: 18 additions & 2 deletions playwright/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import * as path from "path";
import log from 'testsolar-oss-sdk/src/testsolar_sdk/logger';
import Reporter from "testsolar-oss-sdk/src/testsolar_sdk/reporter";
import { Attachment, AttachmentType } from "testsolar-oss-sdk/src/testsolar_sdk/model/testresult";


describe("parsePlaywrightReport", () => { // 更改为与测试函数名称一致
Expand Down Expand Up @@ -404,7 +405,9 @@ describe("parseJsonContent", () => {
projectID: "proj1",
result: "passed",
startTime: 1672531200,
attachments: [],
attachments: [
new Attachment("test-failed-1.png", "/root/work/123test/js_project/test-results/test-1-test-chromium/test-failed-1.png", AttachmentType.FILE),
],
},
]
});
Expand All @@ -417,7 +420,20 @@ describe("parseJsonFile", () => {
const jsonName = "tests/results.json";
const result = parseJsonFile(projPath, jsonName, []);
const expectedResults = {
"/project/spec1.js?Suite 1 Spec 1": [],
"/project/spec1.js?Suite 1 Spec 1": [
{
projectID: "proj1",
result: "passed",
duration: 1,
startTime: 1672531200,
endTime: 1672531201,
message: "",
content: "\n==== 标准输出 ====\n增加日志展示\n进入百度页面\n点击输入框\n输入playwright\n点击百度一下\n等待弹出页面\n点击百度翻译\n",
owner: null,
description: null,
attachments: [],
},
],
};
expect(result).toEqual(expectedResults);
});
Expand Down
2 changes: 1 addition & 1 deletion playwright/testtool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ schemaVersion: 1.0
name: playwright
nameZh: Playwright自动化测试
lang: javascript
version: '0.2.26'
version: '0.2.27'
defaultBaseImage: node:18
langType: INTERPRETED
description: |-
Expand Down
Loading