Selenium IDE Rollups With StoredVars Logic

Creating rollups in Selenium IDE with execution logic based on storedVars can be tricky.   Storing the value of an element or its presence within the rollup command list and using if or other logic blocks via the various “flow” add ins will not work as you expect.   Nor will storing the values and then using JavaScript logic within your rollup.     Getting storedVars working in a rollup is a special kind of black magic.

Here are my methods that are working for me with my home-grown “Go With The Flow” add-in I brewed based on similar control flow plugins.

Set Vars In A Separate Rollup

I find that setting my variables in a separate rollup is a simple solution.   This ensures your variable stack exists and is set using standard Selenium IDE commands.   My example for testing if I am already logged in before prompting for user and password data and running the login:

Use JavaScript Logic

Once your variables are set they are standard JavaScript variables that will be available throughout your rollups.  You can use this fact to build standard JavaScript logic in your rollup and decide what commands the rollup will push on the stack during each execution.   In your Selenium IDE command list you can then change storedVars using various store commands and then call the rollup afterwards; in essence changing what commands are executed “on the fly”.

You’ll notice that I also do a quick check at the top to ensure the set_my_vars rollup was already run by checking that the storedVars key I need is set.   If not it echos a message to the Selenium IDE log console and skips doing anything else.

 

Hopefully these tricks will help you with your Selenium IDE rollup builds.    Some things to keep in mind is that things like Selenium labels or endif markers do not exist within the rollup itself if you are creating them in the rollup.    Same concept with storedVars, the storedVars[<key>] will not exists and be available to your rollup JavaScript logic if you create the var inside the rollup itself.      When a rollup that creates a storedVars entry is complete it is then available for any future commands, including rollups.

Selenium IDE Rollups With Arguments

As I prepare another release of Store Locator Plus with some new features I’ve decided it is time to up my QA-fu with Selenium.   I’ve been using Selenium IDE for a while now and find that , despite being free, it is one of the best user experience testing tools out there.    I’ve paid for a few testing tools over the years and I always come back to Selenium IDE.     The paid tools are do not offer a lot more and are just as complex to learn to get advanced testing techniques in place.

Simple Rollups

Speaking of advanced techniques, here is some new skills I picked up regarding Selenium ID rollups.   If you are not aware of what a rollup is, in the simplest form it is an easy way to group together oft-repeated commands in Selenium into a single entry.  This makes your test files easier to read and limits errors by ensuring consistency across your tests.   For example, of of my earlier rollups does a simple “syntax error” check which is very useful when running tests of my WordPress plugins or the SaaS version of the application when I have debug mode enabled.  It does little more than scan the body of the web page for known strings that typically mean the app crashed.

Here is what that rollup looks like:

 

Rollups With Arguments

Today I added a new level to my rollups: parameter passing.

This allows you to create a single rollup that serves multiple purposes.  For example, before today I had 4 different rollups.  One to open each of 4 different tabs in my application.   I’d call rollup open_general_tab or open_experience_tab depending on what I was about to test next.

Now I have a smaller more efficient rollup thanks to the use of arguments.   I now call rollup open_tab tab=slp_general or rollup open_tab tab=slp_experience and the back end runs a single small set of code.  That means less memory being consumed in the browser and less chance of errors as the codebase is reduced by nearly a factor of 4 in this case.

Here is my open tab rollup:

 

And here is how it looks in the IDE:

Selenium IDE Rollup With Args 2017-04-18_16-05-38

Selenium IDE Rollup With Args 2017-04-18_16-05-38

If you are not familiar with extending Selenium, you will want to review their documentation on creating rollups.    It is a basic JavaScript file that you include in Selenium IDE by setting the file name as a core extension.   I also strongly recommend getting a flow control extension to Selenium IDE as well.  I’ve “rolled my own” based on the ide-flow-control modules of others called “Go With The Flow” the includes basic if, gotoIf, etc. calls.

 

Selenium IDE Extensions Hacking

I use Selenium IDE as a tool for testing the Store Locator Plus WordPress plugin.   It is a great tool for automating browser interactions and sussing out basic problems.  With the launch of the My Store Locator Plus SaaS service we need to build more complex tests for multiple accounts and services.    Thankfully Selenium IDE not only has a myriad of plugins but makes it easy to create your own.

While “officially sanctioned” Selenium Plugins are true Firefox browser plugins you can deploy your own commands written using the Selenium IDE objects and interfaces.   These can be included by listing your local JavaScript file in the core extensions file list.

Now for the bad news.   The documentation for building extensions is horrid, outdated, and typically non-existent.   Which is where this article comes in.   This is my personal notes on how to hackify your own Selenium IDE extensions.

You can create your Selenium Extension as any other JavaScript program.    When you’ve created it you can add it via the Selenium Options menu drop down under core menu extensions.

Selenium.prototype.do<command>

This is the JavaScript command that you use to define a new Selenium IDE command.   If needs to point to a function that is passed 2 parameters.  The first parameter is what the user put in the target field.   The second parameter is what the user put in the value field.

This would add a new command you invoke with MyCommand in Selenium IDE.

The command definition in the JavaScript prototype must start with an uppercase.

The command in the SCRIPT FILE will start with a lowercase.

this.continueFromRow( <int> )

Run the Selenium IDE command in the current test from the specified entry.

throw new Error( <string> )

Generate an error that will be logged by Selenium.

Selenium Variables – storedVars

Any store commands will place the variables in a named index in storedVars[<name>] where name is the value you assigned during the store.

testCase

The Selenium object that contains details about the current test being run.

testCase.debugContext.debugIndex

Get the current debug index.    In theory the currently executing row.

testCase.commands

Contains the array of all the commands for the current test case.

testCase.commands[<int>].type

Type of command.    At least one valid type is ‘command‘.

testCase.commands[<int>].command

The command the user entered, first entry in a Selenium row.

 

More Info

You can find some of my hacks based on the Sideflow Selenium plugin on Go With The Flow over at Bitbucket.

%d bloggers like this: