Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sessions): correct when page is cleared in cy.session() #24563

Merged
merged 7 commits into from Nov 15, 2022
11 changes: 3 additions & 8 deletions packages/app/cypress/e2e/runner/sessions.ui.cy.ts
Expand Up @@ -19,10 +19,7 @@ const validateSetupSessionGroup = (isNewSession = true) => {
cy.get('@setupSession').find('.command-expander').click()
cy.get('@setupSession').find('.command-alias').contains('runSetup')

return cy.contains(groupText)
.closest('.command')
.find('.command-name-Clear-page')
.should('have.length', 1)
return cy.get('@setupSession')
}

describe('runner/cypress sessions.ui.spec', {
Expand Down Expand Up @@ -200,7 +197,7 @@ describe('runner/cypress sessions.ui.spec', {
cy.contains('user1')
cy.contains('restored')

cy.get('.command-name-Clear-page').should('have.length', 1)
cy.get('.command-name-Clear-page').should('have.length', 2)

cy.contains('Restore saved session')

Expand Down Expand Up @@ -258,7 +255,7 @@ describe('runner/cypress sessions.ui.spec', {
.find('.command-expander')
.should('have.class', 'command-expander-is-open')

cy.get('.command-name-Clear-page').should('have.length', 2)
cy.get('.command-name-Clear-page').should('have.length', 3)

validateSetupSessionGroup(false)

Expand Down Expand Up @@ -321,8 +318,6 @@ describe('runner/cypress sessions.ui.spec', {
cy.get('.command-name-Clear-page').should('have.length', 2)

validateSetupSessionGroup(false)
.parent()
.closest('.command')
.next()
.contains('Validate session')
.closest('.command').as('secondValidateSession')
Expand Down
75 changes: 31 additions & 44 deletions packages/driver/cypress/e2e/commands/sessions/sessions.cy.js
Expand Up @@ -147,9 +147,6 @@ describe('cy.session', { retries: 0 }, () => {

const handleValidate = () => {
// both create & restore session clears page after running
cy.contains('Default blank page')
cy.contains('This page was cleared by navigating to about:blank.')

cy.visit('/fixtures/auth/index.html')
cy.contains('Welcome tester')
}
Expand Down Expand Up @@ -205,7 +202,7 @@ describe('cy.session', { retries: 0 }, () => {
})

// test must be first to run before blank page visit between each test
it('clears page after setup runs', () => {
it('clears page after command runs', () => {
cy.url().should('eq', 'about:blank')
})

Expand Down Expand Up @@ -242,7 +239,7 @@ describe('cy.session', { retries: 0 }, () => {

expect(logs[5].get()).to.contain({
name: 'Clear page',
group: createNewSessionGroup.id,
group: sessionGroupId,
})
})

Expand Down Expand Up @@ -285,8 +282,8 @@ describe('cy.session', { retries: 0 }, () => {
})

// test must be first to run before blank page visit between each test
it('does not clear page visit from validate function', () => {
cy.url().should('contain', '/fixtures/auth/index.html')
it('clears page after command runs', () => {
cy.url().should('eq', 'about:blank')
})

it('successfully creates new session and validates it', () => {
Expand Down Expand Up @@ -321,22 +318,22 @@ describe('cy.session', { retries: 0 }, () => {
group: createNewSessionGroup.id,
})

expect(logs[5].get()).to.contain({
name: 'Clear page',
group: createNewSessionGroup.id,
})

const validateSessionGroup = logs[6].get()
const validateSessionGroup = logs[5].get()

expect(validateSessionGroup).to.contain({
displayName: 'Validate session',
group: sessionGroupId,
})

expect(logs[7].get()).to.deep.contain({
expect(logs[6].get()).to.deep.contain({
alias: ['validateSession'],
group: validateSessionGroup.id,
})

expect(logs[7].get()).to.contain({
name: 'Clear page',
group: sessionGroupId,
})
})
})

Expand All @@ -348,7 +345,7 @@ describe('cy.session', { retries: 0 }, () => {
cy.once('fail', (err) => {
expect(setup).to.be.calledOnce
expect(validate).to.be.calledOnce
expect(clearPageCount, 'total times session cleared the page').to.eq(2)
expect(clearPageCount, 'total times session cleared the page').to.eq(1)
expect(err.message).to.contain('This error occurred while validating the created session')
expect(logs[0].get()).to.deep.contain({
name: 'session',
Expand All @@ -375,19 +372,14 @@ describe('cy.session', { retries: 0 }, () => {
group: createNewSessionGroup.id,
})

expect(logs[5].get()).to.contain({
name: 'Clear page',
group: createNewSessionGroup.id,
})

const validateSessionGroup = logs[6].get()
const validateSessionGroup = logs[5].get()

expect(validateSessionGroup).to.contain({
displayName: 'Validate session',
group: sessionGroupId,
})

expect(logs[7].get()).to.deep.contain({
expect(logs[6].get()).to.deep.contain({
alias: ['validateSession'],
group: validateSessionGroup.id,
})
Expand Down Expand Up @@ -426,7 +418,7 @@ describe('cy.session', { retries: 0 }, () => {
it('successfully restores saved session', () => {
expect(setup).to.not.be.called
expect(validate).to.not.be.called
expect(clearPageCount, 'total times session cleared the page').to.eq(1)
expect(clearPageCount, 'total times session cleared the page').to.eq(2)
})

it('groups session logs correctly', () => {
Expand Down Expand Up @@ -474,14 +466,14 @@ describe('cy.session', { retries: 0 }, () => {
})

// test must be first to run before blank page visit between each test
it('does not clear page visit from validate function', () => {
cy.url().should('contain', '/fixtures/auth/index.html')
it('clears page after command runs', () => {
cy.url().should('eq', 'about:blank')
})

it('successfully restores saved session', () => {
expect(setup).to.not.be.called
expect(validate).to.be.calledOnce
expect(clearPageCount, 'total times session cleared the page').to.eq(1)
expect(clearPageCount, 'total times session cleared the page').to.eq(2)
})

it('groups session logs correctly', () => {
Expand Down Expand Up @@ -548,8 +540,8 @@ describe('cy.session', { retries: 0 }, () => {
})

// test must be first to run before blank page visit between each test
it('does not clear page visit from validate function', () => {
cy.url().should('contain', '/fixtures/auth/index.html')
it('clears page after command runs', () => {
cy.url().should('eq', 'about:blank')
})

it('successfully recreates session', () => {
Expand Down Expand Up @@ -613,22 +605,22 @@ describe('cy.session', { retries: 0 }, () => {
group: createNewSessionGroup.id,
})

expect(logs[10].get()).to.contain({
name: 'Clear page',
group: createNewSessionGroup.id,
})

const secondValidateSessionGroup = logs[11].get()
const secondValidateSessionGroup = logs[10].get()

expect(secondValidateSessionGroup).to.contain({
displayName: 'Validate session',
group: sessionGroupId,
})

expect(logs[12].get()).to.deep.contain({
expect(logs[11].get()).to.deep.contain({
alias: ['validateSession'],
group: secondValidateSessionGroup.id,
})

expect(logs[12].get()).to.contain({
name: 'Clear page',
group: sessionGroupId,
})
})
})

Expand All @@ -638,7 +630,7 @@ describe('cy.session', { retries: 0 }, () => {
cy.log('Creating new session for test')
cy.session(`session-${Cypress.state('test').id}`, setup, { validate })
.then(() => {
// reset and only test restored session
// reset and only test restored session
resetMocks()
validate.callsFake(() => Promise.reject(false))
})
Expand All @@ -647,7 +639,7 @@ describe('cy.session', { retries: 0 }, () => {
expect(err.message).to.contain('Your `cy.session` **validate** promise rejected with false')
expect(setup).to.be.calledOnce
expect(validate).to.be.calledTwice
expect(clearPageCount, 'total times session cleared the page').to.eq(3)
expect(clearPageCount, 'total times session cleared the page').to.eq(2)

expect(logs[0].get()).to.contain({
name: 'session',
Expand Down Expand Up @@ -703,19 +695,14 @@ describe('cy.session', { retries: 0 }, () => {
group: createNewSessionGroup.id,
})

expect(logs[10].get()).to.contain({
name: 'Clear page',
group: createNewSessionGroup.id,
})

const secondValidateSessionGroup = logs[11].get()
const secondValidateSessionGroup = logs[10].get()

expect(secondValidateSessionGroup).to.contain({
displayName: 'Validate session',
group: sessionGroupId,
})

expect(logs[12].get()).to.deep.contain({
expect(logs[11].get()).to.deep.contain({
alias: ['validateSession'],
group: secondValidateSessionGroup.id,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/cy/commands/sessions/index.ts
Expand Up @@ -196,7 +196,6 @@ export default function (Commands, Cypress, cy) {
})
.then(async () => {
cy.state('onQueueFailed', null)
await navigateAboutBlank()
const data = await sessions.getCurrentSessionData()

_.extend(existingSession, data)
Expand Down Expand Up @@ -512,7 +511,8 @@ export default function (Commands, Cypress, cy) {
}

return restoreSessionWorkflow(session)
}).then(() => {
}).then(async () => {
await navigateAboutBlank()
_log.set({ state: 'passed' })
})
})
Expand Down