| Share
  1. Explore
    Introduction, screen-shots, features, limitations
  2. Getting started
    Prerequisites, download, install, browser configuration, record, playback, view logs
  3. Sahi Scripting Basics - I
    Statements, variables, functions, conditions and looping, _include
  4. Sahi Scripting Basics - II
  5. Sahi APIs (built-in functions)
    1. Browser Accessor APIs
    2. Browser Action APIs
    3. Miscellaneous APIs
  6. Sahi Scripting - Calling Java
  7. Exception handling using try-catch
  8. Recovering without try-catch using _setRecovery
  9. Data Driven Testing
    _getDB, CSV Files, Excel, Databases
  10. Multithreaded Playback (Parallel execution)
    suites, commandline, ant
  11. Advanced techniques, tips and examples
    1. HTTPS/SSL Sites
    2. Configuring an External proxy
    3. Adding jars to Sahi's classpath
  12. Other language drivers Driving Sahi from Java, Ruby etc.
    1. Java
    2. Ruby

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




---