> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/ai-test-automation/3.0/use-ai-test-automation/test-authoring/create-tests/conditionals.md).

# Conditionals

## Overview

Conditionals enable you to create dynamic test flows with different execution paths based on specified conditions. Harness AI Test Automation provides two types of control flow mechanisms:

**Conditional Branching**: Use IF, ELSE IF, ELSE, and END IF statements to build branching logic that executes different steps based on runtime conditions.

**Guaranteed Execution**: Use FINALLY blocks to ensure critical cleanup or verification steps always run at the end of a test, regardless of whether the test passes or fails.

<figure><img src="https://3045140541-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuDnmxay2kxlZlHUlzYGy%2Fuploads%2Fgit-blob-97eee21adf73a8633f4a23d63f5f3d79f65dc046%2Fimage%20(2).png?alt=media" alt=""><figcaption></figcaption></figure>

## Conditional statements

### IF

Creates conditional branches in test execution based on specified conditions.

| Feature                         | Description                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Description**                 | Evaluates a condition that must return TRUE or FALSE. If the condition evaluates to true, the subsequent steps within the conditional block are executed.                                                                                                                                                                                                                              |
| **Parameter option - Question** | <p>• <strong>User question</strong>: Prompts the user with a question during test execution<br>• <strong>Text exists</strong>: Checks if specific text is present on the page<br>• <strong>Text does not exist</strong>: Verifies that specific text is not present on the page<br>• <strong>Use custom script</strong>: Accepts custom JavaScript that evaluates to TRUE or FALSE</p> |
| **Advanced options**            | <p>• <strong>Use page HTML as context</strong>: When enabled, evaluates the condition against the page's HTML source<br>• <strong>Disable AI code generation for this assertion</strong>: When enabled, causes Copilot to rely solely on visual inspection.</p>                                                                                                                        |
| **Return value**                | TRUE or FALSE                                                                                                                                                                                                                                                                                                                                                                          |

{% hint style="info" %}
**USAGE**\
Every IF block must be closed with an END IF statement. Tests with unclosed IF blocks cannot be saved.
{% endhint %}

### ELSE

Creates an alternative execution path.

| Feature               | Description                                                                                                         |
| --------------------- | ------------------------------------------------------------------------------------------------------------------- |
| **Description**       | Used to create an alternative path for test execution when the preceding IF or ELSE IF conditions evaluate to FALSE |
| **Parameter options** | None                                                                                                                |
| **Advanced options**  | None                                                                                                                |
| **Return value**      | None                                                                                                                |

{% hint style="info" %}
**USAGE**

ELSE must be preceded by an IF or ELSE IF statement within the same conditional block.
{% endhint %}

### ELSE IF

Creates an alternative execution path.

| Feature                         | Description                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Description**                 | Evaluates an additional condition when the preceding IF or ELSE IF conditions evaluate to FALSE. If this condition returns true, the subsequent steps within this block are executed.                                                                                                                                                                                                  |
| **Parameter option - Question** | <p>• <strong>User question</strong>: Prompts the user with a question during test execution<br>• <strong>Text exists</strong>: Checks if specific text is present on the page<br>• <strong>Text does not exist</strong>: Verifies that specific text is not present on the page<br>• <strong>Use custom script</strong>: Accepts custom JavaScript that evaluates to TRUE or FALSE</p> |
| **Advanced options**            | <p>• <strong>Use page HTML as context</strong>: When enabled, evaluates the condition against the page's HTML source<br>• <strong>Disable AI code generation for this assertion</strong>: When enabled, causes Copilot to rely solely on visual inspection.</p>                                                                                                                        |
| **Return value**                | TRUE or FALSE                                                                                                                                                                                                                                                                                                                                                                          |

<figure><img src="https://3045140541-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuDnmxay2kxlZlHUlzYGy%2Fuploads%2Fgit-blob-09a3ac7a4a2b7b8d7e780cdc845e9785b7dbd54e%2Fimage%20(5).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**USAGE**\
ELSE IF must be preceded by an IF or another ELSE IF statement within the same conditional block.
{% endhint %}

### END IF

Marks the end of a conditional block, completing the IF/ELSE IF/ELSE structure.

| Feature               | Description                                                                           |
| --------------------- | ------------------------------------------------------------------------------------- |
| **Description**       | Used to end a conditional block, marking the conclusion of IF/ELSE IF/ELSE statements |
| **Parameter options** | None                                                                                  |
| **Advanced options**  | None                                                                                  |
| **Return value**      | None                                                                                  |

{% hint style="info" %}
**USAGE**\
END IF must be preceded by an IF, ELSE IF, or ELSE statement to close the conditional block.
{% endhint %}

### FINALLY

Ensures critical cleanup or verification steps are always executed, regardless of test outcome.

| Feature               | Description                                                                                              |
| --------------------- | -------------------------------------------------------------------------------------------------------- |
| **Description**       | Defines a block of steps that will always execute at the end of a test, whether the test passes or fails |
| **Parameter options** | None                                                                                                     |
| **Advanced options**  | None                                                                                                     |
| **Return value**      | None                                                                                                     |

<figure><img src="https://3045140541-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuDnmxay2kxlZlHUlzYGy%2Fuploads%2Fgit-blob-20de0535575208bb23b8cad519abcea115e9e0a0%2Fimage%20(6).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**INFO**

* **Scope**: Each test can have at most one FINALLY block.
* **Can contain**: Any test steps, including IF/ELSE conditionals.
* **Restrictions**: Cannot be nested inside conditional blocks or used within [tasks](/ai-test-automation/3.0/use-ai-test-automation/test-authoring/create-tests/tasks.md).
  {% endhint %}

> Use FINALLY blocks to ensure your test environment is always left in a clean state, even when unexpected failures occur. This helps maintain test reliability and prevents side effects that could impact subsequent test runs.

{% @harness-feedback/feedback %}
