You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sources/k6/v1.3.x/using-k6-browser/migrate-from-playwright-to-k6.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,12 @@ weight: 325
6
6
7
7
# Migrate a Playwright script to k6
8
8
9
-
Playwright is an end-to-end test framework for modern web apps. It can be used for web testing across browsers, mobile web testing, API testing, and general-purpose browser automation.
9
+
Playwright is an end-to-end testing framework for modern web apps. You can use it for web testing across browsers, mobile web testing, API testing, and general-purpose browser automation.
10
10
11
-
You can convert your Playwright scripts to k6 browser scripts, and then use them in:
11
+
You can convert your Playwright scripts to k6 browser scripts and use them in the following ways:
12
12
13
-
-k6 OSS or Grafana Cloud k6, to run performance tests and frontend testing at the same time, and see how your application behaves in a real-world scenario.
14
-
- Synthetic Monitoring, and make sure your application is being monitored and working correctly on a consistent schedule.
13
+
-Run performance tests and frontend testing simultaneously in k6 OSS or Grafana Cloud k6 to see how your application behaves in real-world scenarios.
14
+
-Use Synthetic Monitoring to ensure your application is monitored and working correctly on a consistent schedule.
15
15
16
16
In this guide, you'll learn the key differences between Playwright and k6, and how to migrate your scripts.
17
17
@@ -23,7 +23,7 @@ To run a k6 test, you'll need:
23
23
24
24
## Example migration
25
25
26
-
Here's an example Playwright script, and common steps you can take to migrate it to a k6 script:
26
+
The following example shows a Playwright script and the common steps to migrate it to a k6 script:
k6 browser is a library that's part of k6. Any Playwright script that you migrate needs to include the `import { browser } from 'k6/browser';` line at the top.
63
+
k6 browser is a library that's part of k6. Any Playwright script that you migrate must include the `import { browser } from 'k6/browser';` line at the top.
64
64
65
65
k6 browser doesn't implement a `test` framework. Instead, the logic of test is handled inside the `export default async function ()`.
66
66
@@ -91,7 +91,7 @@ export default async function () {
91
91
}
92
92
```
93
93
94
-
k6 doesn't implement `fixtures` like Playwright does. Instead, you'll need to use the `browser` class to retrieve a `page` within its own context. After that, you can use the usual `page` methods such as `goto` or `click`:
94
+
k6 doesn't implement fixtures like Playwright does. Instead, use the `browser` class to retrieve a `page` within its own context. After that, you can use the usual `page` methods such as `goto` or `click`:
This example shows a Playwright test file containing two tests. To migrate multiple tests, use k6 scenarios to create equivalent test logic:
179
+
The following example shows a Playwright test file that contains two tests. To migrate multiple tests, use k6 scenarios to create equivalent test logic:
180
180
181
181
```js
182
182
import { test, expect } from '@playwright/test';
@@ -246,11 +246,11 @@ export const options = {
246
246
};
247
247
248
248
export async functionadminLogin() {
249
-
// paste here
249
+
// Paste admin test code here
250
250
}
251
251
252
252
export async functionuserLogin() {
253
-
// paste here
253
+
// Paste user test code here
254
254
}
255
255
```
256
256
@@ -383,11 +383,11 @@ user ✓ [======================================] 1 VUs 00m06.0s/10m0s 1/1 sh
383
383
384
384
### Test isolation patterns
385
385
386
-
In k6, [scenarios](https://grafana.com/docs/k6/latest/using-k6/scenarios/) let you configure and model diverse workloads and organize your tests. Playwright has a dedicated test framework. The difference stems from k6 being a performance testing tool.
386
+
In k6, [scenarios](https://grafana.com/docs/k6/latest/using-k6/scenarios/) let you configure and model diverse workloads and organize your tests. Playwright has a dedicated testing framework. This difference stems from k6 being a performance testing tool.
387
387
388
388
{{< admonition type="note">}}
389
389
390
-
There are plans to create a test framework in k6. You can refer to this [GitHub issue](https://github.com/grafana/k6-jslib-testing/issues/30)for more details, or if you'd like to contribute.
390
+
There are plans to create a testing framework in k6. For more details or to contribute, refer to this [GitHub issue](https://github.com/grafana/k6-jslib-testing/issues/30).
391
391
392
392
{{< /admonition >}}
393
393
@@ -399,35 +399,35 @@ Refer to [Built-in metrics](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/me
399
399
400
400
### k6 concepts
401
401
402
-
k6 was originally designed as a load testing tool, so there are concepts that are important to understand in order to use the tool:
402
+
To effectively use k6 for browser testing, it's important to understand a few core concepts from its load testing foundation:
403
403
404
-
- **Virtual User**: A Virtual User (VU) is an independent thread of execution that runs concurrently with other VU threads. Often, scripts are designed so that one VU activity represents that of one real user.
405
-
- **Iteration**: The number of times a single VU will run the test script.
406
-
- **Thresholds and checks**: [Thresholds](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/thresholds/) are pass/fail criteria that you can configure for your test metrics. For example, you can configure a threshold for your test to fail if more than 1% of requests return an error. [Checks](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/checks/) validate a boolean condition in your test. For example, you can check if the status code for a response is equal to 200. The main difference between the two is that thresholds that are not met make a test finish with a failed status, while checks do not. k6 also provides a k6-testing library that behaves similarly to how assertions work in Playwright. Refer to the [k6-testing](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/) docs for more details.
404
+
- **Virtual User**: A Virtual User (VU) is an independent thread of execution that runs concurrently with other VUs. Scripts are often designed so that one VU represents the activity of one real user.
405
+
- **Iteration**: The number of times a single VU runs the test script.
406
+
- **Thresholds and checks**: [Thresholds](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/thresholds/) are pass/fail criteria that you configure for your test metrics. For example, you can configure a threshold to fail if more than 1% of requests return an error. [Checks](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/checks/) validate a boolean condition in your test. For example, you can check whether the response status code equals 200. The main difference is that unmet thresholds cause a test to finish with a failed status, while checks don't. k6 also provides a k6-testing library that behaves similarly to assertions in Playwright. For more details, refer to the [k6-testing](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/) documentation.
407
407
408
408
### Browser context restrictions
409
409
410
-
Unlike in Playwright, k6 can only work with a single `browserContext` at a time. The following script fails when running it with k6:
410
+
Unlike Playwright, k6 can only work with a single `browserContext` at a time. The following script fails when you run it with k6:
411
411
412
412
<!-- md-k6:skip -->
413
413
414
414
```js
415
415
const bc1 = await browser.newContext();
416
-
// This next call will result in an error "existing browser context must be closed before creating a new one"
416
+
// This next call results in an error:"existing browser context must be closed before creating a new one"
417
417
const bc2 = await browser.newContext();
418
418
```
419
419
420
-
To fix it, you'll have to close the existing `browserContext` first, before creating a new one.
420
+
To fix this, close the existing `browserContext` before creating a new one.
421
421
422
422
## Hybrid tests
423
423
424
-
Hybrid tests are performance tests that run browser-level and protocol-level requests at the same time. Hybrid tests are a great alternative to running resource-intensive browser-based load testing, while still measuring how an application performs under load by making requests to the frontend and backend.
424
+
Hybrid tests are performance tests that run browser-level and protocol-level requests simultaneously. They're a great alternative to resource-intensive browser-based load testing, while still measuring application performance under load by making requests to both the frontend and backend.
425
425
426
426
Refer to [Hybrid performance with k6 browser](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/hybrid-approach-to-performance/) for more details.
427
427
428
428
## Run k6 tests in Grafana Cloud
429
429
430
-
In addition to running k6 scripts locally by installing k6 in your machine, you can also use Grafana Cloud fora seamless experience. Using Grafana Cloud means you don't have to worry about if your machine has the right resources to run a performance test, while also having pre-made Grafana dashboards to analyze test results, and being able to collaborate with other team membersin debugging performance issues.
430
+
In addition to running k6 scripts locally by installing k6 on your machine, you can use Grafana Cloud for a seamless experience. Using Grafana Cloud means you don't have to worry about whether your machine has the right resources to run a performance test. You also get pre-made Grafana dashboards to analyze test results and can collaborate with other team members to debug performance issues.
431
431
432
432
Refer to [Run a test using Grafana Cloud k6](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/run-k6-test-script/#run-a-test-using-grafana-cloud-k6) for more details.
0 commit comments