Skip to content

Commit

Permalink
add labels and links to playwright via attachments (fixes #332, fixes #…
Browse files Browse the repository at this point in the history
…352, fixes #341, via #408)
  • Loading branch information
vovsemenv authored Jan 22, 2022
1 parent 5c4d33d commit 782d17a
Show file tree
Hide file tree
Showing 8 changed files with 667 additions and 36 deletions.
64 changes: 63 additions & 1 deletion packages/allure-playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project implements Allure integration with [Playwright Test](https://playwr
```bash
npm i -D @playwright/test allure-playwright
```

or via yarn:

```bash
yarn add @playwright/test allure-playwright --dev
```
Expand All @@ -18,7 +20,7 @@ Either add **allure-playwright** into **playwright.config.ts**:

```js
{
reporter: 'allure-playwright'
reporter: "allure-playwright";
}
```

Expand All @@ -31,12 +33,72 @@ npx playwright test --reporter=line,allure-playwright
Specify location for allure results:

Mac / Linux

```bash
ALLURE_RESULTS_DIR=my-allure-results npx playwright test --reporter=line,allure-playwright
```

Windows

```bash
set ALLURE_RESULTS_DIR=my-allure-results
npx playwright test --reporter=line,allure-playwright
```

## Proving extra information

You can use allure labels to provide extra information about tests such via

- label
- link
- id
- epic
- feature
- story
- suite
- parentSuite
- subSuite
- owner
- severity
- tag
- issue
- tms

### Labels Usage

```js
import { test, expect } from "@playwright/test";
import { allure } from "allure-playwright";

test("basic test", async ({ page }, testInfo) => {
allure.epic("Some Epic");
allure.story("Some Story");
});
```

### Links Usage

```js
import { test, expect } from "@playwright/test";
import { allure } from "allure-playwright";

test("basic test", async ({ page }, testInfo) => {
allure.link({ url: "https://playwright.dev", name: "playwright-site" });
allure.issue({
url: "https://github.com/allure-framework/allure-js/issues/352",
name: "Target issue",
});
});
```

### Attachments Usage

```js
import { test, expect } from "@playwright/test";

test("basic test", async ({ page }, testInfo) => {
const path = testInfo.outputPath("screenshot.png");
await page.screenshot({ path });
testInfo.attachments.push({ name: "screenshot", path, contentType: "image/png" });
});
```
2 changes: 1 addition & 1 deletion packages/allure-playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint:fix": "eslint ./src --ext .ts --fix"
},
"devDependencies": {
"@playwright/test": "^1.15.2",
"@playwright/test": "^1.18.0",
"eslint": "^7.32.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
Expand Down
118 changes: 118 additions & 0 deletions packages/allure-playwright/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import test from "@playwright/test";
import { Label, LabelName, Link, LinkType } from "allure-js-commons";


export const ALLURE_METADATA_CONTENT_TYPE = "application/vnd.allure.metadata+json";
export interface Metadata {
labels?: Label[];
links?: Link[];
}

export class allure {
static addMetadataAttachment(metadata: Metadata) {
test.info().attach("allure-metadata.json",{
contentType: ALLURE_METADATA_CONTENT_TYPE,
body: Buffer.from(JSON.stringify(metadata), "utf8"),
});
}

static label(label: Label | Label[]) {
this.addMetadataAttachment({
labels: Array.isArray(label) ? label : [label],
});
}

static link(link: Link) {
this.addMetadataAttachment({
links: Array.isArray(link) ? link : [link],
});
}

static id(id: string) {
this.label({
value: id,
name: LabelName.AS_ID,
});
}

static epic(epic: string) {
this.label({
name: LabelName.EPIC,
value: epic,
});
}

static feature(epic: string) {
this.label({
name: LabelName.FEATURE,
value: epic,
});
}

static story(story: string): void {
this.label({
name: LabelName.STORY,
value: story,
});
}

static suite(name: string): void {
this.label({
name: LabelName.SUITE,
value: name,
});
}

static parentSuite(name: string) {
this.label({
name: LabelName.PARENT_SUITE,
value: name,
});
}

static subSuite(name: string) {
this.label({
name: LabelName.SUB_SUITE,
value: name,
});
}

static owner(owner: string) {
this.label({
name: LabelName.OWNER,
value: owner,
});
}

static severity(severity: string) {
this.label({
name: LabelName.SEVERITY,
value: severity,
});
}

static tag(tag: string) {
this.label({
name: LabelName.TAG,
value: tag,
});
}

static issue(issueData: Omit<Link, "type">) {
this.link({
url: issueData.url,
name: issueData.name,
type: LinkType.ISSUE,
});
}

static tms(issueData: Omit<Link, "type">) {
this.link({
url: issueData.url,
name: issueData.name,
type: LinkType.TMS,
});
}
}

export {LabelName} from "allure-js-commons";
16 changes: 16 additions & 0 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import {
ExecutableItemWrapper,
InMemoryAllureWriter,
LabelName,
LinkType,
Status,
} from "allure-js-commons";

import { ALLURE_METADATA_CONTENT_TYPE, Metadata } from "./helpers";

class AllureReporter implements Reporter {
config!: FullConfig;
suite!: Suite;
Expand Down Expand Up @@ -93,6 +96,17 @@ class AllureReporter implements Reporter {
continue;
}

if (attachment.contentType === ALLURE_METADATA_CONTENT_TYPE) {
if (!attachment.body) {
continue;
}

const metadata: Metadata = JSON.parse(attachment.body.toString());
metadata.links?.forEach((val) => allureTest.addLink(val.url, val.name, val.type));
metadata.labels?.forEach((val) => allureTest.addLabel(val.name, val.value));
continue;
}

let fileName;
if (attachment.body) {
fileName = runtime.writeAttachment(attachment.body, attachment.contentType);
Expand Down Expand Up @@ -175,3 +189,5 @@ const appendStep = (parent: ExecutableItemWrapper, step: TestStep) => {
appendStep(allureStep, child);
}
};

export * from "./helpers";
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function runPlaywrightTest(
}
}
const outputDir = path.join(baseDir, "test-results");
const args = [require.resolve("@playwright/test/lib/cli/cli.js"), "test"];
const args = [require.resolve("@playwright/test/cli"), "test"];
args.push(
"--output=" + outputDir,
"--reporter=" + require.resolve("../dist/index.js"),
Expand Down
20 changes: 20 additions & 0 deletions packages/allure-playwright/test/label.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Label, LabelName } from "allure-js-commons";
import { test, expect } from "./fixtures";

test("should have label", async ({ runInlineTest }) => {
const result: Label[] = await runInlineTest(
{
"a.test.ts": `
import { test, expect } from '@playwright/test';
import { allure, LabelName } from '../../dist/index'
test('should add epic label', async ({}, testInfo) => {
allure.label({name:LabelName.EPIC,value:'Test epic label'});
});
`,
},
(writer) => {
return writer.tests.map((t) => t.labels);
},
);
expect(result[0]).toContainEqual({ name: LabelName.EPIC, value: "Test epic label" });
});
38 changes: 38 additions & 0 deletions packages/allure-playwright/test/link.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Label } from "allure-js-commons";
import { test, expect } from "./fixtures";
test("should have link", async ({ runInlineTest }) => {
const result: Label[] = await runInlineTest(
{
"a.test.ts": `
import { test } from '@playwright/test';
import { allure } from '../../dist/index'
test('should add epic link', async ({}, testInfo) => {
allure.link({url:"https://playwright.dev/docs/api/class-page#page-workers"});
});
`,
},
(writer) => {
return writer.tests.map(val => val.links);
},
);

expect(result[0]).toContainEqual({
url: "https://playwright.dev/docs/api/class-page#page-workers",
});
});
Loading

0 comments on commit 782d17a

Please sign in to comment.