Skip to content

Commit 3c19473

Browse files
Update Handle stale elements docs (#2116)
* Update wait-dom-changes.md * Rename wait-dom-changes to handle-dynamic-elements * Update page weight * Apply to previous versions * Apply force click info to previous versions Related to #2109
1 parent 1f804b7 commit 3c19473

File tree

32 files changed

+204
-34
lines changed

32 files changed

+204
-34
lines changed

docs/sources/k6/next/using-k6-browser/recommended-practices/clean-up-test-resources-page-close.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 'Use Page.close to clean up test resources'
33
description: 'Use Page.close in your browser tests to ensure k6 properly flushes metrics and cleans up resources'
4-
weight: 01
4+
weight: 650
55
---
66

77
# Use Page.close to clean up test resources

docs/sources/k6/next/using-k6-browser/recommended-practices/wait-dom-changes.md renamed to docs/sources/k6/next/using-k6-browser/recommended-practices/handle-dynamic-elements.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
---
2-
title: 'Handling Stale or Dynamic Elements After Navigation'
2+
title: 'Handle stale or dynamic elements after navigation'
33
description: 'Wait for elements to appear in k6 browser tests to avoid interacting with stale or dynamic content'
4-
weight: 01
4+
weight: 50
55
---
66

7-
# How To Handle Stale or Dynamic Elements After Navigation
8-
Modern websites often update the DOM asynchronously after navigation or user interactions. Waiting for navigation to complete is not sufficient, as test scripts may still fail or attempt to interact with elements that are not yet available. Instead, wait for specific elements to appear before continuing your test. Use [locator APIs](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) such as `waitFor` to ensure elements are ready for interaction. This is especially important when testing SPAs or any pages with dynamic content, where elements may be added, removed, or updated asynchronously.
7+
# Handle stale or dynamic elements after navigation
8+
9+
Modern websites often update the DOM asynchronously after navigation or user interactions. Waiting for navigation to complete isn't sufficient, as test scripts may still fail or attempt to interact with elements that aren't yet available.
10+
11+
To avoid these issues, wait for specific elements to appear before you continue your test. Use [locator APIs](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) such as `waitFor` to ensure elements are ready for interaction.
12+
13+
This approach is especially important when you test single-page applications (SPAs) or any pages with dynamic content, where elements may be added, removed, or updated asynchronously.
914

1015
## Example
11-
{{< code >}}
1216

1317
```javascript
1418
import { browser } from 'k6/browser';
@@ -36,5 +40,3 @@ export default async function () {
3640
});
3741
}
3842
```
39-
40-
{{< /code >}}

docs/sources/k6/next/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Hybrid approach to performance'
33
heading: 'Hybrid performance with k6 browser'
44
head_title: 'Hybrid performance with k6 browser'
55
description: 'An example on how to implement a hybrid approach to performance with k6 browser'
6-
weight: 200
6+
weight: 100
77
---
88

99
# Hybrid performance with k6 browser

docs/sources/k6/next/using-k6-browser/recommended-practices/prevent-cookie-banners-blocking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Prevent cookie banners from blocking interactions"
33
description: "How to reveal and dismiss cookie banners in k6 browser tests to prevent blocked interactions and improve test reliability."
4-
weight: 100
4+
weight: 350
55
---
66

77
# Prevent cookie banners from blocking interactions

docs/sources/k6/v1.1.x/javascript-api/k6-browser/elementhandle/click.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Mouse click on the chosen element.
2020
| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. |
2121
| options.clickCount | number | `1` | The number of times the action is performed. |
2222
| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. |
23-
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). |
23+
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). Avoid using `force: true` in `click` actions, as it may mask underlying test design or app issues. It bypasses normal user interaction checks, and elements may not be clickable due to [cookie banner interference](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/prevent-cookie-banners-blocking/). |
2424
| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. |
2525
| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. |
2626
| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. |

docs/sources/k6/v1.1.x/javascript-api/k6-browser/frame/click.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This method clicks on an element matching a `selector`.
2121
| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. |
2222
| options.clickCount | number | `1` | The number of times the action is performed. |
2323
| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. |
24-
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). |
24+
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). Avoid using `force: true` in `click` actions, as it may mask underlying test design or app issues. It bypasses normal user interaction checks, and elements may not be clickable due to [cookie banner interference](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/prevent-cookie-banners-blocking/). |
2525
| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. |
2626
| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. |
2727
| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. |

docs/sources/k6/v1.1.x/javascript-api/k6-browser/locator/click.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Mouse click on the chosen element.
2020
| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. |
2121
| options.clickCount | number | `1` | The number of times the action is performed. |
2222
| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. |
23-
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). |
23+
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). Avoid using `force: true` in `click` actions, as it may mask underlying test design or app issues. It bypasses normal user interaction checks, and elements may not be clickable due to [cookie banner interference](https://grafana.com/docs/k6/latest/using-k6-browser/recommended-practices/prevent-cookie-banners-blocking/). |
2424
| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. |
2525
| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. |
2626
| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. |

docs/sources/k6/v1.1.x/javascript-api/k6-browser/page/click.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This method clicks on an element matching a `selector`.
2121
| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. |
2222
| options.clickCount | number | `1` | The number of times the action is performed. |
2323
| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. |
24-
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). |
24+
| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). Avoid using `force: true` in `click` actions, as it may mask underlying test design or app issues. It bypasses normal user interaction checks, and elements may not be clickable due to [cookie banner interference](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/recommended-practices/prevent-cookie-banners-blocking/). |
2525
| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. |
2626
| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. |
2727
| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. |

docs/sources/k6/v1.1.x/using-k6-browser/recommended-practices/clean-up-test-resources-page-close.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 'Use Page.close to clean up test resources'
33
description: 'Use Page.close in your browser tests to ensure k6 properly flushes metrics and cleans up resources'
4-
weight: 01
4+
weight: 650
55
---
66

77
# Use Page.close to clean up test resources
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: 'Handle stale or dynamic elements after navigation'
3+
description: 'Wait for elements to appear in k6 browser tests to avoid interacting with stale or dynamic content'
4+
weight: 50
5+
---
6+
7+
# Handle stale or dynamic elements after navigation
8+
9+
Modern websites often update the DOM asynchronously after navigation or user interactions. Waiting for navigation to complete isn't sufficient, as test scripts may still fail or attempt to interact with elements that aren't yet available.
10+
11+
To avoid these issues, wait for specific elements to appear before you continue your test. Use [locator APIs](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) such as `waitFor` to ensure elements are ready for interaction.
12+
13+
This approach is especially important when you test single-page applications (SPAs) or any pages with dynamic content, where elements may be added, removed, or updated asynchronously.
14+
15+
## Example
16+
17+
```javascript
18+
import { browser } from 'k6/browser';
19+
20+
export const options = {
21+
scenarios: {
22+
browser: {
23+
executor: 'shared-iterations',
24+
options: {
25+
browser: {
26+
type: 'chromium',
27+
},
28+
},
29+
},
30+
},
31+
};
32+
33+
export default async function () {
34+
const page = await browser.newPage();
35+
36+
await page.goto('https://test.k6.io/browser.php');
37+
const text = page.locator('#input-text-hidden');
38+
await text.waitFor({
39+
state: 'hidden',
40+
});
41+
}
42+
```

0 commit comments

Comments
 (0)