Skip to content

Commit ae5cbb5

Browse files
fix: persist registered spec sessions (#23347)
1 parent 7489561 commit ae5cbb5

File tree

8 files changed

+100
-80
lines changed

8 files changed

+100
-80
lines changed

packages/app/cypress/e2e/runner/sessions.ui.cy.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,51 +131,68 @@ describe('runner/cypress sessions.ui.spec', {
131131
cy.percySnapshot()
132132
})
133133

134-
it('restores saved session', () => {
135-
loadSpec({
136-
projectName: 'session-and-origin-e2e-specs',
137-
filePath: 'session/restores_saved_session.cy.js',
138-
passCount: 2,
134+
describe('restores saved session', () => {
135+
beforeEach(() => {
136+
loadSpec({
137+
projectName: 'session-and-origin-e2e-specs',
138+
filePath: 'session/restores_saved_session.cy.js',
139+
passCount: 5,
140+
failCount: 1,
141+
})
139142
})
140143

141-
cy.get('.test').each(($el) => cy.wrap($el).click())
144+
it('restores session as expected', () => {
145+
cy.get('.test').each(($el) => cy.wrap($el).click())
142146

143-
cy.log('validate new session was created in first test')
144-
cy.get('.test').eq(0).within(() => {
145-
validateSessionsInstrumentPanel(['user1'])
146-
cy.get('.command-name-session').contains('created')
147-
})
147+
cy.log('validate new session was created in first test')
148+
cy.get('.test').eq(0).within(() => {
149+
validateSessionsInstrumentPanel(['user1'])
150+
cy.get('.command-name-session').contains('created')
151+
})
148152

149-
cy.log('validate saved session was used in second test')
150-
cy.get('.test').eq(1).within(() => {
151-
validateSessionsInstrumentPanel(['user1'])
153+
cy.log('validate saved session was used in second test')
154+
cy.get('.test').eq(1).within(() => {
155+
validateSessionsInstrumentPanel(['user1'])
152156

153-
cy.get('.command-name-session')
154-
.within(() => {
155-
cy.get('.command-expander').first().click()
156-
cy.contains('user1')
157-
cy.contains('restored')
157+
cy.get('.command-name-session')
158+
.within(() => {
159+
cy.get('.command-expander').first().click()
160+
cy.contains('user1')
161+
cy.contains('restored')
158162

159-
cy.get('.command-name-Clear-page').should('have.length', 2)
163+
cy.get('.command-name-Clear-page').should('have.length', 2)
160164

161-
cy.contains('Restore saved session')
165+
cy.contains('Restore saved session')
162166

163-
cy.contains('Validate session')
164-
.closest('.command').as('validateSession')
167+
cy.contains('Validate session')
168+
.closest('.command').as('validateSession')
165169

166-
cy.get('@validateSession')
167-
.find('.command-expander')
168-
.should('not.have.class', 'command-expander-is-open')
169-
.click()
170+
cy.get('@validateSession')
171+
.find('.command-expander')
172+
.should('not.have.class', 'command-expander-is-open')
173+
.click()
170174

171-
cy.get('@validateSession')
172-
.find('.command-alias')
173-
.contains('runValidation')
175+
cy.get('@validateSession')
176+
.find('.command-alias')
177+
.contains('runValidation')
178+
})
179+
180+
cy.get('.command-name-session').get('.command-expander').first().click()
181+
182+
cy.get('.command').should('have.length', 2)
174183
})
184+
})
175185

176-
cy.get('.command-name-session').get('.command-expander').first().click()
186+
// https://github.com/cypress-io/cypress/issues/22381
187+
it('ensures sessionid integrity is maintained across tests', () => {
188+
cy.contains('test sessionid integrity is maintained').closest('.runnable').should('have.class', 'runnable-failed')
189+
cy.get('.test').should('have.length', 6)
177190

178-
cy.get('.command').should('have.length', 2)
191+
cy.get('.test').eq(2).should('have.class', 'runnable-passed')
192+
cy.get('.test').eq(3).should('have.class', 'runnable-passed')
193+
cy.get('.test').eq(4).should('have.class', 'runnable-passed')
194+
cy.get('.test').eq(5).should('have.class', 'runnable-failed')
195+
cy.contains('If you want to specify different options, please use a unique name').should('exist')
179196
})
180197
})
181198

packages/driver/cypress/e2e/commands/sessions/manager.cy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('src/cy/commands/sessions/manager.ts', () => {
1616

1717
expect(sessionsManager).to.haveOwnProperty('cy')
1818
expect(sessionsManager).to.haveOwnProperty('Cypress')
19-
expect(sessionsManager).to.haveOwnProperty('currentTestRegisteredSessions')
20-
expect(sessionsManager.currentTestRegisteredSessions).to.be.instanceOf(Map)
19+
expect(sessionsManager).to.haveOwnProperty('registeredSessions')
20+
expect(sessionsManager.registeredSessions).to.be.instanceOf(Map)
2121
})
2222

2323
describe('.setActiveSession()', () => {

packages/driver/cypress/e2e/commands/sessions/sessions.cy.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('cy.session', { retries: 0 }, () => {
2424

2525
it('accepts array as id', () => {
2626
cy.session('session-id', () => {})
27+
cy.session('session-id')
2728
})
2829

2930
it('accepts object as id', () => {
@@ -35,7 +36,7 @@ describe('cy.session', { retries: 0 }, () => {
3536
const setup = cy.stub().as('setupSession')
3637
const validate = cy.stub().as('validateSession')
3738

38-
cy.session('session-id', setup, { validate })
39+
cy.session('session-id-3rd-arg', setup, { validate })
3940
cy.then(() => {
4041
expect(setup).to.be.calledOnce
4142
expect(validate).to.be.calledOnce
@@ -236,7 +237,7 @@ describe('cy.session', { retries: 0 }, () => {
236237
setupTestContext()
237238
cy.log('Creating new session with validation to test against')
238239

239-
cy.session('session-1', setup, { validate })
240+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
240241
cy.url().should('eq', 'about:blank')
241242
})
242243

@@ -366,22 +367,22 @@ describe('cy.session', { retries: 0 }, () => {
366367

367368
validate.callsFake(() => false)
368369

369-
cy.session('session-1', setup, { validate })
370+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
370371
})
371372
})
372373

373374
describe('restores saved session flow', () => {
374375
before(() => {
375376
setupTestContext()
376377
cy.log('Creating new session for test')
377-
cy.session('session-1', setup)
378+
cy.session(`session-${Cypress.state('test').id}`, setup)
378379
.then(() => {
379380
// reset and only test restored session
380381
resetMocks()
381382
})
382383

383384
cy.log('restore session to test against')
384-
cy.session('session-1', setup)
385+
cy.session(`session-${Cypress.state('test').id}`, setup)
385386
cy.url().should('eq', 'about:blank')
386387
})
387388

@@ -433,14 +434,14 @@ describe('cy.session', { retries: 0 }, () => {
433434
before(() => {
434435
setupTestContext()
435436
cy.log('Creating new session for test')
436-
cy.session('session-1', setup, { validate })
437+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
437438
.then(() => {
438439
// reset and only test restored session
439440
resetMocks()
440441
})
441442

442443
cy.log('restore session to test against')
443-
cy.session('session-1', setup, { validate })
444+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
444445
cy.url().should('eq', 'about:blank')
445446
})
446447

@@ -504,7 +505,7 @@ describe('cy.session', { retries: 0 }, () => {
504505
before(() => {
505506
setupTestContext()
506507
cy.log('Creating new session for test')
507-
cy.session('session-1', setup, { validate })
508+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
508509
.then(() => {
509510
// reset and only test restored session
510511
resetMocks()
@@ -516,7 +517,7 @@ describe('cy.session', { retries: 0 }, () => {
516517
})
517518

518519
cy.log('restore session to test against')
519-
cy.session('session-1', setup, { validate })
520+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
520521
cy.url().should('eq', 'about:blank')
521522
})
522523

@@ -627,7 +628,7 @@ describe('cy.session', { retries: 0 }, () => {
627628
it('fails to recreate session and logs correctly', function (done) {
628629
setupTestContext()
629630
cy.log('Creating new session for test')
630-
cy.session('session-1', setup, { validate })
631+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
631632
.then(() => {
632633
// reset and only test restored session
633634
resetMocks()
@@ -733,7 +734,7 @@ describe('cy.session', { retries: 0 }, () => {
733734
})
734735

735736
cy.log('restore session to test against')
736-
cy.session('session-1', setup, { validate })
737+
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
737738
})
738739
})
739740
})

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export default function (Commands, Cypress, cy) {
4040
Cypress.on('run:start', () => {
4141
Cypress.on('test:before:run:async', () => {
4242
if (Cypress.config('experimentalSessionAndOrigin')) {
43-
sessionsManager.currentTestRegisteredSessions.clear()
44-
4543
const clearPage = Cypress.config('testIsolation') === 'strict' ? navigateAboutBlank(false) : new Cypress.Promise.resolve()
4644

4745
return clearPage
@@ -92,18 +90,18 @@ export default function (Commands, Cypress, cy) {
9290
}
9391

9492
let existingSession: SessionData = sessionsManager.getActiveSession(id)
95-
const isRegisteredSessionForTest = sessionsManager.currentTestRegisteredSessions.has(id)
93+
const isRegisteredSessionForSpec = sessionsManager.registeredSessions.has(id)
9694

9795
if (!setup) {
98-
if (!existingSession || !isRegisteredSessionForTest) {
96+
if (!existingSession || !isRegisteredSessionForSpec) {
9997
$errUtils.throwErrByPath('sessions.session.not_found', { args: { id } })
10098
}
10199
} else {
102100
const isUniqSessionDefinition = !existingSession || existingSession.setup.toString().trim() !== setup.toString().trim()
103101

104102
if (isUniqSessionDefinition) {
105-
if (isRegisteredSessionForTest) {
106-
$errUtils.throwErrByPath('sessions.session.duplicateId', { args: { id: existingSession.id } })
103+
if (isRegisteredSessionForSpec) {
104+
$errUtils.throwErrByPath('sessions.session.duplicateId', { args: { id } })
107105
}
108106

109107
existingSession = sessions.defineSession({
@@ -112,7 +110,7 @@ export default function (Commands, Cypress, cy) {
112110
validate: options.validate,
113111
})
114112

115-
sessionsManager.currentTestRegisteredSessions.set(id, true)
113+
sessionsManager.registeredSessions.set(id, true)
116114
}
117115
}
118116

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const getLogProperties = (displayName) => {
2525
export default class SessionsManager {
2626
Cypress
2727
cy
28-
currentTestRegisteredSessions = new Map()
28+
registeredSessions = new Map()
2929

3030
constructor (Cypress, cy) {
3131
this.Cypress = Cypress

system-tests/__snapshots__/session_spec.ts.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ exports['e2e sessions / session tests'] = `
8686
can wait for login redirect automatically
8787
✓ t1
8888
89-
can wait for a js redirect with an assertion
90-
✓ t1
91-
9289
same session name, different options, multiple tests
9390
✓ t1
9491
✓ t2
@@ -107,15 +104,15 @@ exports['e2e sessions / session tests'] = `
107104
✓ clears only secure context data - 2/2
108105
109106
110-
43 passing
107+
42 passing
111108
1 pending
112109
113110
114111
(Results)
115112
116113
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
117-
│ Tests: 44
118-
│ Passing: 43
114+
│ Tests: 43
115+
│ Passing: 42
119116
│ Failing: 0 │
120117
│ Pending: 1 │
121118
│ Skipped: 0 │
@@ -133,9 +130,9 @@ exports['e2e sessions / session tests'] = `
133130
134131
Spec Tests Passing Failing Pending Skipped
135132
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
136-
│ ✔ session.cy.js XX:XX 44 43 - 1 - │
133+
│ ✔ session.cy.js XX:XX 43 42 - 1 - │
137134
└────────────────────────────────────────────────────────────────────────────────────────────────┘
138-
✔ All specs passed! XX:XX 44 43 - 1 -
135+
✔ All specs passed! XX:XX 43 42 - 1 -
139136
140137
141138
`

system-tests/projects/e2e/cypress/e2e/session.cy.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ describe('options.validate reruns steps when rejecting', () => {
405405
})
406406

407407
describe('options.validate reruns steps when throwing', () => {
408-
SuiteWithValidateFn('validate_reject', (callCount) => {
408+
SuiteWithValidateFn('validate_throw', (callCount) => {
409409
if (callCount === 2) {
410410
throw new Error('validate error')
411411
}
@@ -460,22 +460,6 @@ describe('can wait for login redirect automatically', () => {
460460
cy.session('redirect-login', () => {
461461
cy.visit('https://localhost:4466/form')
462462
cy.get('[name="delay"]').type('100{enter}')
463-
// not needed since cypress will pause command queue during the redirect
464-
// cy.url().should('include', '/home')
465-
})
466-
467-
expectCurrentSessionData({
468-
cookies: ['/form', '/home'],
469-
})
470-
})
471-
})
472-
473-
describe('can wait for a js redirect with an assertion', () => {
474-
it('t1', () => {
475-
cy.session('redirect-login', () => {
476-
cy.visit('https://localhost:4466/form')
477-
cy.get('[name="delay"]').type('100{enter}')
478-
// cy.url().should('include', '/home')
479463
})
480464

481465
expectCurrentSessionData({
@@ -486,7 +470,7 @@ describe('can wait for a js redirect with an assertion', () => {
486470

487471
describe('same session name, different options, multiple tests', () => {
488472
it('t1', () => {
489-
cy.session('bob', () => {
473+
cy.session('bob_1', () => {
490474
localStorage.bob = '1'
491475
})
492476
.then(() => {
@@ -495,7 +479,7 @@ describe('same session name, different options, multiple tests', () => {
495479
})
496480

497481
it('t2', () => {
498-
cy.session('bob', () => {
482+
cy.session('bob_2', () => {
499483
localStorage.bob = '2'
500484
})
501485
.then(() => {

system-tests/projects/session-and-origin-e2e-specs/cypress/e2e/session/restores_saved_session.cy.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ it('t2', () => {
2121

2222
cy.log('after')
2323
})
24+
25+
// https://github.com/cypress-io/cypress/issues/22381
26+
describe('test sessionid integrity is maintained', () => {
27+
it('use same session 2x and 2nd does not provide setup', () => {
28+
cy.session('session-2', setupFn)
29+
cy.session('session-2')
30+
})
31+
32+
it('restore prev session 2x and 2nd does not provide setup', () => {
33+
cy.session('session-2', setupFn)
34+
cy.session('session-2')
35+
})
36+
37+
it('restore prev session without setup', () => {
38+
cy.session('session-2')
39+
})
40+
41+
it('fails when trying to use existing sessionid with diff args', () => {
42+
cy.session('session-2', () => {
43+
// something else
44+
})
45+
})
46+
})

0 commit comments

Comments
 (0)