Skip to content

Language Syntax (v0.3)

()z edited this page Feb 8, 2017 · 5 revisions

Scripting Language

The test engine processes test scripts which comprise lists of commands and tests using the following syntax:

Basic Syntax

The parser consumes words and strings and integers separated by white space. // and /* ... */ comments are also supported.

keyword keyword "string" 0 11 22 ...

Some commands enclose their arguments in { ... } braces generally where the number of arguments may vary for example when calling an external script or defining an alias.

Aliases or functions may also take an argument name sequence defined as ( name1, name2, .. ). These can be referenced inside the alias/function using $name or when embedded in a string, $(name) or $I(name).

Commands can appear on the same line or separate lines, it doesn't matter, as newline behaves exactly like space.

Control

browser prefs

Set a browser preference.
See https://sites.google.com/a/chromium.org/chromedriver/capabilities

browser prefs <preference> <value>

browser option

Sets a browser command line option.

browser option "<option-string>"

browser start

Start the browser using the options and preferences defined so far.

browser start

browser chrome

Specifies the size of the browser chrome (used in conjunction with browser size to correctly size the inner window).

browser chrome <width>,<height>

browser size

Sizes the browser window to the specified width and height.

browser size <width>,<height>

browser pos

Moves the browse window to the specified screen position.

browser pos <x>,<y>

browser get

Navigate the browser window to the specified url.

browser get "<url>"

browser refresh

Performs an F5 or CMD+R (refresh) of the browser.

browser refresh

browser back

Navigate backward, same as pressing the back button.

browser back

browser forward

Navigate forward, same as pressing the forward button..

browser forward

browser close

Close the browser window.

browser close

browser wait

Sets the default wait timeout when looking for elements in the Selenium Driver. This is potentially more reliable that script drivers wait command, though it doesn't replace it, simply the first line of defense.

browser wait <seconds>

include

Load and run the specified script.

include "<filename>"

alias

A synonym for function.

function | alias

Define a function or alias (there are interchangable) that can be called using its name. They become keywords that can be used anywhere a keyword can and can optionally support passing a fixed number of named arguments.

alias <name> { <statements> ... }
function <name> (<params>) { <statements> ... }

Examples

Create an alias that will click the currently element and then sleep for 1 second.

alias CLICK { click sleep 1 }
test-id "mybutton" CLICK

Create a function that checks the value of a cell within a grid. Parameters are expanded using either $name if its the only value, or using $(name) if part of a larger string. The $I(name) syntax is used to convert the parameter to an integer and should e used where an integer value is required, for example in an :nth-child(1) css selector.

alias CHECK_CELL (row, col, value) {
  echo "Checks if cell at $I(row),$I(col) matches $(value)"
  select ".gridRow:nth-child($I(row)) :nth-child($I(col))" check $value 
}
CHECK_CELL 1 3 "test value"

exec

Runs an external <command> passing the supplied arguments. If no arguments are required use { }.

exec <command> { args ... }
exec <command> { }

exec-include

Similar to exec except that if the script returns an exit status of 0, standard output from the command is processed as an included test script.

exec-include <command> { args ... }

debugger

Used to aid debugging the test engine, allows setting of a breakpoint within a script when running the engine under eclipse.

debugger

sleep

Pause execution for the specified number of seconds

sleep <seconds>

wait

Used to specify a timeout for following select statements, allowing the script to wait for elements to become available but respond to them as soon as they do (whereas sleep always waits for the specified duration). This allows scripts to perform as fast a possible but be able to cater for subtile timing differences between runs.

wait <seconds>

default wait

Sets the default wait timeout if one is not specified when a test would otherwise fail.

default wait <seconds>

default screenshot

Sets the default screen shot path. If defined, this path will be prefixed to any path name specified in the screenshot command

default screenshot <path>

push wait

pop wait

Pushes the current wait timeout onto the stack. Designed to be used by aliases, functions and included scripts. The wait timeout can be restored by calling pop wait.

push wait
pop wait

if

If the test-statements yield a positive result (only selectors can do this) the then part is processed otherwise the else part is processed.

if <test-statement> then <statements> else <statements> endif

while

Repeatedly executes { statement ... } until it fails.

while { statement ... }

call

Calls RegressionTest.test(verb, args) in the executing web page. This can be useful for performing custom actions within your page.

call <verb> { <arg ...> }

alert

Allows the script to respond to browser alerts. Only supported verb is *accept which OKs the dialog.

alert accept

fail

Causes the current script to fail. If executed within the body of a while statement causes the while statement to stop executing.

fail "<message>"

Selectors

Selectors are use to select the current element which other commands then operate on.

test-id | field | id

Locate an element with a test-id attribute matching id-string and make it the current context.

test-id "<id-string>"

select

Locate an element using css-selector and make it the current context.

select "<css-selector>"

xpath

Locate an element using xpath-expression and make it the current context.

xpath "<xpath-expression>"

Tests

check

Tests if the current elements's value or text node (depending on type of element) matches string-value, if not aborts the test.

check "<string-value>"

Within a function | alias with named parameters:-

check $<name>
check "$(<name>)"
check "Test $(<name>)"
check "$I(<name>)"

checksum

Tests if a checksum of the current element's value or text node matches <checksum>.

checksum "<checksum>"

tag

Test if the current element's tag name matches tag-name, if not aborts the test.

tag "<tag-name>"

displayed

Tests if the current element is displayed (is visible), if not aborts the test.

displayed

enabled

Tests if the current element is enabled, if not aborts the test.

enabled

selected

Tests if the current element is selected, if not aborts the test.

selected

at

Test if the location of the current context is at the specified position, if not aborts the test. The wildcard * can be substituted for the x position and means any x position will pass the test.

at <x>,<y>

size

Test if the location of the current context size matches the specified size, if not aborts the test. The wildcard * can be substituted for the width and means any width will pass the test. A range can also be specified for the width by specifying <min-width>:<max-width>,<height>.

size <width>,<height>

not

Reverses the result of a condition.

not check ""
not enabled
not displayed
if not test-id "some-field" then
  do-something
endif

Info

info

Dumps the info for a the current context in a form suitable for inclusion in a test script.

info

dump

Dumps the info for all elements with a test-id in a form suitable for inclusion in a test script. This is particuarly useful when initially building scripts. Get to a certain point and the dump to get a bunch of checks for all the fields with test-ids.

dump

log dump

Dumps the browsers console.log to standard output, and then clears it. This can be helpful when so that when a test fails, the console output can be seen at point of failure.

log dump

log auto

Enables or disables automatic logging of the console to standard output. When auto logging is enabled, the browsers console log is copied to standard output before each command is executed.

log auto <on|off>
log auto <true|false>

version

Displays the ScriptDriver version.

version

echo

Simply echo's its argument to standard output.

echo "<string>"

screenshot

Takes a screen shot of the current browser window and saves it to the named file. The path is relative to the current directory or can be absolute if it begins with a / or a drive letter as in C:. If a default screenshot path is specified and the path specified here is relative, the default screenshot path is prepended to this path to build the full path name. Screen shots are output in PNG format.

screenshot "<path>/<name>.png"
screenshot "<name>.png"

Events

Event handlers are special aliases, that are called when an event occurs. Two events are currently supported:

alias "--onfail" { dump log dump sleep 30000 }
alias "--onsuccess" { sleep 1 }

As their name suggests, --onsuccess is called when the tests script run through to completion and --onfail is called whenever a test fails (which also terminates the script).

Input

clear

Clears the current context's input data.

clear

send

Sends characters to current context. Note that this does not replace the current contents, it adds to them.

send "<string>"

Within a function | alias with named parameters:-

send $<name>
send "$(<name>)"
send "Test $(<name>)"

set

Set the current context to the specified value. The set command is equivalent to doing clear send "string".

set "<string>"

Within a function | alias with named parameters:-

set $<name>
set "$(<name>)"
set "Test $(<name>)"

click | click-now

The click command clicks the currently selected element (see selection). The script engine will wait for the element to become clickable before attempting to click it. This is not always desirable, particularly when the element that matches the current selector changes. Waiting does not pick up this new element so the click just waits until it times out. For those cases, use click-now which will not wait for the element to become clickable, it will just try and click it, and if it fails, the normal wait retry logic kicks in which will reselect the element using the current selector before retrying the click.

click
click-now

scroll-into-view

Scrolls the currently selected element into view, forces it to be within the visible part of the viewport.

scroll-into-view

mouse

Starts a sequence of mouse movements in relation to the current context.

mouse { <mouse-cmd> ... }

All mouse commands are relative to an element. There are two elements that can be used, one is the currently selected element (that has previously been selected with test-id or select etc) the other is the body element. Mouse commands are a sequence of keywords or co-ordinates enclosed within { ... } braces of the mouse command arguments.

The following mouse commands are available:-

origin | "0,0"

Moves the mouse position to the top left corner of the current context.

origin
"0,0"

center | centre

Moves the mouse position to the center of the current context.

center

body

Moves the mouse position to the top left of the body element.

body

down

Clicks and holds the left mouse button.

down

up

Releases the left mouse button.

up

"x,y"

Moves the mouse position relatively by the specified x, y offsets.

"-100,100"

click

Performs a left mouse click at the current position.

click

Examples

Example Aliases (Basic)

// Setup Some Aliases
alias HOME   { dump log dump call "goHome" select ".rmcMenuBar :first-child" log dump }
alias No     { select ".rmcAlertDialog .prompt :last-child" click sleep 0.1 }
alias Yes    { select ".rmcAlertDialog .prompt :first-child" click sleep 0.1 }
alias OK     { select ".rmcAlertDialog .buttons :first-child" click sleep 0.1 }
alias SBL    { select ".rmcTabStripSBL" click sleep 0.1 }
alias PROMPT { wait 1 select ".rmcAlertDialog .type-prompt .title" }
alias SYNC   { select "#signal-on" click }
alias ANSWER

Example Aliases (Advanced)

Example Functions

Example Mouse Script

mouse {
    "0,0"
    down "387,0" "0,205" "-387,0" "0,-205" up
    center "-25,-25"
    down "50,0" "0,50" "-50,0" "0,-50" up
    center "-50,-50"
    down "100,0" "0,100" "-100,0" "0,-100" up
    center "-75,-75"
    down "150,0" "0,150" "-150,0" "0,-150" up
    center "-100,-100"
    down "200,0" "0,200" "-200,0" "0,-200" up
}