Sahi Scripting Basics

Sahi script is based on javascript. Sahi script is parsed by Sahi and the parsed script is fully valid javascript which is executed by the rhino javascript engine. Below are the normal constructs used in Sahi script. They are the same as javascript except for the mandatory $ used in variables.

Statements

Statements are normal lines of code. They end with a semi-colon.

Eg.
_click(_link("Login"));


Variable declaration

var $variableName = value;
or
var $variableName; // declaration
$variableName = value; // assignment
All variables start with a $. The keyword var is used for local variables.

Eg.
var $username = "SahiTestUser";
var $password; // declaration;
$password = $username + "_password"; //  "SahiTestUser_password"


Comments

// This is a single line comment

/*
This is a multiline comment.
This has two lines
*/


if statements

if (condition) {
// statements
}


Eg.
// Comparing normal values
if ($username == "PartnerUser"){
  _click(_link("Partner Login"));
}

// Comparing with browser attributes exposed by Sahi
if (_getText(_div("page_type")) == "Partner Page"){
  _click(_link("Partner Login"));
}

// Comparing with browser attributes NOT exposed by Sahi's built-in fetch apis
// using _fetch
if (_fetch(_link(0).href) == "http://sahi.co.in/"){
  _click(_link("Partner Login"));
}

// Comparing with browser attributes NOT exposed by Sahi's built-in fetch apis
// using _condition
if (_condition(_link(0).href == "http://sahi.co.in/")){
  _click(_link("Partner Login"));
}


for loops

for (var $i=0; $i<$max; $i++){
// statements
}


Eg.
// This loop will login with user1, password1, user2, password2 etc.
// login and logout are custom functions.
for (var $i=0; $i<10; $i++){
  login("user"+$i, "password"+$i);
  logout();
}


while loops

while (condition) {
// statements
}


Eg.
$i = 0;
while ($i++ < 10) {
  login("user"+$i, "password"+$i);
  logout();
}


Structuring Sahi Code

info While recording gives us the low level steps to be performed on the browser, code needs to be restructured for better readability and maintainability. This section explains how to structure your code for effective but simple use of Sahi.

Functions

Functions are the basic building blocks in your automation. Functions are used to club together multiple steps into one logical action.

Functions reduce code duplication and allow easier maintenance of scripts.

function functionName($parameter1, $parameter2) {
// statements
}


For example a login function may look like this
// function declaration
function login($usr, $pwd){
  _click(_link("Login"));
  _setValue(_textbox("username"), $usr);
  _setValue(_password("password"), $pwd);
  _click(_submit("Login"));
}
// function call
login("test", "secret");


Including another Sahi script file

To reuse a function from one script in another, we include the relevant script using _include. Functions and variables declared in the included script are available in the including script.

Eg.

common.sah
// File: common.sah

// declare $DEFAULT_USER variable
var $DEFAULT_USER = "test";

// login function declaration
function login($usr, $pwd){
  _click(_link("Login"));
  _setValue(_textbox("username"), $usr);
  _setValue(_password("password"), $pwd);
  _click(_submit("Login"));
}


testcase1.sah
// File: testcase1.sah

// include common.sah shown above.
_include("common.sah");

// Note how login and $DEFAULT_USER are actually declared
// in common.sah but available here in testcase1.sah.
login($DEFAULT_USER, "secret");