@@ -37,6 +37,11 @@ export interface Config {
37
37
mergeVideos ?: boolean ;
38
38
}
39
39
40
+ export interface Credentials {
41
+ username : string ;
42
+ accessKey : string ;
43
+ }
44
+
40
45
// Types of attachments relevant for UI display.
41
46
const webAssetsTypes = [
42
47
'.log' ,
@@ -52,6 +57,17 @@ const webAssetsTypes = [
52
57
'.svg' ,
53
58
] ;
54
59
60
+ const hasCredentials = function ( ) {
61
+ return process . env . SAUCE_USERNAME && process . env . SAUCE_ACCESS_KEY ;
62
+ } ;
63
+
64
+ const getCredentials = function ( ) : Credentials {
65
+ return {
66
+ username : process . env . SAUCE_USERNAME ,
67
+ accessKey : process . env . SAUCE_ACCESS_KEY ,
68
+ } as Credentials ;
69
+ } ;
70
+
55
71
export default class SauceReporter implements Reporter {
56
72
projects : { [ k : string ] : any } ;
57
73
@@ -93,21 +109,25 @@ export default class SauceReporter implements Reporter {
93
109
94
110
constructor ( reporterConfig : Config ) {
95
111
this . projects = { } ;
96
-
97
112
this . buildName = reporterConfig ?. buildName || '' ;
98
113
this . tags = reporterConfig ?. tags || [ ] ;
99
114
this . region = reporterConfig ?. region || 'us-west-1' ;
100
115
this . outputFile =
101
116
reporterConfig ?. outputFile || process . env . SAUCE_REPORT_OUTPUT_NAME ;
102
117
this . shouldUpload = reporterConfig ?. upload !== false ;
103
118
this . mergeVideos = reporterConfig ?. mergeVideos === true ;
119
+ this . playwrightVersion = 'unknown' ;
104
120
105
121
this . webAssetsDir =
106
122
reporterConfig . webAssetsDir || process . env . SAUCE_WEB_ASSETS_DIR ;
107
123
if ( this . webAssetsDir && ! fs . existsSync ( this . webAssetsDir ) ) {
108
124
fs . mkdirSync ( this . webAssetsDir , { recursive : true } ) ;
109
125
}
110
126
127
+ if ( ! hasCredentials ( ) || ! this . shouldUpload ) {
128
+ return ;
129
+ }
130
+
111
131
let reporterVersion = 'unknown' ;
112
132
try {
113
133
const packageData = JSON . parse (
@@ -118,28 +138,20 @@ export default class SauceReporter implements Reporter {
118
138
/* empty */
119
139
}
120
140
121
- if (
122
- process . env . SAUCE_USERNAME &&
123
- process . env . SAUCE_USERNAME !== '' &&
124
- process . env . SAUCE_ACCESS_KEY &&
125
- process . env . SAUCE_ACCESS_KEY !== ''
126
- ) {
127
- this . api = new TestComposer ( {
128
- region : this . region ,
129
- username : process . env . SAUCE_USERNAME ,
130
- accessKey : process . env . SAUCE_ACCESS_KEY ,
131
- headers : {
132
- 'User-Agent' : `playwright-reporter/${ reporterVersion } ` ,
133
- } ,
134
- } ) ;
135
- this . testRunsApi = new TestRunsApi ( {
136
- region : this . region ,
137
- username : process . env . SAUCE_USERNAME ,
138
- accessKey : process . env . SAUCE_ACCESS_KEY ,
139
- } ) ;
140
- }
141
-
142
- this . playwrightVersion = 'unknown' ;
141
+ const { username, accessKey } = getCredentials ( ) ;
142
+ this . api = new TestComposer ( {
143
+ region : this . region ,
144
+ username,
145
+ accessKey,
146
+ headers : {
147
+ 'User-Agent' : `playwright-reporter/${ reporterVersion } ` ,
148
+ } ,
149
+ } ) ;
150
+ this . testRunsApi = new TestRunsApi ( {
151
+ region : this . region ,
152
+ username,
153
+ accessKey,
154
+ } ) ;
143
155
}
144
156
145
157
onBegin ( config : FullConfig , suite : PlaywrightSuite ) {
@@ -168,24 +180,22 @@ export default class SauceReporter implements Reporter {
168
180
for await ( const projectSuite of this . rootSuite . suites ) {
169
181
const { report, assets } = await this . createSauceReport ( projectSuite ) ;
170
182
171
- const result = await this . reportToSauce ( projectSuite , report , assets ) ;
183
+ suites . push ( ...report . suites ) ;
184
+
185
+ if ( this . isWebAssetSyncEnabled ( ) ) {
186
+ this . syncAssets ( assets ) ;
187
+ }
172
188
189
+ if ( ! hasCredentials ( ) || ! this . shouldUpload ) {
190
+ continue ;
191
+ }
192
+
193
+ const result = await this . reportToSauce ( projectSuite , report , assets ) ;
173
194
if ( result ?. id ) {
174
195
jobUrls . push ( {
175
196
url : result . url ,
176
197
name : projectSuite . title ,
177
198
} ) ;
178
- try {
179
- await this . reportTestRun ( projectSuite , report , result ?. id ) ;
180
- } catch ( e : any ) {
181
- console . warn ( 'failed to send report to insights: ' , e ) ;
182
- }
183
- }
184
-
185
- suites . push ( ...report . suites ) ;
186
-
187
- if ( this . isWebAssetSyncEnabled ( ) ) {
188
- this . syncAssets ( assets ) ;
189
199
}
190
200
}
191
201
@@ -233,7 +243,11 @@ export default class SauceReporter implements Reporter {
233
243
} ;
234
244
}
235
245
236
- await this . testRunsApi ?. create ( [ req ] ) ;
246
+ try {
247
+ await this . testRunsApi ?. create ( [ req ] ) ;
248
+ } catch ( e : any ) {
249
+ console . warn ( 'failed to send report to insights: ' , e ) ;
250
+ }
237
251
}
238
252
239
253
getDuration ( projectSuite : PlaywrightSuite ) {
@@ -280,17 +294,14 @@ export default class SauceReporter implements Reporter {
280
294
}
281
295
282
296
displayReportedJobs ( jobs : { name : string ; url : string } [ ] ) {
297
+ if ( ! hasCredentials ( ) && this . shouldUpload ) {
298
+ console . warn (
299
+ `\nNo results reported to Sauce Labs. SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables must be defined in order for reports to be uploaded to Sauce.` ,
300
+ ) ;
301
+ console . log ( ) ;
302
+ return ;
303
+ }
283
304
if ( jobs . length < 1 ) {
284
- let msg = '' ;
285
- const hasCredentials =
286
- process . env . SAUCE_USERNAME &&
287
- process . env . SAUCE_USERNAME !== '' &&
288
- process . env . SAUCE_ACCESS_KEY &&
289
- process . env . SAUCE_ACCESS_KEY !== '' ;
290
- if ( hasCredentials && this . shouldUpload ) {
291
- msg = `\nNo results reported to Sauce. $SAUCE_USERNAME and $SAUCE_ACCESS_KEY environment variables must be defined in order for reports to be uploaded to Sauce.` ;
292
- }
293
- console . log ( msg ) ;
294
305
console . log ( ) ;
295
306
return ;
296
307
}
@@ -529,25 +540,24 @@ ${err.stack}
529
540
const browserVersion = '1.0' ;
530
541
const browserName = this . getBrowserName ( projectSuite ) ;
531
542
532
- if ( this . shouldUpload ) {
533
- const resp = await this . api ?. createReport ( {
534
- name : projectSuite . title ,
535
- browserName : `playwright-${ browserName } ` ,
536
- browserVersion,
537
- platformName : this . getPlatformName ( ) ,
538
- framework : 'playwright' ,
539
- frameworkVersion : this . playwrightVersion ,
540
- passed : didSuitePass ,
541
- startTime : this . startedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
542
- endTime : this . endedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
543
- build : this . buildName ,
544
- tags : this . tags ,
545
- } ) ;
546
- if ( resp ?. id ) {
547
- await this . uploadAssets ( resp . id , consoleLog , report , assets ) ;
548
- }
549
- return resp ;
543
+ const resp = await this . api ?. createReport ( {
544
+ name : projectSuite . title ,
545
+ browserName : `playwright-${ browserName } ` ,
546
+ browserVersion,
547
+ platformName : this . getPlatformName ( ) ,
548
+ framework : 'playwright' ,
549
+ frameworkVersion : this . playwrightVersion ,
550
+ passed : didSuitePass ,
551
+ startTime : this . startedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
552
+ endTime : this . endedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
553
+ build : this . buildName ,
554
+ tags : this . tags ,
555
+ } ) ;
556
+ if ( resp ?. id ) {
557
+ await this . uploadAssets ( resp . id , consoleLog , report , assets ) ;
558
+ await this . reportTestRun ( projectSuite , report , resp . id ) ;
550
559
}
560
+ return resp ;
551
561
}
552
562
553
563
async uploadAssets (
0 commit comments