Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/using-k6-browser/recommended-practices/sleep-vs-page-wait-for-timeout.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/using-k6-browser/recommended-practices/sleep-vs-page-wait-for-timeout/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
When to use sleep() and page.waitForTimeout()
In Grafana k6, both
sleep() and
page.waitForTimeout() can be used to add pauses in your test scripts, but they work very differently. Understanding these differences is essential for accurate and reliable browser testing.
sleep()is synchronous, blocking the thread and halting all background activity.page.waitForTimeout()is asynchronous, allowing background events to continue while pausing the script.
For most browser-based scenarios, you should use page.waitForTimeout() to simulate user delays without interrupting normal browser behavior or affecting performance measurements.
When to use sleep()
sleep() is a built-in k6 function designed to suspend Virtual User (VU) execution for a specified duration. It’s a synchronous function that blocks the JavaScript event loop, which means that all asynchronous work is also suspended until sleep completes.
It’s best used to simulate:
- Navigating to a page
- Sleeping for one second to simulate a user looking for a specific element on the page
- Clicking on the element
When to use page.waitForTimeout()
page.waitForTimeout() is a function provided by the k6 browser module that pauses script execution for a specified amount of time without blocking the JavaScript event loop.
Unlike sleep(), it’s asynchronous, meaning other browser activities, such as rendering, network requests, and event handling, can continue while the delay is in effect. This makes it ideal for browser based testing, where multiple asynchronous operations happen in the background.
You can use page.waitForTimeout() to simulate real user pauses, like reading a page, waiting for a visual change, or mimicking natural interaction timing, all without interrupting the browser’s internal processes.
Was this page helpful?
Related resources from Grafana Labs

