@@ -130,7 +130,7 @@ export default function (Commands, Cypress, cy) {
130
130
message : `${ existingSession . id . length > 50 ? `${ existingSession . id . substr ( 0 , 47 ) } ...` : existingSession . id } ` ,
131
131
} )
132
132
133
- function runSetup ( existingSession ) {
133
+ function createSession ( existingSession , recreateSession = false ) {
134
134
Cypress . log ( {
135
135
name : 'session' ,
136
136
displayName : 'Create New Session' ,
@@ -141,17 +141,20 @@ export default function (Commands, Cypress, cy) {
141
141
groupStart : true ,
142
142
} )
143
143
144
- if ( ! hadValidationError ) {
145
- _log . set ( {
146
- renderProps : ( ) => {
147
- return {
148
- indicator : 'successful' ,
149
- message : `(new) ${ _log . get ( ) . message } ` ,
150
- }
151
- } ,
152
- } )
144
+ let renderProps = {
145
+ indicator : 'successful' ,
146
+ message : `(new) ${ _log . get ( ) . message } ` ,
153
147
}
154
148
149
+ if ( recreateSession ) {
150
+ renderProps = {
151
+ indicator : 'bad' ,
152
+ message : `(recreated) ${ _log . get ( ) . message } ` ,
153
+ }
154
+ }
155
+
156
+ _log . set ( { renderProps : ( ) => renderProps } )
157
+
155
158
return cy . then ( async ( ) => {
156
159
await navigateAboutBlank ( )
157
160
await sessions . clearCurrentSessionData ( )
@@ -179,8 +182,44 @@ export default function (Commands, Cypress, cy) {
179
182
} )
180
183
}
181
184
185
+ function restoreSession ( existingSession ) {
186
+ Cypress . log ( {
187
+ name : 'session' ,
188
+ displayName : 'Restore Saved Session' ,
189
+ event : true ,
190
+ state : 'passed' ,
191
+ type : 'system' ,
192
+ message : `` ,
193
+ groupStart : true ,
194
+ } )
195
+
196
+ return cy . then ( async ( ) => {
197
+ await navigateAboutBlank ( )
198
+
199
+ _log . set ( {
200
+ renderProps : ( ) => {
201
+ return {
202
+ indicator : 'pending' ,
203
+ message : `(saved) ${ _log . get ( ) . message } ` ,
204
+ }
205
+ } ,
206
+ } )
207
+
208
+ dataLog . set ( {
209
+ consoleProps : ( ) => getConsoleProps ( existingSession ) ,
210
+ } )
211
+
212
+ await sessions . setSessionData ( existingSession )
213
+ Cypress . log ( { groupEnd : true , emitOnly : true } )
214
+ } )
215
+ }
216
+
182
217
// uses Cypress hackery to resolve `false` if validate() resolves/returns false or throws/fails a cypress command.
183
218
function validateSession ( existingSession , _onFail ) {
219
+ if ( ! existingSession . validate ) {
220
+ return
221
+ }
222
+
184
223
const validatingLog = Cypress . log ( {
185
224
name : 'session' ,
186
225
displayName : 'Validate Session' ,
@@ -193,19 +232,14 @@ export default function (Commands, Cypress, cy) {
193
232
} )
194
233
195
234
const onSuccess = ( ) => {
196
- validatingLog . set ( {
197
- name : 'session' ,
198
- displayName : 'Validate Session: valid' ,
199
- message : '' ,
200
- type : 'system' ,
201
- event : true ,
202
- state : 'warning' ,
203
- } )
235
+ validatingLog . set ( { displayName : 'Validate Session: valid' } )
204
236
205
237
Cypress . log ( { groupEnd : true , emitOnly : true } )
206
238
}
207
239
208
240
const onFail = ( err ) => {
241
+ validatingLog . set ( { displayName : 'Validate Session: invalid' } )
242
+
209
243
_onFail ( err , validatingLog )
210
244
}
211
245
@@ -297,65 +331,66 @@ export default function (Commands, Cypress, cy) {
297
331
return _catchCommand
298
332
}
299
333
300
- let hadValidationError = false
301
- let onValidationError : Function = ( err , log ) => {
302
- log . set ( {
303
- name : 'session' ,
304
- displayName : 'Validate Session: invalid' ,
305
- message : '' ,
306
- type : 'system' ,
307
- event : true ,
308
- state : 'warning' ,
309
- } )
310
-
311
- const errorLog = Cypress . log ( {
334
+ const onRestoreSessionValidationError = ( err , log ) => {
335
+ // create error log to show validation error to the user in the reporter
336
+ Cypress . log ( {
312
337
showError : true ,
313
338
type : 'system' ,
314
339
event : true ,
315
340
name : 'session' ,
316
341
displayName : '' ,
317
342
message : '' ,
318
- } )
343
+ } ) . error ( err )
319
344
320
- errorLog . error ( err )
321
- errorLog . set ( {
322
- state : 'warn' ,
345
+ log . endGroup ( )
323
346
324
- } )
347
+ const recreateSession = true
325
348
326
- _log . set ( {
327
- renderProps : ( ) => {
328
- return {
329
- indicator : 'bad' ,
330
- message : `(recreated) ${ _log . get ( ) . message } ` ,
331
- }
332
- } ,
333
- } )
349
+ return createSessionWorkflow ( existingSession , recreateSession )
350
+ }
334
351
335
- Cypress . log ( { groupEnd : true , emitOnly : true } )
352
+ const throwValidationError = ( err , log ) => {
353
+ log . endGroup ( )
354
+ $errUtils . modifyErrMsg ( err , `\n\nThis error occurred in a session validate hook after initializing the session. Because validation failed immediately after session setup we failed the test.` , _ . add )
336
355
337
- hadValidationError = true
356
+ cy . fail ( err )
357
+ }
338
358
339
- return runSetup ( existingSession )
359
+ /**
360
+ * Creates session flow:
361
+ * 1. create session
362
+ * 2. validate session
363
+ */
364
+ const createSessionWorkflow = ( existingSession , recreateSession = false ) => {
365
+ return createSession ( existingSession , recreateSession )
340
366
. then ( ( ) => {
341
- cy . then ( ( ) => {
342
- return validateSession ( existingSession , throwValidationError )
343
- } )
344
- . then ( ( ) => {
345
- cy . then ( async ( ) => {
346
- await navigateAboutBlank ( )
347
- Cypress . log ( { groupEnd : true , name : 'session' , message : '' , emitOnly : true } )
348
- } )
349
- } )
367
+ validateSession ( existingSession , throwValidationError )
350
368
} )
351
369
}
352
370
353
- const throwValidationError = ( err ) => {
354
- $errUtils . modifyErrMsg ( err , `\n\nThis error occurred in a session validate hook after initializing the session. Because validation failed immediately after session setup we failed the test.` , _ . add )
355
-
356
- cy . fail ( err )
371
+ /**
372
+ * Restore session flow:
373
+ * 1. restore session
374
+ * 2. validation session
375
+ * 3. if validation fails, catch error and recreate session
376
+ */
377
+ const restoreSessionWorkflow = ( existingSession ) => {
378
+ return restoreSession ( existingSession )
379
+ . then ( ( ) => {
380
+ validateSession ( existingSession , onRestoreSessionValidationError )
381
+ } )
357
382
}
358
383
384
+ /**
385
+ * Session command rules:
386
+ * If session does not exists or was no previously saved to the server, create session
387
+ * 1. run create session flow
388
+ * 2. clear page
389
+ *
390
+ * If session exists or has been saved to the server, restore session
391
+ * 1. run restore session flow
392
+ * 2. clear page
393
+ */
359
394
return cy . then ( async ( ) => {
360
395
if ( ! existingSession . hydrated ) {
361
396
const serverStoredSession = await sessions . getSession ( existingSession . id ) . catch ( _ . noop )
@@ -365,50 +400,15 @@ export default function (Commands, Cypress, cy) {
365
400
_ . extend ( existingSession , _ . omit ( serverStoredSession , 'setup' ) )
366
401
existingSession . hydrated = true
367
402
} else {
368
- onValidationError = throwValidationError
369
-
370
- return runSetup ( existingSession )
403
+ return createSessionWorkflow ( existingSession )
371
404
}
372
405
}
373
406
374
- Cypress . log ( {
375
- name : 'session' ,
376
- displayName : 'Restore Saved Session' ,
377
- event : true ,
378
- state : 'passed' ,
379
- type : 'system' ,
380
- message : `` ,
381
- groupStart : true ,
382
- } )
383
-
384
- await navigateAboutBlank ( )
385
-
386
- _log . set ( {
387
- renderProps : ( ) => {
388
- return {
389
- indicator : 'pending' ,
390
- message : `(saved) ${ _log . get ( ) . message } ` ,
391
- }
392
- } ,
393
- } )
394
-
395
- dataLog . set ( {
396
- consoleProps : ( ) => getConsoleProps ( existingSession ) ,
397
- } )
398
-
399
- await sessions . setSessionData ( existingSession )
407
+ return restoreSessionWorkflow ( existingSession )
400
408
} )
401
409
. then ( async ( ) => {
410
+ await navigateAboutBlank ( )
402
411
Cypress . log ( { groupEnd : true , emitOnly : true } )
403
- if ( existingSession . validate ) {
404
- await validateSession ( existingSession , onValidationError )
405
- }
406
- } )
407
- . then ( async ( ) => {
408
- if ( ! hadValidationError ) {
409
- await navigateAboutBlank ( )
410
- Cypress . log ( { groupEnd : true , emitOnly : true } )
411
- }
412
412
} )
413
413
} ,
414
414
} )
0 commit comments