メインコンテンツにスキップ

クッキーを設定する

現在のページに対して1つまたは複数のクッキーを設定します。クッキーを受け取るページ上にいることを確認してください。そのページにアクセスせずに任意のページのクッキーを設定することはできません。

使用方法
browser.setCookies({ name, value, path, domain, secure, httpOnly, expiry, sameSite })
パラメータ
名前詳細
cookieArray<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'}
// ]
});

Welcome! How can I help?

WebdriverIO AI Copilot