A poor man’s for loop in Selenium
I find myself in the situation where I want to break one of my own guidelines for creating automated tests. In particular, the one that says that an automated test should test exactly one thing so as to not confuse the results if something one of the conditions being tested fails.
The scenario is this.
- A drop-down box has 3 values
- Each of these values triggers the same backend condition
- Depending on whether something was checked in correctly or not, the backend condition for one item in the drop-down could work for one item, but not the other
The end result being that I have to create each script 3 times where the only difference is which drop-down item to choose. This got me thinking ‘I should just loop over a list and do the test for each item in the list’. Of course, this is not as easy as it seems as there is no handy for loop for Selenium as there is in Python.
Not being one to shy away from a rabbit hole I’ve cobbled together a pattern (for lack of a better term) which duplicates the behaviour I wanted.
store | 0 | counter |
---|---|---|
store | ‘FC00ZR’:’FC00BA’:’FC00ZB’ | FSes |
store | javascript{storedVars[‘FSes’].split(‘:’).length} | numFSes |
label | startTest | |
store | http://hostname:9080 | baseUrl |
open | ${baseUrl}/WscValidationServiceWebTest/init.do | |
storeEval | javascript{storedVars[‘FSes’].split(‘:’)[storedVars[‘counter’]]} | currFS |
select | financeSourceId | label=${currFS} |
… more actions …
assertTextPresent | Validation Successful | |
---|---|---|
storeEval | javascript{storedVars[‘counter’] + 1} | counter |
gotoIf | storedVars[‘counter’] < storedVars[‘numFSes’] | startTest |
Some gotchas
- You need to have the flow control user extension
- Because of the above, it does not work in the Selenium IDE (but does work in the ‘Test Runner’
- You might have to get version 0.8.7 of the Selenium IDE in order to work around a bug accessing the storedVars map — which of course isnt released yet and you have to build it yourself from source
Might not be pretty, or all that effecient, but it does what I want it to and I only have to create 40 tests instead of 120 🙂