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
Name | Type | Description |
cookies | WebDriver.Cookie or WebDriver.Cookie[] | A cookie object or an array of cookie objects. |
WebDriver.Cookie Object Parameters
Name | Type | Default | Description |
name | String | N/A | The name of the cookie. |
value | String | N/A | The value of the cookie. |
path | String | "/" | The path of the cookie. |
domain | String | see description | The domain the cookie is visible to. If omitted, defaults to the domain of the current document's URL in the browser context. |
secure | Boolean | false | The secure flag of the cookie. |
httpOnly | Boolean | false | The HTTP-only flag of the cookie. |
expiry | Number | not set | The expiry date of the cookie as the number of seconds since the Unix epoch. |
sameSite | String | "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' }
// ]
});
Related Commands
References
We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.