> 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/use-ai-test-automation/test-authoring/create-tests/conditionals.md).

# Conditionals

Reference for conditional branching and guaranteed execution blocks in Harness AI Test Automation, including IF, ELSE, and FINALLY statements

## 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://3826766287-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkdyOjoJRBxElRIRFmNjP%2Fuploads%2FxUgJ2Yzw5BRLXRDVP3B3%2Fimage.png?alt=media&amp;token=61d8e231-c64e-496d-9943-0946dc3fe7af" alt="AIT step panel with Conditionals tab open listing IF, ELSE, ELSE IF, END IF, and FINALLY options"><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://3826766287-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkdyOjoJRBxElRIRFmNjP%2Fuploads%2FsGcztRGzCRGV4kI6FTeP%2Fimage.png?alt=media&amp;token=9e70933f-cf01-4631-b523-c11df99bc5b8" alt="ELSE IF configuration panel with User question selected and options for page HTML context and AI code generation"><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://3826766287-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkdyOjoJRBxElRIRFmNjP%2Fuploads%2F9weeMtVUNO0Iqjv6LjAk%2Fimage.png?alt=media&amp;token=0fe0d8e5-fa07-45a5-b4d0-58041b175716" alt="AIT test step panel showing a FINALLY block with two click steps and the AI input area below"><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/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 %}
