Top Rounds
| We are Hiring! |

Sahi Scripting Basics - Part 1 ·

Sahi script is based on javascript. Sahi script is parsed by the proxy 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"


Function declaration:

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

Eg.

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


Comments:

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

if statements:

 
if (condition) {
  // statements
}
Eg.
 
if ($username == "PartnerUser"){
  _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();
}

Including another Sahi script file:

Use _include to include another Script file.

Eg.

_include("includes/common_functions.sah");


Related topics

Sahi Scripting Basics - Part 1
Sahi Scripting Basics - Part 2
Sahi Scripting - Calling Java
Scripting Changes in Sahi V2
For loops
While loops
If condition
Functions
Exception handling using try-catch
Recovering without try-catch using _setRecovery




---


Top Rounds