---
title: Conditionals & Loops
description: "Handle optional UI with IF/ELSE and repeat actions with WHILE in YAML E2E tests, evaluated by AI or with js: conditions."
---

# Conditionals & Loops

<div class="view-markdown-wrapper">
<ViewMarkdown />
</div>

## Conditional Logic

Handle optional UI elements with IF/ELSE:

```yaml
statements:
  - IF: cookie consent dialog is visible
    THEN:
      - intent: Click "Accept All"
  - IF: user is logged in
    THEN:
      - intent: Click the logout button
    ELSE:
      - intent: Click the login button
      - intent: Enter credentials and submit
```

Conditions are evaluated by the AI at runtime using the current page state. JavaScript conditions are also supported with the `js:` prefix:

```yaml
- IF: "js: testContext.retryCount < 3"
  THEN:
    - intent: Click the retry button
```

::: warning
`js:` conditions have no AI fallback — if the JavaScript fails, the condition fails. Avoid brittle DOM checks (e.g., `document.querySelector('.some-class')`). Use `js:` only for simple, reliable checks like URL matching or counters. For UI state checks, prefer natural language conditions.
:::

## Loops

Repeat actions until a condition is met with WHILE:

```yaml
statements:
  - WHILE: "Load More" button is visible
    DO:
      - intent: Click the "Load More" button
      - intent: Wait for new items to appear
    timeout_ms: 30000
  - VERIFY: all items are loaded
```

JavaScript conditions work in loops too (same `js:` caveat applies):

```yaml
- WHILE: "js: testContext.itemCount < 10"
  DO:
    - intent: Click "Load More"
    - description: Increment the item counter
      js: "testContext.itemCount = (testContext.itemCount || 0) + 1"
```
