From 3eaca1ab7c28887b8c3154a3ee40fdd1c404c222 Mon Sep 17 00:00:00 2001 From: lianjunyu Date: Mon, 2 Feb 2026 18:11:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=8D=E5=88=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playwright/package.json | 2 +- playwright/src/playwrightx/utils.ts | 54 +++++++++++++++-------------- playwright/testtool.yaml | 2 +- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/playwright/package.json b/playwright/package.json index eede5e8..e4a5aab 100644 --- a/playwright/package.json +++ b/playwright/package.json @@ -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": { diff --git a/playwright/src/playwrightx/utils.ts b/playwright/src/playwrightx/utils.ts index 9c6fbfb..8a1c983 100644 --- a/playwright/src/playwrightx/utils.ts +++ b/playwright/src/playwrightx/utils.ts @@ -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); @@ -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}`); @@ -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, + }; } } diff --git a/playwright/testtool.yaml b/playwright/testtool.yaml index ccbcf40..e184e88 100644 --- a/playwright/testtool.yaml +++ b/playwright/testtool.yaml @@ -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: |- From 15ede9da5dc692a379f3f722d4b4410c3f2ffa3c Mon Sep 17 00:00:00 2001 From: lianjunyu Date: Mon, 2 Feb 2026 19:48:19 +0800 Subject: [PATCH 2/2] fix: update test expectations for attachment processing --- playwright/tests/utils.test.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/playwright/tests/utils.test.ts b/playwright/tests/utils.test.ts index e545aca..f961b90 100644 --- a/playwright/tests/utils.test.ts +++ b/playwright/tests/utils.test.ts @@ -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", () => { // 更改为与测试函数名称一致 @@ -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), + ], }, ] }); @@ -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); });