Loop and conditional conventions
Convention — Loop flows
Use one of three loop forms.
Repeat one statement
Repeat one statement up to a maximum count until an observable condition is achieved or no longer achieved. Stop immediately when the condition reaches the requested state.
Repeat clicking the "Refresh" button up to `10` times, until the "Status" field shows "Completed"
Then the "Status" field should show "Completed"Repeat clicking the "Load More" button up to `20` times, until the "Load More" button is no longer visible
Then all result rows should be loadedFail clearly if the condition does not reach the requested state within the maximum repetitions.
For each item, perform one statement
Use one line when every item needs one action.
For each row in the "Opportunity Products" table, ensure its "Selected" checkbox is checked
Then every row in the "Opportunity Products" table should be selectedFor each data point in the "Monthly Revenue" chart, record its label and value
Then the recorded data-point count should equal the rendered data-point countSTART LOOP and END LOOP
Use a block when every item or repetition needs multiple steps.
START LOOP for each unprocessed row in the "Quote Lines" table
When I fill `2` in the row's "Quantity" field
And I click "Calculate"
And I wait until the row's "Net Total" updates
And I mark the row as processed
END LOOP when no unprocessed quote-line rows remain
Then the processed row count should equal the quote-line row countSTART LOOP identifies the collection or starting state. END LOOP states the
achieved or no-longer-achieved stop condition.
REQUIRED
Every loop must have a finite collection, exact count, maximum attempt count, or maximum duration.
API-agent loop examples
Poll an asynchronous API job with a bounded repeat:
Given I call `POST /api/reports` with body [$.testdata.report.request]
And I require the response status code to be `202`
And I save response field `job_id` as [$.output.job_id]
Repeat calling `GET /api/jobs/[$.output.job_id]` every `10` seconds for up to `30` attempts, until response field `status` is "Completed" or "Failed"
Then the final response field `status` should be "Completed"Process paginated API responses with a loop block:
Given I set [$.runtime.next_page] to `/api/orders?limit=100`
START LOOP while [$.runtime.next_page] is not empty
When I call `GET [$.runtime.next_page]`
And I require the response status code to be `200`
And I append response array `items` to [$.output.orders]
And I set [$.runtime.next_page] to response field `next_page`
END LOOP when response field `next_page` is empty
Then the collected order count should equal response field `total_count`SQL-agent loop examples
Use a one-line loop for one operation per returned row:
Given I run the parameterized query `SELECT order_id, status FROM orders WHERE created_at >= :start_date ORDER BY order_id` with `start_date` [$.testdata.start_date]
For each returned row, record its `order_id` and `status`
Then the recorded row count should equal the SQL result row countUse a loop block for deterministic SQL batches:
Given I set [$.runtime.last_order_id] to `0`
START LOOP while the current SQL batch contains rows
When I run the parameterized query `SELECT order_id, status FROM orders WHERE order_id > :last_order_id ORDER BY order_id LIMIT :batch_size` with the current cursor and [$.testdata.batch_size]
And I require every returned `order_id` to be greater than [$.runtime.last_order_id]
And I append the returned rows to [$.output.orders]
And I set [$.runtime.last_order_id] to the greatest returned `order_id`
END LOOP when the SQL query returns no rows
Then the collected `order_id` values should be unique and strictly increasingUse parameterized, read-only SQL unless an authorized test explicitly requires mutation. Every SQL loop needs deterministic ordering and final row reconciliation.
Convention — Conditional flows
Conditions must describe observable application state.
Single-line IF and ELSE
Use one line when each branch has one action.
If the "Existing Account" result is visible, click its "Open" link; else click the "Create Account" button
Then the Account page or the "New Account" modal should be visibleSTART IF and END IF
Use a block when the achieved condition requires multiple steps.
START IF the "Approval Required" message is visible
When I select [$.testdata.approver.name] from the "Approver" lookup
And I fill [$.testdata.approval.reason] in the "Approval Reason" textarea
And I click the "Submit for Approval" button
Then the "Approval Status" field should show "Submitted"
END IFUse ELSE inside the block when a multi-step alternate path is required:
START IF the "Existing Account" result is visible
When I open the exact matching account
Then the Account record page should be visible
ELSE
When I click the "Create Account" button
Then the "New Account" modal should be visible
END IFThe IF branch runs when the condition is achieved. The ELSE branch runs when it is not achieved. Do not make a required assertion conditional.
API-agent conditional examples
Given I call `GET /api/customers/by-external-id/[$.testdata.customer.external_id]`
If the response status code is `200`, save response field `id` as [$.output.customer_id]; else call `POST /api/customers` with body [$.testdata.customer.create_request]
Then [$.output.customer_id] or the create-response field `id` should be non-emptyGiven I call `POST /api/exports` with body [$.testdata.export.request]
START IF the response status code is `202`
And I save response field `job_id` as [$.output.export_job_id]
Repeat calling `GET /api/exports/[$.output.export_job_id]` up to `30` times, until response field `status` is "Completed" or "Failed"
Then the final response field `status` should be "Completed"
END IFUse exact response status codes, fields, headers, array lengths, or business states as API conditions.
SQL-agent conditional examples
Given I run the parameterized query `SELECT COUNT(*) AS duplicate_count FROM customers WHERE external_id = :external_id` with [$.testdata.customer.external_id]
If `duplicate_count` is `0`, record the result as "Unique"; else record the result as "Duplicate"
Then the recorded result should equal [$.testdata.customer.expected_uniqueness]Given I run the parameterized query `SELECT customer_id, status, updated_at FROM customers WHERE external_id = :external_id` with [$.testdata.customer.external_id]
START IF the SQL query returns exactly one row
Then its `customer_id` should be non-empty
And its `status` should equal [$.testdata.customer.expected_status]
And its `updated_at` should be later than [$.testdata.test_start_time]
ELSE
Then the SQL result should fail with "Expected exactly one customer row"
END IFSQL conditions must identify the parameterized query, expected row count, and exact result columns. Keep database credentials and unescaped values out of SQL text.