Reusing Tests and Organising Suites in Selenium IDE
On this page
The fastest way to make a recorded suite unmaintainable is to record the login ten times. When the login form changes, you fix ten tests. Selenium IDE has one tool against that, the run command, and one way to group tests, suites. This chapter covers both, plus the two suite settings that change how tests run: Parallel and Persist Session.
The run command#
run plays another test from the same project, in the same browser, as if its steps were pasted in at that point. Write the shared steps once as their own test and call it.
The shared test, named Log in:
| Command | Target | Value |
|---|---|---|
open |
/login |
|
type |
id=username |
${username} |
type |
id=password |
${password} |
click |
css=.radius |
The test that uses it, Log in and log out:
| Command | Target | Value |
|---|---|---|
store |
tomsmith |
username |
store |
SuperSecretPassword! |
password |
run |
Log in |
|
assertElementPresent |
css=#flash.success |
|
click |
linkText=Logout |
|
waitForText |
css=h2 |
Login Page |
How it behaves:
-
The called test sees the caller's variables.
Log inreads${username}and${password}, which the caller stored. That is how you pass "parameters": store the values, thenrun. Variables the called test stores are visible to the caller afterwards, too. -
The target is the test's name. Renaming
Log inbreaks everyrunthat calls it, so give shared tests a clear name early. A name that does not exist fails withCan't run unknown test: .... -
Keep the checks in the caller.
Log inonly performs the login. Whether a login should succeed (valid user) or fail (wrong password) is the caller's business, so the same shared test serves both.
Shared tests do not belong in a suite#
A test like Log in cannot run on its own: without username and password stored first, it types the literal text ${username}. Keep it out of every suite. selenium-side-runner runs the suites of a project and skips tests that are in none, so Log in only ever runs when something calls it.
Six tests start by logging in, and the login form is about to get a "Remember me" checkbox that must be unticked. How should the suite be organised so that this is a one-place change?
With run, the new step goes into one test and all callers get it. A persistent session would make every test depend on the first one passing and on the order they run in.
Suites#
A suite is a named list of tests, managed in the Suites tab: drag tests from Available tests into Tests in suite. A test can be in several suites. Our project has two:
- Smoke: the six tests that should pass on every deploy.
- Variables and control flow: the two tests from chapter 4.
Organise suites by when you want to run them (on every commit, nightly, before a release) or by area of the product, not by who recorded the tests. On the command line, --filter Smoke runs just the suites whose name matches (chapter 6).
Each suite has three settings:
| Setting | What it does |
|---|---|
| Timeout | a time limit in milliseconds, stored with the suite (selenium-side-runner does not use it; it has its own --jest-timeout per test) |
| Parallel | its tests may run at the same time, each in its own browser |
| Persist Session | all its tests share one browser session and one set of variables |
Parallel#
We ran the Smoke suite with selenium-side-runner both ways, with three workers (-w 3):
| Smoke suite | Run time |
|---|---|
| Parallel off | 21.2 s |
| Parallel on | 11.0 s |
With Parallel off, the runner plays the tests one after another however many workers you allow. With Parallel on, it starts up to --max-workers tests at once. Each test gets its own browser, so this only works when the tests are independent: none relies on data another one created, none changes something the others read (such as the same user's settings).
On a cloud grid, parallel tests count against your plan's concurrency. Starting more than your plan allows does not make the run faster; the extra sessions wait in a queue or are refused (chapter 7).
Persist Session: use with care#
With Persist Session, the suite opens one browser and runs all its tests in it, in order, with shared variables. It looks attractive: log in once in the first test, and the next tests start logged in.
The cost is that every test now depends on the ones before it:
- If the first test fails, the others fail too, for reasons that have nothing to do with them.
- You cannot run one test on its own, because it relies on the state the earlier ones left.
- Reordering tests changes their results.
- It cannot be combined with Parallel.
Persistent sessions fit a genuine journey, such as "sign up, confirm, set a password, log in", split into steps you want reported separately. For everything else, leave it off and use run for the shared setup. The flaky tests course explains why tests that depend on each other's order become flaky.
You switch on Parallel for a suite. "Edit profile" now fails now and then; it passed every time before. What is the likely cause?
In sequence, the two tests never overlapped. In parallel they do, and both write to the same account. Give each test its own data (a different user) or keep that pair out of the parallel suite.
Where reuse stops#
run reuses a whole test, with variables as an implicit way in and out. There are no functions with real parameters, no return values, and nothing shared between projects: two .side files cannot call each other's tests. When you find yourself copying the same shared tests into several projects, or storing five variables before every run, that is the structure code gives you for free. The Page Object Model course shows what that structure looks like once you move to code.