await page.goto('about:blank');
let error!: Error; try {
await page.setCookie({name: 'example-cookie', value: 'best'});
} catch (error_) {
error = error_ as Error;
}
expect(error.message).toContain( 'At least one of the url and domain needs to be specified',
);
});
it('should not set a cookie with blank page URL', async () => { const {page, server} = await getTestState();
let error!: Error;
await page.goto(server.EMPTY_PAGE); try {
await page.setCookie(
{name: 'example-cookie', value: 'best'},
{url: 'about:blank', name: 'example-cookie-blank', value: 'best'},
);
} catch (error_) {
error = error_ as Error;
}
expect(error.message).toEqual(
`Blank page can not have cookie "example-cookie-blank"`,
);
});
it('should not set a cookie on a data URL page', async () => { const {page} = await getTestState();
let error!: Error;
await page.goto('data:,Hello%2C%20World!'); try {
await page.setCookie({name: 'example-cookie', value: 'best'});
} catch (error_) {
error = error_ as Error;
}
expect(error.message).toContain( 'At least one of the url and domain needs to be specified',
);
});
it('should default to setting secure cookie for HTTPS websites', async () => { const {page, server} = await getTestState();
await page.goto(server.EMPTY_PAGE); const SECURE_URL = 'https://example.com';
await page.setCookie({
url: SECURE_URL,
name: 'foo',
value: 'bar',
}); const [cookie] = await page.cookies(SECURE_URL);
expect(cookie!.secure).toBe(true);
});
it('should be able to set insecure cookie for HTTP website', async () => { const {page, server} = await getTestState();
await page.goto(server.EMPTY_PAGE); const HTTP_URL = 'http://example.com';
await page.setCookie({
url: HTTP_URL,
name: 'foo',
value: 'bar',
}); const [cookie] = await page.cookies(HTTP_URL);
expect(cookie!.secure).toBe(false);
});
it('should set a cookie on a different domain', async () => { const {page, server} = await getTestState();
await page.goto(server.EMPTY_PAGE); // Set a cookie for the current page.
await page.setCookie({
name: COOKIE_NAME,
value: 'local page cookie value',
});
expect(await page.cookies()).toHaveLength(1);
// Set a cookie for different domain.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'COOKIE_DESTINATION_URL cookie value',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);
await page.deleteCookie({name: COOKIE_NAME});
// Verify the cookie is deleted for the current page.
expect(await page.cookies()).toHaveLength(0);
await page.goto(server.EMPTY_PAGE); // Set a cookie for the current page.
await page.setCookie({
name: COOKIE_NAME,
value: 'some_cookie_value',
});
expect(await page.cookies()).toHaveLength(1);
// Set a cookie for specified URL.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'another_cookie_value',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);
// Delete the cookie for specified URL.
await page.deleteCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
});
// Verify the cookie is deleted for specified URL.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);
// Verify the cookie is not deleted for the current page.
await expectCookieEquals(await page.cookies(), [
{
name: COOKIE_NAME,
value: 'some_cookie_value',
domain: 'localhost',
path: '/',
sameParty: false,
expires: -1,
size: 33,
httpOnly: false,
secure: false,
session: true,
sourceScheme: 'NonSecure',
},
]);
});
it('should delete cookie for specified URL regardless of the current page', async () => { // This test verifies the page.deleteCookie method deletes cookies for the custom // destination URL, even if it was set from another page. Depending on the cookie // partitioning implementation, this test case does not pass, if source origin is in // the default cookie partition.
await page.goto(URL_1); // Set a cookie for the COOKIE_DESTINATION from URL_1.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'Cookie from URL_1',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);
await page.goto(URL_2); // Set a cookie for the COOKIE_DESTINATION from URL_2.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'Cookie from URL_2',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);
// Delete the cookie for the COOKIE_DESTINATION from URL_2.
await page.deleteCookie({
name: COOKIE_NAME,
url: COOKIE_DESTINATION_URL,
});
// Expect the cookie for the COOKIE_DESTINATION from URL_2 is deleted.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);
// Navigate back to the URL_1.
await page.goto(server.EMPTY_PAGE); // Expect the cookie for the COOKIE_DESTINATION from URL_1 is deleted.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);
});
it('should only delete cookie from the default partition if partitionkey is not specified', async () => { const {page, server} = await getTestState(); const url = new URL(server.EMPTY_PAGE);
await page.goto(url.toString());
await page.setCookie({
url: url.toString(),
name: 'partitionCookie',
value: 'partition',
secure: true,
partitionKey: url.origin,
});
expect(await page.cookies()).toHaveLength(1);
await page.deleteCookie({
url: url.toString(),
name: 'partitionCookie',
});
expect(await page.cookies()).toHaveLength(0);
});
it('should delete cookie with partition key if partition key is specified', async () => { const {page, server, isChrome} = await getTestState(); const url = new URL(server.EMPTY_PAGE);
await page.goto(url.toString()); const origin = isChrome
? url.origin.replace(`:${url.port}`, '')
: url.origin;
await page.setCookie({
url: url.toString(),
name: 'partitionCookie',
value: 'partition',
secure: true,
partitionKey: origin,
});
expect(await page.cookies()).toHaveLength(1);
await page.deleteCookie({
url: url.toString(),
name: 'partitionCookie',
partitionKey: origin,
});
expect(await page.cookies()).toHaveLength(0);
});
});
});
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.30 Sekunden
(vorverarbeitet am 2026-06-05)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.