From 4c12f38c356d75ce0da415789d5664b8c87bef0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 19 Sep 2023 14:35:26 +0300 Subject: [PATCH 01/12] Add cookie type --- .../01 browser/02 BrowserContext/cookie.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookie.md 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..c33968da9f --- /dev/null +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookie.md @@ -0,0 +1,18 @@ +--- +title: "Cookie" +excerpt: "Browser module: Cookie Class" +--- + +Cookie class representing a cookie in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). + +| Property | Type | 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. Defaults to `'/'`. | +| url | string | The cookie's URL. | +| expires | number | The cookie's expiration date as the number of seconds since the UNIX epoch. Defaults to `-1`, meaning a session cookie. | +| httpOnly | bool | 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`. Defaults to `false`. | +| secure | bool | The cookie's secure flag. Defaults to `false`. | +| sameSite | string | The cookie's same site flag. It can be one of `'Strict'`, `'Lax'`, and `'None'`. | \ No newline at end of file From 65746e122f20f1bd2597d04fd026c314aac86f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 19 Sep 2023 14:35:56 +0300 Subject: [PATCH 02/12] Update addCookies --- .../02 BrowserContext/addCookies.md | 58 +++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md index 8250285753..62ccfbac81 100644 --- a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md @@ -3,7 +3,14 @@ title: 'addCookies()' excerpt: 'Clears context cookies.' --- -Adds cookies into the `BrowserContext`. All pages within this context will have these cookies installed. +Adds a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) into the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie). All pages within this [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie) will have these [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) installed. + +
+ +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,47 @@ 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: '127.0.0.1', + path: '/', + httpOnly: true, + secure: true, + }, + // this cookie expires in a day + { + name: 'testcookie2', + value: '2', + sameSite: 'Lax', + domain: '127.0.0.1', + path: '/', + expires: dayAfter, + }, + // this cookie expires in the past, so it will be removed. + { + name: 'testcookie3', + value: '3', + sameSite: 'Lax', + domain: '127.0.0.1', + path: '/', + expires: dayBefore + } + ]); + } finally { + page.close(); + } } ``` From 1384f78daa383f4a0ebec3340d4ec374ecd127a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 19 Sep 2023 14:47:27 +0300 Subject: [PATCH 03/12] Add cookies --- .../01 browser/02 BrowserContext.md | 29 +++---- .../01 browser/02 BrowserContext/cookies.md | 80 +++++++++++++++++++ 2 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/cookies.md 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..bfd597a674 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/) | Clear the `BrowserContext`'s cookies. | -| [BrowserContext.clearPermissions()](/javascript-api/k6-experimental/browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | -| [BrowserContext.close()](/javascript-api/k6-experimental/browser/browsercontext/close) | Close the `BrowserContext` and all its [page](/javascript-api/k6-experimental/browser/page/)s. | -| [BrowserContext.grantPermissions(permissions[, options])](/javascript-api/k6-experimental/browser/browsercontext/grantpermissions) | Grants specified permissions to the `BrowserContext`. | -| [BrowserContext.newPage()](/javascript-api/k6-experimental/browser/browsercontext/newpage) | Uses the `BrowserContext` to create a new [Page](/javascript-api/k6-experimental/browser/page/) and returns it. | -| [BrowserContext.pages()](/javascript-api/k6-experimental/browser/browsercontext/pages) | Returns a list of [page](/javascript-api/k6-experimental/browser/page/)s that belongs to the `BrowserContext`. | -| [BrowserContext.setDefaultNavigationTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaultnavigationtimeout) | Sets the default navigation timeout in milliseconds. | -| [BrowserContext.setDefaultTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. | -| [BrowserContext.setGeolocation(geolocation)](/javascript-api/k6-experimental/browser/browsercontext/setgeolocation) | Sets the `BrowserContext`'s geolocation. | -| [BrowserContext.setOffline(offline)](/javascript-api/k6-experimental/browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. | -| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](/javascript-api/k6-experimental/browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. | +| 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/) | Clear the `BrowserContext`'s [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | +| [BrowserContext.clearPermissions()](/javascript-api/k6-experimental/browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | +| [BrowserContext.cookies()](/javascript-api/k6-experimental/browser/browsercontext/cookies) | Returns a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) from the `BrowserContext`. | +| [BrowserContext.close()](/javascript-api/k6-experimental/browser/browsercontext/close) | Close the `BrowserContext` and all its [page](/javascript-api/k6-experimental/browser/page/)s. | +| [BrowserContext.grantPermissions(permissions[, options])](/javascript-api/k6-experimental/browser/browsercontext/grantpermissions) | Grants specified permissions to the `BrowserContext`. | +| [BrowserContext.newPage()](/javascript-api/k6-experimental/browser/browsercontext/newpage) | Uses the `BrowserContext` to create a new [Page](/javascript-api/k6-experimental/browser/page/) and returns it. | +| [BrowserContext.pages()](/javascript-api/k6-experimental/browser/browsercontext/pages) | Returns a list of [page](/javascript-api/k6-experimental/browser/page/)s that belongs to the `BrowserContext`. | +| [BrowserContext.setDefaultNavigationTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaultnavigationtimeout) | Sets the default navigation timeout in milliseconds. | +| [BrowserContext.setDefaultTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. | +| [BrowserContext.setGeolocation(geolocation)](/javascript-api/k6-experimental/browser/browsercontext/setgeolocation) | Sets the `BrowserContext`'s geolocation. | +| [BrowserContext.setOffline(offline)](/javascript-api/k6-experimental/browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. | +| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](/javascript-api/k6-experimental/browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. | 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..c8acce5515 --- /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). | + +### 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 + + + +```javascript +import { browser } from 'k6/experimental/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = browser.newContext(); + const page = context.newPage(); + + try { + // get cookies from the browser context + let cookies = context.cookies(); + console.log("initial cookies length:", cookies.length); // prints 0 + + // let's add more cookies to filter by urls. + context.addCookies([ + { name: 'foo', value: 'foovalue', sameSite: 'Strict', url: 'http://foo.com' }, + { name: 'bar', value: 'barvalue', sameSite: 'Lax', url: 'https://bar.com' }, + { name: 'baz', value: 'bazvalue', sameSite: 'Lax', url: 'https://baz.com' } + ]); + + // get all cookies + cookies = context.cookies(); + console.log("filtered cookies length:", cookies.length); // prints 3 + + // get cookies filtered by urls + cookies = context.cookies('http://foo.com', 'https://baz.com'); + console.log("filtered cookies length:", cookies.length); // prints 2 + + // the first filtered cookie + console.log("1st cookie's name :", cookies[0].name); // prints foo + console.log("1st cookie's value:", cookies[0].value); // prints foovalue + // the first filtered cookie + console.log("2nd cookie's name :", cookies[1].name); // prints baz + console.log("2nd cookie's value:", cookies[1].value); // prints bazvalue + } finally { + page.close(); + } +} +``` + + From 1d6b340866ea7ec0ea70a7af3e49952f61f0ece4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 19 Sep 2023 17:09:19 +0300 Subject: [PATCH 04/12] Fix the cookie description --- .../07 k6-experimental/01 browser/02 BrowserContext/cookie.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c33968da9f..730d1fe012 100644 --- 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 @@ -3,7 +3,7 @@ title: "Cookie" excerpt: "Browser module: Cookie Class" --- -Cookie class representing a cookie in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). +Cookie class represents a cookie in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). | Property | Type | Description | | -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | From 358dbc7da8ed0238e3366117cfde1c1de1ae54e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 19 Sep 2023 17:10:34 +0300 Subject: [PATCH 05/12] Clarify the cookies urls description --- .../07 k6-experimental/01 browser/02 BrowserContext/cookies.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c8acce5515..2071dd3427 100644 --- 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 @@ -7,7 +7,7 @@ Returns a list of [cookies](/javascript-api/k6-experimental/browser/browserconte | Parameter | Type | Description | |----------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| urls | array | A string array of URLs to filter the [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | +| 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 From aa1e308e138f97f999dc46933f5a49d29bb52865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 10:21:20 +0300 Subject: [PATCH 06/12] Update cookies docs with small fixes Co-authored-by: ka3de --- .../01 browser/02 BrowserContext/addCookies.md | 2 +- .../07 k6-experimental/01 browser/02 BrowserContext/cookies.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md index 62ccfbac81..f38a387de3 100644 --- a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md @@ -3,7 +3,7 @@ title: 'addCookies()' excerpt: 'Clears context cookies.' --- -Adds a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) into the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie). All pages within this [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie) will have these [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) installed. +Adds a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) into the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie). All pages within this [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/cookie) will have these [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) set.
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 index 2071dd3427..3805dc75c1 100644 --- 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 @@ -50,7 +50,7 @@ export default async function () { let cookies = context.cookies(); console.log("initial cookies length:", cookies.length); // prints 0 - // let's add more cookies to filter by urls. + // let's add more cookies to filter by urls context.addCookies([ { name: 'foo', value: 'foovalue', sameSite: 'Strict', url: 'http://foo.com' }, { name: 'bar', value: 'barvalue', sameSite: 'Lax', url: 'https://bar.com' }, From 25091d7a77093b079acf52ee9fe776325867d8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 10:27:35 +0300 Subject: [PATCH 07/12] Add cookie defaults to the table Resolves: https://github.com/grafana/k6-docs/pull/1328#discussion_r1331049483 --- .../01 browser/02 BrowserContext/cookie.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 index 730d1fe012..5318ab527e 100644 --- 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 @@ -5,14 +5,14 @@ excerpt: "Browser module: Cookie Class" Cookie class represents a cookie in the [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext). -| Property | Type | 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. Defaults to `'/'`. | -| url | string | The cookie's URL. | -| expires | number | The cookie's expiration date as the number of seconds since the UNIX epoch. Defaults to `-1`, meaning a session cookie. | -| httpOnly | bool | 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`. Defaults to `false`. | -| secure | bool | The cookie's secure flag. Defaults to `false`. | -| sameSite | string | The cookie's same site flag. It can be one of `'Strict'`, `'Lax'`, and `'None'`. | \ No newline at end of file +| 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 From 7bf56523703d3c38cc63c1f2661e62aba27ab8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 10:36:22 +0300 Subject: [PATCH 08/12] Add cookie link to mozilla --- .../07 k6-experimental/01 browser/02 BrowserContext/cookie.md | 2 ++ 1 file changed, 2 insertions(+) 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 index 5318ab527e..4d0b1a9088 100644 --- 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 @@ -5,6 +5,8 @@ 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. | From 50e93218387fc8d5f364105534fce0333971bef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 10:38:18 +0300 Subject: [PATCH 09/12] Remove clearCookie warning This PR solved the issue: https://github.com/grafana/xk6-browser/pull/1040 --- .../01 browser/02 BrowserContext.md | 30 +++++++++---------- .../02 BrowserContext/clearCookies.md | 7 ----- 2 files changed, 15 insertions(+), 22 deletions(-) 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 bfd597a674..793d01fec6 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,18 +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/) | Clear the `BrowserContext`'s [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | -| [BrowserContext.clearPermissions()](/javascript-api/k6-experimental/browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | -| [BrowserContext.cookies()](/javascript-api/k6-experimental/browser/browsercontext/cookies) | Returns a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) from the `BrowserContext`. | -| [BrowserContext.close()](/javascript-api/k6-experimental/browser/browsercontext/close) | Close the `BrowserContext` and all its [page](/javascript-api/k6-experimental/browser/page/)s. | -| [BrowserContext.grantPermissions(permissions[, options])](/javascript-api/k6-experimental/browser/browsercontext/grantpermissions) | Grants specified permissions to the `BrowserContext`. | -| [BrowserContext.newPage()](/javascript-api/k6-experimental/browser/browsercontext/newpage) | Uses the `BrowserContext` to create a new [Page](/javascript-api/k6-experimental/browser/page/) and returns it. | -| [BrowserContext.pages()](/javascript-api/k6-experimental/browser/browsercontext/pages) | Returns a list of [page](/javascript-api/k6-experimental/browser/page/)s that belongs to the `BrowserContext`. | -| [BrowserContext.setDefaultNavigationTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaultnavigationtimeout) | Sets the default navigation timeout in milliseconds. | -| [BrowserContext.setDefaultTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. | -| [BrowserContext.setGeolocation(geolocation)](/javascript-api/k6-experimental/browser/browsercontext/setgeolocation) | Sets the `BrowserContext`'s geolocation. | -| [BrowserContext.setOffline(offline)](/javascript-api/k6-experimental/browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. | -| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](/javascript-api/k6-experimental/browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. | +| 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/) | Clear the `BrowserContext`'s [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | +| [BrowserContext.clearPermissions()](/javascript-api/k6-experimental/browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | +| [BrowserContext.cookies()](/javascript-api/k6-experimental/browser/browsercontext/cookies) | Returns a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) from the `BrowserContext`. | +| [BrowserContext.close()](/javascript-api/k6-experimental/browser/browsercontext/close) | Close the `BrowserContext` and all its [page](/javascript-api/k6-experimental/browser/page/)s. | +| [BrowserContext.grantPermissions(permissions[, options])](/javascript-api/k6-experimental/browser/browsercontext/grantpermissions) | Grants specified permissions to the `BrowserContext`. | +| [BrowserContext.newPage()](/javascript-api/k6-experimental/browser/browsercontext/newpage) | Uses the `BrowserContext` to create a new [Page](/javascript-api/k6-experimental/browser/page/) and returns it. | +| [BrowserContext.pages()](/javascript-api/k6-experimental/browser/browsercontext/pages) | Returns a list of [page](/javascript-api/k6-experimental/browser/page/)s that belongs to the `BrowserContext`. | +| [BrowserContext.setDefaultNavigationTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaultnavigationtimeout) | Sets the default navigation timeout in milliseconds. | +| [BrowserContext.setDefaultTimeout(timeout)](/javascript-api/k6-experimental/browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. | +| [BrowserContext.setGeolocation(geolocation)](/javascript-api/k6-experimental/browser/browsercontext/setgeolocation) | Sets the `BrowserContext`'s geolocation. | +| [BrowserContext.setOffline(offline)](/javascript-api/k6-experimental/browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. | +| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](/javascript-api/k6-experimental/browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. | 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..a0e73d1733 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 From 3a8e4f1dadf0e18bcddbede321143e755484fafe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 11:04:15 +0300 Subject: [PATCH 10/12] Improve addCookies example Resolves: https://github.com/grafana/k6-docs/pull/1328#discussion_r1331052305 --- .../01 browser/02 BrowserContext/addCookies.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md index f38a387de3..a84eb0a904 100644 --- a/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md +++ b/src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/02 BrowserContext/addCookies.md @@ -48,7 +48,7 @@ export default async function () { name: 'testcookie', value: '1', sameSite: 'Strict', - domain: '127.0.0.1', + domain: 'httpbin.org', path: '/', httpOnly: true, secure: true, @@ -58,7 +58,7 @@ export default async function () { name: 'testcookie2', value: '2', sameSite: 'Lax', - domain: '127.0.0.1', + domain: 'httpbin.org', path: '/', expires: dayAfter, }, @@ -67,11 +67,18 @@ export default async function () { name: 'testcookie3', value: '3', sameSite: 'Lax', - domain: '127.0.0.1', + 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(); } From a83e0f485dac2bccd76f568a996252012280f183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 11:10:33 +0300 Subject: [PATCH 11/12] Improve clear cookies example --- .../01 browser/02 BrowserContext/clearCookies.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 a0e73d1733..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 @@ -29,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 } ``` From c942397ab6b5bf67fd9feef95706f2cad2be64f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 20 Sep 2023 12:48:08 +0300 Subject: [PATCH 12/12] Add cookie link to context add cookies Co-authored-by: Ankur Agarwal --- .../07 k6-experimental/01 browser/02 BrowserContext.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 793d01fec6..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 @@ -12,7 +12,7 @@ If a [page](/javascript-api/k6-experimental/browser/page/) opens another page, e | Method | Description | | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -| [BrowserContext.addCookies()](/javascript-api/k6-experimental/browser/browsercontext/addcookies/) | Adds cookies into the `BrowserContext`. | +| [BrowserContext.addCookies()](/javascript-api/k6-experimental/browser/browsercontext/addcookies/) | Adds [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) into the `BrowserContext`. | | [BrowserContext.clearCookies()](/javascript-api/k6-experimental/browser/browsercontext/clearcookies/) | Clear the `BrowserContext`'s [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie). | | [BrowserContext.clearPermissions()](/javascript-api/k6-experimental/browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | | [BrowserContext.cookies()](/javascript-api/k6-experimental/browser/browsercontext/cookies) | Returns a list of [cookies](/javascript-api/k6-experimental/browser/browsercontext/cookie) from the `BrowserContext`. |