/
index.ts
516 lines (428 loc) · 17.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import _ from 'lodash'
import stringifyStable from 'json-stable-stringify'
import $errUtils from '../../../cypress/error_utils'
import $utils from '../../../cypress/utils'
import logGroup from '../../logGroup'
import SessionsManager from './manager'
import {
getConsoleProps,
navigateAboutBlank,
SESSION_STEPS,
statusMap,
} from './utils'
/**
* Session data should be cleared with spec browser launch.
*
* Rules for clearing session data:
* - if page reloads due to top navigation OR user hard reload, session data should NOT be cleared
* - if user relaunches the browser or launches a new spec, session data SHOULD be cleared
* - session data SHOULD be cleared between specs in run mode
*/
export default function (Commands, Cypress, cy) {
const sessionsManager = new SessionsManager(Cypress, cy)
const sessions = sessionsManager.sessions
Cypress.on('run:start', () => {
// @ts-ignore
Object.values(Cypress.state('activeSessions') || {}).forEach((sessionData: Cypress.ServerSessionData) => {
if (sessionData.cacheAcrossSpecs) {
sessionsManager.registeredSessions.set(sessionData.id, true)
}
})
Cypress.on('test:before:run:async', () => {
if (!Cypress.config('testIsolation')) {
return
}
return navigateAboutBlank()
.then(() => sessions.clearCurrentSessionData())
.then(() => Cypress.backend('reset:rendered:html:origins'))
})
})
Commands.addAll({
session (id: string | object, setup: () => void, options: Cypress.SessionOptions = { cacheAcrossSpecs: false }) {
if (!id || !_.isString(id) && !_.isObject(id)) {
$errUtils.throwErrByPath('sessions.session.wrongArgId')
}
// stringify deterministically if we were given an object
id = _.isString(id) ? id : stringifyStable(id)
if (!setup || !_.isFunction(setup)) {
$errUtils.throwErrByPath('sessions.session.wrongArgSetup')
}
// backup session command so we can set it as codeFrame location for errors later on
const sessionCommand = cy.state('current')
const withinSubjectChain = cy.state('withinSubjectChain')
if (options) {
if (!_.isObject(options)) {
$errUtils.throwErrByPath('sessions.session.wrongArgOptions')
}
const validOpts = {
'validate': 'function',
'cacheAcrossSpecs': 'boolean',
}
Object.entries(options).forEach(([key, value]) => {
const expectedType = validOpts[key]
if (!expectedType) {
$errUtils.throwErrByPath('sessions.session.wrongArgOptionUnexpected', { args: { key } })
}
const actualType = typeof value
if (actualType !== expectedType) {
$errUtils.throwErrByPath('sessions.session.wrongArgOptionInvalid', { args: { key, expected: expectedType, actual: actualType } })
}
})
}
let session: Cypress.SessionData = sessionsManager.getActiveSession(id)
const isRegisteredSessionForSpec = sessionsManager.registeredSessions.has(id)
if (session) {
const hasUniqSetupDefinition = session.setup.toString().trim() !== setup.toString().trim()
const hasUniqValidateDefinition = (!!session.validate !== !!options.validate) || (!!session.validate && !!options.validate && session.validate.toString().trim() !== options.validate.toString().trim())
const hasUniqPersistence = session.cacheAcrossSpecs !== !!options.cacheAcrossSpecs
if (isRegisteredSessionForSpec && (hasUniqSetupDefinition || hasUniqValidateDefinition || hasUniqPersistence)) {
$errUtils.throwErrByPath('sessions.session.duplicateId', {
args: {
id,
hasUniqSetupDefinition,
hasUniqValidateDefinition,
hasUniqPersistence,
},
})
}
if (session.cacheAcrossSpecs && _.isString(session.setup)) {
session.setup = setup
}
if (session.cacheAcrossSpecs && session.validate && _.isString(session.validate)) {
session.validate = options.validate
}
} else {
if (isRegisteredSessionForSpec) {
$errUtils.throwErrByPath('sessions.session.duplicateId', { args: { id } })
}
session = sessionsManager.defineSession({
id,
setup,
validate: options.validate,
cacheAcrossSpecs: options.cacheAcrossSpecs,
})
}
function setSessionLogStatus (status: string) {
_log.set({
state: statusMap.commandState(status),
sessionInfo: {
id: session.id,
isGlobalSession: session.cacheAcrossSpecs,
status,
},
})
}
function createSession (existingSession, step: 'create' | 'recreate') {
logGroup(Cypress, {
name: 'session',
displayName: statusMap.stepName(step),
message: '',
type: 'system',
}, (setupLogGroup) => {
return cy.then(async () => {
// Catch when a cypress command fails in the setup function to correctly update log status
// before failing command and ending command queue.
cy.state('onQueueFailed', (err, _queue) => {
if (!_.isObject(err)) {
err = new Error(err)
}
setupLogGroup.set({
state: 'failed',
consoleProps: () => {
return {
Step: statusMap.stepName(step),
Error: err?.stack || err?.message,
}
},
})
setSessionLogStatus('failed')
$errUtils.modifyErrMsg(err, `\n\nThis error occurred while ${statusMap.inProgress(step)} the session. Because the session setup failed, we failed the test.`, _.add)
return err
})
try {
return existingSession.setup()
} finally {
cy.breakSubjectLinksToCurrentChainer()
}
})
.then(async () => {
cy.state('onQueueFailed', null)
const data = await sessions.getCurrentSessionData()
_.extend(existingSession, data)
existingSession.hydrated = true
_log.set({ consoleProps: () => getConsoleProps(existingSession) })
setupLogGroup.set({
consoleProps: () => {
return {
Step: statusMap.stepName(step),
Message: 'The following is the collected session data after the session was successfully setup:',
...getConsoleProps(existingSession),
}
},
})
return
})
})
}
async function restoreSession (testSession) {
Cypress.log({
name: 'session',
displayName: 'Restore saved session',
message: '',
type: 'system',
consoleProps: () => {
return {
Step: 'Restore saved session',
...getConsoleProps(testSession),
}
},
})
_log.set({ consoleProps: () => getConsoleProps(testSession) })
return sessionsManager.setSessionData(testSession)
}
function validateSession (existingSession, step: keyof typeof SESSION_STEPS) {
const isValidSession = true
if (!existingSession.validate) {
return isValidSession
}
return logGroup(Cypress, {
name: 'session',
displayName: 'Validate session',
message: '',
type: 'system',
consoleProps: () => {
return {
Step: 'Validate Session',
}
},
}, (validateLog) => {
return cy.then(async () => {
const isValidSession = true
let caughtCommandErr = false
let _commandToRunAfterValidation
const enhanceErr = (err) => {
Cypress.state('onQueueFailed', null)
if (typeof err !== 'object') {
err = new Error(err)
}
err = $errUtils.enhanceStack({
err,
userInvocationStack: $errUtils.getUserInvocationStack(err, Cypress.state),
projectRoot: Cypress.config('projectRoot'),
})
// show validation error and allow sessions workflow to recreate the session
if (step === 'restore') {
$errUtils.modifyErrMsg(err, `\n\nThis error occurred while validating the restored session. Because validation failed, we will try to recreate the session.`, _.add)
// @ts-ignore
err.isRecovered = true
validateLog.set({
state: 'failed',
consoleProps: () => {
return {
Error: err.stack,
}
},
// explicitly set via .set() so we don't end the log group early
...(!caughtCommandErr && { error: err }),
})
return err
}
setSessionLogStatus('failed')
validateLog.set({
state: 'failed',
consoleProps: () => {
return {
Error: err.stack,
}
},
snapshot: true,
})
$errUtils.modifyErrMsg(err, `\n\nThis error occurred while validating the ${statusMap.complete(step)} session. Because validation failed immediately after ${statusMap.inProgress(step)} the session, we failed the test.`, _.add)
return err
}
cy.state('onQueueFailed', (err, queue): Error => {
if (typeof err !== 'object') {
err = new Error(err)
}
if (step === 'restore') {
const commands = queue.get()
// determine command queue index of _commandToRunAfterValidation's index
let index = _.findIndex(commands, (command: any) => {
return (
_commandToRunAfterValidation
&& command.attributes.chainerId === _commandToRunAfterValidation.chainerId
)
})
// skip all commands between this command which errored and _commandToRunAfterValidation
for (let i = cy.queue.index; i < index; i++) {
commands[i].skip()
}
// restore within subject back to the original subject used when
// the session command kicked off
Cypress.state('withinSubjectChain', withinSubjectChain)
// move to _commandToRunAfterValidation's index to ensure failures are
// handled correctly if next index was not found, the error was caused by
// a sync validation failure and _commandToRunAfterValidation is our next
// cmd
queue.index = index === -1 ? queue.index + 1 : index
err.isRecovered = true
caughtCommandErr = true
}
return enhanceErr(err)
})
let returnVal
try {
returnVal = existingSession.validate.call(cy.state('ctx'))
} catch (err) {
err.onFail = (err) => {
validateLog.set({
error: err,
state: 'failed',
})
}
throw err
}
_commandToRunAfterValidation = cy.then(async () => {
Cypress.state('onQueueFailed', null)
if (caughtCommandErr) {
return !isValidSession
}
const failValidation = (err) => {
if (step === SESSION_STEPS.restore) {
enhanceErr(err)
// move to recreate session flow
return !isValidSession
}
err.onFail = (err) => {
validateLog.error(err)
}
throw enhanceErr(err)
}
// when the validate function returns a promise, ensure it does not return false or throw an error
if ($utils.isPromiseLike(returnVal)) {
return returnVal
.then((val) => {
if (val === false) {
// set current command to cy.session for more accurate codeFrame
cy.state('current', sessionCommand)
throw $errUtils.errByPath('sessions.validate_callback_false', { reason: 'promise resolved false' })
}
return isValidSession
})
.catch((err) => {
if (!(err instanceof Error)) {
// set current command to cy.session for more accurate codeFrame
cy.state('current', sessionCommand)
err = $errUtils.errByPath('sessions.validate_callback_false', { reason: `promise rejected with ${String(err)}` })
}
return failValidation(err)
})
}
if (returnVal === undefined || Cypress.isCy(returnVal)) {
const yielded = cy.state('current').get('prev')?.attributes?.subject
if (yielded === false) {
// set current command to cy.session for more accurate codeframe
cy.state('current', sessionCommand)
return failValidation($errUtils.errByPath('sessions.validate_callback_false', { reason: 'callback yielded false' }))
}
}
// collect all session data again that may have been updated during the validation check
const data = await sessions.getCurrentSessionData()
_.extend(existingSession, data)
validateLog.set({
consoleProps: () => {
return {
Step: 'Validate Session',
Message: 'The following is the collected session data after the session was successfully validated:',
...getConsoleProps(existingSession),
}
},
})
return isValidSession
})
return _commandToRunAfterValidation
})
})
}
/**
* Creates session flow:
* 1. create session
* 2. validate session
*/
const createSessionWorkflow = (existingSession, step: 'create' | 'recreate') => {
return cy.then(async () => {
setSessionLogStatus(statusMap.inProgress(step))
await navigateAboutBlank()
await sessions.clearCurrentSessionData()
return cy.whenStable(() => createSession(existingSession, step))
})
.then(() => validateSession(existingSession, step))
.then(async (isValidSession: boolean) => {
if (!isValidSession) {
return 'failed'
}
sessionsManager.registeredSessions.set(existingSession.id, true)
await sessionsManager.saveSessionData(existingSession)
return statusMap.complete(step)
})
}
/**
* Restore session flow:
* 1. restore session
* 2. validate session
* 3. if validation fails, catch error and recreate session
*/
const restoreSessionWorkflow = (existingSession: Cypress.SessionData) => {
return cy.then(async () => {
setSessionLogStatus(statusMap.inProgress(SESSION_STEPS.restore))
await navigateAboutBlank()
await sessions.clearCurrentSessionData()
return restoreSession(existingSession)
})
.then(() => validateSession(existingSession, SESSION_STEPS.restore))
.then((isValidSession: boolean) => {
if (!isValidSession) {
return createSessionWorkflow(existingSession, SESSION_STEPS.recreate)
}
return statusMap.complete(SESSION_STEPS.restore)
})
}
/**
* Session command rules:
* If session does not exists or was no previously saved to the server, create session
* 1. run create session flow
* 2. clear page
*
* If session exists or has been saved to the server, restore session
* 1. run restore session flow
* 2. clear page
*/
let _log
const groupDetails = {
message: `${session.id.length > 50 ? `${session.id.substring(0, 47)}...` : session.id}`,
}
return logGroup(Cypress, groupDetails, (log) => {
return cy.then(async () => {
_log = log
if (!session.hydrated) {
const serverStoredSession = await sessions.getSession(session.id).catch(_.noop)
// we have a saved session on the server and setup matches
if (serverStoredSession && serverStoredSession.setup === session.setup.toString()) {
_.extend(session, _.omit(serverStoredSession, 'setup', 'validate'))
session.hydrated = true
} else {
return createSessionWorkflow(session, SESSION_STEPS.create)
}
}
return restoreSessionWorkflow(session)
}).then((status: 'created' | 'restored' | 'recreated' | 'failed') => {
return navigateAboutBlank()
.then(() => {
setSessionLogStatus(status)
})
})
})
},
})
Cypress.session = sessions
}