Selblocks

Selblocks

Installer: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-sel-blocks/versions/

description

Provides commands for javascript-like looping and callable functions, with scoped variables, and XML driven parameterization.

features

  * Adds the following control structures to Selenese: if/else, for/foreach/while, continue/break, loadVars, forXml, call/script/return
  * Script and loop parameters use regular selenium variables that are local to the block, overriding variables of the same name, and that are restored when the block exits.
  * Command parameters are javascript expressions that are evaluated with the selenium variables in scope, which can therefore be referenced by their simple names, e.g.: i+1
  * Variables can be configured via external XML.
  * A script definition can appear anywhere; they are skipped over in normal execution flow.
  * Script functions can be called recursively.

examples of use

Note that Selenium performs variable substitution before each command executes. When Selenium variables are used in Selblock parameters, which are JavaScript expressions, they are evaluated as literals. So for example, you would write: "$(userid}". Or more simply: userid.

foreach|userid|"dilbert","dogbert"
  getEval|LOG.info("${userid}");
  if|${userid}=="dilbert"
    getEval|LOG.info("${userid}, our hero");
  else
    getEval|LOG.info("${userid}");
  endIf
endForeach

for|i=1; i <= 10; i++|i
  getEval|LOG.info(${i});
endFor

loadVars|varset.xml
loadVars|varset.xml|userid=="wally"
getEval|LOG.info("${role}");

forXml|varset.xml
  getEval|LOG.info("${userid}: ${role}");
endForXml

-- log odd numbers from 0 to 10
for|i=0; i <= 100; i++|i
  break|i==10
  continue|i%2==0
  getEval|LOG.info("${i}");
endFor

-- extravagant example, but demonstrates possibilities
call|factorial|n=7
script|factorial
  if|n<=1
    return|1
  else
    call|factorial|n=n-1
    return|n*_result
  endIf
endScript
getEval|LOG.info("${_result}");

skipNext|1
getEval|LOG.info("this will NOT execute");
skipNext|0
getEval|LOG.info("this WILL execute");
goto|HERE
getEval|LOG.info("this will NOT execute");
label|HERE
gotoIf|true|THERE
getEval|LOG.info("this will NOT execute");
label|THERE

Sample varset.xml (the location of this file is presumed to be in the same directory as the invoking script, although the full location of the file can be provided as an URI or using a relative path):

  <testdata>
    <vars userid="dilbert" role="superuser" />
    <vars userid="wally"   role="lazyuser" />
  </testdata>

automatic variables

Selblocks provides automatic variables in two situations:

  * Inside a foreach/endForeach loop, ${_i} holds the zero-based index of the loop iteration.

  * When a script terminates via the return command, ${_result} holds the result value.

These variables can of course be referenced in Selblocks commands as _i and _result.

javascript extensions

These can be used in Selblock expressions:

String.isOneOf : Example: if|employee.isOneOf("dilbert","wally","ratbert")
String.mapTo : Example: if|employee.mapTo("peon", ["dilbert","wally"]) == "peon"

$e(locator) : Example: $e("link=Selblocks review")
$x(xpath) : Example: $x("//input[@id=(//label[.='User ID']/@for)]")

required extensions

NONE

limitations

Incompatible with flowControl (and derivatives), because they unilaterally modify selenium.reset(). Note however that Selblocks provides equivalent commands, and is generally a plug-compatible replacement.

philosophical note

I tend to agree, in principle, with Dan Fabulich's view on keeping HTML Selenese simple. However, in practice I'm able to maintain MUCH simpler scripts by using if/else and looping. There is no language translation for Selblock commands, so they're comment-out in an exported script. Although hand-translation in the target language ought to be pretty straightforward. I would just say, use these constructs judiciously.

revision history

  * 2011-08-14: v1.3
  * 2011-08-05: v1.2
  * 2011-04-10: v1.1
  * 2011-03-28: v1.0

All versions: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-sel-blocks/versions/

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jan 03, 2012

    Peter Kehl says:

    I've enhanced SelBlocks. Now scripts can be called cross-test case. However, it ...

    I've enhanced SelBlocks. Now scripts can be called cross-test case. However, it only works if you run 1 test case - somehow it doesn't work when you run the whole test suite. Indeed, the test case that you run may call scripts from other test cases within the suite.

    You can get it from http://paneris.org/~peterk/sel-blocks.js - then add it to Selenium > Options > Options... > General > Selenium Core extensions (and remove original SelBlocks, if you've got it installed).

    A few more limitations - when you need to reload the test suite:

    • if you've run a test case, then change it (or change another test case), then sometimes the changes are ignored if you re-run the test case again
    • sometimes it can't re-run after a failure

    Also, I have a vision of DB-driven structured tests in Se IDE (using SQLite).

  2. Nov 28, 2011

    Martin "Hasan Bramwell says:

    Hi Chris, I have created a fork of your code and published it here https://gith...

    Hi Chris,

    I have created a fork of your code and published it here https://github.com/martinhbramwell/SEL-Blocks

    You'll see that I have taken your "examples of use" and turned them into a demo/test suite, and I've added build scripts.

    I have also added ForEach looping through JSON objects from a loaded file, but I haven't gone as far as you did with asserts and validation of consistency. (Sadly, I'm nothing like such a JavaScript expert as you.)

    I hope will be interested enough in this to add it to your Mozilla AddOns page.

    Sincere regards,

    Hasan