Skip to content

Commit

Permalink
Added changes by code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel.butuzov committed Nov 6, 2024
1 parent 5e4f88f commit f2a082e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
42 changes: 13 additions & 29 deletions testit-adapter-playwright/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Attachment
} from "testit-js-commons";
import { MetadataMessage } from "./labels";
import { getStatusDetails, isAllStepsWithPassedOutcome } from "./utils";


export enum Status {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -75,15 +83,17 @@ export class Converter {
}

static convertTestStepsToSteps(steps: TestStep[], attachmentsMap: Map<Attachment, TestStep>): 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<Attachment, TestStep>): Step {
const steps = step.steps.length !== 0 ? this.convertTestStepsToSteps(step.steps, attachmentsMap) : [];

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),
};
Expand All @@ -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",
);
15 changes: 10 additions & 5 deletions testit-adapter-playwright/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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";
}

Expand Down
33 changes: 33 additions & 0 deletions testit-adapter-playwright/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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",
);

0 comments on commit f2a082e

Please sign in to comment.