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