diff --git a/docs/sources/next/javascript-api/k6-experimental/browser/_index.md b/docs/sources/next/javascript-api/k6-experimental/browser/_index.md index 3930c4742c..885a29b0da 100644 --- a/docs/sources/next/javascript-api/k6-experimental/browser/_index.md +++ b/docs/sources/next/javascript-api/k6-experimental/browser/_index.md @@ -39,6 +39,7 @@ The browser module is the entry point for all your tests, and it is what interac | Method | Description | | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [browser.context()](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/context) | Returns the current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/browsercontext). | +| [browser.closeContext()](/javascript-api/k6-experimental/browser/closecontext) | Closes the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/). | | [browser.isConnected](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/isconnected) | Indicates whether the [CDP](https://chromedevtools.github.io/devtools-protocol/) connection to the browser process is active or not. | | [browser.newContext([options])](/javascript-api/k6-experimental/browser/newcontext/) | Creates and returns a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/browsercontext). | | [browser.newPage([options])](/javascript-api/k6-experimental/browser/newpage) | Creates a new [Page](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/page) in a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/browsercontext) and returns the page. Pages that have been opened ought to be closed using [`Page.close`](https://grafana.com/docs/k6//javascript-api/k6-experimental/browser/page/close). Pages left open could potentially distort the results of Web Vital metrics. | diff --git a/docs/sources/next/javascript-api/k6-experimental/browser/closecontext.md b/docs/sources/next/javascript-api/k6-experimental/browser/closecontext.md new file mode 100644 index 0000000000..9bd666b26b --- /dev/null +++ b/docs/sources/next/javascript-api/k6-experimental/browser/closecontext.md @@ -0,0 +1,46 @@ +--- +title: 'closeContext()' +excerpt: 'Browser module: close context method' +--- + +Closes the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/). If there is no active browser context, because none has been created yet or because it has been previously closed, this method throws an error. + + +### 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 page1 = browser.newPage({ + isMobile: true, + }); // implicitly creates a new context + + await page1.goto('https:/test.k6.io/'); + page1.close(); + browser.closeContext(); // closes the context created on newPage + + const page2 = browser.newPage({ + isMobile: false, + }); // creates a new context with different settings + + await page2.goto('https://test.k6.io/'); + page2.close(); + browser.closeContext(); + + browser.closeContext(); // throws an error as browser has no active context +} +```