playwright-slack-report 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@ Publish your Playwright test results to your favorite Slack channel(s).
16
16
  - 🧑‍🎨 Define your own custom Slack message layout!
17
17
 
18
18
 
19
- # 📦 Installation
19
+ # 📦 Installation
20
20
 
21
21
  Run following commands:
22
22
 
@@ -73,9 +73,9 @@ You will need to have Slack administrator rights to perform the steps below.
73
73
 
74
74
  ![Click the Add an OAuth Scope](https://github.com/ryanrosello-og/playwright-slack-report/blob/main/assets/2022-08-09_5-48-30.png?raw=true)
75
75
 
76
- * chat:write
77
- * chat:write.public
78
- * chat:write.customize
76
+ * chat:write
77
+ * chat:write.public
78
+ * chat:write.customize
79
79
 
80
80
  6. Scroll up to the OAuth Tokens for Your Workspace and click the **Install to Workspace** button
81
81
 
@@ -85,7 +85,7 @@ You will need to have Slack administrator rights to perform the steps below.
85
85
 
86
86
  ![click the Allow button](https://github.com/ryanrosello-og/playwright-slack-report/blob/main/assets/2022-08-09_5-49-49.png?raw=true)
87
87
 
88
- The final step will be to copy the generated Bot User OAuth Token aka `SLACK_BOT_USER_OAUTH_TOKEN`.
88
+ The final step will be to copy the generated Bot User OAuth Token aka `SLACK_BOT_USER_OAUTH_TOKEN`.
89
89
 
90
90
  >**Treat this token as a secret.**
91
91
 
@@ -128,13 +128,13 @@ An example advanced configuration is shown below:
128
128
  },
129
129
  ],
130
130
  },
131
-
131
+
132
132
  ],
133
133
  ],
134
134
  ```
135
135
 
136
136
  ### **channels**
137
- An array of Slack channels to post to, atleast one channel is required
137
+ An array of Slack channels to post to, at least one channel is required
138
138
  ### **sendResults**
139
139
  Can either be *"always"*, *"on-failure"* or *"off"*, this configuration is required:
140
140
  * **always** - will send the results to Slack at completion of the test run
@@ -143,6 +143,8 @@ Can either be *"always"*, *"on-failure"* or *"off"*, this configuration is requi
143
143
  ### **layout**
144
144
  A function that returns a layout object, this configuration is optional. See section below for more details.
145
145
  * meta - an array of meta data to be sent to Slack, this configuration is optional.
146
+ ### **layoutAsync**
147
+ Same as **layout** above, but asynchronous in that it returns a promise.
146
148
  ### **maxNumberOfFailuresToShow**
147
149
  Limits the number of failures shown in the Slack message, defaults to 10.
148
150
 
@@ -162,7 +164,7 @@ meta: [
162
164
  key: 'GITHUB_REF',
163
165
  value: process.env.GITHUB_REF,
164
166
  },
165
- ],
167
+ ],
166
168
  ...
167
169
  ```
168
170
 
@@ -188,7 +190,7 @@ const generateCustomLayout = (summaryResults: SummaryResults):Array<KnownBlock |
188
190
  export default generateCustomLayout;
189
191
  ```
190
192
 
191
- In your, `playwright.confing.ts` file, add your function into the config.
193
+ In your, `playwright.config.ts` file, add your function into the config.
192
194
 
193
195
  ```typescript
194
196
  import { generateCustomLayout } from "./my_custom_layout";
@@ -260,7 +262,6 @@ Add the meta block in your config:
260
262
  },
261
263
  ],
262
264
  },
263
-
264
265
  ],
265
266
  ],
266
267
  ```
@@ -308,6 +309,204 @@ Generates the following message in Slack:
308
309
 
309
310
  ![Final](https://github.com/ryanrosello-og/playwright-slack-report/blob/main/assets/2022-08-13_8-17-46.png?raw=true)
310
311
 
312
+
313
+ **Example 3: - with screenshots and/or recorded videos**
314
+
315
+ In your, `playwright.config.ts` file, add these params (Make sure you use **layoutAsync** rather than **layout**):
316
+
317
+ ```typescript
318
+ import { generateCustomLayoutAsync } from "./my_custom_layout";
319
+ ...
320
+ reporter: [
321
+ [
322
+ "./node_modules/playwright-slack-report/dist/src/SlackReporter.js",
323
+ {
324
+ ...
325
+ layoutAsync: generateCustomLayoutAsync,
326
+ ...
327
+ },
328
+ ],
329
+ ],
330
+ use: {
331
+ ...
332
+ screenshot: "only-on-failure",
333
+ video: "retain-on-failure",
334
+ ...
335
+ },
336
+ ```
337
+
338
+ Create the function to generate the layout asynchronously in `my_custom_layout.ts`:
339
+
340
+ ```typescript
341
+ import fs from "fs";
342
+ import path from "path";
343
+ import { Block, KnownBlock } from "@slack/types";
344
+ import { SummaryResults } from "playwright-slack-report/dist/src";
345
+ import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
346
+
347
+ const s3Client = new S3Client({
348
+ credentials: {
349
+ accessKeyId: process.env.S3_ACCESS_KEY || "",
350
+ secretAccessKey: process.env.S3_SECRET || "",
351
+ },
352
+ region: process.env.S3_REGION,
353
+ });
354
+
355
+ async function uploadFile(filePath, fileName) {
356
+ try {
357
+ const ext = path.extname(filePath);
358
+ const name = `${fileName}${ext}`;
359
+
360
+ await s3Client.send(
361
+ new PutObjectCommand({
362
+ Bucket: process.env.S3_BUCKET,
363
+ Key: name,
364
+ Body: fs.createReadStream(filePath),
365
+ })
366
+ );
367
+
368
+ return `https://${process.env.S3_BUCKET}.s3.${process.env.S3_REGION}.amazonaws.com/${name}`;
369
+ } catch (err) {
370
+ console.log("🔥🔥 Error", err);
371
+ }
372
+ }
373
+
374
+
375
+ export async function generateCustomLayoutAsync (summaryResults: SummaryResults): Promise<Array<KnownBlock | Block>> {
376
+ const { tests } = summaryResults;
377
+ // create your custom slack blocks
378
+
379
+ const header = {
380
+ type: "header",
381
+ text: {
382
+ type: "plain_text",
383
+ text: "🎭 *Playwright E2E Test Results*",
384
+ emoji: true,
385
+ },
386
+ };
387
+
388
+ const summary = {
389
+ type: "section",
390
+ text: {
391
+ type: "mrkdwn",
392
+ text: `✅ *${summaryResults.passed}* | ❌ *${summaryResults.failed}* | ⏩ *${summaryResults.skipped}*`,
393
+ },
394
+ };
395
+
396
+ const fails: Array<KnownBlock | Block> = [];
397
+
398
+ for (const t of tests) {
399
+ if (t.status === "failed" || t.status === "timedOut") {
400
+
401
+ fails.push({
402
+ type: "section",
403
+ text: {
404
+ type: "mrkdwn",
405
+ text: `👎 *[${t.browser}] | ${t.suiteName.replace(/\W/gi, "-")}*`,
406
+ },
407
+ });
408
+
409
+ const assets: Array<string> = [];
410
+
411
+ if (t.attachments) {
412
+ for (const a of t.attachments) {
413
+ // Upload failed tests screenshots and videos to the service of your choice
414
+ // In my case I upload the to S3 bucket
415
+ const permalink = await uploadFile(
416
+ a.path,
417
+ `${t.suiteName}--${t.name}`.replace(/\W/gi, "-").toLowerCase()
418
+ );
419
+
420
+ if (permalink) {
421
+ let icon = "";
422
+ if (a.name === "screenshot") {
423
+ icon = "📸";
424
+ } else if (a.name === "video") {
425
+ icon = "🎥";
426
+ }
427
+
428
+ assets.push(`${icon} See the <${permalink}|${a.name}>`);
429
+ }
430
+ }
431
+ }
432
+
433
+ if (assets.length > 0) {
434
+ fails.push({
435
+ type: "context",
436
+ elements: [{ type: "mrkdwn", text: assets.join("\n") }],
437
+ });
438
+ }
439
+ }
440
+ }
441
+
442
+ return [header, summary, { type: "divider" }, ...fails]
443
+ }
444
+
445
+ ```
446
+
447
+ **Also you can upload the attachments to slack.** But it might be more expensive for you and also you'll have to extend the scope.
448
+
449
+ ```typescript
450
+ ...
451
+ const web_api_1 = require('@slack/web-api');
452
+ const slackClient = new web_api_1.WebClient(process.env.SLACK_BOT_USER_OAUTH_TOKEN);
453
+
454
+ async function uploadFile(filePath) {
455
+ try {
456
+ const result = await slackClient.files.uploadV2({
457
+ channels: 'you_cannel_name',
458
+ file: fs.createReadStream(filePath),
459
+ filename: filePath.split('/').at(-1),
460
+ });
461
+
462
+ return result.file;
463
+ } catch (error) {
464
+ console.log('🔥🔥 error', error);
465
+ }
466
+ }
467
+
468
+ export async function generateCustomLayoutAsync (summaryResults: SummaryResults): Promise<Array<KnownBlock | Block>> {
469
+ const { tests } = summaryResults;
470
+ ....
471
+ // See the snippet above ^^^
472
+
473
+
474
+ if (t.attachments) {
475
+ for (const a of t.attachments) {
476
+ const file = await uploadFile(a.path);
477
+
478
+ if (file) {
479
+ if (a.name === 'screenshot' && file.permalink) {
480
+ fails.push({
481
+ alt_text: '',
482
+ image_url: file.permalink,
483
+ title: { type: 'plain_text', text: file.name || '' },
484
+ type: 'image',
485
+ });
486
+ }
487
+
488
+ if (a.name === 'video' && file.permalink) {
489
+ fails.push({
490
+ alt_text: '',
491
+ // NOTE:
492
+ // Slack requires thumbnail_url length to be more that 0
493
+ // Either set screenshot url as the thumbnail or add a placeholder image url
494
+ thumbnail_url: '',
495
+ title: { type: 'plain_text', text: file.name || '' },
496
+ type: 'video',
497
+ video_url: file.permalink,
498
+ });
499
+ }
500
+ }
501
+ }
502
+ }
503
+ ....
504
+
505
+ return [header, summary, { type: "divider" }, ...fails]
506
+ }
507
+
508
+ ```
509
+
311
510
  # 🔑 License
312
511
 
313
512
  [MIT](https://github.com/ryanrosello-og/playwright-slack-report/blob/main/LICENSE)
@@ -324,7 +523,7 @@ Run the tests using `npm run pw`
324
523
  Run `npm pack`
325
524
 
326
525
  Create a new playwright project using `yarn create playwright`
327
- Modify the `package.json` and a local dependancy to the generated `tgz` file
526
+ Modify the `package.json` and a local dependancy to the generated `tgz` file
328
527
 
329
528
  e.g.
330
529
 
@@ -1,4 +1,4 @@
1
- import { KnownBlock, Block } from '@slack/types';
2
- import { SummaryResults } from '.';
3
- declare const generateBlocks: (summaryResults: SummaryResults, maxNumberOfFailures: number) => Promise<Array<KnownBlock | Block>>;
4
- export default generateBlocks;
1
+ import { KnownBlock, Block } from '@slack/types';
2
+ import { SummaryResults } from '.';
3
+ declare const generateBlocks: (summaryResults: SummaryResults, maxNumberOfFailures: number) => Promise<Array<KnownBlock | Block>>;
4
+ export default generateBlocks;
@@ -1,69 +1,69 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const generateBlocks = async (summaryResults, maxNumberOfFailures) => {
4
- const maxNumberOfFailureLength = 650;
5
- const fails = [];
6
- const meta = [];
7
- const header = {
8
- type: 'section',
9
- text: {
10
- type: 'mrkdwn',
11
- text: '🎭 *Playwright Results*',
12
- },
13
- };
14
- const summary = {
15
- type: 'section',
16
- text: {
17
- type: 'mrkdwn',
18
- text: `✅ *${summaryResults.passed}* | ❌ *${summaryResults.failed}* | ⏩ *${summaryResults.skipped}*`,
19
- },
20
- };
21
- for (let i = 0; i < summaryResults.failures.length; i += 1) {
22
- const { failureReason, test } = summaryResults.failures[i];
23
- const formattedFailure = failureReason
24
- .substring(0, maxNumberOfFailureLength)
25
- .split('\n')
26
- .map((l) => `>${l}`)
27
- .join('\n');
28
- fails.push({
29
- type: 'section',
30
- text: {
31
- type: 'mrkdwn',
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const generateBlocks = async (summaryResults, maxNumberOfFailures) => {
4
+ const maxNumberOfFailureLength = 650;
5
+ const fails = [];
6
+ const meta = [];
7
+ const header = {
8
+ type: 'section',
9
+ text: {
10
+ type: 'mrkdwn',
11
+ text: '🎭 *Playwright Results*',
12
+ },
13
+ };
14
+ const summary = {
15
+ type: 'section',
16
+ text: {
17
+ type: 'mrkdwn',
18
+ text: `✅ *${summaryResults.passed}* | ❌ *${summaryResults.failed}* | ⏩ *${summaryResults.skipped}*`,
19
+ },
20
+ };
21
+ for (let i = 0; i < summaryResults.failures.length; i += 1) {
22
+ const { failureReason, test } = summaryResults.failures[i];
23
+ const formattedFailure = failureReason
24
+ .substring(0, maxNumberOfFailureLength)
25
+ .split('\n')
26
+ .map((l) => `>${l}`)
27
+ .join('\n');
28
+ fails.push({
29
+ type: 'section',
30
+ text: {
31
+ type: 'mrkdwn',
32
32
  text: `*${test}*
33
- \n${formattedFailure}`,
34
- },
35
- });
36
- if (i > maxNumberOfFailures) {
37
- fails.push({
38
- type: 'section',
39
- text: {
40
- type: 'mrkdwn',
41
- text: `*There are too many failures to display - ${fails.length} out of ${summaryResults.failures.length} failures shown*`,
42
- },
43
- });
44
- break;
45
- }
46
- }
47
- if (summaryResults.meta) {
48
- for (let i = 0; i < summaryResults.meta.length; i += 1) {
49
- const { key, value } = summaryResults.meta[i];
50
- meta.push({
51
- type: 'section',
52
- text: {
53
- type: 'mrkdwn',
54
- text: `\n*${key}* :\t${value}`,
55
- },
56
- });
57
- }
58
- }
59
- return [
60
- header,
61
- summary,
62
- ...meta,
63
- {
64
- type: 'divider',
65
- },
66
- ...fails,
67
- ];
68
- };
69
- exports.default = generateBlocks;
33
+ \n${formattedFailure}`,
34
+ },
35
+ });
36
+ if (i > maxNumberOfFailures) {
37
+ fails.push({
38
+ type: 'section',
39
+ text: {
40
+ type: 'mrkdwn',
41
+ text: `*There are too many failures to display - ${fails.length} out of ${summaryResults.failures.length} failures shown*`,
42
+ },
43
+ });
44
+ break;
45
+ }
46
+ }
47
+ if (summaryResults.meta) {
48
+ for (let i = 0; i < summaryResults.meta.length; i += 1) {
49
+ const { key, value } = summaryResults.meta[i];
50
+ meta.push({
51
+ type: 'section',
52
+ text: {
53
+ type: 'mrkdwn',
54
+ text: `\n*${key}* :\t${value}`,
55
+ },
56
+ });
57
+ }
58
+ }
59
+ return [
60
+ header,
61
+ summary,
62
+ ...meta,
63
+ {
64
+ type: 'divider',
65
+ },
66
+ ...fails,
67
+ ];
68
+ };
69
+ exports.default = generateBlocks;
@@ -1,46 +1,46 @@
1
- /// <reference types="node" />
2
- import { failure, SummaryResults } from '.';
3
- export declare type testResult = {
4
- suiteName: string;
5
- name: string;
6
- browser?: string;
7
- projectName: string;
8
- endedAt: string;
9
- reason: string;
10
- retry: number;
11
- retries: number;
12
- startedAt: string;
13
- status: 'passed' | 'failed' | 'timedOut' | 'skipped';
14
- attachments?: {
15
- body: string | undefined | Buffer;
16
- contentType: string;
17
- name: string;
18
- path: string;
19
- }[];
20
- };
21
- export declare type testSuite = {
22
- testSuite: {
23
- title: string;
24
- tests: testResult[];
25
- testRunId?: number;
26
- };
27
- };
28
- export default class ResultsParser {
29
- private result;
30
- constructor();
31
- getParsedResults(): Promise<SummaryResults>;
32
- getFailures(): Promise<Array<failure>>;
33
- static getTestName(failedTest: any): any;
34
- updateResults(data: {
35
- testSuite: any;
36
- }): void;
37
- addTestResult(suiteName: any, testCase: any): void;
38
- safelyDetermineFailure(result: {
39
- errors: any[];
40
- error: {
41
- message: string;
42
- stack: string;
43
- };
44
- }): string;
45
- cleanseReason(rawReaseon: string): string;
46
- }
1
+ /// <reference types="node" />
2
+ import { failure, SummaryResults } from '.';
3
+ export declare type testResult = {
4
+ suiteName: string;
5
+ name: string;
6
+ browser?: string;
7
+ projectName: string;
8
+ endedAt: string;
9
+ reason: string;
10
+ retry: number;
11
+ retries: number;
12
+ startedAt: string;
13
+ status: 'passed' | 'failed' | 'timedOut' | 'skipped';
14
+ attachments?: {
15
+ body: string | undefined | Buffer;
16
+ contentType: string;
17
+ name: string;
18
+ path: string;
19
+ }[];
20
+ };
21
+ export declare type testSuite = {
22
+ testSuite: {
23
+ title: string;
24
+ tests: testResult[];
25
+ testRunId?: number;
26
+ };
27
+ };
28
+ export default class ResultsParser {
29
+ private result;
30
+ constructor();
31
+ getParsedResults(): Promise<SummaryResults>;
32
+ getFailures(): Promise<Array<failure>>;
33
+ static getTestName(failedTest: any): any;
34
+ updateResults(data: {
35
+ testSuite: any;
36
+ }): void;
37
+ addTestResult(suiteName: any, testCase: any): void;
38
+ safelyDetermineFailure(result: {
39
+ errors: any[];
40
+ error: {
41
+ message: string;
42
+ stack: string;
43
+ };
44
+ }): string;
45
+ cleanseReason(rawReaseon: string): string;
46
+ }