1
- import type { XrayTestResult , XrayTest , XrayTestSteps } from '../types/cloud.types' ;
1
+ import type { XrayTestResult , XrayTestSteps , XrayTestEvidence , XrayTest } from '../types/cloud.types' ;
2
2
import type { XrayOptions } from '../types/xray.types' ;
3
3
import type { Reporter , TestCase , TestResult } from '@playwright/test/reporter' ;
4
+ import * as fs from 'fs' ;
5
+ import * as path from 'path' ;
4
6
5
7
import { XrayService } from './xray.service' ;
6
8
7
9
class XrayReporter implements Reporter {
8
10
private xrayService ! : XrayService ;
9
11
private testResults ! : XrayTestResult ;
10
12
private testCaseKeyPattern = / \[ ( .* ?) \] / ;
13
+ private receivedRegEx : RegExp = / R e c e i v e d s t r i n g : " ( .* ?) " (? = \n ) / ;
11
14
private options : XrayOptions ;
12
15
private totalDuration : number ;
13
16
private readonly defaultRunName = `[${ new Date ( ) . toUTCString ( ) } ] - Automated run` ;
@@ -16,8 +19,6 @@ class XrayReporter implements Reporter {
16
19
this . options = options ;
17
20
this . xrayService = new XrayService ( this . options ) ;
18
21
this . totalDuration = 0 ;
19
-
20
- //const finishTime = new Date(this.xrayService.startTime.getTime() + (result.duration * 1000));
21
22
const testResults : XrayTestResult = {
22
23
info : {
23
24
summary : this . defaultRunName ,
@@ -31,15 +32,13 @@ class XrayReporter implements Reporter {
31
32
this . testResults = testResults ;
32
33
}
33
34
34
- async onBegin ( ) { }
35
-
36
35
async onTestEnd ( testCase : TestCase , result : TestResult ) {
37
36
const testCaseId = testCase . title . match ( this . testCaseKeyPattern ) ;
38
37
const testCode : string = testCaseId != null ? testCaseId [ 1 ] ! : '' ;
39
38
if ( testCode != '' ) {
40
39
// @ts -ignore
41
40
const browserName = testCase . _pool . registrations . get ( 'browserName' ) . fn ;
42
- const finishTime = new Date ( result . startTime . getTime ( ) + result . duration * 1000 ) ;
41
+ const finishTime = new Date ( result . startTime . getTime ( ) + result . duration ) ;
43
42
this . totalDuration = this . totalDuration + result . duration ;
44
43
45
44
let xrayTestData : XrayTest = {
@@ -49,28 +48,51 @@ class XrayReporter implements Reporter {
49
48
finish : finishTime . toISOString ( ) ,
50
49
steps : [ ] as XrayTestSteps [ ] ,
51
50
} ;
52
-
53
- // Generated step and error messages
51
+
54
52
await Promise . all (
55
53
result . steps . map ( async ( step ) => {
56
54
if ( step . category != 'hook' ) {
55
+ // Add Step to request
56
+ const errorMessage = step . error ?. stack ?. toString ( ) ?. replace ( / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g, '' ) ;
57
+ const received = this . receivedRegEx . exec ( errorMessage ! ) ;
58
+ let dataReceived = ""
59
+ if ( received ?. [ 1 ] !== undefined ) {
60
+ dataReceived = received ?. [ 1 ] ;
61
+ }
62
+
57
63
const xrayTestStep : XrayTestSteps = {
58
- status : typeof step . error == 'object' ? 'FAILED' : 'SUCCESS ' ,
59
- comment : step . title ,
60
- actualResult : typeof step . error == 'object' ? step . error . message ?. toString ( ) ! : '' ,
64
+ status : typeof step . error == 'object' ? 'FAILED' : 'PASSED ' ,
65
+ comment : typeof step . error == 'object' ? errorMessage : '' ,
66
+ actualResult : dataReceived ,
61
67
} ;
62
68
xrayTestData . steps ! . push ( xrayTestStep ) ;
63
69
}
64
70
} ) ,
65
71
) ;
72
+
73
+ // Get evidences from test results (video, images, text)
74
+ const evidences : XrayTestEvidence [ ] = [ ] ;
75
+ if ( result . attachments . length > 0 ) {
76
+ result . attachments . map ( async ( attach ) => {
77
+ const filename = path . basename ( attach . path ! ) ;
78
+ const attachData = fs . readFileSync ( attach . path ! , { encoding : 'base64' } ) ;
79
+ const evid : XrayTestEvidence = {
80
+ data : attachData ,
81
+ filename : filename ,
82
+ contentType : attach . contentType
83
+ }
84
+ evidences . push ( evid ) ;
85
+ } )
86
+ }
87
+
88
+ xrayTestData . evidence = evidences ;
66
89
this . testResults . tests ! . push ( xrayTestData ) ;
67
90
}
68
91
}
69
92
70
93
async onEnd ( ) {
71
94
// Update test Duration
72
- this . testResults . info . finishDate = new Date ( new Date ( this . testResults . info . startDate ) . getTime ( ) + this . totalDuration ) . toISOString ( ) ;
73
-
95
+ this . testResults ?. info ?. finishDate != new Date ( new Date ( this . testResults ?. info ?. startDate ! ) . getTime ( ) + this . totalDuration ) . toISOString ( ) ;
74
96
if ( typeof this . testResults != 'undefined' && typeof this . testResults . tests != 'undefined' && this . testResults . tests . length > 0 ) {
75
97
await this . xrayService . createRun ( this . testResults ) ;
76
98
} else {
0 commit comments