Enable javascript in your browser for better experience. Need to know to enable it? Go here.
Blogs Banner

An introduction to Sahi: Part 1

Sahi is an automation and testing tool for web applications. Different from many other commercial testing tools, Sahi is an open source tool. As an automation tool, Sahi provides the facility of recording and play backing of scripts. Sahi supports Java and JavaScript. Even though the Sahi script looks like JavaScript, it is not executed as the regular JavaScript on the browser.

Introduction

Sahi is an automation and testing tool for web applications. Different from many other commercial testing tools, Sahi is an open source tool. As an automation tool, Sahi provides the facility of recording and play backing of scripts. Sahi supports Java and JavaScript. Even though the Sahi script looks like JavaScript, it is not executed as the regular JavaScript on the browser.

The basic idea of how Sahi work is described below: Sahi's core parts include the Sahi proxy server and the JavaScript engine.

HTML responses which pass through the proxy are modified such that JavaScript is injected at the start and the end of the response. This will allow the browser to record and playback scripts and talk back to the proxy when needed. Apart from handling requests for pages that the browser requests, Sahi also handles custom commands related to recording, playback etc. which the browser sends.

Besides the normal features like supporting ant, Sahi has its own features, like good support of database files, supporting JavaScript, AJAX and also the simple APIs.

Why Sahi

Compared to other web testing tools, Sahi controller (IDE) can be used in various browsers.

Sahi's APIs do not depend much on the HTML structure. Sahi does not use XPaths and has APIs like _near, _in etc. to help finding one element in relation to another. Even if the structure of the page changes, these work properly. Our client website is an ExtJS site; Sahi is helpful in dealing with the changing pages.

Getting Started

First of all, we can download the latest Sahi zip file from sourceforge. The prerequisite for Sahi is JRE 1.4 or above.

  • Unzip sahi.zip to a desired location.
  • Navigate to bin to start sahi proxy server.
  • Next, config the browser to use sahi proxy.

After finishing the setup, we can start creating scripts. The scripts can be written using any text editor with the facility of recording by controller. Press ALT and double click on the window, which you want to record.

The controller is as below:

Recording

  • On the popup that appears, give a name for the script you wish to generate, and click 'Record'
  • Most actions on the page will now get recorded.
  • Moving the mouse over any html element while pressing Ctrl key will show the JavaScript accessor info in the popup.
  • Clicking the "Add Assertions" button generates assertions for the element. These assertions can be added to the script by clicking "Add to Script".The script being generated can be viewed by opening the .sah file in the scripts directory. The script can be edited even while recording, so that logical segregation into functions etc. can be done while recording happens.
  • Once done, click stop.

Note that custom code can be tested, by entering code in the "debug" text area and clicking on "Test->". The command will be executed and the result will be displayed in the textbox near the” Test->"button.

Once confirmed that the desired result has been achieved, that code snippet can be added to the recording by clicking "Append to Recording".

Script Playback

  • Open (alt-dblclick) the popup window.
  • Select script file from the dropdown.
  • Or go to the "[from url]" section and specify the full URL of the script file. (e.g. http://proxyhost:proxyport/_s_/scrcipts/scriptName.sah)
  • Click 'Set'.
  • Wait for the pages to reload.
  • Click 'Play'.

Test Suite

Multiple tests can be run through ant. For each test in the suite, the ant target opens a browser, runs the test and closes the browser. The ant target specifies a suite file, say SuiteName.suite.

Data Driven Testing

Sahi has some inbuilt features for data driven testing. Sahi can read and write to CSV (Comma Separated Value) files, databases and Excel sheets.

  • _readCSVFile (csvFilePath) reads a CSV file and returns a two dimensional array.
  • _writeCSVFile (array2D, filePath) writes the two dimensional array into a file in CSV format.

What's next?

View Sahi in-detail with more examples in part 2 of this article.

Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.

Keep up to date with our latest insights