Skip to content

Commit 599e0c8

Browse files
fix(sessions): refactor flows, fix grouping bugs and align validation fail text (#21379)
* chore(sessions): break out sessions manager code * manager manages registered sessions * some unit tests * add more tests and some slight clean up * . * fix run mode issue. * bind correctly for spies * remove types. not sure on the return values. * fix tests * check in dump * add some command tests * more driver tests and fix session config error * Fix parsing error argument * test for failed validation error messges * wait for diff pr * update ui tests * add more ui tests * align with 10.0 test setup * clean up * will add later * fix * fix tests * baseline for session flow tests * test for logs...lots here.... * update log attrs to be able to collect session logs for tests * refactor flows, fix grouping bugs and align validation fail text * update UI test these changes fixed. * fix test failures observed in run mode * reduce flake in log updates
1 parent d4dfd2f commit 599e0c8

File tree

3 files changed

+120
-137
lines changed

3 files changed

+120
-137
lines changed

packages/driver/cypress/integration/commands/sessions/sessions.spec.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ describe('cy.session', { retries: 0 }, () => {
162162

163163
expect(logs[6].get()).to.contain({
164164
name: 'Clear Page',
165-
// FIX ME...
166-
// group: sessionGroupId,
165+
group: sessionGroupId,
167166
})
168167
})
169168

@@ -247,8 +246,7 @@ describe('cy.session', { retries: 0 }, () => {
247246

248247
expect(validateSessionGroup).to.contain({
249248
displayName: 'Validate Session: valid',
250-
// FIXME....
251-
// group: sessionGroupId,
249+
group: sessionGroupId,
252250
})
253251

254252
expect(logs[7].get()).to.deep.contain({
@@ -258,8 +256,7 @@ describe('cy.session', { retries: 0 }, () => {
258256

259257
expect(logs[8].get()).to.contain({
260258
name: 'Clear Page',
261-
// FIXME...
262-
// group: sessionGroupId,
259+
group: sessionGroupId,
263260
})
264261
})
265262
})
@@ -317,11 +314,8 @@ describe('cy.session', { retries: 0 }, () => {
317314
const validateSessionGroup = logs[6].get()
318315

319316
expect(validateSessionGroup).to.contain({
320-
displayName: 'Validate Session',
321-
// FIXME....
322-
// displayName: 'Validate Session: invalid',
323-
// FIXME....
324-
// group: sessionGroupId,
317+
displayName: 'Validate Session: invalid',
318+
group: sessionGroupId,
325319
})
326320

327321
done()
@@ -669,8 +663,7 @@ describe('cy.session', { retries: 0 }, () => {
669663
const secondValidateSessionGroup = logs[11].get()
670664

671665
expect(secondValidateSessionGroup).to.contain({
672-
displayName: 'Validate Session',
673-
// displayName: 'Validate Session: invalid',
666+
displayName: 'Validate Session: invalid',
674667
group: sessionGroupId,
675668
})
676669

packages/driver/src/cy/commands/sessions/index.ts

+97-97
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default function (Commands, Cypress, cy) {
130130
message: `${existingSession.id.length > 50 ? `${existingSession.id.substr(0, 47)}...` : existingSession.id}`,
131131
})
132132

133-
function runSetup (existingSession) {
133+
function createSession (existingSession, recreateSession = false) {
134134
Cypress.log({
135135
name: 'session',
136136
displayName: 'Create New Session',
@@ -141,17 +141,20 @@ export default function (Commands, Cypress, cy) {
141141
groupStart: true,
142142
})
143143

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}`,
153147
}
154148

149+
if (recreateSession) {
150+
renderProps = {
151+
indicator: 'bad',
152+
message: `(recreated) ${_log.get().message}`,
153+
}
154+
}
155+
156+
_log.set({ renderProps: () => renderProps })
157+
155158
return cy.then(async () => {
156159
await navigateAboutBlank()
157160
await sessions.clearCurrentSessionData()
@@ -179,8 +182,44 @@ export default function (Commands, Cypress, cy) {
179182
})
180183
}
181184

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+
182217
// uses Cypress hackery to resolve `false` if validate() resolves/returns false or throws/fails a cypress command.
183218
function validateSession (existingSession, _onFail) {
219+
if (!existingSession.validate) {
220+
return
221+
}
222+
184223
const validatingLog = Cypress.log({
185224
name: 'session',
186225
displayName: 'Validate Session',
@@ -193,19 +232,14 @@ export default function (Commands, Cypress, cy) {
193232
})
194233

195234
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' })
204236

205237
Cypress.log({ groupEnd: true, emitOnly: true })
206238
}
207239

208240
const onFail = (err) => {
241+
validatingLog.set({ displayName: 'Validate Session: invalid' })
242+
209243
_onFail(err, validatingLog)
210244
}
211245

@@ -297,65 +331,66 @@ export default function (Commands, Cypress, cy) {
297331
return _catchCommand
298332
}
299333

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({
312337
showError: true,
313338
type: 'system',
314339
event: true,
315340
name: 'session',
316341
displayName: '',
317342
message: '',
318-
})
343+
}).error(err)
319344

320-
errorLog.error(err)
321-
errorLog.set({
322-
state: 'warn',
345+
log.endGroup()
323346

324-
})
347+
const recreateSession = true
325348

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+
}
334351

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)
336355

337-
hadValidationError = true
356+
cy.fail(err)
357+
}
338358

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)
340366
.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)
350368
})
351369
}
352370

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+
})
357382
}
358383

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+
*/
359394
return cy.then(async () => {
360395
if (!existingSession.hydrated) {
361396
const serverStoredSession = await sessions.getSession(existingSession.id).catch(_.noop)
@@ -365,50 +400,15 @@ export default function (Commands, Cypress, cy) {
365400
_.extend(existingSession, _.omit(serverStoredSession, 'setup'))
366401
existingSession.hydrated = true
367402
} else {
368-
onValidationError = throwValidationError
369-
370-
return runSetup(existingSession)
403+
return createSessionWorkflow(existingSession)
371404
}
372405
}
373406

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)
400408
})
401409
.then(async () => {
410+
await navigateAboutBlank()
402411
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-
}
412412
})
413413
},
414414
})

0 commit comments

Comments
 (0)