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: the cookie_behavior tests by syncing cookies immediately if … #25855

Merged
merged 7 commits into from
Feb 20, 2023
Next Next commit
fix: fix the cookie_behavior tests by syncing cookies immediately if …
…the application is already stable
  • Loading branch information
AtofStryker committed Feb 16, 2023
commit 5de89c793335b5921b0bf6ab2ae8da772bcdae3a
92 changes: 20 additions & 72 deletions packages/driver/cypress/e2e/e2e/origin/cookie_behavior.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {

// though request is cross origin, site should have access directly to cookie because it is same site
// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
// but send the cookies in the request
Expand Down Expand Up @@ -298,17 +288,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {

// though request is cross origin, site should have access directly to cookie because it is same site
// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
// but send the cookies in the request
Expand Down Expand Up @@ -376,17 +356,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
})

// assert cookie value is actually set in the browser
// current expected assertion.
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/test-request-credentials`, 'fetch', 'include'))
Expand Down Expand Up @@ -418,17 +388,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
})

// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/test-request-credentials`, 'fetch'))
Expand Down Expand Up @@ -529,14 +489,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand Down Expand Up @@ -572,12 +529,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'xmlHttpRequest', true))
Expand Down Expand Up @@ -649,14 +604,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand Down Expand Up @@ -695,14 +647,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser

// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'fetch', 'include'))
Expand Down
18 changes: 15 additions & 3 deletions packages/driver/src/cross-origin/events/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
// this event allows running a handler before stability is released.
// this prevents subsequent commands from running until the cookies
// are set via automation
AtofStryker marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
Cypress.once('before:stability:release', () => {

const addCookiesCallback = () => {
AtofStryker marked this conversation as resolved.
Show resolved Hide resolved
const cookies = cookiesToSend

cookiesToSend = []
Expand All @@ -37,6 +37,18 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
.catch(() => {
// errors here can be ignored as they're not user-actionable
})
})
}

// if the application is already stable, sync the cookies to the automation client immediately
if (cy.state('isStable')) {
addCookiesCallback()
} else {
// otherwise, wait until stability is achieved
// this event allows running a handler before stability is released.
// this prevents subsequent commands from running until the cookies
// are set via automation
// @ts-ignore
Cypress.once('before:stability:release', addCookiesCallback)
}
})
}