Skip to content

Commit

Permalink
Add browser.closeContext() docs
Browse files Browse the repository at this point in the history
Co-authored-by: ka3de <[email protected]>
  • Loading branch information
ankur22 and ka3de committed Nov 14, 2023
1 parent 672a21f commit 46d6448
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/<K6_VERSION>/javascript-api/k6-experimental/browser/context) | Returns the current [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/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/<K6_VERSION>/javascript-api/k6-experimental/browser/isconnected) <BWIPT id="453"/> | Indicates whether the [CDP](https://chromedevtools.github.io/devtools-protocol/) connection to the browser process is active or not. |
| [browser.newContext([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/newcontext/) <BWIPT id="455"/> | Creates and returns a new [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |
| [browser.newPage([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/newpage) <BWIPT id="455"/> | Creates a new [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page) in a new [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/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/<K6_VERSION>/javascript-api/k6-experimental/browser/page/close). Pages left open could potentially distort the results of Web Vital metrics. |
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
```

0 comments on commit 46d6448

Please sign in to comment.