Skip to content

Commit 158f1a7

Browse files
ankur22inancgumus
authored andcommitted
Update browser.contexts to browser.context
We've made a change which means that there is now a 1-to-1 mapping between browser and a browserContext. This means that there can only ever be one browserContext open at any given point per browser. So the contexts method on browser now only needs to return the current browserContext.
1 parent 8514dcd commit 158f1a7

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The browser module is the entry point for all your tests, and it is what interac
3535

3636
| Method | Description |
3737
|-------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
38-
| [browser.contexts()](/javascript-api/k6-experimental/browser/contexts) | Lets you access all open [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/)s. |
38+
| [browser.context()](/javascript-api/k6-experimental/browser/contexts) | Returns the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/). |
3939
| [browser.isConnected](/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. |
4040
| [browser.newContext([options])](/javascript-api/k6-experimental/browser/newcontext/) <BWIPT id="455"/> | Creates and returns a new [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/). |
4141
| [browser.newPage([options])](/javascript-api/k6-experimental/browser/newpage) <BWIPT id="455"/> | Creates a new [Page](/javascript-api/k6-experimental/browser/page/) in a new [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) and returns the page. |

src/data/markdown/docs/02 javascript api/07 k6-experimental/01 browser/contexts.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
---
22
title: 'contexts()'
3-
excerpt: 'Browser module: contexts method'
3+
excerpt: 'Browser module: context method'
44
---
55

6-
Access all open [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/)s.
6+
Returns the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/).
7+
8+
<Blockquote mod="note" title="">
9+
10+
A 1-to-1 mapping between [Browser](/javascript-api/k6-experimental/browser) and `BrowserContext` means you cannot run `BrowserContexts` concurrently. If you wish to create a new `BrowserContext` while one already exists, you will need to [close](/javascript-api/k6-experimental/browser/browsercontext/close) the current one, and create a new one with either [newContext](/javascript-api/k6-experimental/browser/newcontext/) or [newPage](/javascript-api/k6-experimental/browser/newpage). All resources associated to the closed `BrowserContext` will also be closed and cleaned up (such as [Page](/javascript-api/k6-experimental/browser/page/)s).
11+
12+
</Blockquote>
713

814
### Returns
915

10-
| Type | Description |
11-
| ----- | ------------------------------------------------------------------------------ |
12-
| Array | Array of [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) objects |
16+
| Type | Description |
17+
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
18+
| object \| null | The current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) if one has been created, otherwise `null`. |
1319

1420

1521
### Example
@@ -31,11 +37,13 @@ export const options = {
3137
}
3238

3339
export default function () {
34-
console.log(browser.contexts().length); // 0
40+
console.log(browser.context()); // null
3541

36-
const context = browser.newContext();
37-
console.log(browser.contexts().length); // 1
42+
const page = browser.newPage();
43+
const context = browser.context();
44+
console.log(context); // {"base_event_emitter":{}}
3845

46+
page.close();
3947
context.close();
4048
}
4149
```

0 commit comments

Comments
 (0)