Exit course
Chapter 5 of 10 Beginner 8 min read 2 quick checks Updated

Variables, store and Control Flow in Selenium IDE

On this page
  1. Storing values
  2. Variables inside JavaScript
  3. Environment variables
  4. if, elseIf, else, end
  5. times, while, do and repeatIf, forEach
  6. 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#

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 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#

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#

${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#

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#

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 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#

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.