> For the complete documentation index, see [llms.txt](https://docs.bugbug.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bugbug.io/preventing-failed-tests/delay-sleep.md).

# Delay / Sleep

How to add a fixed delay before a step in BugBug, its unit and limits, when it runs, how it relates to the timeout and waiting conditions, and when to use it.

A delay makes BugBug wait a fixed number of **seconds** before a step starts.

You usually don't need it: BugBug already waits for the page and the element with [waiting conditions](https://docs.bugbug.io/preventing-failed-tests/waiting-conditions.md), only as long as necessary. A fixed delay makes tests slower and can still be too short on a slow day. But a delay can be a quick fix when you don't have time to find out what the step is waiting for.

## How the delay works

1. **The delay runs first.** BugBug waits for the delay, then starts checking the step's waiting conditions.
2. **It doesn't use up the timeout.** The step's [timeout](https://docs.bugbug.io/preventing-failed-tests/timeout.md) starts when the delay ends.
3. **It applies to one step.** To wait between two steps, set the delay on the second one.

For example, with a delay of `3` and a timeout of `30`, BugBug waits 3 seconds, then has up to 30 seconds to wait for the conditions and run the step.

## Add a delay to a step

Open the step and enter a whole number of seconds, up to 300, in the **Delay / Sleep** field. Leave it empty to use the project's default delay.

![Delay / Sleep field in the step settings](https://3168433179-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKixgeBPbLvnD0l1eiV%2Fuploads%2FxiksPKFV9ae5EKKQSCks%2FScreenshot%202022-04-11%20at%2019.27.20.png?alt=media\&token=4d880d62-ec87-43d9-9e91-16dfc362fa67)

In [YAML](https://docs.bugbug.io/export-import-and-backup/yaml-schema.md) the field is `execution.sleep`, and in the API and MCP `sleep`, both in seconds:

```yaml
- action: click
  action_details:
    selector: "css=button.submit"
  execution:
    sleep: 2
```

## Default delay for the project

[Project settings](https://docs.bugbug.io/preventing-failed-tests/project-settings.md) have a default delay that applies to every step without its own delay. It's **0 seconds** by default. Keep it at 0: a delay on every step makes all tests slower.

## Better options than a delay

| Problem                                        | Instead of a delay                                                                           |
| ---------------------------------------------- | -------------------------------------------------------------------------------------------- |
| The element appears after a while              | Nothing: **Element is visible** already waits for it.                                        |
| A loader or modal covers the element           | **Element is not covered** waits for it to disappear.                                        |
| The page keeps loading data                    | **Page network requests are finished** waits for the requests.                               |
| The button is disabled until the form is valid | **Element must be active** waits for it.                                                     |
| The element must reach a specific state        | Add **Element must have expected attribute**, for example `data-state="ready"`.              |
| A step needs more time than usual              | Raise the [timeout](https://docs.bugbug.io/preventing-failed-tests/timeout.md) of that step. |

## FAQ

<details>

<summary>Is the delay in seconds or milliseconds?</summary>

Seconds, as a whole number from 0 to 300. For a 30-second delay, enter `30`.

</details>

<details>

<summary>Does the delay run before or after the step?</summary>

Before. BugBug waits for the delay, then checks the step's waiting conditions and runs it. The delay doesn't count towards the step's timeout.

</details>

<details>

<summary>How do I make the test wait 30 seconds before the next step?</summary>

Set **Delay / Sleep** to `30` on the next step. There's no separate wait step. If you need to wait for something specific, a waiting condition or an assertion is faster and more reliable.

</details>

<details>

<summary>Can an Execute JavaScript step wait, for example with await?</summary>

Yes. The code can use `await`, and BugBug waits until it finishes, within the step's timeout. For example, `await new Promise((resolve) => setTimeout(resolve, 3000));` waits 3 seconds. For a plain fixed wait, the **Delay / Sleep** field is simpler.

</details>

<details>

<summary>What does the default delay in project settings do?</summary>

It adds the same delay before every step that doesn't have its own. It's 0 by default, and it's best to keep it that way.

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bugbug.io/preventing-failed-tests/delay-sleep.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
