Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(cookie): export/import chips cookies (#36168)
Yury Semikhatskyc930e6763d62
diff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.mdindex add918f234..f9dda43e11 100644--- a/docs/src/api/class-browsercontext.md+++ b/docs/src/api/class-browsercontext.md@@ -363,6 +363,7 @@ await context.AddCookiesAsync(new[] { cookie1, cookie2 });- `httpOnly` ?<[boolean]> Optional.- `secure` ?<[boolean]> Optional.- `sameSite` ?<[SameSiteAttribute]<"Strict"|"Lax"|"None">> Optional.+ - `partitionKey` ?<[string]> For partitioned third-party cookies (aka [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the partition key. Optional.## async method: BrowserContext.addInitScript* since: v1.8@@ -602,6 +603,7 @@ The default browser context cannot be closed.- `httpOnly` <[boolean]>- `secure` <[boolean]>- `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">>+ - `partitionKey` ?<[string]>If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLsare returned.@@ -1504,6 +1506,7 @@ Whether to emulate network being offline for the browser context.- `httpOnly` <[boolean]>- `secure` <[boolean]>- `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">>+ - `partitionKey` ?<[string]>- `origins` <[Array]<[Object]>>- `origin` <[string]>- `localStorage` <[Array]<[Object]>>diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.tsindex aaf8a14c42..a0f466d11b 100644--- a/packages/playwright-client/types/types.d.ts+++ b/packages/playwright-client/types/types.d.ts@@ -8830,6 +8830,13 @@ export interface BrowserContext {* Optional.*/sameSite?: "Strict"|"Lax"|"None";++ /**+ * For partitioned third-party cookies (aka+ * [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the+ * partition key. Optional.+ */+ partitionKey?: string;}>): Promise<void>;/**@@ -9305,6 +9312,8 @@ export interface BrowserContext {secure: boolean;sameSite: "Strict"|"Lax"|"None";++ partitionKey?: string;}>;origins: Array<{@@ -22502,6 +22511,8 @@ export interface Cookie {secure: boolean;sameSite: "Strict"|"Lax"|"None";++ partitionKey?: string;}interface PageWaitForSelectorOptions {diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.tsindex f3de08b065..71903879ce 100644--- a/packages/playwright-core/src/protocol/validator.ts+++ b/packages/playwright-core/src/protocol/validator.ts@@ -136,6 +136,8 @@ scheme.SetNetworkCookie = tObject({httpOnly: tOptional(tBoolean),secure: tOptional(tBoolean),sameSite: tOptional(tEnum(['Strict', 'Lax', 'None'])),+ partitionKey: tOptional(tString),+ _crHasCrossSiteAncestor: tOptional(tBoolean),});scheme.NetworkCookie = tObject({name: tString,@@ -146,6 +148,8 @@ scheme.NetworkCookie = tObject({httpOnly: tBoolean,secure: tBoolean,sameSite: tEnum(['Strict', 'Lax', 'None']),+ partitionKey: tOptional(tString),+ _crHasCrossSiteAncestor: tOptional(tBoolean),});scheme.NameValue = tObject({name: tString,diff --git a/packages/playwright-core/src/server/chromium/crBrowser.ts b/packages/playwright-core/src/server/chromium/crBrowser.tsindex 3cb4b7bd74..0557ea11cf 100644--- a/packages/playwright-core/src/server/chromium/crBrowser.ts+++ b/packages/playwright-core/src/server/chromium/crBrowser.ts@@ -381,19 +381,56 @@ export class CRBrowserContext extends BrowserContext {async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {const { cookies } = await this._browser._session.send('Storage.getCookies', { browserContextId: this._browserContextId });return network.filterCookies(cookies.map(c => {- const copy: any = { sameSite: 'Lax', ...c };- delete copy.size;- delete copy.priority;- delete copy.session;- delete copy.sameParty;- delete copy.sourceScheme;- delete copy.sourcePort;- return copy as channels.NetworkCookie;+ const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;+ const copy: channels.NetworkCookie = {+ name,+ value,+ domain,+ path,+ expires,+ httpOnly,+ secure,+ sameSite: sameSite ?? 'Lax',+ };+ // If hasCrossSiteAncestor is false, the cookie is a partitioned first party cookie,+ // this is Chromium specific, see https://chromestatus.com/feature/5144832583663616+ // and https://github.com/explainers-by-googlers/CHIPS-spec.+ if (c.partitionKey) {+ copy._crHasCrossSiteAncestor = c.partitionKey.hasCrossSiteAncestor;+ copy.partitionKey = c.partitionKey.topLevelSite;+ }+ return copy;}), urls);}async addCookies(cookies: channels.SetNetworkCookie[]) {- await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId });+ function toChromiumCookie(cookie: channels.SetNetworkCookie) {+ const { name, value, url, domain, path, expires, httpOnly, secure, sameSite, partitionKey, _crHasCrossSiteAncestor } = cookie;+ const copy: Protocol.Network.CookieParam = {+ name,+ value,+ url,+ domain,+ path,+ expires,+ httpOnly,+ secure,+ sameSite+ };+ if (partitionKey) {+ copy.partitionKey = {+ topLevelSite: partitionKey,+ // _crHasCrossSiteAncestor is non-standard, set it true by default if the cookie is partitioned.+ hasCrossSiteAncestor: _crHasCrossSiteAncestor ?? true,+ };+ }+ return copy;+ }++ await this._browser._session.send('Storage.setCookies', {+ cookies: network.rewriteCookies(cookies).map(toChromiumCookie),+ browserContextId: this._browserContextId+ });}async doClearCookies() {diff --git a/packages/playwright-core/src/server/firefox/ffBrowser.ts b/packages/playwright-core/src/server/firefox/ffBrowser.tsindex 6acfa8aa4e..4b529d0ad8 100644--- a/packages/playwright-core/src/server/firefox/ffBrowser.ts+++ b/packages/playwright-core/src/server/firefox/ffBrowser.ts@@ -297,18 +297,35 @@ export class FFBrowserContext extends BrowserContext {async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {const { cookies } = await this._browser.session.send('Browser.getCookies', { browserContextId: this._browserContextId });return network.filterCookies(cookies.map(c => {- const copy: any = { ... c };- delete copy.size;- delete copy.session;- return copy as channels.NetworkCookie;+ const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;+ return {+ name,+ value,+ domain,+ path,+ expires,+ httpOnly,+ secure,+ sameSite,+ };}), urls);}async addCookies(cookies: channels.SetNetworkCookie[]) {- const cc = network.rewriteCookies(cookies).map(c => ({- ...c,- expires: c.expires === -1 ? undefined : c.expires,- }));+ const cc = network.rewriteCookies(cookies).map(c => {+ const { name, value, url, domain, path, expires, httpOnly, secure, sameSite } = c;+ return {+ name,+ value,+ url,+ domain,+ path,+ expires: expires === -1 ? undefined : expires,+ httpOnly,+ secure,+ sameSite+ };+ });await this._browser.session.send('Browser.setCookies', { browserContextId: this._browserContextId, cookies: cc });}diff --git a/packages/playwright-core/src/server/webkit/wkBrowser.ts b/packages/playwright-core/src/server/webkit/wkBrowser.tsindex 4cdeb3a353..aaf4c197a4 100644--- a/packages/playwright-core/src/server/webkit/wkBrowser.ts+++ b/packages/playwright-core/src/server/webkit/wkBrowser.ts@@ -255,19 +255,37 @@ export class WKBrowserContext extends BrowserContext {async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {const { cookies } = await this._browser._browserSession.send('Playwright.getAllCookies', { browserContextId: this._browserContextId });return network.filterCookies(cookies.map((c: channels.NetworkCookie) => {- const copy: any = { ... c };- copy.expires = c.expires === -1 ? -1 : c.expires / 1000;- delete copy.session;- return copy as channels.NetworkCookie;+ const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;+ const copy: channels.NetworkCookie = {+ name,+ value,+ domain,+ path,+ expires: expires === -1 ? -1 : expires / 1000,+ httpOnly,+ secure,+ sameSite,+ };+ return copy;}), urls);}async addCookies(cookies: channels.SetNetworkCookie[]) {- const cc = network.rewriteCookies(cookies).map(c => ({- ...c,- session: c.expires === -1 || c.expires === undefined,- expires: c.expires && c.expires !== -1 ? c.expires * 1000 : c.expires,- })) as Protocol.Playwright.SetCookieParam[];+ const cc = network.rewriteCookies(cookies).map(c => {+ const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;+ const copy: Protocol.Playwright.SetCookieParam = {+ name,+ value,+ domain: domain!,+ path: path!,+ expires: expires && expires !== -1 ? expires * 1000 : expires,+ httpOnly,+ secure,+ sameSite,+ session: expires === -1 || expires === undefined,+ };+ return copy;+ });await this._browser._browserSession.send('Playwright.setCookies', { cookies: cc, browserContextId: this._browserContextId });}diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.tsindex aaf8a14c42..a0f466d11b 100644--- a/packages/playwright-core/types/types.d.ts+++ b/packages/playwright-core/types/types.d.ts@@ -8830,6 +8830,13 @@ export interface BrowserContext {* Optional.*/sameSite?: "Strict"|"Lax"|"None";++ /**+ * For partitioned third-party cookies (aka+ * [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the+ * partition key. Optional.+ */+ partitionKey?: string;}>): Promise<void>;/**@@ -9305,6 +9312,8 @@ export interface BrowserContext {secure: boolean;sameSite: "Strict"|"Lax"|"None";++ partitionKey?: string;}>;origins: Array<{@@ -22502,6 +22511,8 @@ export interface Cookie {secure: boolean;sameSite: "Strict"|"Lax"|"None";++ partitionKey?: string;}interface PageWaitForSelectorOptions {diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.tsindex 7831b4e26e..90ab1680d9 100644--- a/packages/protocol/src/channels.d.ts+++ b/packages/protocol/src/channels.d.ts@@ -260,6 +260,8 @@ export type SetNetworkCookie = {httpOnly?: boolean,secure?: boolean,sameSite?: 'Strict' | 'Lax' | 'None',+ partitionKey?: string,+ _crHasCrossSiteAncestor?: boolean,};export type NetworkCookie = {@@ -271,6 +273,8 @@ export type NetworkCookie = {httpOnly: boolean,secure: boolean,sameSite: 'Strict' | 'Lax' | 'None',+ partitionKey?: string,+ _crHasCrossSiteAncestor?: boolean,};export type NameValue = {diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.ymlindex 805c0396d6..8e85b73422 100644--- a/packages/protocol/src/protocol.yml+++ b/packages/protocol/src/protocol.yml@@ -223,6 +223,8 @@ SetNetworkCookie:- Strict- Lax- None+ partitionKey: string?+ _crHasCrossSiteAncestor: boolean?NetworkCookie:@@ -241,6 +243,8 @@ NetworkCookie:- Strict- Lax- None+ partitionKey: string?+ _crHasCrossSiteAncestor: boolean?NameValue:diff --git a/tests/library/browsercontext-cookies-third-party.spec.ts b/tests/library/browsercontext-cookies-third-party.spec.tsindex 0fbce763ed..fcb8083057 100644--- a/tests/library/browsercontext-cookies-third-party.spec.ts+++ b/tests/library/browsercontext-cookies-third-party.spec.ts@@ -14,73 +14,210 @@* limitations under the License.*/-import { contextTest as test, expect } from '../config/browserTest';+import { contextTest, expect } from '../config/browserTest';-import type { Page, BrowserContext } from 'playwright';+import type { Page, BrowserContext, Cookie } from 'playwright';import type { TestServer } from '../config/testserver';+type TestUrls = {+ origin1: string;+ origin2: string;+ read_origin1: string;+ read_origin2_origin1: string;+ read_origin1_origin1: string;+ read_origin1_origin2_origin1: string;+ set_origin1: string;+ set_origin2_origin1: string;+ set_origin1_origin2_origin1: string;+};++const test = contextTest.extend<{ urls: TestUrls }>({+ urls: async ({ httpsServer }, run) => {+ const origin1 = httpsServer.PREFIX;+ const origin2 = httpsServer.CROSS_PROCESS_PREFIX;+ await run({+ origin1,+ origin2,+ read_origin1: origin1 + '/read-cookie.html',+ read_origin2_origin1: origin2 + '/frame-read-cookie.html',+ read_origin1_origin1: origin1 + '/frame-read-cookie.html',+ read_origin1_origin2_origin1: origin1 + '/nested-frame-read-cookie.html',+ set_origin1: origin1 + '/set-cookie.html',+ set_origin2_origin1: origin2 + '/frame-set-cookie.html',+ set_origin1_origin2_origin1: origin1 + '/nested-frame-set-cookie.html',+ });+ },+});+test.use({ignoreHTTPSErrors: true,});-test(`third party non-partitioned cookies`, async ({ page, browserName, httpsServer, isMac }) => {- httpsServer.setRoute('/empty.html', (req, res) => {- res.setHeader('Set-Cookie', `name=value; SameSite=None; Path=/; Secure;`);+/**+ * origin1:+ * top-level-partitioned=value+ * top-level-non-partitioned=value+ *+ * origin2:+ * origin1:+ * frame-partitioned=value+ * frame-non-partitioned=value+ *+ * origin1:+ * origin2:+ * origin1:+ * frame-partitioned=value+ * frame-non-partitioned=value+ *+ * origin1 = httpsServer.PREFIX+ * origin2 = httpsServer.CROSS_PROCESS_PREFIX+ */+function addCommonCookieHandlers(httpsServer: TestServer, urls: TestUrls) {+ // '/set-cookie.html' handlers are added in the tests.+ httpsServer.setRoute('/read-cookie.html', (req, res) => {res.setHeader('Content-Type', 'text/html');const cookies = req.headers.cookie?.split(';').map(c => c.trim()).sort().join('; ');res.end(`Received cookie: ${cookies}`);});- httpsServer.setRoute('/with-frame.html', (req, res) => {+ httpsServer.setRoute('/frame-set-cookie.html', (req, res) => {+ res.setHeader('Content-Type', 'text/html');+ res.end(`<iframe src='${urls.origin1}/set-cookie.html'></iframe>`);+ });+ httpsServer.setRoute('/frame-read-cookie.html', (req, res) => {+ res.setHeader('Content-Type', 'text/html');+ res.end(`<iframe src='${urls.origin1}/read-cookie.html'></iframe>`);+ });+ // Nested cross-origin iframe:+ // main frame: (origin1 or origin2) -> iframe1: origin2 -> iframe2: origin1+ httpsServer.setRoute('/nested-frame-set-cookie.html', (req, res) => {res.setHeader('Content-Type', 'text/html');- res.end(`<iframe src='${httpsServer.PREFIX}/empty.html'></iframe>`);+ res.end(`<iframe src='${urls.origin2}/frame-set-cookie.html'></iframe>`);});+ httpsServer.setRoute('/nested-frame-read-cookie.html', (req, res) => {+ res.setHeader('Content-Type', 'text/html');+ res.end(`<iframe src='${urls.origin2}/frame-read-cookie.html'></iframe>`);+ });+}- await page.goto(httpsServer.EMPTY_PAGE);- await page.goto(httpsServer.EMPTY_PAGE);- expect(await page.locator('body').textContent()).toBe('Received cookie: name=value');+function findCookie(cookies: Cookie[], name: string) {+ const result = cookies.find(cookie => cookie.name === name);+ expect(result, `Cookie ${name} not found in ${JSON.stringify(cookies, null, 2)}`).toBeTruthy();+ return result;+}- await page.goto(httpsServer.CROSS_PROCESS_PREFIX + '/with-frame.html');+function expectPartitionKey(cookies: Cookie[], name: string, partitionKey: string) {+ const cookie = findCookie(cookies, name);+ if (partitionKey !== cookie.partitionKey)+ throw new Error(`Cookie ${name} has partitionKey ${cookie.partitionKey} but expected ${partitionKey}.`);+}++async function runNonPartitionedTest(page: Page, httpsServer: TestServer, browserName: string, isMac: boolean, isLinux: boolean, urls: TestUrls) {+ addCommonCookieHandlers(httpsServer, urls);+ httpsServer.setRoute('/set-cookie.html', (req, res) => {+ res.setHeader('Set-Cookie', `${req.headers.referer ? 'frame' : 'top-level'}=value; SameSite=None; Path=/; Secure;`);+ res.setHeader('Content-Type', 'text/html');+ res.end();+ });++ await page.goto(urls.set_origin1);+ await page.goto(urls.read_origin1);+ expect(await page.locator('body').textContent()).toBe('Received cookie: top-level=value');++ await page.goto(urls.read_origin2_origin1);const frameBody = page.locator('iframe').contentFrame().locator('body');// WebKit does not support third-party cookies without a 'Partition' attribute.if (browserName === 'webkit' && isMac)await expect(frameBody).toHaveText('Received cookie: undefined');else- await expect(frameBody).toHaveText('Received cookie: name=value');+ await expect(frameBody).toHaveText('Received cookie: top-level=value');++ // Set cookie and do second navigation.+ await page.goto(urls.set_origin2_origin1);+ await page.goto(urls.read_origin2_origin1);+ const expectedThirdParty = browserName === 'webkit' && isMac ?+ 'Received cookie: undefined' : browserName === 'webkit' && isLinux ?+ 'Received cookie: top-level=value' :+ 'Received cookie: frame=value; top-level=value';+ await expect(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });++ // Check again the top-level cookie.+ await page.goto(urls.read_origin1);+ const expectedTopLevel = browserName === 'webkit' && (isMac || isLinux) ?+ 'Received cookie: top-level=value' :+ 'Received cookie: frame=value; top-level=value';+ expect(await page.locator('body').textContent()).toBe(expectedTopLevel);++ return {+ expectedTopLevel,+ expectedThirdParty,+ };+}++test(`third party non-partitioned cookies`, async ({ page, browserName, httpsServer, isMac, isLinux, urls }) => {+ await runNonPartitionedTest(page, httpsServer, browserName, isMac, isLinux, urls);});-test(`third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServer, isMac }) => {- httpsServer.setRoute('/empty.html', (req, res) => {+test(`save/load third party non-partitioned cookies`, async ({ page, browserName, httpsServer, isMac, isLinux, browser, urls }) => {+ // Run the test to populate the cookies.+ const { expectedTopLevel, expectedThirdParty } = await runNonPartitionedTest(page, httpsServer, browserName, isMac, isLinux, urls);++ async function checkCookies(page: Page) {+ // Check top-level cookie first.+ await page.goto(urls.read_origin1);+ expect.soft(await page.locator('body').textContent()).toBe(expectedTopLevel);++ // Check third-party cookie.+ await page.goto(urls.read_origin2_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('body');+ await expect.soft(frameBody).toHaveText(expectedThirdParty);+ }++ await checkCookies(page);++ await test.step('export via cookies/addCookies', async () => {+ const cookies = await page.context().cookies();+ const context2 = await browser.newContext();+ await context2.addCookies(cookies);+ const page2 = await context2.newPage();+ await checkCookies(page2);+ });++ await test.step('export via storageState', async () => {+ const storageState = await page.context().storageState();+ const context3 = await browser.newContext({ storageState });+ const page3 = await context3.newPage();+ await checkCookies(page3);+ });+});++async function runPartitionedTest(page: Page, httpsServer: TestServer, browserName: string, isMac: boolean, urls: TestUrls) {+ addCommonCookieHandlers(httpsServer, urls);+ httpsServer.setRoute('/set-cookie.html', (req, res) => {res.setHeader('Set-Cookie', [- `name=value; SameSite=None; Path=/; Secure; Partitioned;`,- `nonPartitionedName=value; SameSite=None; Path=/; Secure;`+ `${req.headers.referer ? 'frame' : 'top-level'}-partitioned=value; SameSite=None; Path=/; Secure; Partitioned;`,+ `${req.headers.referer ? 'frame' : 'top-level'}-non-partitioned=value; SameSite=None; Path=/; Secure;`]);- res.setHeader('Content-Type', 'text/html');- const cookies = req.headers.cookie?.split(';').map(c => c.trim()).sort().join('; ');- res.end(`Received cookie: ${cookies}`);- });- httpsServer.setRoute('/with-frame.html', (req, res) => {- res.setHeader('Content-Type', 'text/html');- res.end(`<iframe src='${httpsServer.PREFIX}/empty.html'></iframe>`);+ res.end();});- await page.goto(httpsServer.EMPTY_PAGE);- await page.goto(httpsServer.EMPTY_PAGE);- expect(await page.locator('body').textContent()).toBe('Received cookie: name=value; nonPartitionedName=value');+ await page.goto(urls.set_origin1);+ await page.goto(urls.read_origin1);+ expect(await page.locator('body').textContent()).toBe('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');- await page.goto(httpsServer.CROSS_PROCESS_PREFIX + '/with-frame.html');+ await page.goto(urls.read_origin2_origin1);const frameBody = page.locator('iframe').contentFrame().locator('body');// Firefox cookie partitioning is disabled in Firefox.// TODO: reenable cookie partitioning?if (browserName === 'firefox') {- await expect(frameBody).toHaveText('Received cookie: name=value; nonPartitionedName=value');+ await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');return;}// Linux and Windows WebKit builds do not partition third-party cookies at all.if (browserName === 'webkit' && !isMac) {- await expect(frameBody).toHaveText('Received cookie: name=value; nonPartitionedName=value');+ await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');return;}@@ -90,7 +227,7 @@ test(`third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServ} else {// For non-partitioned cookies, the cookie is sent to the iframe right away,// if third-party cookies are supported by the browser.- await expect(frameBody).toHaveText('Received cookie: nonPartitionedName=value');+ await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value');}// First navigation:@@ -98,11 +235,263 @@ test(`third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServ// - sets the third-party cookie for the top-level context// Second navigation:// - sends the cookie as it was just set for the (top-level site, iframe url) partition.- await page.goto(httpsServer.CROSS_PROCESS_PREFIX + '/with-frame.html');+ await page.goto(urls.set_origin2_origin1);+ await page.goto(urls.read_origin2_origin1);if (browserName === 'webkit')await expect(frameBody).toHaveText('Received cookie: undefined');else- await expect(frameBody).toHaveText('Received cookie: name=value; nonPartitionedName=value');+ await expect(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value');+}++test(`third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServer, isMac, urls }) => {+ await runPartitionedTest(page, httpsServer, browserName, isMac, urls);+});++test(`save/load third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServer, isMac, browser, urls }) => {+ test.fixme(browserName === 'firefox', 'Firefox cookie partitioning is disabled in Firefox.');+ test.fixme(browserName === 'webkit' && !isMac, 'Linux and Windows WebKit builds do not partition third-party cookies at all.');++ await runPartitionedTest(page, httpsServer, browserName, isMac, urls);++ async function checkCookies(page: Page) {+ {+ // Check top-level cookie first.+ await page.goto(urls.read_origin1);+ const expectedTopLevel = browserName === 'webkit' && isMac ?+ 'Received cookie: top-level-non-partitioned=value; top-level-partitioned=value' :+ 'Received cookie: frame-non-partitioned=value; top-level-non-partitioned=value; top-level-partitioned=value';+ expect.soft(await page.locator('body').textContent()).toBe(expectedTopLevel);+ }+ {+ // Check third-party cookie.+ await page.goto(urls.read_origin2_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('body');+ const expectedThirdParty = browserName === 'webkit' ?+ 'Received cookie: undefined' :+ 'Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value';+ await expect.soft(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });+ }+ {+ await page.goto(urls.read_origin1_origin2_origin1); // read-origin1-origin2-origin1.html+ const frameBody = page.locator('iframe').contentFrame().locator('iframe').contentFrame().locator('body');+ const expectedThirdParty = browserName === 'webkit' ?+ 'Received cookie: top-level-non-partitioned=value; top-level-partitioned=value' :+ 'Received cookie: frame-non-partitioned=value; top-level-non-partitioned=value';+ await expect.soft(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });+ }+ }++ await checkCookies(page);++ function checkStorageCookies(cookies: Cookie[]) {+ const expectedTopLevelPartitioned = browserName === 'webkit' && isMac ?+ undefined :+ 'https://localhost';+ expectPartitionKey(cookies, 'top-level-partitioned', expectedTopLevelPartitioned);+ expectPartitionKey(cookies, 'top-level-non-partitioned', undefined);+ if (browserName === 'webkit' && isMac) {+ expect(cookies.find(cookie => cookie.name === 'frame-partitioned')).toBeUndefined();+ expect(cookies.find(cookie => cookie.name === 'frame-non-partitioned')).toBeUndefined();+ } else {+ expectPartitionKey(cookies, 'frame-partitioned', 'https://127.0.0.1');+ expectPartitionKey(cookies, 'frame-non-partitioned', undefined);+ }+ }+ checkStorageCookies(await page.context().cookies());+ checkStorageCookies((await page.context().storageState()).cookies);++ await test.step('export via cookies/addCookies', async () => {+ const cookies = await page.context().cookies();+ const context2 = await browser.newContext();+ await context2.addCookies(cookies);+ const page2 = await context2.newPage();+ await checkCookies(page2);+ });++ await test.step('export via storageState', async () => {+ const storageState = await page.context().storageState();+ const context3 = await browser.newContext({ storageState });+ const page3 = await context3.newPage();+ await checkCookies(page3);+ });+});++test(`add 'Partitioned;' cookie via API`, async ({ page, context, browserName, httpsServer, isMac, urls }) => {+ addCommonCookieHandlers(httpsServer, urls);++ await context.addCookies([+ {+ name: 'top-level-partitioned',+ value: 'value',+ domain: 'localhost',+ path: '/',+ expires: -1,+ httpOnly: false,+ secure: true,+ sameSite: 'None',+ partitionKey: 'https://localhost',+ _crHasCrossSiteAncestor: false+ } as any,+ {+ name: 'top-level-non-partitioned',+ value: 'value',+ domain: 'localhost',+ path: '/',+ expires: -1,+ httpOnly: false,+ secure: true,+ sameSite: 'None'+ },+ {+ name: 'frame-partitioned',+ value: 'value',+ domain: 'localhost',+ path: '/',+ expires: -1,+ httpOnly: false,+ secure: true,+ sameSite: 'None',+ partitionKey: 'https://127.0.0.1',+ _crHasCrossSiteAncestor: true+ } as any,+ {+ name: 'frame-non-partitioned',+ value: 'value',+ domain: 'localhost',+ path: '/',+ expires: -1,+ httpOnly: false,+ secure: true,+ sameSite: 'None'+ }+ ]);++ async function checkCookies(page: Page) {+ {+ // Check top-level cookie first.+ await page.goto(urls.read_origin1);+ const expectedTopLevel = browserName === 'webkit' || browserName === 'firefox' ?+ 'Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value; top-level-partitioned=value' :+ 'Received cookie: frame-non-partitioned=value; top-level-non-partitioned=value; top-level-partitioned=value';+ expect.soft(await page.locator('body').textContent()).toBe(expectedTopLevel);+ }+ {+ // Check third-party cookie.+ await page.goto(urls.read_origin2_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('body');+ const expectedThirdParty = browserName === 'webkit' && isMac ?+ 'Received cookie: undefined' : browserName === 'chromium' ?+ 'Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value' :+ // Firefox and WebKit on Linux/Windows do not partition third-party cookies.+ 'Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value; top-level-partitioned=value';+ await expect.soft(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });+ }+ {+ await page.goto(urls.read_origin1_origin2_origin1); // read-origin1-origin2-origin1.html+ const frameBody = page.locator('iframe').contentFrame().locator('iframe').contentFrame().locator('body');+ const expectedThirdParty = browserName === 'webkit' || browserName === 'firefox' ?+ 'Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value; top-level-partitioned=value' :+ 'Received cookie: frame-non-partitioned=value; top-level-non-partitioned=value';+ await expect.soft(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });+ }+ }++ await checkCookies(page);+});+++test(`same origin third party 'Partitioned;' cookie with different origin intermediate iframe`, async ({ page, httpsServer, browser, urls }) => {+ addCommonCookieHandlers(httpsServer, urls);+ httpsServer.setRoute('/set-cookie.html', (req, res) => {+ res.setHeader('Set-Cookie', [+ `${req.headers.referer ? 'frame' : 'top-level'}-partitioned=value; SameSite=None; Path=/; Secure; Partitioned;`,+ `${req.headers.referer ? 'frame' : 'top-level'}-non-partitioned=value; SameSite=None; Path=/; Secure;`+ ]);+ res.end();+ });+ // main frame: origin1 -> iframe1: origin2 -> iframe2: origin1+ // In this case the cookie in iframe2 is a third-party partitioned cookie, even though+ // it's the same origin as the main frame.+ await page.goto(urls.set_origin1_origin2_origin1);++ async function checkCookies(page: Page) {+ await page.goto(urls.read_origin1_origin2_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('iframe').contentFrame().locator('body');+ await expect.soft(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value');+ }++ await checkCookies(page);++ await test.step('export via cookies/addCookies', async () => {+ const cookies = await page.context().cookies();+ const context2 = await browser.newContext();+ await context2.addCookies(cookies);+ const page2 = await context2.newPage();+ await checkCookies(page2);+ });++ await test.step('export via storageState', async () => {+ const storageState = await page.context().storageState();+ const context3 = await browser.newContext({ storageState });+ const page3 = await context3.newPage();+ await checkCookies(page3);+ });+});++test(`top level 'Partitioned;' cookie and same origin iframe`, async ({ page, browserName, httpsServer, browser, urls }) => {+ addCommonCookieHandlers(httpsServer, urls);+ httpsServer.setRoute('/set-cookie.html', (req, res) => {+ res.setHeader('Set-Cookie', [+ `${req.headers.referer ? 'frame' : 'top-level'}=value; SameSite=None; Path=/; Secure; Partitioned;`,+ `${req.headers.referer ? 'frame' : 'top-level'}-non-partitioned=value; SameSite=None; Path=/; Secure;`+ ]);+ res.end();+ });++ // Same origin iframe cookies are partitioned the same way as top-level cookies.+ await page.goto(urls.set_origin1);+ await page.context().storageState({ path: '/tmp/state2.json' });++ async function checkCookies(page: Page) {+ {+ // Check top-level cookie first.+ await page.goto(urls.read_origin1);+ expect.soft(await page.locator('body').textContent()).toBe('Received cookie: top-level-non-partitioned=value; top-level=value');+ }+ {+ // Same origin iframe.+ await page.goto(urls.read_origin1_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('body');+ await expect.soft(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level=value', { timeout: 1000 });+ }+ {+ // Check third-party cookie.+ // main frame: origin1 -> iframe1: origin2 -> iframe2: origin1+ await page.goto(urls.read_origin1_origin2_origin1);+ const frameBody = page.locator('iframe').contentFrame().locator('iframe').contentFrame().locator('body');+ const expectedThirdParty = browserName === 'chromium'+ ? 'Received cookie: top-level-non-partitioned=value'+ : 'Received cookie: top-level-non-partitioned=value; top-level=value';+ await expect.soft(frameBody).toHaveText(expectedThirdParty, { timeout: 1000 });+ }+ }++ await checkCookies(page);++ await test.step('export via cookies/addCookies', async () => {+ const cookies = await page.context().cookies();+ const context2 = await browser.newContext();+ await context2.addCookies(cookies);+ const page2 = await context2.newPage();+ await checkCookies(page2);+ });++ await test.step('export via storageState', async () => {+ const storageState = await page.context().storageState();+ const context3 = await browser.newContext({ storageState });+ const page3 = await context3.newPage();+ await checkCookies(page3);+ });});test('should be able to send third party cookies via an iframe', async ({ browser, httpsServer, browserName, isMac }) => {