Running Sahi Scripts from Java

Problem

In an enterprise application, some times the web application may be only a small part of an other wise larger use case. One may be invoking a lot of test Java code for setting up a database, verifying infrastructure, making SOAP calls, copying over files, invoking shell commands and then, eventually verifying the application state via a web interface. In such cases it may make sense to write normal Java code for anything other than web, and then invoke a Sahi script when needed. This brings in a necessity to invoke a Sahi script from Java. The main class for invoking Sahi from Java is net.sf.sahi.test.TestRunner.

Java code invoking TestRunner

package in.co.sahi.sample;
import java.io.IOException;
import java.util.HashMap;
import net.sf.sahi.test.TestRunner;
/<strong></strong>
<ul><li>Launch a Sahi script or suite.</li>
*
*/
public class SahiScriptLauncher {
  public static void main(String[] args) throws IOException, InterruptedException {
    final String suiteName = "scripts/demo/integration.sah";
    final String browserType = "firefox";
    String base = "http://sahi.co.in/demo/training/";
    String threads = "1";
    </ul>
    TestRunner testRunner =
    new TestRunner(suiteName, browserType, base, threads);
    HashMap<String, Object> variableHashMap = new HashMap<String, Object>();
    variableHashMap.put("$user", "test");
    variableHashMap.put("$pwd", "secret");
    testRunner.setInitJS(variableHashMap);
    String status = testRunner.execute();
    System.out.println(status);
  }
}
NOTE: The ant-sahi.jar file needs to be added to your classpath. It is located in sahi/lib folder.

The Sahi script being invoked

The script we are invoking is this:
// Test Manager can pass in data to this script via "initJS" parameter.
// initJS will be "eval"ed before a script starts
// Pass String "$user='test';$pwd='secret';" as parameter "initJS"
// Or pass a HashMap of variable name and value as called in above Java code
_setValue(_textbox("user"), $user);
_setValue(_password("password"), $pwd);
_click(_submit("Login"));
_setValue(_textbox("q"), "2");
_setValue(_textbox("q[1]"), "1");
_setValue(_textbox("q[2]"), "1");
_click(_button("Add"));
_click(_cell("Rs.200[1]"));
_assertExists(_textbox("total"));
_assert(_isVisible(_textbox("total")));
_assertEqual("1150", _textbox("total").value);
_click(_button("Logout"));

Passing parameters into Sahi script from Java

Parameters can be passed in as a simple string which contains the Javascript to be executed before a script starts. In the above example. notice how we pass in two variables $user and $pwd from the Java code into Sahi code.

Passing data values from the Script back to Java

This is best done via writing into a file in the Script and then reading the file in Java to obtain the parameters. To write to a file, use "_writeFile":miscellaneous-apis
_writeFile($filePath, $text);
To read it in your Java code, you can write your own custom code to read file contents, or use Sahi's Utils class
String contents = net.sf.sahi.util.Utils.readFileAsString(new File(filePath));
This class is also part of ant-sahi.jar which you would have already added to your classpath to invoke TestRunner.