Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impr(playwright): new fullname format and improved test plan support #971

Merged
merged 3 commits into from
May 27, 2024
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
10 changes: 6 additions & 4 deletions packages/allure-js-commons/src/sdk/TestPlan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface TestPlanV1Test {
id: string | number;
selector: string;
}

export interface TestPlanV1 {
version: "1.0";
tests: {
id: string | number;
selector: string;
}[];
tests: TestPlanV1Test[];
}
4 changes: 2 additions & 2 deletions packages/allure-js-commons/src/sdk/node/TestPlan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from "node:fs";
import { TestPlanV1 } from "../TestPlan.js";
import { TestPlanV1, TestPlanV1Test } from "../TestPlan.js";

export { TestPlanV1 };
export { TestPlanV1, TestPlanV1Test };

export const parseTestPlan = (): TestPlanV1 | undefined => {
const testPlanPath = process.env.ALLURE_TESTPLAN_PATH;
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-js-commons/src/sdk/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export * from "../context/index.js";
export { AllureNodeReporterRuntime } from "./ReporterRuntime.js";
export { MessageAllureWriter, FileSystemAllureWriter, AllureInMemoryWriter } from "./writers/index.js";
export { AllureNodeCrypto } from "./Crypto.js";
export { parseTestPlan, TestPlanV1 } from "./TestPlan.js";
export { parseTestPlan, TestPlanV1, TestPlanV1Test } from "./TestPlan.js";
export { ALLURE_TEST_RUNTIME_KEY, getGlobalTestRuntime, setGlobalTestRuntime } from "../../TestRuntime.js";
export { readImageAsBase64 } from "../utils.js";
2 changes: 1 addition & 1 deletion packages/allure-playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lint": "eslint ./src ./test --ext .ts",
"lint:fix": "eslint ./src ./test --ext .ts --fix",
"pretest": "run compile",
"test": "vitest run"
"test": "vitest run test/spec"
},
"dependencies": {
"allure-js-commons": "workspace:*",
Expand Down
124 changes: 117 additions & 7 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { FullConfig } from "@playwright/test";
import { TestResult as PlaywrightTestResult, Reporter, Suite, TestCase, TestStep } from "@playwright/test/reporter";
import {
FullResult,
TestResult as PlaywrightTestResult,
Suite,
TestCase,
TestError,
TestStep,
} from "@playwright/test/reporter";
import { existsSync } from "node:fs";
import os from "node:os";
import path from "node:path";
Expand All @@ -16,8 +23,11 @@ import {
RuntimeMessage,
Stage,
Status,
TestPlanV1Test,
TestResult,
escapeRegExp,
extractMetadataFromString,
parseTestPlan,
readImageAsBase64,
} from "allure-js-commons/sdk/node";
import { AllurePlaywrightReporterConfig } from "./model.js";
Expand All @@ -28,7 +38,35 @@ const diffEndRegexp = /-((expected)|(diff)|(actual))\.png$/;
// 12 (allureattach) + 1 (_) + 36 (uuid v4) + 1 (_)
const stepAttachPrefixLength = 50;

export class AllureReporter implements Reporter {
interface ReporterV2 {
onConfigure(config: FullConfig): void;

onBegin(suite: Suite): void;

onTestBegin(test: TestCase, result: PlaywrightTestResult): void;

onStdOut(chunk: string | Buffer, test?: TestCase, result?: PlaywrightTestResult): void;

onStdErr(chunk: string | Buffer, test?: TestCase, result?: PlaywrightTestResult): void;

onTestEnd(test: TestCase, result: PlaywrightTestResult): void;

onEnd(result: FullResult): Promise<{ status?: FullResult["status"] } | undefined | void> | void;

onExit(): void | Promise<void>;

onError(error: TestError): void;

onStepBegin(test: TestCase, result: PlaywrightTestResult, step: TestStep): void;

onStepEnd(test: TestCase, result: PlaywrightTestResult, step: TestStep): void;

printsToStdio(): boolean;

version(): "v2";
}

export class AllureReporter implements ReporterV2 {
config!: FullConfig;
suite!: Suite;
options: AllurePlaywrightReporterConfig;
Expand All @@ -44,14 +82,76 @@ export class AllureReporter implements Reporter {
this.options = { suiteTitle: true, detail: true, ...config };
}

onBegin(config: FullConfig, suite: Suite): void {
onConfigure(config: FullConfig): void {
this.config = config;

const testPlan = parseTestPlan();

if (!testPlan) {
return;
}

// @ts-ignore
const configElement = config[Object.getOwnPropertySymbols(config)[0]];

if (!configElement) {
return;
}

const testsWithSelectors = testPlan.tests.filter((test) => test.selector);
const v1ReporterTests: TestPlanV1Test[] = [];
const v2ReporterTests: TestPlanV1Test[] = [];
const cliArgs: string[] = [];

testsWithSelectors.forEach((test) => {
if (!/#/.test(test.selector as string)) {
v2ReporterTests.push(test);
return;
}

v1ReporterTests.push(test);
});

if (v2ReporterTests.length) {
// we need to cut off column because playwright works only with line number
const v2SelectorsArgs = v2ReporterTests
.map((test) => test.selector.replace(/:\d+$/, ""))
.map((selector) => escapeRegExp(selector));

cliArgs.push(...(v2SelectorsArgs as string[]));
}

if (v1ReporterTests.length) {
const v1SelectorsArgs = v1ReporterTests
// we can filter tests only by absolute path, so we need to cut off test name
.map((test) => test.selector.split("#")[0])
.map((selector) => escapeRegExp(selector));

cliArgs.push(...(v1SelectorsArgs as string[]));
}

if (!cliArgs.length) {
return;
}

configElement.cliArgs = cliArgs.map((selector) => `/${selector}`);
}

onError(): void {}

onExit(): void {}

onStdErr(): void {}

onStdOut(): void {}

onBegin(suite: Suite): void {
const writer = this.options.testMode
? new MessageAllureWriter()
: new FileSystemAllureWriter({
resultsDir: this.options.resultsDir || "./allure-results",
});

this.config = config;
this.suite = suite;
this.allureRuntime = new AllureNodeReporterRuntime({
...this.options,
Expand All @@ -67,14 +167,14 @@ export class AllureReporter implements Reporter {
// root > project > file path > test.describe...
const [, , , ...suiteTitles] = suite.titlePath();
const nameSuites = suiteTitles.length > 0 ? `${suiteTitles.join(" ")} ` : "";
const fullName = `${relativeFile}#${nameSuites}${test.title}`;
const testCaseIdBase = `${relativeFile}#${nameSuites}${test.title}`;
const result: Partial<TestResult> = {
name: titleMetadata.cleanTitle,
labels: titleMetadata.labels,
links: [],
parameters: [],
testCaseId: this.allureRuntime!.crypto.md5(fullName),
fullName,
testCaseId: this.allureRuntime!.crypto.md5(testCaseIdBase),
fullName: `${relativeFile}:${test.location.line}:${test.location.column}`,
};

result.labels!.push({ name: LabelName.LANGUAGE, value: "JavaScript" });
Expand Down Expand Up @@ -352,10 +452,20 @@ export class AllureReporter implements Reporter {

this.processedDiffs.push(pathWithoutEnd);
}

version(): "v2" {
return "v2";
}
}

/**
* @deprecated for removal, import functions directly from "allure-js-commons".
*/
export * from "allure-js-commons";

/**
* @deprecated for removal, import functions directly from "@playwright/test".
*/
export { test, expect } from "@playwright/test";

export default AllureReporter;
9 changes: 4 additions & 5 deletions packages/allure-playwright/test/spec/history.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import md5 from "md5";
import { expect, it } from "vitest";
import { runPlaywrightInlineTest } from "../utils";

Expand All @@ -12,15 +11,15 @@ it("historical data should be fine", async () => {
});
`,
});
const fullName = "sample.test.js#nested test";
const fullName = "sample.test.js:5:13";

expect(tests).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "test",
fullName: fullName,
testCaseId: md5(fullName),
historyId: `${md5(fullName)}:${md5("Project:project")}`,
fullName,
testCaseId: "bf3198c05e4d7aaeeffe4ca4b5244d0f",
historyId: "bf3198c05e4d7aaeeffe4ca4b5244d0f:4d32f1bb70ce8096643fc1cc311d1fe1",
}),
]),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/allure-playwright/test/spec/skip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ it("reports programmatically skipped results", async () => {
expect(tests).toEqual(
expect.arrayContaining([
expect.objectContaining({
fullName: "sample.test.js#should be skipped 1",
fullName: "sample.test.js:4:12",
status: Status.SKIPPED,
}),
expect.objectContaining({
fullName: "sample.test.js#should not be skipped",
fullName: "sample.test.js:6:11",
status: Status.PASSED,
}),
]),
Expand Down
Loading
Loading