-
Notifications
You must be signed in to change notification settings - Fork 239
Update the "Using k6 browser" section #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4fc28b7
Update k6 browser homepage to use k6 new command
heitortsergent 1f073dd
Create a write your first browser test page
heitortsergent 3a80162
Fix alias on using k6 browser homepage
heitortsergent 3f68dc6
Fix weight on k6 browser page section
heitortsergent 147994f
Add section for how to write k6 browser tests
heitortsergent 917dbb0
Move interact with elements on a webpage to its own page
heitortsergent a79ac0d
Move asynchronous operation section to a separate page
heitortsergent 38b4a29
Update hybrid performance testing page
heitortsergent e21cfa4
Update page weight
heitortsergent 2a261c8
Update recommended practices weight
heitortsergent c5792d6
Merge branch 'main' into k6-browser-updates
heitortsergent 2983f21
Update Asynchronous operations page
heitortsergent 1955aa1
Skip code block
heitortsergent c9301fd
Skip code blocks
heitortsergent 17ad0f5
Update Write your first browser test
heitortsergent 1e62824
Fix hybrid performance code example
heitortsergent 14a3005
Skip code block
heitortsergent b7bad5c
Update docs/sources/k6/next/using-k6-browser/how-to-write-browser-tes…
heitortsergent 3af4bfa
Add heading link to JavaScript promises
heitortsergent 51c79d8
Add small example at the top of the page
heitortsergent 7faa81c
Update docs/sources/k6/next/using-k6-browser/how-to-write-browser-tes…
heitortsergent 89cacb8
Merge branch 'main' into k6-browser-updates
heitortsergent c0c6d4e
Apply to v1.1
heitortsergent 5828b85
Apply to v1.0
heitortsergent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
docs/sources/k6/next/using-k6-browser/how-to-write-browser-tests/_index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: How to write browser tests | ||
description: 'Learn how to write k6 browser tests.' | ||
weight: 300 | ||
--- | ||
|
||
# How to write browser tests | ||
|
||
{{< section >}} |
150 changes: 150 additions & 0 deletions
150
.../k6/next/using-k6-browser/how-to-write-browser-tests/asynchronous-operations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
--- | ||
title: Asynchronous operations | ||
description: 'Learn how the k6 browser module uses asynchronous operations.' | ||
weight: 100 | ||
--- | ||
|
||
# Asynchronous operations | ||
|
||
Most methods in the browser module return [JavaScript promises](#why-the-browser-module-uses-asynchronous-apis), and k6 scripts must be written to handle this properly. This usually means using the `await` keyword to wait for the async operation to complete. | ||
|
||
For example: | ||
|
||
<!-- eslint-skip --> | ||
|
||
```js | ||
const page = await browser.newPage(); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
const locator = page.locator('button[name="pizza-please"]'); | ||
|
||
await locator.click(); | ||
``` | ||
|
||
In addition to using `await`, another important part of writing k6 browser tests is handling page navigations. There are two recommended methods for doing that: using `Promise.all` or using the `waitFor` method. | ||
|
||
heitortsergent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Promise.all | ||
|
||
To avoid timing errors or other race conditions in your script, if you have actions that load up a different page, you need to make sure that you wait for that action to finish before continuing. | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; | ||
|
||
export const options = { | ||
scenarios: { | ||
ui: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
thresholds: { | ||
checks: ['rate==1.0'], | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.goto('https://test.k6.io/my_messages.php'); | ||
|
||
await page.locator('input[name="login"]').type('admin'); | ||
await page.locator('input[name="password"]').type('123'); | ||
|
||
const submitButton = page.locator('input[type="submit"]'); | ||
|
||
await Promise.all([page.waitForNavigation(), submitButton.click()]); | ||
|
||
await check(page.locator('h2'), { | ||
header: async (lo) => (await lo.textContent()) == 'Welcome, admin!', | ||
}); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
The preceding code uses `Promise.all([])` to wait for the two promises to be resolved before continuing. Since clicking the submit button causes page navigation, `page.waitForNavigation()` is needed because the page won't be ready until the navigation completes. This is required because there can be a race condition if these two actions don't happen simultaneously. | ||
|
||
Then, you can use [`check`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6/check) from the k6 API to assert the text content of a specific element. Finally, you close the page and the browser. | ||
|
||
## Wait for specific elements | ||
|
||
We also encourage the use of `locator.waitFor` where possible. When you navigate to a website, there are usually one or more elements that are important for your test. Once those elements load, you can safely proceed to the next step. For example, in a search scenario: | ||
|
||
1. Navigate to the search site | ||
1. Wait for the search bar and submit button to appear | ||
1. Fill in the search bar with the query | ||
1. Click the submit button | ||
1. Wait for the search results | ||
|
||
We should be able to do these actions like so: | ||
|
||
<!-- eslint-skip --> | ||
|
||
```js | ||
await page.goto('https://my-search-engine.com'); | ||
|
||
const searchBar = page.locator('.search-bar'); | ||
const submitButton = page.locator('.submit-button'); | ||
|
||
await searchBar.waitFor(); | ||
await submitButton.waitFor(); | ||
|
||
await searchBar.fill('k6'); | ||
await submitButton.click(); | ||
|
||
const searchResults = page.locator('.search-results-table'); | ||
|
||
await searchResults.waitFor(); | ||
``` | ||
|
||
This avoids the use of `Promise.all` which can be confusing to work with, and instead makes the script easier to follow. | ||
|
||
## Why the browser module uses asynchronous APIs | ||
|
||
The browser module uses asynchronous APIs that require `await` for several reasons: | ||
|
||
1. JavaScript is single-threaded with a single event loop. Asynchronous APIs prevent blocking the thread and event loop with long-running or I/O-based tasks. | ||
1. Consistency with [Playwright](https://playwright.dev/), a popular browser automation library. | ||
1. Alignment with how developers expect to work with modern JavaScript APIs. | ||
|
||
For example: | ||
|
||
<!-- eslint-skip --> | ||
|
||
```js | ||
const page = await browser.newPage(); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
const locator = page.locator('button[name="pizza-please"]'); | ||
|
||
await locator.click(); | ||
``` | ||
|
||
API calls that interact with Chromium are asynchronous and require `await` to ensure completion before proceeding. Synchronous APIs, such as `page.locator`, do not require `await`, but using it does not cause issues since the JavaScript runtime will simply return the value immediately. | ||
|
||
If you don't add `await` on asynchronous APIs, it can cause the script to finish before the test completes, resulting in errors like `"Uncaught (in promise) TypeError: Object has no member 'goto'"`. That can happen because the page object from `browser.newPage()` without an `await` is actually a JavaScript promise. You can try and see the error using the following code snippet: | ||
|
||
<!-- eslint-skip --> | ||
|
||
```js | ||
const page = browser.newPage(); | ||
|
||
page.goto('https://quickpizza.grafana.com/'); // An error should occur since we're not using await in the line above. | ||
|
||
const locator = page.locator('button[name="pizza-please"]'); | ||
|
||
locator.click(); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.