@@ -8,24 +8,40 @@ import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
88 */
99import * as Slack from '../utils/slack' ;
1010
11+ function slugifyForFileName ( input : string ) : string {
12+ return input
13+ . toLowerCase ( )
14+ . replace ( / [ ^ \w \d - ] + / g, '-' ) // Replace non-alphanumeric chars with dashes
15+ . replace ( / - + / g, '-' ) // Collapse multiple dashes
16+ . replace ( / ^ - | - $ / g, '' ) ; // Trim leading/trailing dashes
17+ }
18+
1119class SlackReporter implements Reporter {
1220 onTestEnd ( test : TestCase , result : TestResult ) {
1321 // If the test has already failed, we don't want to send a duplicate message.
1422 if ( result . retry !== 0 ) {
1523 return ;
1624 }
1725
18- if ( test . outcome ( ) === 'unexpected' ) {
19- Slack . sendFailedTestMessageToSlack ( test . title ) ;
26+ if ( test . outcome ( ) !== 'unexpected' ) {
27+ return ;
28+ }
2029
21- const screenshots = result . attachments . filter (
22- ( { name, path } ) => name === 'screenshot' && path
23- ) ;
30+ Slack . sendFailedTestMessageToSlack ( test . titlePath ( ) . join ( ' › ' ) ) ;
2431
25- if ( screenshots . length > 0 ) {
26- const [ screenshot ] = screenshots ;
27- Slack . sendFailedTestScreenshotToSlack ( screenshot . path ) ;
32+ const screenshots = result . attachments . filter (
33+ ( { name, path } ) => name === 'screenshot' && path
34+ ) ;
35+ if ( screenshots . length > 0 ) {
36+ const [ screenshot ] = screenshots ;
37+ if ( ! screenshot . path ) {
38+ return ;
2839 }
40+
41+ Slack . sendFailedTestScreenshotToSlack (
42+ screenshot . path ,
43+ slugifyForFileName ( test . title )
44+ ) ;
2945 }
3046 }
3147}
0 commit comments