diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext.md
index a5539ded3d..f1ae72f549 100644
--- a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext.md
+++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext.md
@@ -10,17 +10,18 @@ The [browser module API](/javascript-api/k6-experimental/browser#browser-module-
If a [page](/javascript-api/k6-experimental/browser/page/) opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's `BrowserContext`.
-| Method | Description |
-|-------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
-| [BrowserContext.addCookies()](/javascript-api/k6-experimental/browser/browsercontext/addcookies/) | Adds cookies into the `BrowserContext`. |
-| [BrowserContext.clearCookies()](/javascript-api/k6-experimental/browser/browsercontext/clearcookies/)
+ +If a [cookie](/javascript-api/k6-experimental/browser/browsercontext/cookie)'s `url` property is not provided, both `domain` and `path` properties must be specified. + ++ ### Example @@ -27,18 +34,54 @@ export const options = { export default async function () { const context = browser.newContext(); + const page = context.newPage(); - context.addCookies([ - { - name: 'myCookie', - value: 'hello world', - url: 'https://test.k6.io/', - }, - ]); + try { + const unixTimeSinceEpoch = Math.round(new Date() / 1000); + const day = 60*60*24; + const dayAfter = unixTimeSinceEpoch+day; + const dayBefore = unixTimeSinceEpoch-day; - const page = context.newPage(); - await page.goto('https://test.k6.io/'); - page.close(); + 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(); + } } ``` diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/clearCookies.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/clearCookies.md index 244c30db25..20e102910f 100644 --- a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/clearCookies.md +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/clearCookies.md @@ -3,13 +3,6 @@ title: 'clearCookies()' excerpt: 'Clears context cookies.' --- -
- -This feature has **known issues**. For details, refer to -[#442](https://github.com/grafana/xk6-browser/issues/442). - -- Clears the `BrowserContext`'s cookies. ### Example @@ -36,8 +29,11 @@ export default async function () { const context = browser.newContext(); const page = context.newPage(); - await page.goto('https://test.k6.io/'); + await page.goto('https://httpbin.org/cookies/set?testcookie=testcookievalue'); + console.log(context.cookies().length); // prints: 1 + context.clearCookies(); + console.log(context.cookies().length); // prints: 0 } ``` diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookie.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookie.md new file mode 100644 index 0000000000..4d0b1a9088 --- /dev/null +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookie.md @@ -0,0 +1,20 @@ +--- +title: "Cookie" +excerpt: "Browser module: Cookie Class" +--- + +Cookie class represents a cookie in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). + +See the [HTTP Cookies documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) on the Mozilla website for more details about cookies. + +| Property | Type | Default | Description | +| -------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | string | `""` | The cookie's name. Required. | +| value | string | `""` | The cookie's value. Required. | +| domain | string | `""` | The cookie's domain. | +| path | string | `'/'` | The cookie's path. | +| url | string | `""` | The cookie's URL. | +| expires | number | `-1` | The cookie's expiration date as the number of seconds since the UNIX epoch. `-1` means a session cookie. | +| httpOnly | bool | `false` | A cookie is inaccessible to the JavaScript [document.cookie](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie) API when this property is `true`. | +| secure | bool | `false` | The cookie's secure flag. | +| sameSite | string | `'Lax'` | The cookie's same site flag. It can be one of `'Strict'`, `'Lax'`, and `'None'`. | \ No newline at end of file diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookies.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookies.md new file mode 100644 index 0000000000..3805dc75c1 --- /dev/null +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookies.md @@ -0,0 +1,80 @@ +--- +title: 'cookies([urls])' +excerpt: 'Retrieves context cookies.' +--- + +Returns a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) from the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext) filtered by the provided `urls`. If no `urls` are provided, all cookies are returned. + +| Parameter | Type | Description | +|----------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| urls | array | A string array of URLs to filter the [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). | + +### Returns + +| Type | Description | +| ---- | ----------- | +| array | A list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | + +
+ +[Cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) can be added with [BrowserContext.addCookies](/javascript-api/k6-experimental/browser/browsercontext/addcookies/). + ++ +### Example + +