Submitted by lalbright on
- Adds the following control structures to Selenese:
if
/else
,while
,for
,foreach
,forXml
,continue
/break
,loadVars
,call
/script
/return
- Script and loop parameters use regular Selenium variables, e.g.:
${i}
, 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.
- Script functions can be called recursively.
Documentation here.
(Available on Internet Archive Wayback Machine until content can be migrated)
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.
File:
Attachment | Size |
---|---|
![]() | 10.34 KB |
1 Comment
You may be interested in
Submitted by peter.kehl on
You may be interested in SelBlocksGlobal. It's an enhancement of SelBlocks. It lets you to call scripts across test cases, so you can reuse same scripts more. A test case can be a part of multiple test suites. Soyou can share scripts between test suites, too. That means less copy and paste code. If you structure your scripts well, you'll have
See https://code.google.com/p/selite/wiki/SelBlocksGlobal. And while you're there, have a look at other extensions of SeLite family. They allow your tests to use advanced Selenese and to have a separate test database. See https://code.google.com/p/selite/wiki/ProjectHome.