setCookies
現在のページに1つ以上のCookieを設定します。Cookieを受け取るべきページにいることを確認してください。そのページにアクセスせずに、任意のページにCookieを設定することはできません。
使用方法
browser.setCookies({ name, value, path, domain, secure, httpOnly, expiry, sameSite })
パラメータ
名前 | 型 | 詳細 |
---|---|---|
cookie | Array<WebDriverCookie> , WebDriverCookie | Cookieオブジェクトまたはオブジェクト配列。 |
cookie.name オプション | 文字列 | Cookieの名前。 |
cookie.value オプション | 文字列 | Cookieの値。 |
cookie.path オプション | 文字列 | Cookieのパス。Cookieを追加するときに省略された場合は、デフォルトで "/" になります。 |
cookie.domain オプション | 文字列 | Cookieが表示されるドメイン。Cookieを追加するときに省略された場合は、現在のブラウジングコンテキストのアクティブドキュメントのURLドメインがデフォルトになります。 |
cookie.secure オプション | 真偽値 | CookieがセキュアCookieかどうか。Cookieを追加するときに省略された場合は、デフォルトでfalseになります。 |
cookie.httpOnly オプション | 真偽値 | CookieがHTTP only Cookieかどうか。Cookieを追加するときに省略された場合は、デフォルトでfalseになります。 |
cookie.expiry オプション | 数値 | Cookieの有効期限。Unixエポックからの経過秒数で指定します。Cookieを追加するときに省略された場合は、設定しないでください。 |
cookie.sameSite オプション | 文字列 | CookieがSameSiteポリシーに適用されるかどうか。Cookieを追加するときに省略された場合は、デフォルトでNoneになります。"Lax"または"Strict"に設定できます。 |
例
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'}
// ]
});