# setCookies

Sets one or more [cookies](https://w3c.github.io/webdriver/#cookies) for the current page. Make sure you are on the page that should receive the cookie. You can't set a cookie for an arbitrary page without being on that page.

## Usage[​](#usage "Direct link to Usage")

```
await browser.setCookies({ name, value, path, domain, secure, httpOnly, expiry, sameSite })
```

## Parameters[​](#parameters "Direct link to Parameters")

| Name                              | Type                                      | Details                                                                                                                                       |
| --------------------------------- | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `cookie`                          | `Array<WebDriverCookie>, WebDriverCookie` | cookie object or object array.                                                                                                                |
| `cookie.name`<br />*optional*     | `String`                                  | The name of the cookie.                                                                                                                       |
| `cookie.value`<br />*optional*    | `String`                                  | The cookie value.                                                                                                                             |
| `cookie.path`<br />*optional*     | `String`                                  | The cookie path. Defaults to "/" if omitted when adding a cookie.                                                                             |
| `cookie.domain`<br />*optional*   | `String`                                  | The domain the cookie is visible to. Defaults to the current browsing context’s active document’s URL domain if omitted when adding a cookie. |
| `cookie.secure`<br />*optional*   | `Boolean`                                 | Whether the cookie is a secure cookie. Defaults to false if omitted when adding a cookie.                                                     |
| `cookie.httpOnly`<br />*optional* | `Boolean`                                 | Whether the cookie is an HTTP only cookie. Defaults to false if omitted when adding a cookie.                                                 |
| `cookie.expiry`<br />*optional*   | `Number`                                  | When the cookie expires, specified in seconds since Unix Epoch. Must not be set if omitted when adding a cookie.                              |
| `cookie.sameSite`<br />*optional* | `String`                                  | Whether the cookie applies to a SameSite policy. Defaults to None if omitted when adding a cookie. Can be set to either "Lax" or "Strict".    |

## Example[​](#example "Direct link to Example")

setCookies.js

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

    // set a single cookie
    await browser.setCookies({
        name: 'test1',
        value: 'one'
        // The below options are optional
        // path: '/foo', // The cookie path. Defaults to "/"
        // domain: '.example.com', // The domain the cookie is visible to. Defaults to the current browsing context’s active document’s URL domain
        // secure: true, // Whether the cookie is a secure cookie. Defaults to false
        // httpOnly: true, // Whether the cookie is an HTTP only cookie. Defaults to false
        // expiry: 1551393875 // When the cookie expires, specified in seconds since Unix Epoch
    })

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

    const cookies = await browser.getCookies()
    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'}
    // ]
});
```
