diff --git a/packages/driver/cypress/e2e/commands/sessions/sessions.cy.js b/packages/driver/cypress/e2e/commands/sessions/sessions.cy.js index dda1626167d4..120b76b9224d 100644 --- a/packages/driver/cypress/e2e/commands/sessions/sessions.cy.js +++ b/packages/driver/cypress/e2e/commands/sessions/sessions.cy.js @@ -256,7 +256,8 @@ describe('cy.session', { retries: 0 }, () => { const sessionStorageData = consoleProps.groups[0].groups[0] expect(sessionStorageData.name).to.contain('Session Storage - (1)') - expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' }) + expect(sessionStorageData.items).to.have.property('cypressAuthToken') + expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"') }) }) @@ -839,29 +840,39 @@ describe('cy.session', { retries: 0 }, () => { let clearPageCount = 0 let sessionGroupId let setup + let slowSetup let validate - const handleSetup = () => { + const handleSetup = (slowLogin = false) => { // create session clears page before running cy.contains('Default blank page').should('not.exist') cy.visit('/fixtures/auth/index.html') cy.contains('You are not logged in') - cy.window().then((win) => { - win.sessionStorage.setItem('cypressAuthToken', JSON.stringify({ body: { username: 'tester' } })) - }) + cy.get('[data-cy=login-same-origin]').click() + cy.get('input').type('tester') + if (slowLogin) { + cy.get('[data-cy=slow-login]').click() + } else { + cy.get('[data-cy=login]').click() + } } + const handleSlowSetup = () => handleSetup(true) + const handleValidate = () => { - // both create & restore session clears page after running + // both create & restore session clears page after running cy.contains('Default blank page').should('not.exist') - cy.visit('/fixtures/auth/index.html') - cy.contains('Welcome tester') + cy.window() + .its('sessionStorage') + .its('cypressAuthToken', { timeout: 5000 }) + .should('contain', '"username":"tester"') } before(() => { setup = cy.stub().callsFake(handleSetup).as('setupSession') + slowSetup = cy.stub().callsFake(handleSlowSetup).as('setupSlowSession') validate = cy.stub().callsFake(handleValidate).as('validateSession') }) @@ -879,7 +890,7 @@ describe('cy.session', { retries: 0 }, () => { resetMocks() clearAllSavedSessions() cy.on('log:added', (attrs, log) => { - if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.name === 'page load' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('validateSession')) { + if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('setupSlowSession') || attrs.alias?.includes('validateSession')) { logs.push(log) if (!sessionGroupId) { sessionGroupId = attrs.id @@ -974,7 +985,8 @@ describe('cy.session', { retries: 0 }, () => { const sessionStorageData = consoleProps.groups[0].groups[0] expect(sessionStorageData.name).to.contain('Session Storage - (1)') - expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' }) + expect(sessionStorageData.items).to.have.property('cypressAuthToken') + expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"') }) }) @@ -985,7 +997,7 @@ describe('cy.session', { retries: 0 }, () => { setupTestContext() cy.log('Creating new session with validation to test against') sessionId = `session-${Cypress.state('test').id}` - cy.session(sessionId, setup, { validate }) + cy.session(sessionId, slowSetup, { validate }) }) it('does not clear the page after command', () => { @@ -993,7 +1005,7 @@ describe('cy.session', { retries: 0 }, () => { }) it('successfully creates new session and validates it', () => { - expect(setup).to.be.calledOnce + expect(slowSetup).to.be.calledOnce expect(validate).to.be.calledOnce expect(clearPageCount, 'total times session cleared the page').to.eq(0) }) @@ -1024,7 +1036,7 @@ describe('cy.session', { retries: 0 }, () => { }) expect(logs[3].get()).to.deep.contain({ - alias: ['setupSession'], + alias: ['setupSlowSession'], group: createNewSessionGroup.id, }) @@ -1040,6 +1052,24 @@ describe('cy.session', { retries: 0 }, () => { group: validateSessionGroup.id, }) }) + + it('has session details in the consoleProps', () => { + const consoleProps = logs[0].get('consoleProps')() + + expect(consoleProps.Command).to.eq('session') + expect(consoleProps.id).to.eq(sessionId) + expect(consoleProps.Domains).to.eq('This session captured data from localhost.') + + expect(consoleProps.groups).to.have.length(1) + expect(consoleProps.groups[0].name).to.eq('localhost data:') + expect(consoleProps.groups[0].groups).to.have.length(1) + + const sessionStorageData = consoleProps.groups[0].groups[0] + + expect(sessionStorageData.name).to.contain('Session Storage - (1)') + expect(sessionStorageData.items).to.have.property('cypressAuthToken') + expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"') + }) }) describe('create session with failed validation flow', () => { diff --git a/packages/driver/cypress/fixtures/auth/index.html b/packages/driver/cypress/fixtures/auth/index.html index c2406435849a..02f6c680821a 100644 --- a/packages/driver/cypress/fixtures/auth/index.html +++ b/packages/driver/cypress/fixtures/auth/index.html @@ -13,7 +13,7 @@ if (newToken) { - sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken)); + sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken)) urlParams.delete('token') const newSearchParams = urlParams.toString() @@ -43,13 +43,23 @@ // Add Login button that redirects to the idp const loginBtn = document.createElement("button"); - loginBtn.innerHTML = "Login IDP" - loginBtn.dataset.cy = "login-idp" + loginBtn.innerHTML = "Login Same Origin" + loginBtn.dataset.cy = "login-same-origin" loginBtn.onclick = function () { - window.location.href = `http://www.idp.com:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}` + window.location.href = `http://localhost:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}` }; document.body.appendChild(loginBtn); + + // Add Login button that redirects to the idp + const loginIDPBtn = document.createElement("button"); + loginIDPBtn.innerHTML = "Login IDP" + loginIDPBtn.dataset.cy = "login-idp" + loginIDPBtn.onclick = function () { + window.location.href = `http://www.idp.com:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}` + }; + document.body.appendChild(loginIDPBtn); + // Add Login button that redirects to the idp const loginFoobarBtn = document.createElement("button"); loginFoobarBtn.innerHTML = "Login Foobar" diff --git a/packages/driver/src/cy/commands/sessions/index.ts b/packages/driver/src/cy/commands/sessions/index.ts index 32c1fa680f9a..3d168ccd2a54 100644 --- a/packages/driver/src/cy/commands/sessions/index.ts +++ b/packages/driver/src/cy/commands/sessions/index.ts @@ -185,6 +185,8 @@ export default function (Commands, Cypress, cy) { consoleProps: () => { return { Step: statusMap.stepName(step), + Message: 'The following is the collected session data after the session was successfully setup:', + ...getConsoleProps(existingSession), } }, @@ -396,6 +398,20 @@ export default function (Commands, Cypress, cy) { } } + // 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 })