> 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="https://295337183-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aroNmDWVJAmMjkdy0hx%2Fuploads%2FiRg5BiYv57vESFy1oQNG%2Fimage.png?alt=media&amp;token=0ae02586-3d6c-4a52-a402-2790086bae9f" 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="https://295337183-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aroNmDWVJAmMjkdy0hx%2Fuploads%2FnHIdqb58TuwoSXvG9xzv%2Fimage.png?alt=media&amp;token=e3ff4b60-548d-42ec-ac25-8fd421df07d1" 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="https://295337183-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aroNmDWVJAmMjkdy0hx%2Fuploads%2FsHvKxxWzNEvBcTCCBZMB%2Fimage.png?alt=media&amp;token=b1e8e503-bcee-456d-a2ce-0cd5f832d8c3" 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="https://295337183-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aroNmDWVJAmMjkdy0hx%2Fuploads%2FfFLp3c9LUFOdo4j41RYO%2Fimage.png?alt=media&amp;token=4cc5393e-593e-4fce-b3d5-a08a04a1cbdb" 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="https://295337183-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aroNmDWVJAmMjkdy0hx%2Fuploads%2Fuk1z5bymE8WAjpWu8jLy%2Fimage.png?alt=media&amp;token=5eb9186b-0a98-4e1b-8980-da80af77eb0f" 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
