Skip to main content

setCookies

Overview

Use the setCookies command to set cookies on the current page.

Make sure you are on the page for which you want to set the cookies. You cannot set cookies for an arbitrary page without being on it.

Usage

await browser.setCookies(cookies);

Command Parameters

NameTypeDescription
cookiesWebDriver.Cookie or WebDriver.Cookie[]A cookie object or an array of cookie objects.

WebDriver.Cookie Object Parameters

NameTypeDefaultDescription
nameStringN/AThe name of the cookie.
valueStringN/AThe value of the cookie.
pathString"/"The path of the cookie.
domainStringsee descriptionThe domain the cookie is visible to. If omitted, defaults to the domain of the current document's URL in the browser context.
secureBooleanfalseThe secure flag of the cookie.
httpOnlyBooleanfalseThe HTTP-only flag of the cookie.
expiryNumbernot setThe expiry date of the cookie as the number of seconds since the Unix epoch.
sameSiteString"None"The SameSite attribute of the cookie. Valid values are "Lax" or "Strict".

Usage Examples

it("should set a cookie for the page", async ({ browser }) => {
await browser.url("/");

// set a single cookie
await browser.setCookies({
name: "test1",
value: "one",
// Optional parameters:
// cookie path, defaults to "/"
// path: '/foo',
// domain the cookie is visible to
// defaults to domain of the current document's URL in the browser context
// domain: '.example.com',
// secure cookie flag, defaults to false
// secure: true,
// HTTP-only cookie flag, defaults to false
// httpOnly: true,
// expiry date of the cookie in seconds since the Unix epoch
// expiry: 1551393875
});

// set multiple cookies
await browser.setCookies([
{ name: "test2", value: "two" },
{ name: "test3", value: "three" },
]);

const cookies = await browser.getCookies();

await console.log(cookies);
// outputs:
// [
// { name: 'test1', value: 'one', domain: 'www.example.com' },
// { name: 'test2', value: 'two', domain: 'www.example.com' },
// { name: 'test3', value: 'three', domain: 'www.example.com' }
// ]
});

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.