This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Overview
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.
What is sleep()
?
sleep()
is a built-in k6 function designed to suspend Virtual User (VU) execution for a specified duration. It is a synchronous function that blocks the JavaScript event loop, which means that all asynchronous work will also be suspended until sleep completes. It is 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
What is 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 is 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.