---
title: Variables, store and Control Flow in Selenium IDE
description: 'Recorded steps are a straight line: do this, then this, then check that.
  Real pages need a little more: remember a value and compare it later, repeat an…'
source_url:
  html: https://testingbot.com/resources/courses/selenium-ide/variables-and-control-flow
  md: https://testingbot.com/resources/courses/selenium-ide/variables-and-control-flow.md
---

Course

[Selenium IDE: Codeless Test Automation](https://testingbot.com/resources/courses/selenium-ide)

Chapter 5 of 10

1. [1 Selenium IDE Tutorial: Codeless Test Automation](https://testingbot.com/resources/courses/selenium-ide)
2. [2 Install Selenium IDE and Record Your First Test](https://testingbot.com/resources/courses/selenium-ide/install-and-record-your-first-test)
3. [3 Commands, Targets and Locators in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/commands-targets-and-locators)
4. [4 Assertions, Verifications and Waits in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/assertions-verifications-and-waits)
5. [5 Variables, store and Control Flow in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/variables-and-control-flow)
6. [6 Reusing Tests and Organising Suites in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/reusing-tests-and-suites)
7. [7 Run .side Files with selenium-side-runner](https://testingbot.com/resources/courses/selenium-ide/run-side-files-with-selenium-side-runner)
8. [8 Run Selenium IDE Tests on the TestingBot Grid](https://testingbot.com/resources/courses/selenium-ide/run-on-testingbot)
9. [9 Export Selenium IDE Tests to Code, and When to Graduate](https://testingbot.com/resources/courses/selenium-ide/export-to-code)
10. [10 Knowledge check](https://testingbot.com/resources/courses/selenium-ide/knowledge-check)

[Exit course](https://testingbot.com/resources/courses)

 Chapter 5 of 10 Beginner 8 min read 2 quick checks Updated Nov 2026 

# Variables, store and Control Flow in Selenium IDE
On this page
1. [Storing values](https://testingbot.com#storing-values)
2. [Variables inside JavaScript](https://testingbot.com#variables-inside-javascript)
3. [Environment variables](https://testingbot.com#environment-variables)
4. [if, elseIf, else, end](https://testingbot.com#if-elseif-else-end)
5. [times, while, do and repeatIf, forEach](https://testingbot.com#times-while-do-and-repeatif-foreach)
6. [When control flow is a sign to move to code](https://testingbot.com#when-control-flow-is-a-sign-to-move-to-code)

Recorded steps are a straight line: do this, then this, then check that. Real pages need a little more: remember a value and compare it later, repeat an action a number of times, or do something only when a condition holds. Selenium IDE covers this with variables and a set of control flow commands (`if`, `while`, `times` and friends), still without writing a program. This chapter builds the two tests in the "Variables and control flow" suite.

## Storing values[#](https://testingbot.com#storing-values)

Every `store...` command saves something under a variable name. You then use it anywhere a target or value is expected by writing `${name}`.

| Command | Target | Value | Stores |
| --- | --- | --- | --- |
| `store` | `tomsmith` | `username` | the text you typed in the target |
| `storeText` | `css=h2` | `heading` | an element's visible text |
| `storeValue` | `id=username` | `typed` | an input field's current value |
| `storeAttribute` | `id=username@name` | `fieldName` | one attribute: the locator, `@`, the attribute name |
| `storeElementCount` | `css=.added-manually` | `added` | how many elements match |
| `storeTitle` | `pageTitle` | | the page title |
| `executeScript` | `return document.querySelectorAll('input').length` | `inputCount` | whatever the script returns |

Two of these are easy to get wrong:

- **`storeTitle` takes the variable name as its target** , not its value, because it needs no locator.
- **`storeAttribute` packs the locator and the attribute name into one field** , separated by the last `@`.

To check a variable, use `assert` (or `verify`) with the variable name, without `${}`, as the target and the expected value as the value. To print one while you debug, use `echo`.

Here is the "Store and reuse values" test:

| Command | Target | Value |
| --- | --- | --- |
| `open` | `/login` | |
| `storeText` | `css=h2` | `heading` |
| `echo` | `The heading is ${heading}` | |
| `assert` | `heading` | `Login Page` |
| `storeTitle` | `pageTitle` | |
| `assert` | `pageTitle` | `The Internet` |
| `storeAttribute` | `id=username@name` | `fieldName` |
| `assert` | `fieldName` | `username` |
| `executeScript` | `return document.querySelectorAll('input').length` | `inputCount` |
| `assert` | `inputCount` | `2` |

Run with the runner's `--steps` option (chapter 6), the `echo` line shows up in the output:

```
info: ✓ Store and reuse values › storeText | css=h2 | heading
info: echo: The heading is Login Page
info: ✓ Store and reuse values › assert | heading | Login Page
```

Variables live for one test run. Two tests in a suite do not share them, unless the suite uses a persistent session (chapter 5).

You stored the heading with `storeText | css=h2 | heading`. Which command checks it?

 `assert | ${heading} | Login Page` `assert | heading | Login Page` `assertText | heading | Login Page`

`assert` takes the variable's name as its target. `${heading}` would be replaced by the text first, so the command would look for a variable called "Login Page". `assertText` expects a locator, not a variable.

## Variables inside JavaScript[#](https://testingbot.com#variables-inside-javascript)

`executeScript`, and the conditions of `if` and `while`, are JavaScript that runs in the browser. Variables work differently there: the IDE does not paste the text in, it passes each variable to the script as an argument and replaces `${heading}` with `arguments[0]`. So **do not put quotes around a variable in a script** :

| Condition | Result in our run |
| --- | --- |
| `${heading} === "Login Page"` | true |
| `"${heading}" === "Login Page"` | false: the script compares the literal text `arguments[0]` |

This one trips up many people, because quoting looks right and outside scripts (in `echo`, `type`, `open`) the text really is pasted in.

## Environment variables[#](https://testingbot.com#environment-variables)

`${env:NAME}` reads the environment variable `NAME` of the process that runs the test. On the command line that is your shell; in the IDE it is the IDE's own process, where the variable is normally not set and reads as `undefined`. We ran the same test twice:

```
info: echo: echo sees Login Page and env undefined
```

```
DEMO_VAR=yes selenium-side-runner quoting.side
info: echo: echo sees Login Page and env yes
info: echo: DEMO_VAR is yes
```

That makes environment variables the one way to give a `.side` file a value from outside: a step can behave differently in CI or on a cloud grid than on your laptop. Chapter 7 uses exactly this to report results to TestingBot only when the tests run there. Do not put real secrets in them for tests you share: `echo` and the runner's debug output print what they contain.

## if, elseIf, else, end[#](https://testingbot.com#if-elseif-else-end)

Conditional steps run only when a JavaScript condition is true. Every block ends with `end`:

| Command | Target |
| --- | --- |
| `if` | `${added} === 0` |
| `echo` | `All elements removed` |
| `else` | |
| `echo` | `${added} elements left` |
| `end` | |

`elseIf` takes a condition too. A missing `end` stops the test before its first step: when we deleted the `end` above, the runner failed with `SyntaxError: Incomplete block at else`.

Use `if` sparingly in tests. A test that does one thing on some runs and another thing on others is hard to trust. Good uses are optional pop-ups (a cookie banner that only appears on the first visit) and environment differences, not "if the bug happens, skip the check".

## times, while, do and repeatIf, forEach[#](https://testingbot.com#times-while-do-and-repeatif-foreach)

| Loop | Use it for |
| --- | --- |
| `times` with a number as target | doing the same steps a fixed number of times |
| `while` with a condition | repeating while something is true, checked before each pass |
| `do` ... `repeatIf` with a condition | the same, checked after each pass, so it runs at least once |
| `forEach` with an array variable and an iterator name | going through a list, such as one stored with `executeScript` returning an array |

Every loop has a safety limit of 1000 passes. A loop that would run forever fails with "Max retry limit exceeded"; you can set a different limit in the loop command's value field.

Here is the "Add and remove elements" test. The page's **Add Element** button adds a **Delete** button each time it is clicked:

| Command | Target | Value |
| --- | --- | --- |
| `open` | `/add_remove_elements/` | |
| `times` | `3` | |
| `click` | `css=button[onclick='addElement()']` | |
| `end` | | |
| `storeElementCount` | `css=.added-manually` | `added` |
| `assert` | `added` | `3` |
| `while` | `${added} > 0` | |
| `click` | `css=.added-manually` | |
| `storeElementCount` | `css=.added-manually` | `added` |
| `end` | | |
| `if` | `${added} === 0` | |
| `echo` | `All elements removed` | |
| `else` | | |
| `echo` | `${added} elements left` | |
| `end` | | |
| `assertElementNotPresent` | `css=.added-manually` | |

Two details make the `while` loop work:

- **The count is stored again inside the loop.** The condition reads `${added}`; if nothing updates it, the loop runs until the limit.
- **`storeElementCount` does not wait.** It counts what is on the page at that moment, which is right here because each click removes a button immediately. If removing took a request to the server, you would wait for the change before counting.

A `while | ${count} > 0` loop clicks a Delete button, and the test fails after a long time with "Max retry limit exceeded". What is the most likely cause?

 The page has more than 1000 elements `while` needs quotes around the variable Nothing inside the loop stores `count` again, so the condition never changes

The condition is re-evaluated before each pass, but it reads the variable, not the page. Store the count again inside the loop. Quoting the variable would make things worse: in a script `"${count}"` becomes the text `arguments[0]`.

## When control flow is a sign to move to code[#](https://testingbot.com#when-control-flow-is-a-sign-to-move-to-code)

If a test needs nested loops, several variables passed between tests, or a page of `executeScript` JavaScript, you are writing a program in a table. That is the point to consider exporting it (chapter 8). Until then, the next chapter shows the cleaner way to avoid repeating steps: reusing whole tests.

[Previous · Chapter 4 Assertions, Verifications and Waits in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/assertions-verifications-and-waits) [Next · Chapter 6 Reusing Tests and Organising Suites in Selenium IDE](https://testingbot.com/resources/courses/selenium-ide/reusing-tests-and-suites)

On this page

1. [Storing values](https://testingbot.com#storing-values)
2. [Variables inside JavaScript](https://testingbot.com#variables-inside-javascript)
3. [Environment variables](https://testingbot.com#environment-variables)
4. [if, elseIf, else, end](https://testingbot.com#if-elseif-else-end)
5. [times, while, do and repeatIf, forEach](https://testingbot.com#times-while-do-and-repeatif-foreach)
6. [When control flow is a sign to move to code](https://testingbot.com#when-control-flow-is-a-sign-to-move-to-code)

Certification

Earn the free, resume-worthy TestingBot Selenium IDE Certification for this course.

[How it works](https://testingbot.com/resources/courses/selenium-ide/certification)
