Skip to content

Commit 46b35d3

Browse files
authored
fix: the cookie_behavior tests by syncing cookies immediately if … (#25855)
* fix: fix the cookie_behavior tests by syncing cookies immediately if the application is already stable * chore: add changelog entry * [run ci] * chore: address comments from code review
1 parent 59c1175 commit 46b35d3

File tree

3 files changed

+35
-78
lines changed

3 files changed

+35
-78
lines changed

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _Released 03/1/2023 (PENDING)_
1010
**Bugfixes:**
1111

1212
- Fixed an issue where cookies were being duplicated with the same hostname, but a prepended dot. Fixed an issue where cookies may not be expiring correctly. Fixes [#25174](https://github.com/cypress-io/cypress/issues/25174), [#25205](https://github.com/cypress-io/cypress/issues/25205) and [#25495](https://github.com/cypress-io/cypress/issues/25495).
13+
- Fixed an issue where cookies weren't being synced when the application was stable. Fixed in [#25855](https://github.com/cypress-io/cypress/pull/25855). Fixes [#25835](https://github.com/cypress-io/cypress/issues/25835).
1314
- Added missing TypeScript type definitions for the [`cy.reload()`](https://docs.cypress.io/api/commands/reload) command. Addressed in [#25779](https://github.com/cypress-io/cypress/pull/25779).
1415
- Ensure Angular components are mounted inside the correct element. Fixes [#24385](https://github.com/cypress-io/cypress/issues/24385)
1516
- Fix a bug where files outside the project root in a monorepo are not correctly served when using Vite. Addressed in [#25801](https://github.com/cypress-io/cypress/pull/25801)

packages/driver/cypress/e2e/e2e/origin/cookie_behavior.cy.ts

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
251251

252252
// though request is cross origin, site should have access directly to cookie because it is same site
253253
// assert cookie value is actually set in the browser
254-
// current expected assertion. NOTE: This SHOULD be consistent
255-
if (Cypress.isBrowser('firefox')) {
256-
// firefox actually sets the cookie correctly
257-
cy.getCookie('foo1').its('value').should('equal', 'bar1')
258-
} else {
259-
cy.getCookie('foo1').should('equal', null)
260-
}
261-
262-
// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
263-
// future expected assertion
264-
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
254+
cy.getCookie('foo1').its('value').should('equal', 'bar1')
265255

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

299289
// though request is cross origin, site should have access directly to cookie because it is same site
300290
// assert cookie value is actually set in the browser
301-
// current expected assertion. NOTE: This SHOULD be consistent
302-
if (Cypress.isBrowser('firefox')) {
303-
// firefox actually sets the cookie correctly
304-
cy.getCookie('foo1').its('value').should('equal', 'bar1')
305-
} else {
306-
cy.getCookie('foo1').should('equal', null)
307-
}
308-
309-
// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
310-
// future expected assertion
311-
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
291+
cy.getCookie('foo1').its('value').should('equal', 'bar1')
312292

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

378358
// assert cookie value is actually set in the browser
379-
// current expected assertion.
380-
if (Cypress.isBrowser('firefox')) {
381-
// firefox actually sets the cookie correctly
382-
cy.getCookie('foo1').its('value').should('equal', 'bar1')
383-
} else {
384-
cy.getCookie('foo1').should('equal', null)
385-
}
386-
387-
// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
388-
// future expected assertion
389-
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
359+
cy.getCookie('foo1').its('value').should('equal', 'bar1')
390360

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

420390
// assert cookie value is actually set in the browser
421-
// current expected assertion. NOTE: This SHOULD be consistent
422-
if (Cypress.isBrowser('firefox')) {
423-
// firefox actually sets the cookie correctly
424-
cy.getCookie('foo1').its('value').should('equal', 'bar1')
425-
} else {
426-
cy.getCookie('foo1').should('equal', null)
427-
}
428-
429-
// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
430-
// future expected assertion
431-
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
391+
cy.getCookie('foo1').its('value').should('equal', 'bar1')
432392

433393
cy.window().then((win) => {
434394
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/test-request-credentials`, 'fetch'))
@@ -529,14 +489,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
529489
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))
530490
})
531491

532-
// assert cookie value is actually set in the browser
533492
if (scheme === 'https') {
534-
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
535-
cy.getCookie('bar1').should('equal', null)
536-
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https
537-
538-
//expected future assertion
539-
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
493+
// assert cookie value is actually set in the browser, even if in a different domain
494+
cy.getCookie('bar1', {
495+
domain: 'barbaz.com',
496+
}).its('value').should('equal', 'baz1')
540497
} else {
541498
cy.getCookie('bar1').should('equal', null)
542499
}
@@ -572,12 +529,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
572529
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))
573530
})
574531

575-
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
576-
cy.getCookie('bar1').should('equal', null)
577-
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https
578-
579-
//expected future assertion
580-
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
532+
// assert cookie value is actually set in the browser, even if in a different domain
533+
cy.getCookie('bar1', {
534+
domain: 'barbaz.com',
535+
}).its('value').should('equal', 'baz1')
581536

582537
cy.window().then((win) => {
583538
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'xmlHttpRequest', true))
@@ -649,14 +604,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
649604
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'))
650605
})
651606

652-
// assert cookie value is actually set in the browser
653607
if (scheme === 'https') {
654-
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
655-
cy.getCookie('bar1').should('equal', null)
656-
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https
657-
658-
//expected future assertion
659-
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
608+
// assert cookie value is actually set in the browser, even if in a different domain
609+
cy.getCookie('bar1', {
610+
domain: 'barbaz.com',
611+
}).its('value').should('equal', 'baz1')
660612
} else {
661613
cy.getCookie('bar1').should('equal', null)
662614
}
@@ -695,14 +647,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
695647
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'))
696648
})
697649

698-
// assert cookie value is actually set in the browser
699-
700-
// 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.
701-
cy.getCookie('bar1').should('equal', null)
702-
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https
703-
704-
//expected future assertion
705-
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
650+
// assert cookie value is actually set in the browser, even if in a different domain
651+
cy.getCookie('bar1', {
652+
domain: 'barbaz.com',
653+
}).its('value').should('equal', 'baz1')
706654

707655
cy.window().then((win) => {
708656
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'fetch', 'include'))

packages/driver/src/cross-origin/events/cookies.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
2222

2323
waitingToSend = true
2424

25-
// this event allows running a handler before stability is released.
26-
// this prevents subsequent commands from running until the cookies
27-
// are set via automation
28-
// @ts-ignore
29-
Cypress.once('before:stability:release', () => {
25+
const syncCookiesViaAutomation = () => {
3026
const cookies = cookiesToSend
3127

3228
cookiesToSend = []
@@ -37,6 +33,18 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
3733
.catch(() => {
3834
// errors here can be ignored as they're not user-actionable
3935
})
40-
})
36+
}
37+
38+
// if the application is already stable, sync the cookies to the automation client immediately
39+
if (cy.state('isStable')) {
40+
syncCookiesViaAutomation()
41+
} else {
42+
// otherwise, wait until stability is achieved
43+
// this event allows running a handler before stability is released.
44+
// this prevents subsequent commands from running until the cookies
45+
// are set via automation
46+
// @ts-ignore
47+
Cypress.once('before:stability:release', syncCookiesViaAutomation)
48+
}
4149
})
4250
}

0 commit comments

Comments
 (0)