クッキーを設定する
現在のページに対して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'}
// ]
});