- Explore
Introduction, screen-shots, features, limitations
- Getting started
Prerequisites, download, install, browser configuration, record, playback, view logs
- Sahi Scripting Basics - I
Statements, variables, functions, conditions and looping, _include
- Sahi Scripting Basics - II
- Sahi APIs (built-in functions)
- All APIs
- Browser Accessor APIs
- Browser Action APIs
- Miscellaneous APIs
- Sahi Scripting - Calling Java
- Exception handling using try-catch
- Recovering without try-catch using _setRecovery
- Script lifecycle call back functions
onScriptFailure, onScriptError, onScriptEnd
- Data Driven Testing
_getDB, CSV Files, Excel, Databases
- Multithreaded Playback (Parallel execution)
suites, commandline, ant
- Advanced techniques, tips and examples
- HTTPS/SSL Sites
- Configuring an External proxy
- Adding jars to Sahi's classpath
- Configuring Browser Types
- Sahi GUI Less Installation
- Sahi headless with PhantomJS
- Sahi headless with Xvfb
- Sahi with Android
- Tweaking Sahi APIs
- Jenkins Integration
- Sending Emails
- CSV Files as Suites with Tags
- Working with SSH
- Reading PDF Files
- Run Sahi Scripts from Java
- Other language drivers
Driving Sahi from Java, Ruby etc.
- Java
- Ruby
- Trouble Shooting Sahi
- Sahi Pro
- Sahi Pro V4.2 Documentation (PDF)
- Excel Framework
- Load Testing (Beta)
- Sahi Flex Support - sfl (Beta)
- Sahi Applet Support (Beta)
- Running tests on multiple machines
- Web based Testrunner
Sahi scripts run on the Rhino javascript engine. This allows Sahi to call any Java code in the classpath of Sahi.
Calling inbuilt Java classes
Inbuilt Java classes can be directly called using their fully qualified name.
Eg.
function printThroughJava(s){
java.lang.System.out.println("Through Java: " + s);
}
printThroughJava("Hi there");
// Should print "Through Java: Hi there" on the console.
Working with Java object directly in Sahi Script
Since Javascript does not have variable types, variables are declared with just var. Below is an example of reading property files via Java and Sahi Script:
| Java Code | Sahi Code |
public Properties loadProperties(String path, boolean isXML) {
Properties props = new Properties();
try {
FileInputStream inStream = new FileInputStream(path);
if (isXML) {
props.loadFromXML(inStream);
} else {
props.load(inStream);
}
inStream.close();
} catch (Exception e) {
// do nothing
}
return props;
}
// Usage
Properties props = loadProperties("a.properties", false);
System.out.println(props.getProperty("name"));
|
function loadProperties($path, $isXML) {
var $props = new java.util.Properties();
try {
var $inStream = new java.io.FileInputStream($path);
if ($isXML) {
$props.loadFromXML($inStream);
} else {
$props.load($inStream);
}
$inStream.close();
} catch (e) {
// do nothing
}
return $props;
}
// Usage
var $props = loadProperties("a.properties", false);
_alert($props.getProperty("name"));
|
Calling Java classes in Custom Packages
Custom packages need to be prefixed with ‘Packages’ keyword. This is how _readFile is implemented in Sahi:
// Part of lib.js
Sahi.prototype._readFile = function (filePath) {
return "" + Packages.net.sf.sahi.util.Utils.readFileAsString(filePath);
};
Accessing Your Own Custom Classes from Sahi
Sample Custom Class
Suppose you have built a custom class called LanguageUtils.java which has a function to read property files.
package com.example.utils;
import java.io.FileInputStream;
import java.util.Properties;
public class LanguageUtils {
public static Properties loadProperties(String path, boolean isXML) {
Properties props = new Properties();
try {
FileInputStream inStream = new FileInputStream(path);
if (isXML) {
props.loadFromXML(inStream);
} else {
props.load(inStream);
}
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return props;
}
}
Let us assume that this class has been compiled and added to languageutils.jar file.
Add to Classpath
Copy languageutils.jar to sahi/userdata/extlib/ folder (create extlib folder if not present). Add languageutils.jar to Sahi’s classpath by modifying start_dashboard.bat (or the relevant bat, sh or ant file.)
@ECHO ON
SET SAHI_HOME=..\..
SET SAHI_USERDATA_DIR=..
SET SAHI_EXT_CLASS_PATH=%SAHI_USERDATA_DIR%\extlib\languageutils.jar
CALL %SAHI_HOME%\bin\dashboard.bat
Have a look at adding-jars-to-sahis-classpath for more details.
Restart Sahi.
Call loadProperties Method from Sahi Script
var $LangUtils = Packages.com.example.utils.LanguageUtils;
var $props = $LangUtils.loadProperties("a.properties", false);
_alert($props.getProperty("name"));