Menu
Open source
addCookies()
Adds a list of cookies into the BrowserContext. All pages within this BrowserContext will have these cookies set.
Note
If a cookie’surl
property is not provided, bothdomain
andpath
properties must be specified.
Example
JavaScript
import { browser } from 'k6/experimental/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const context = browser.newContext();
const page = context.newPage();
try {
const unixTimeSinceEpoch = Math.round(new Date() / 1000);
const day = 60 * 60 * 24;
const dayAfter = unixTimeSinceEpoch + day;
const dayBefore = unixTimeSinceEpoch - day;
context.addCookies([
// this cookie expires at the end of the session
{
name: 'testcookie',
value: '1',
sameSite: 'Strict',
domain: 'httpbin.org',
path: '/',
httpOnly: true,
secure: true,
},
// this cookie expires in a day
{
name: 'testcookie2',
value: '2',
sameSite: 'Lax',
domain: 'httpbin.org',
path: '/',
expires: dayAfter,
},
// this cookie expires in the past, so it will be removed.
{
name: 'testcookie3',
value: '3',
sameSite: 'Lax',
domain: 'httpbin.org',
path: '/',
expires: dayBefore,
},
]);
const response = await page.goto('https://httpbin.org/cookies', {
waitUntil: 'networkidle',
});
console.log(response.json());
// prints:
// {"cookies":{"testcookie":"1","testcookie2":"2"}}
} finally {
page.close();
}
}
Was this page helpful?
Related documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video
Performance testing and observability in Grafana Cloud
In this webinar, learn how Grafana Cloud k6 offers you the best developer experience for performance testing.
User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.