Skip to content

Commit 145777f

Browse files
ankur22ka3de
andcommitted
Add browser.closeContext() docs
Co-authored-by: ka3de <[email protected]>
1 parent 78cf939 commit 145777f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

docs/sources/next/javascript-api/k6-experimental/browser/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The browser module is the entry point for all your tests, and it is what interac
3939
| Method | Description |
4040
| --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4141
| [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). |
42+
| [browser.closeContext()](/javascript-api/k6-experimental/browser/closecontext) | Closes the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/). |
4243
| [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. |
4344
| [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). |
4445
| [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. |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: 'closeContext()'
3+
excerpt: 'Browser module: close context method'
4+
---
5+
6+
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.
7+
8+
9+
### Example
10+
11+
```javascript
12+
import { browser } from 'k6/experimental/browser';
13+
14+
export const options = {
15+
scenarios: {
16+
browser: {
17+
executor: 'shared-iterations',
18+
options: {
19+
browser: {
20+
type: 'chromium',
21+
},
22+
},
23+
},
24+
},
25+
}
26+
27+
export default async function () {
28+
const page1 = browser.newPage({
29+
isMobile: true,
30+
}); // implicitly creates a new context
31+
32+
await page1.goto('https:/test.k6.io/');
33+
page1.close();
34+
browser.closeContext(); // closes the context created on newPage
35+
36+
const page2 = browser.newPage({
37+
isMobile: false,
38+
}); // creates a new context with different settings
39+
40+
await page2.goto('https://test.k6.io/');
41+
page2.close();
42+
browser.closeContext();
43+
44+
browser.closeContext(); // throws an error as browser has no active context
45+
}
46+
```

0 commit comments

Comments
 (0)