Skip to content

Commit fc8599d

Browse files
committed
Add browser ctx limit example
1 parent 2bec7cb commit fc8599d

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/data/markdown/translated-guides/en/03 Using k6 browser/04 Migrating to k6 v0-46.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,44 @@ page.close();
265265

266266
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.
267267

268-
Please click on the links below to see usage examples.
269-
270268
* 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.
271269
* 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.
272270
* 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/).
273271

272+
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.
273+
274+
<CodeGroup labels={["Incorrect usage"]} lineNumbers={[true]}>
275+
276+
<!-- eslint-skip -->
277+
278+
```javascript
279+
export default async function() {
280+
const context1 = browser.newContext();
281+
// Simultaneous browser contexts are not permitted!
282+
const context2 = browser.newContext();
283+
}
284+
```
285+
286+
</CodeGroup>
287+
288+
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.
289+
290+
<CodeGroup labels={["Correct usage"]} lineNumbers={[true]}>
291+
292+
<!-- eslint-skip -->
293+
294+
```javascript
295+
export default async function() {
296+
const context1 = browser.newContext();
297+
context1.close();
298+
299+
// Since the first browser context is closed, a new browser context can be created.
300+
const context2 = browser.newContext();
301+
context2.close()
302+
}
303+
```
304+
305+
</CodeGroup>
306+
307+
274308
These updates make the usage of our API more straightforward for users, aiding in more consistent and automatic resource management.

0 commit comments

Comments
 (0)