From f2a082e2eeaf9445138e04921a4fe9acc1bfc3b0 Mon Sep 17 00:00:00 2001 From: "pavel.butuzov" Date: Wed, 6 Nov 2024 11:54:18 +0300 Subject: [PATCH] Added changes by code review. --- testit-adapter-playwright/src/converter.ts | 42 +++++++--------------- testit-adapter-playwright/src/reporter.ts | 15 +++++--- testit-adapter-playwright/src/utils.ts | 33 +++++++++++++++++ 3 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 testit-adapter-playwright/src/utils.ts diff --git a/testit-adapter-playwright/src/converter.ts b/testit-adapter-playwright/src/converter.ts index eaa207b..c35686d 100644 --- a/testit-adapter-playwright/src/converter.ts +++ b/testit-adapter-playwright/src/converter.ts @@ -13,6 +13,7 @@ import { Attachment } from "testit-js-commons"; import { MetadataMessage } from "./labels"; +import { getStatusDetails, isAllStepsWithPassedOutcome } from "./utils"; export enum Status { @@ -21,6 +22,11 @@ export enum Status { SKIPPED = "Skipped", } +export type StatusDetails = { + message?: string; + trace?: string; +}; + export class Converter { static convertTestCaseToAutotestPost(autotestData: MetadataMessage): AutotestPost { return { @@ -64,7 +70,9 @@ export class Converter { } static convertTestStepsToShortSteps(steps: TestStep[]): ShortStep[] { - return steps.filter((step: TestStep) => step.category === "test.step").map(step => this.convertTestStepToShortStep(step)); + return steps + .filter((step: TestStep) => step.category === "test.step") + .map(step => this.convertTestStepToShortStep(step)); } static convertTestStepToShortStep(step: TestStep): ShortStep { @@ -75,7 +83,9 @@ export class Converter { } static convertTestStepsToSteps(steps: TestStep[], attachmentsMap: Map): Step[] { - return steps.filter((step: TestStep) => step.category === "test.step").map(step => this.convertTestStepToStep(step, attachmentsMap)); + return steps + .filter((step: TestStep) => step.category === "test.step") + .map(step => this.convertTestStepToStep(step, attachmentsMap)); } static convertTestStepToStep(step: TestStep, attachmentsMap: Map): Step { @@ -83,7 +93,7 @@ export class Converter { return { title: step.title, - outcome: step.error || steps.find((step: Step) => step.outcome === Status.FAILED) ? Status.FAILED : Status.PASSED, + outcome: step.error || !isAllStepsWithPassedOutcome(steps) ? Status.FAILED : Status.PASSED, steps: steps, attachments: [...attachmentsMap.keys()].filter((attachmentId: Attachment) => attachmentsMap.get(attachmentId) === step), }; @@ -99,29 +109,3 @@ export class Converter { return Status.FAILED; }; } - -export type StatusDetails = { - message?: string; - trace?: string; -}; - -const getStatusDetails = (error: TestError): StatusDetails => { - const message = error.message && stripAscii(error.message); - let trace = error.stack && stripAscii(error.stack); - if (trace && message && trace.startsWith(`Error: ${message}`)) { - trace = trace.substr(message.length + "Error: ".length); - } - return { - message: message, - trace: trace, - }; -}; - -export const stripAscii = (str: string): string => { - return str.replace(asciiRegex, ""); -}; - -const asciiRegex = new RegExp( - "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", - "g", -); diff --git a/testit-adapter-playwright/src/reporter.ts b/testit-adapter-playwright/src/reporter.ts index 8b1b3e9..4f3f60f 100644 --- a/testit-adapter-playwright/src/reporter.ts +++ b/testit-adapter-playwright/src/reporter.ts @@ -9,6 +9,7 @@ import { import { ConfigComposer, Client, StrategyFactory, IStrategy, Utils, Additions, Attachment, Step } from "testit-js-commons"; import { Converter, Status } from "./converter"; import { MetadataMessage } from "./labels"; +import { isAllStepsWithPassedOutcome } from "./utils"; const stepAttachRegexp = /^stepattach_(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})_/i; @@ -62,7 +63,7 @@ class TmsReporter implements Reporter { if (step.category !== "test.step") { return; } - if (step.parent !== undefined) { + if (step.parent) { return; } if (this.stepCache.get(step)) { @@ -115,10 +116,14 @@ class TmsReporter implements Reporter { for (const attachment of result.attachments) { if (!attachment.body) { if (attachment.path && attachment.name !== "screenshot") { - await this.additions.addAttachments(Utils.readBuffer(attachment.path), attachment.name).then((ids) => { - autotestData.addAttachments?.push(...ids); - }); + const content = Utils.readBuffer(attachment.path); + + await this.additions.addAttachments(content, attachment.name) + .then((ids) => { + autotestData.addAttachments?.push(...ids); + }); } + continue; } @@ -202,7 +207,7 @@ class TmsReporter implements Reporter { const steps = [...this.stepCache.keys()].filter((step: TestStep) => this.stepCache.get(step) === test); const stepResults = Converter.convertTestStepsToSteps(steps, this.attachmentSteps); - if (stepResults.find((step: Step) => step.outcome === Status.FAILED)) { + if (!isAllStepsWithPassedOutcome(stepResults)) { result.status = "failed"; } diff --git a/testit-adapter-playwright/src/utils.ts b/testit-adapter-playwright/src/utils.ts new file mode 100644 index 0000000..95c7d15 --- /dev/null +++ b/testit-adapter-playwright/src/utils.ts @@ -0,0 +1,33 @@ +import { TestError } from "@playwright/test/reporter"; +import { Step } from "testit-js-commons"; +import { Status } from "./converter"; + +export type StatusDetails = { + message?: string; + trace?: string; +}; + +export function getStatusDetails (error: TestError): StatusDetails { + const message = error.message && stripAscii(error.message); + let trace = error.stack && stripAscii(error.stack); + if (trace && message && trace.startsWith(`Error: ${message}`)) { + trace = trace.substr(message.length + "Error: ".length); + } + return { + message: message, + trace: trace, + }; +}; + +export function isAllStepsWithPassedOutcome(steps: Step[]): boolean { + return !steps.find((step: Step) => step.outcome === Status.FAILED); +} + +export function stripAscii (str: string): string { + return str.replace(asciiRegex, ""); +}; + +const asciiRegex = new RegExp( + "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", + "g", +);