Skip to content

Commit

Permalink
Add browser ctx limit example
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Aug 14, 2023
1 parent 2bec7cb commit fc8599d
Showing 1 changed file with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,44 @@ page.close();

The new browser implementation limits the usage to a single active [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) per iteration. This change enhances the prediction of resource requirements for a test run and promotes the use of [scenarios](/using-k6/scenarios/) to separate independent browser sessions.

Please click on the links below to see usage examples.

* If a new [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) needs to be created, the existing one must be closed first using the [browserContext.close()](/javascript-api/k6-experimental/browser/browsercontext/close) method.
* A new [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) can be created either with the [browser.newContext()](/javascript-api/k6-experimental/browser/newcontext/) or [browser.newPage()](/javascript-api/k6-experimental/browser/newpage) methods.
* Alongside these changes, the method `browser.contexts()` has been altered to [browser.context()](/javascript-api/k6-experimental/browser/context/) to retrieve the current [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/).

For instance, the code below will not function as intended, as it attempts to execute two simultaneous [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/)s within the same iteration.

<CodeGroup labels={["Incorrect usage"]} lineNumbers={[true]}>

<!-- eslint-skip -->

```javascript
export default async function() {
const context1 = browser.newContext();
// Simultaneous browser contexts are not permitted!
const context2 = browser.newContext();
}
```

</CodeGroup>

On the other hand, the subsequent example will function correctly by closing the initial [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) prior to establishing a new one.

<CodeGroup labels={["Correct usage"]} lineNumbers={[true]}>

<!-- eslint-skip -->

```javascript
export default async function() {
const context1 = browser.newContext();
context1.close();

// Since the first browser context is closed, a new browser context can be created.
const context2 = browser.newContext();
context2.close()
}
```

</CodeGroup>


These updates make the usage of our API more straightforward for users, aiding in more consistent and automatic resource management.

0 comments on commit fc8599d

Please sign in to comment.