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
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.
Eg.
Use _click(_link("Click Me")) instead of _link("Click Me").click()
Use _setValue(_textbox("tb"), "value") instead of _textbox("tb").value = "value"
var $email1 = null;
_set($email1, _textbox("email1").value);
if ($email1 == "a@sahi.example.com"){
_setValue(_password("pwd"), "secret");
}
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.
var $name = "karthik";
_setValue(_textbox("userName"), $name);
Note that $name’s value “karthik” is independent of any page.
“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.
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
}
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