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
Section titled “Statements”Statements are normal lines of code. They end with a semi-colon.
Eg.
_click(_link("Login"));Variable declaration
Section titled “Variable declaration”var $variableName = value;or
var $variableName; // declaration$variableName = value; // assignmentAll 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
Section titled “Comments”// This is a single line comment
/*This is a multiline comment.This has two lines*/if statements
Section titled “if statements”if (condition) {// statements}Eg.
// Comparing normal valuesif ($username == "PartnerUser"){ _click(_link("Partner Login"));}
// Comparing with browser attributes exposed by Sahiif (_getText(_div("page_type")) == "Partner Page"){ _click(_link("Partner Login"));}
// Comparing with browser attributes NOT exposed by Sahi's built-in fetch apis// using _fetchif (_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 _conditionif (_condition(_link(0).href="=" "http://sahi.co.in/")){ _click(_link("Partner Login"));}for loops
Section titled “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
Section titled “while loops”while (condition) {// statements}Eg.
$i = 0;while ($i++ < 10) { login("user"+$i, "password"+$i); logout();}Structuring Sahi Code
Section titled “Structuring Sahi Code”Functions
Section titled “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 declarationfunction login($usr, $pwd){ _click(_link("Login")); _setValue(_textbox("username"), $usr); _setValue(_password("password"), $pwd); _click(_submit("Login"));}// function calllogin("test", "secret");Including another Sahi script file
Section titled “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 variablevar $DEFAULT_USER = "test";
// login function declarationfunction 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");
