Skip to content

Commit 404b5d9

Browse files
committed
Update simulate user input delay in next section
1 parent 076380b commit 404b5d9

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

docs/sources/next/using-k6-browser/recommended-practices/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ This section presents some examples and recommended practices when working with
1111
- [Hybrid approach to performance](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/hybrid-approach-to-performance)
1212
- [Page object model pattern](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/page-object-model-pattern)
1313
- [Selecting elements](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/selecting-elements)
14-
- [Simulating user input delay](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/simulate-user-input-delay)
14+
- [Simulate user input delay](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/simulate-user-input-delay)

docs/sources/next/using-k6-browser/recommended-practices/simulate-user-input-delay.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: 'Simulating user input delay'
2+
title: 'Simulate user input delay'
33
description: 'A guide on how to simulate user input delay.'
44
weight: 04
55
---
66

7-
# Simulating user input delay
7+
# Simulate user input delay
88

99
We will demonstrate how best to work with `sleep` in `k6` and the various `wait*` prepended methods that are available in `k6/browser` to simulate user input delay, wait for navigations, and wait for element state changes. By the end of this page, you should be able to successfully use the correct API where necessary.
1010

@@ -19,33 +19,32 @@ While using the `sleep` or `page.waitForTimeout` functions to wait for element s
1919
[sleep](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6/sleep) is a first class function built into k6. It's main use is to _"suspend VU execution for the specified duration"_ which is most useful when you want to simulate user input delay, such as:
2020

2121
- Navigating to a page.
22-
- Sleeping for a 1 second, which is to simulate a user looking for a specific element on the page.
22+
- Sleeping for one second to simulate a user looking for a specific element on the page.
2323
- Clicking on the element.
24-
- etc.
2524

2625
{{< admonition type="warning" >}}
2726

28-
`sleep` is a synchronous function that blocks the JS event loop, which means that all asynchronous work will also be suspended until the `sleep` completes.
27+
`sleep` is a synchronous function that blocks the JavaScript event loop, which means that all asynchronous work will also be suspended until `sleep` completes.
2928

30-
The browser module predominantly provides asynchronous APIs, and so it's best to avoid working with `sleep`, and instead **we recommend you use [page.waitForTimeout](#pagewaitfortimeout)**.
29+
The browser module predominantly provides asynchronous APIs, so it's best to avoid working with `sleep`. Instead, _use the [page.waitForTimeout](#pagewaitfortimeout) function_.
3130

3231
{{< /admonition >}}
3332

3433
# What is `wait*`?
3534

3635
In the browser modules there are various asynchronous APIs that can be used to wait for certain states:
3736

38-
| Method | Description |
39-
| ------------------------------------------------ | ----------------------------------------------------------------------------- |
40-
| [page.waitForFunction](#pagewaitforfunction) | Waits for the given function to return a truthy value. |
41-
| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. |
42-
| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. |
43-
| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. |
44-
| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. **Use this instead of `sleep` in your frontend tests.** |
37+
| Method | Description |
38+
| ------------------------------------------------ | --------------------------------------------------------------------------- |
39+
| [page.waitForFunction](#pagewaitforfunction) | Waits for the given function to return a truthy value. |
40+
| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. |
41+
| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. |
42+
| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. |
43+
| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. _Use this instead of `sleep` in your frontend tests_. |
4544

4645
## page.waitForFunction
4746

48-
[page.waitForFunction](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforfunction) is useful when you want more control over when a test progresses with a javascript function that returns true when a condition (or many conditions) is met. It can be used to poll for changes in the dom or non dom elements and variables.
47+
[page.waitForFunction](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforfunction) is useful when you want more control over when a test progresses with a javascript function that returns true when a condition (or many conditions) is met. It can be used to poll for changes in the DOM or non-DOM elements and variables.
4948

5049
{{< code >}}
5150

@@ -101,7 +100,7 @@ export default async function () {
101100

102101
## page.waitForLoadState
103102

104-
[page.waitForLoadState](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate) is useful when there’s no explicit navigation, but you need to wait for the page or network to settle. This is mainly used when working with single page applications or when no full page reloads happen.
103+
[page.waitForLoadState](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate) is useful when there’s no explicit navigation, but you need to wait for the page or network to settle. This is mainly used when working with single-page applications or when no full page reloads happen.
105104

106105
{{< code >}}
107106

@@ -142,7 +141,7 @@ export default async function () {
142141

143142
## page.waitForNavigation
144143

145-
[page.waitForNavigation](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation) is a very useful API when performing other actions that could start a page navigation, and they don't automatically wait for a navigation to end. Usually you'll find it in our examples with a `click` API call. Note that [goto](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/goto) is an example of an API that **doesn't** require `waitForNavigation` since it will automatically wait for the navigation to complete before returning.
144+
[page.waitForNavigation](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation) is a very useful API when performing other actions that could start a page navigation, and they don't automatically wait for the navigation to end. Usually, you'll find it in our examples with a `click` API call. Note that [goto](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/goto) is an example of an API that _doesn't_ require `waitForNavigation` since it will automatically wait for the navigation to complete before returning.
146145

147146
It's important to call this in a [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) along with the API that will cause the navigation to start.
148147

@@ -191,7 +190,7 @@ export default async function () {
191190

192191
## locator.waitFor
193192

194-
[locator.waitFor](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/waitfor/) will wait until the element meets the waiting criteria. It's useful when dealing with dynamic websites where elements may take time to appear or change state (they might load after some delay due to async calls, JavaScript execution, etc.).
193+
[locator.waitFor](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/waitfor/) will wait until the element meets the waiting criteria. It's useful when dealing with dynamic websites where elements may take time to appear or change state. For example, if elements load after some delay due to async calls, or because of slow JavaScript execution.
195194

196195
{{< code >}}
197196

@@ -226,7 +225,7 @@ export default async function () {
226225

227226
## page.waitForTimeout
228227

229-
[page.waitForTimeout](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfortimeout) will wait the given amount of time. It's functionally the same as k6's [sleep](#What-is-`sleep`) but it is asynchronous which means it will not block the event loop, thus allowing the background tasks to continue to be worked on. We're also planning on instruementing it with tracing to then allow us visualize it in the timeline in grafana cloud k6.
228+
[page.waitForTimeout](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfortimeout) will wait the given amount of time. It's functionally the same as k6's [sleep](#What-is-`sleep`), but it's asynchronous, which means it will not block the event loop and allows the background tasks to continue to be worked on.
230229

231230
{{< code >}}
232231

0 commit comments

Comments
 (0)