Skip to content

Commit e86f4da

Browse files
Merge pull request #1299 from grafana/chore/k6-browser-migration-guide
Updates to k6 browser migration guide
2 parents 1be4116 + b6e3123 commit e86f4da

File tree

1 file changed

+38
-74
lines changed

1 file changed

+38
-74
lines changed

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

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ excerpt: 'A migration guide to ease the process of transitioning to the new k6 b
44
slug: '/using-k6-browser/migrating-to-k6-v0-46/'
55
---
66

7-
This guide outlines [the key changes](#key-changes) users will need to make when moving their existing k6 browser test scripts to the new browser module (bundled up with [k6 version 0.46](https://github.com/grafana/k6/releases/tag/v0.46.0)).
7+
This guide outlines the key changes you will need to make when moving your existing k6 browser test scripts to the new [k6 browser module](/javascript-api/k6-experimental/browser/) (bundled with [k6 version 0.46](https://github.com/grafana/k6/releases/tag/v0.46.0)).
88

9-
The latest release simplifies the management of the browser lifecycle by automatically providing and releasing browser resources for users. To facilitate this, a new mandatory field is introduced to define the browser type within [scenario options](#scenario-options).
9+
## Key changes
10+
11+
The updated version introduces notable structural changes in its operation and API, including breaking changes:
1012

11-
Previously, users had to launch and close the browser processes themselves. With the recent updates, the API has abstracted the details of `browserType` (i.e., `chromium`). Consequently, the `chromium` named export, previously found in `k6/experimental/browser`, has been replaced with `browser`.
13+
* The [import path](#import-path) for the browser module has switched from `chromium` to [`browser`](/javascript-api/k6-experimental/browser/#browser-module-api).
14+
* [Simplified resource management](#simplified-resource-management). The browser module now handles the startup and shutdown of browser processes automatically. The `chromium.launch()`, `chromium.connect()`, and `browser.close()` methods are no longer necessary and have been removed.
15+
* [Browser options](#browser-options) can now only be set using [environment variables](/javascript-api/k6-experimental/browser/#browser-module-options).
16+
* [Scenario options](#scenario-options) must now be defined for running browser tests.
17+
* [Single browser context per iteration](#browser-context-limit). You can now only run a single [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) at a time in the same iteration.
1218

1319
<Blockquote mod="note" title="">
1420

15-
Users no longer need to use the `K6_BROWSER_ENABLED` flag when running browser tests with the `k6` command.
21+
You no longer need to use the `K6_BROWSER_ENABLED` flag when running browser tests with the `k6` command.
1622

1723
</Blockquote>
1824

19-
2025
## Before and after comparison
2126

22-
Let's start with an overview of the primary differences between the previous and new versions of the k6 browser API. Subsequent sections will delve into each difference in detail.
27+
Let's start with an overview of the main differences between the previous and new versions of the k6 browser API.
2328

2429
<CodeGroup labels={["Before: The previous k6 browser API"]} lineNumbers={[true]}>
2530

@@ -77,23 +82,9 @@ export default async function () {
7782

7883
</CodeGroup>
7984

80-
81-
## Key changes
82-
83-
The updated version introduces notable structural changes in its operation and API. Let's take a look at them.
84-
85-
* The [import path](#import-path) for the browser module has switched from `chromium` to [browser](/javascript-api/k6-experimental/browser/#browser-module-api).
86-
* [Browser options](#browser-options) can now only be set using certain [environment variables](/javascript-api/k6-experimental/browser/#browser-module-options). The `launch()` method, used earlier for this purpose, has been removed.
87-
* [Scenario options](#scenario-options) must now be defined for running browser tests.
88-
* [Simplified resource management](#simplified-resource-management). The browser module now handles the startup and shutdown of browser processes automatically. `chromium.launch()`, `chromium.connect()`, and `browser.close()` methods are no longer necessary, as these methods have been removed.
89-
* [Single browser context per iteration](#browser-context-limit). Users can now only run a single [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) at a time in the same iteration.
90-
91-
92-
93-
9485
## Import path
9586

96-
Changes have been made to how the [browser module](/javascript-api/k6-experimental/browser/) is imported. With the browser type (specifically `chromium`) now set in [scenario options](#scenario-options), users should directly import the [browser](/javascript-api/k6-experimental/browser/#browser-module-api) object from the [browser module](/javascript-api/k6-experimental/browser/). Previously, users relied on `chromium.launch()` for accessing a running browser. Now, a simple import of the [browser](/javascript-api/k6-experimental/browser/#browser-module-api) is sufficient.
87+
With the browser type (specifically `chromium`) now set in [scenario options](#scenario-options), you should directly import the [browser](/javascript-api/k6-experimental/browser/#browser-module-api) object from the [browser module](/javascript-api/k6-experimental/browser/).
9788

9889
<CodeGroup labels={["Before: Import Path"]} lineNumbers={[true]}>
9990

@@ -111,15 +102,17 @@ import { browser } from 'k6/experimental/browser';
111102

112103
</CodeGroup>
113104

105+
## Simplified resource management
114106

107+
The browser lifecycle is now automatically managed by the browser module, and so the `chromium.launch()`, `chromium.connect()`, and `browser.close()` methods are no longer necessary and have been removed.
115108

116-
109+
Now, all that is needed is to specify the `browser.type` within the [scenario options](#scenario-options). If the option is set, a browser instance will automatically start at the beginning and close at the end of each test iteration.
117110

118111
## Browser options
119112

120-
In k6 v0.46.0, the need to manually start a browser via `chromium.launch()` or `chromium.connect()` and set its configuration through these methods has been removed, so this part can simply be omitted from test scripts. Users can still change some browser settings by using environment variables. For more information, refer to the [browser module options](/javascript-api/k6-experimental/browser/#browser-module-options) documentation.
113+
With the removal of the `chromium.launch()` and `chromium.connect()` methods, setting browsers options is now done by using environment variables. For more information, refer to [Browser Module Options](/javascript-api/k6-experimental/browser/#browser-module-options).
121114

122-
### Before:
115+
Before:
123116

124117
<CodeGroup lineNumbers={[true]}>
125118

@@ -136,7 +129,7 @@ export default async function () {
136129

137130
</CodeGroup>
138131

139-
### After:
132+
After:
140133

141134
<CodeGroup labels={["Bash", "Docker", "Windows: CMD", "Windows: PowerShell"]} lineNumbers={[false]}>
142135

@@ -160,20 +153,15 @@ PS C:\k6> $env:K6_BROWSER_HEADLESS="false" ; $env:K6_BROWSER_TIMEOUT='60s' ; k6
160153

161154
</CodeGroup>
162155

163-
164156
<Blockquote mod="note" title="">
165157

166-
The following browser options are no longer supported: `devtools`, `env`, and `proxy` since they weren't providing much value. Although `slowMo` has been temporarily removed, we're working on reintroducing it.
158+
The following browser options are no longer supported: `devtools`, `env`, and `proxy` since they weren't providing much value. `slowMo` has been temporarily removed, and we're working on reintroducing it.
167159

168160
</Blockquote>
169161

170-
171-
172-
173162
## Scenario options
174163

175-
In k6 v0.46.0, users must set the [executor](/using-k6/scenarios/executors/) and browser type as options in a [k6 scenario](/using-k6/scenarios/) definition. Specifically, the `browser.type` option should be set to `chromium`, as Chromium is the only browser supported.
176-
164+
You must now set the [executor](/using-k6/scenarios/executors/) and browser type as options in a [scenario](/using-k6/scenarios/) definition. Specifically, the `browser.type` option should be set to `chromium`, as Chromium is the only browser supported.
177165

178166
<CodeGroup labels={["After"]} lineNumbers={[true]}>
179167

@@ -196,68 +184,45 @@ export const options = {
196184

197185
</CodeGroup>
198186

199-
Previously, users were required to handle the creation and closing of the browser instance using `chromium.launch()` or `chromium.connect()` for creation, and the `browser.close()` method for releasing resources before ending the iteration. This repetitive code has been eliminated.
200-
201-
Now, all that is needed is to specify the browser type within the [scenario options](#scenario-options). A browser instance will be automatically created and closed for each iteration by the browser module, streamlining the process.
202-
203-
This change allows to identify the test as a browser test and provides automatic control of the browser's lifecycle. Users no longer need to start or stop the browser manually through the browser API. If the `browser.type` option is set in the scenario options, a browser instance will automatically start at the beginning and close at the end of each test iteration.
204-
187+
## Opening and closing a page
205188

206-
## Opening a new page
189+
You can open a new page by using the imported [browser](/javascript-api/k6-experimental/browser/#browser-module-api) object's [browser.newPage()](/javascript-api/k6-experimental/browser/newpage) method. You can still use the [Page](/javascript-api/k6-experimental/browser/page/) object as before.
207190

208-
Users can open a new page by using the imported [browser](/javascript-api/k6-experimental/browser/#browser-module-api) object's [browser.newPage()](/javascript-api/k6-experimental/browser/newpage) method. Users can still use the [Page](/javascript-api/k6-experimental/browser/page/) object as before.
209-
210-
<CodeGroup labels={["After"]} lineNumbers={[true]}>
191+
<CodeGroup labels={["Before"]} lineNumbers={[true]}>
211192

212193
<!-- eslint-skip -->
213194

214195
```javascript
215196
export default async function () {
197+
const browser = chromium.launch();
216198
const page = browser.newPage();
217199
// ...
218200
page.close();
201+
browser.close();
219202
}
220203
```
221204
</CodeGroup>
222205

223-
<Blockquote mod="note" title="">
224-
225-
Closing of the page is critical for the calculation of accurate Web Vital metrics. See the [browser metrics](/using-k6-browser/browser-metrics/) for more details.
226-
227-
</Blockquote>
228-
229-
230-
231-
232-
## Simplified resource management
233-
234-
As [mentioned earlier](#scenario-options), in the new API, there's a shift in how the browser's lifecycle is managed.
235-
236-
Since the browser lifecycle is automatically managed by the browser module, the closing of the browser has been simplified. The explicit `browser.close()` call has been removed. Simply close the page using the [page.close()](/javascript-api/k6-experimental/browser/page/close/) method as in the example below.
237-
238-
<CodeGroup labels={["Before"]} lineNumbers={[true]}>
239-
240-
<!-- eslint-skip -->
241-
242-
```javascript
243-
page.close();
244-
browser.close();
245-
```
246-
247-
</CodeGroup>
248-
249206
<CodeGroup labels={["After"]} lineNumbers={[true]}>
250207

251208
<!-- eslint-skip -->
252209

253210
```javascript
254-
page.close();
211+
export default async function () {
212+
const page = browser.newPage();
213+
// ...
214+
page.close();
215+
}
255216
```
256-
257217
</CodeGroup>
258218

219+
The `browser.close()` method has been removed, so you can remove that from your scripts and use [`page.close()`](/javascript-api/k6-experimental/browser/page/close/) once you're done using the page object.
259220

221+
<Blockquote mod="note" title="">
260222

223+
Closing of the page is critical for the calculation of accurate Web Vital metrics. See the [browser metrics](/using-k6-browser/browser-metrics/) for more details.
224+
225+
</Blockquote>
261226

262227
## Browser context limit
263228

@@ -283,7 +248,7 @@ export default async function() {
283248

284249
</CodeGroup>
285250

286-
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.
251+
On the other hand, the next example will function correctly by closing the initial [BrowserContext](/javascript-api/k6-experimental/browser/browsercontext/) prior to establishing a new one.
287252

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

@@ -302,7 +267,6 @@ export default async function() {
302267

303268
</CodeGroup>
304269

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

306-
These updates make the usage of our API more straightforward for users, aiding in more consistent and automatic resource management.
307-
308-
For all the details, make sure to review the complete changelog for [k6 version 0.46](https://github.com/grafana/k6/releases/tag/v0.46.0). For more information watch [k6 Office Hours #98](https://www.youtube.com/watch?v=fK6Hpvt0pY0), where we discuss the latest changes in k6 browser, and, as always, ask in [the community forum](https://community.grafana.com/c/grafana-k6/k6-browser/79) if you need our help!
272+
For all the details, make sure to review the complete changelog for [k6 version 0.46](https://github.com/grafana/k6/releases/tag/v0.46.0). For more information watch [k6 Office Hours #98](https://www.youtube.com/watch?v=fK6Hpvt0pY0), where we discuss the latest changes in k6 browser, and, as always, ask in [the community forum](https://community.grafana.com/c/grafana-k6/k6-browser/79) if you need help!

0 commit comments

Comments
 (0)