> For the complete documentation index, see [llms.txt](https://docs.walnutai.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.walnutai.ai/test-and-quality-management/test-management/add-control-flow-conditional-statements/while-loop.md).

# While Loop

While Loop enables a test case to repeatedly execute a set of steps as long as a specified condition evaluates to true, allowing controlled and condition-based iteration within the test flow.

If certain steps need to be repeated until a condition becomes false (for example, retry login until successful), add them under the **While** section.

* Click **"While Loop"** to add a loop block in the test case.

<figure><img src="/files/nxrOF6pPbgkgwCFI8f6K" alt=""><figcaption></figcaption></figure>

* **Set Condition for While - Choose how to set the condition:**
  * **Element**
  * **Parameter**

* **Element** - Choose **"Element"** when the condition depends on an object (UI element, text, etc.) on the screen.

  * From the dropdown, select the required option:
    * Conditions (is visible, is not visible)
    * Actions
    * Interactions (click, type, focus, etc.)
  * Select the required "**Object"** (stored using XPath / Playwright).
  * Set **Max iterations** (default: 100).
    * **Example:**\
      To retry login until the success message appears:
      * While **"Login Successful"** message is not visible → condition is true
      * If the message becomes visible → condition becomes false
      * The system will keep retrying login until the message appears or max iterations are reached
  * Click "**Save"** to apply the condition.

  <figure><img src="/files/95B6LnmVK4VB1qq9452I" alt=""><figcaption></figcaption></figure>

* **Parameter** - Choose **"Parameter"** when the condition depends on a value.

  * From the **1st dropdown**, choose an existing parameter OR Click "**Create** to add a new parameter" :
    * Enter **Name** (e.g.,retry\_count)
    * Enter **Value** (e.g., 0)
    * Select **Type** (String, Number, etc.)
    * Click **Create**
  * From the **2nd dropdown**, select the comparison type:
    * equals
    * not equals
    * greater than
    * less than
    * contains
  * Enter the comparison value (e.g., 3).
  * Set **Max iterations**.
    * **Example:**\
      To retry login for limited attempts:
      * While retry\_count < 3 → condition is true
        * If retry\_count ≥ 3 → condition becomes false
        * The system will retry the steps until the condition becomes false or max iterations are reached
  * Click "**Save"** to apply the condition.

  <figure><img src="/files/vJSazcj5NY1gq8VKaKqY" alt=""><figcaption></figcaption></figure>

* **While (Loop Steps)**

  * Add the required steps under **While**.
  * These steps execute repeatedly as long as the condition is true.

  <figure><img src="/files/7te8gbWN1JAVTgDMSUdZ" alt=""><figcaption></figcaption></figure>

* End While

  * End While marks the end of the While loop.
  * It closes the loop block and completes the flow.
    * The loop stops when:
      * Condition becomes false
      * OR max iterations limit is reached
    * If needed, update or modify the description
    * Click **"Save"** after changing description

  <figure><img src="/files/0K2og62fjOycfi4zOJzg" alt=""><figcaption></figcaption></figure>

* **Example Flow**
  * While login is not successful:
    * Enter username and password
    * Click login
    * Wait for response
  * After End While:
    * If login is successful → go to dashboard
    * Else → show error and stop execution
  * Continue with remaining test steps


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.walnutai.ai/test-and-quality-management/test-management/add-control-flow-conditional-statements/while-loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
