> ## Documentation Index
> Fetch the complete documentation index at: https://momentic.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# if

> Run a block of steps only when a condition holds.

Branching control flow. The body of `if` declares exactly one condition
(`assert`, `assertVisually`, `checkPageContains`, `checkPageDoesNotContain`,
`javascript`, or any `checkElement...`) plus a `then` array. A failing condition
skips the `then` block rather than failing the test.

## Parameters

| Parameter     | Type                 | Required | Description                                                           |
| ------------- | -------------------- | -------- | --------------------------------------------------------------------- |
| `<condition>` | `AssertionCondition` | Yes      | Exactly one assertion condition at the top level. See description.    |
| `then`        | `Step[]`             | Yes      | Steps to run when the condition is true.                              |
| `saveAs`      | `string`             | No       | Name of the variable to write this step's return value to.            |
| `retries`     | `number`             | No       | Number of times to retry the step on failure before failing the test. |
| `skipped`     | `boolean`            | No       | Skip this step at execution time.                                     |

## Examples

```yaml theme={null}
- if:
    assert:
      that: the page shows a modal for an event/announcement
      timeout: 20000
    then:
      - act: close the announcement pop up if it is still visible
```

```yaml theme={null}
- if:
    assert: Either of the username and password fields are empty
    then:
      - type:
          text: "{{ env.USERNAME }}"
          into: username field
          clear: inputs
      - type:
          text: "{{ env.PASSWORD }}"
          into: The password input
          clear: inputs
```

```yaml theme={null}
- if:
    checkElementExists: the apply button
    then:
      - navigate: https://www.momentic.ai
```

```yaml theme={null}
- if:
    javascript: return env.IS_PRODUCTION
    then:
      - wait: 20000
```

## Related

* [Test format](/core-concepts/test-format)
* [Writing assertions](/core-concepts/writing-assertions)
