Skip to content

Commit 076380b

Browse files
Apply suggestions from code review
Co-authored-by: Heitor Tashiro Sergent <[email protected]>
1 parent 863fba4 commit 076380b

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

docs/sources/v0.52.x/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/v0.52.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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-
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.
7+
# Simulate user input delay
8+
9+
On this page, you'll learn how to best work with `sleep` in `k6` and the various `wait*` prepended methods 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.
810

911
{{< admonition type="note" >}}
1012

@@ -17,15 +19,14 @@ While using the `sleep` or `page.waitForTimeout` functions to wait for element s
1719
[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:
1820

1921
- Navigating to a page.
20-
- 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.
2123
- Clicking on the element.
22-
- etc.
2324

2425
{{< admonition type="warning" >}}
2526

26-
`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.
2728

28-
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_.
2930

3031
{{< /admonition >}}
3132

@@ -39,11 +40,11 @@ In the browser modules there are various asynchronous APIs that can be used to w
3940
| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. |
4041
| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. |
4142
| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. |
42-
| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. **Use this instead of `sleep` in your frontend tests.** |
43+
| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. _Use this instead of `sleep` in your frontend tests_. |
4344

4445
## page.waitForFunction
4546

46-
[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.
4748

4849
{{< code >}}
4950

@@ -98,7 +99,7 @@ export default async function () {
9899

99100
## page.waitForLoadState
100101

101-
[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.
102+
[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.
102103

103104
{{< code >}}
104105

@@ -140,7 +141,7 @@ export default async function () {
140141

141142
## page.waitForNavigation
142143

143-
[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.
144145

145146
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.
146147

@@ -187,7 +188,7 @@ export default async function () {
187188

188189
## locator.waitFor
189190

190-
[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.).
191+
[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.
191192

192193
{{< code >}}
193194

@@ -222,7 +223,7 @@ export default async function () {
222223

223224
## page.waitForTimeout
224225

225-
[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.
226+
[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.
226227

227228
{{< code >}}
228229

docs/sources/v0.53.x/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/v0.53.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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-
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.
7+
# Simulate user input delay
8+
9+
On this page, you'll learn how to best work with `sleep` in `k6` and the various `wait*` prepended methods 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.
810

911
{{< admonition type="note" >}}
1012

@@ -17,33 +19,32 @@ While using the `sleep` or `page.waitForTimeout` functions to wait for element s
1719
[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:
1820

1921
- Navigating to a page.
20-
- 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.
2123
- Clicking on the element.
22-
- etc.
2324

2425
{{< admonition type="warning" >}}
2526

26-
`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.
2728

28-
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_.
2930

3031
{{< /admonition >}}
3132

3233
# What is `wait*`?
3334

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

36-
| Method | Description |
37-
| ------------------------------------------------ | ----------------------------------------------------------------------------- |
38-
| [page.waitForFunction](#pagewaitforfunction) | Waits for the given function to return a truthy value. |
39-
| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. |
40-
| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. |
41-
| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. |
42-
| [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_. |
4344

4445
## page.waitForFunction
4546

46-
[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.
4748

4849
{{< code >}}
4950

@@ -98,7 +99,7 @@ export default async function () {
9899

99100
## page.waitForLoadState
100101

101-
[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.
102+
[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.
102103

103104
{{< code >}}
104105

@@ -140,7 +141,7 @@ export default async function () {
140141

141142
## page.waitForNavigation
142143

143-
[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.
144145

145146
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.
146147

@@ -187,7 +188,7 @@ export default async function () {
187188

188189
## locator.waitFor
189190

190-
[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.).
191+
[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.
191192

192193
{{< code >}}
193194

@@ -222,7 +223,7 @@ export default async function () {
222223

223224
## page.waitForTimeout
224225

225-
[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.
226+
[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.
226227

227228
{{< code >}}
228229

0 commit comments

Comments
 (0)