| 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 2 ·

Sahi is based on Javascript, but they are not the same.

Sahi’s APIs can be classified into three broad categories:

1. Browser Accessor APIs:

These are helper methods which help in accessing DOM elements in simple easy ways.

eg.
_checkbox(), _link() etc.

Click here for a complete list

2. Brower Action APIs:

These are functions which let Sahi perform actions like click, mouse move etc. on the browser.

eg.
_click(), _mouseOver(), _navigateTo(), _assertTrue() etc.

Click here for a complete list

Advanced Scripting

While Sahi scripts can easily be recorded, as a project grows in size, the scripts may have to be more logically organized into functions and split into multiple files. The following section gives more details on the Sahi Script and the rules to follow. Sahi script is based on Javascript. The constructs of javascript, like functions, variables, loops, conditional statements etc. are all available in Sahi script. But the script is parsed and transformed slightly in the Sahi proxy server before execution.

To keep matters simple, follow these rules.

Perform all actions using Browser Action APIs.

Eg.

Use _click(_link("Click Me")) instead of _link("Click Me").click()
Use _setValue(_textbox("tb"), "value") instead of _textbox("tb").value = "value"

Use _set to store a variable with data from browser

 
var $email1 = null;
_set($email1, _textbox("email1").value);
if ($email1 == "a@sahi.example.com"){
    _setValue(_password("pwd"), "secret");
} 

More details on _set

Using custom functions that access the DOM.

In V2, custom functions which access the browser’s DOM to say, identify a particular element, need to be wrapped in a <browser></browser> tag. (This wrapping is not required in the previous Sahi Nightly builds. Note that Sahi V2 is the version which will be developed further and Sahi Nightly versions will become obsolete soon.)

Eg.

<browser>
function getEmailField(){
   return _textbox("email");
}
</browser>
  
_assertEqual("a@sahi.example.com",  getEmailField());

A general tip is to always call a browser based function from inside a Browser Action API. Otherwise they will be executed on the proxy (in V2) and will fail because they do not have access to the browser’s DOM.

Prefix a $ sign before variables which are passed to sahi functions.

 
var $name = "karthik";
_setValue(_textbox("userName"), $name);

Note that $name’s value “karthik” is independent of any page.

IF conditions:

“if” conditions which access the browser’s DOM need to be enclosed in _condition(). Note that this is a short form and is equivalent to first using _set to assign the browser dependent variables and then comparing using plain “if”.

For example,

  
var $email1 = null;
_set($email1, _textbox("email1").value);
if ($email1 == "a@sahi.example.com"){
    _setValue(_password("pwd"), "secret");
} 

can be written like this:

  
if (_condition(_textbox("email1").value == "a@sahi.example.com")){
    _setValue(_password("pwd"), "secret");
}

Note that, in this example, we access _textbox(“email1”).value which accesses the browser’s DOM. Hence we need to enclose it in _condition. In the previous example, $email1 had already been populated using _set, and the if condition no longer needs access to the browser’s DOM. Both the if conditions are valid and equivalent.

WHILE loops:

If the WHILE loop condition is page dependent use while (_condition(booleanExpression))

Eg

 
while (document.loginForm.userName != "karthik"){
	// some statements
}

should be written as

 
while (_condition(document.loginForm.userName != "karthik")){
	// some statements
}

FOR loops:

If the FOR loop condition is page dependent, use _set to set the page dependent variable to a sahi variable and then use that variable in the loop:

Look at For loops


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




---