Sahi Pro - Getting Started

abstract This is a quick tutorial to get started with Sahi Pro Browser Automation.
warningRead this page to get familiar with Sahi Pro before trying the Sahi Pro Desktop, SAP or Mobile Add-ons.

Prerequisites

Java 1.8 or above is needed for running Sahi Pro.

Download Sahi Pro

Download Sahi Pro from Sahi's download archives. You will need a valid license to run Sahi Pro. If you do not have a valid license yet, you can register here for a 30 day evaluation license.

Upgrade Sahi

If you wish to upgrade from an older version of Sahi, refer to Upgrade for details.

Install Sahi

  1. Double click on install_sahi_pro_xxx.jar to start the installer. If Java is configured properly, it will launch the installer.


    info If the installer does not launch, or if the jar file is opened as if it is a zip file, cancel the unzip dialog, open a command prompt, cd to where the file is downloaded, and run
    java -jar install_sahi_pro_xxx.jar
  2. Install Sahi Pro in a folder path without spaces. (If there are spaces, Chrome will have trouble launching correctly.)

Start Sahi Dashboard

Start Sahi Dashboard by any of the following methods
  1. Double click on the desktop shortcut
  2. or
  3. Start from the command line.

    Windows: - Go to <sahi_root>\userdata\bin and run start_dashboard.bat

    Linux - Go to <sahi_root>/userdata/bin and run ./start_dashboard.sh

Click on Launcher button to see installed browsers and add-ons.
warning Sahi looks for browsers in their default install location, and if found, displays it on the Dashboard. If you do not see your browsers, configure your browser paths correctly.

warning If you are using Internet Explorer via a VPN or Datacard (like Tata Photon, Reliance etc.), you may need to explicitly modify the proxy settings for your browser




Add License Product Key

  1. If you have never added your Product Key, you will be presented with Sahi product key popup window.
    • Enter the Product Key and click 'OK'.
    • or
    • Copy the Product Key into userdata\config\productkey.txt (create the file if needed).
    If the Product Key is valid for selected product, Sahi Pro will start normally.

Recording through Sahi

  1. Bring up the browser of your choice by clicking on any of the browser icons. You should see a browser window like this:
  2. To launch controller click on "Sahi Controller" link. Controller can also be launched by ALT and double click on the browser window.
    info If that does not work, press CTRL and ALT keys together and then double click. Make sure popup blockers are turned off.
  3. On the Controller, go the the Record tab (would be selected by default).
  4. Give first_script.sah as the name for the script, and click the 'Record' icon. (Click on the 'Settings' icon to show or hide file paths.)
  5. Click on the "Sample Application" link on Sahi's start page. This will bring up Sahi's sample application.
  6. Enter username 'test' and password 'secret' and click 'Login'. The last recorded step will be visible in the 'Evaluate Expression' box.
  7. On the next page, which shows a shopping cart, add quantities 2,3,1 and click 'Add'. This will display the added items at the bottom and its total.

Add assertions

A script normally consists of actions performed on the web page and then verification of functionality. For verifications, we use assertions. Sahi allows adding assertions while recording. To add an assertion on the total field:
  1. Move the mouse over any html element while pressing CTRL key.
    warningOn Mac, the application window needs to be in focus to receive mouse events. First bring the application window into focus by clicking on the title bar, or by using Command-Tab.
  2. The Accessor field will get populated in the Controller. In this case, let us hover over the text field near 'Grand Total'
  3. Click the "Assert" button to generate assertions for the element.
  4. They will appear in the "Evaluate expression" box.
  5. Click → to check that the assertions are true.
  6. Once satisfied, click on "Append". This will add the assertions to the script.
  7. info You can evaluate any javascript using "Evaluate expression" and →. Actions performed via the Controller will not be automatically recorded. Only actions performed directly on the page are automatically recorded. This lets you experiment on the webpage at recording time with out impacting the script.
  8. Click 'Logout' to logout of the application.
  9. Click 'Stop' on the Controller to finish recording.
info Note that the Controller can be closed and reopened at any time, without disrupting recording.

Playing back

If not already open, open the Sahi Controller (ALT-DblClick on the page).
  1. Enter the script name for example: first_script.sah in the 'File:' field (with the help of the autocompletion feature).
  2. Enter the Start URL as 'http://sahitest.com/demo/training/login.htm'. For your own scripts, if you had started recording from http://www.google.co.in, use that URL.
  3. Click 'Play'.
Steps will start executing, and the Controller will be updated accordingly. Once finished, SUCCESS or FAILURE will be displayed at the end of the steps.

Note that the Controller can be closed at any time, without disrupting playback.

View Logs

On the Controller, go to Playback tab and click on "Logs" link at the bottom right. It will open a window with the results in HTML. Click on any script to view specific logs. Clicking on a line in the logs will drill down to exact line in script. You can also view the logs at http://localhost:9999/logs

Edit the recorded script

Recorded scripts need to be refactored into functions for better readability and maintenance.

Click on the Editor link on the Recorder to view and edit the currently recorded script. Sahi scripts are simple text files which use Javascript syntax. Notice how there are no waits or complex XPaths in the recorded script.

Extract Functions

While the script looks simple, it is still not talking in the language of the business. For example, if one tester were to communicate these action to another, she would describe it as follows:
  1. Login with 'test' and 'secret'
  2. Add books to cart in quantities 2,3 and 1
  3. Verify total is 1550
  4. Logout
To accomplish this, we need to create well named functions from our recorded steps.

On the Script Editor:
  1. Select the statements pertinent to login.
  2. Click on 'Create Function'.
  3. Give a name to the function, in this case 'login'.
  4. Click on Continue. This will create a function, and then call it with the correct parameters.
    /**
    * login($user, $password)
    * @param $user -
    * @param $password -
    */
    function login($user, $password){
      _setValue(_textbox("user"), $user);
      _setPassword(_password("password"), $password);
      _click(_submit("Login"));
    }
    /* --Functions Above-- */
  5. Keep doing this for the other lines, so that the script looks like this:
    /**
    * login($user, $password)
    * @param $user -
    * @param $password -
    */
    
    function login($user, $password){
      _setValue(_textbox("user"), $user);
      _setPassword(_password("password"), $password);
      _click(_submit("Login"));
    }
    
    /**
    * addBooks($qJava, $qRuby, $qPython)
    * @param $qJava -
    * @param $qRuby -
    * @param $qPython -
    */
    
    function addBooks($qJava, $qRuby, $qPython){
      _setValue(_textbox("q"), $qJava);
      _setValue(_textbox("q[1]"), $qRuby);
      _setValue(_textbox("q[2]"), $qPython);
      _click(_button("Add"));
    }
    
    /**
    * verifyTotal($total)
    * @param $total -
    */
    
    function verifyTotal($total){
      _assertExists(_textbox("total"));
      _assert(_isVisible(_textbox("total")));
      _assertEqual($total, _getValue(_textbox("total")));
    }
    
    /**
    * logout()
    */
    
    function logout(){
      _click(_button("Logout"));
    }
    
    /* --Functions Above-- */
    
    _navigateTo("http://sahitest.com/demo/training/");
    
    login("test", "MgkKEQBU");
    addBooks("2", "3", "1");
    verifyTotal("1550");
    logout();


    infoFor more details regarding Scenario Editor, refer here
    infoFor more details regarding Sahi Framework, refer here
  6. Run this script. Click on "Playback" button on Editor. Select browser and enter start URL. Click on "Run" button.
  7. View the logs. The logs now look nicely folded down at the function level.
  8. Clicking on 'login' will just open up the steps that were performed under login. This helps in easy understanding of logs and helps isolate problems easily.

Build a library file

  1. This script can further be broken into 2 scripts, one with the function definitions and another with just the calling code. Now hover over the arrow adjacent to the 'New' button and select the 'Script' option. first_script_lib.sah
    /**
    * login($user, $password)
    * @param $user -
    * @param $password -
    */
    
    function login($user, $password){
      _setValue(_textbox("user"), $user);
      _setPassword(_password("password"), $password);
      _click(_submit("Login"));
    }
    
    /**
    * addBooks($qJava, $qRuby, $qPython)
    * @param $qJava -
    * @param $qRuby -
    * @param $qPython -
    */
    
    function addBooks($qJava, $qRuby, $qPython){
      _setValue(_textbox("q"), $qJava);
      _setValue(_textbox("q[1]"), $qRuby);
      _setValue(_textbox("q[2]"), $qPython);
      _click(_button("Add"));
    }
    
    /**
    * verifyTotal($total)
    * @param $total -
    */
    
    function verifyTotal($total){
      _assertExists(_textbox("total"));
      _assert(_isVisible(_textbox("total")));
      _assertEqual($total, _getValue(_textbox("total")));
    }
    
    /**
    * logout()
    */
    
    function logout(){
      _click(_button("Logout"));
    }


    first_script.sah
    _include("first_script_lib.sah");
    
    _navigateTo("http://sahitest.com/demo/training/");
    
    login("test", "MgkKEQBU");
    addBooks("2", "3", "1");
    verifyTotal("1550");
    logout();

Create a Scenario file

The Sahi Framework allows testers to write their testcases in Spreadsheet Format(Excel like) and run it from Sahi. Often a testing team consists of a mix of subject matter experts, some manual testers and testers with some automation experience. Writing tests in the language of the business allows all stake holders to participate and derive value out of the automation process.
  1. To create a new scenario file, hover over the arrow adjacent to the 'New' button and select the 'Scenario' option.
  2. Default scenario file will look like this:
  3. Right click on cell, and select "Remove Row" from the context menu, for the rows with data in blue color.
  4. Save the file.
  5. Start typing the name of function. In this case our function name is "login", so on typing initial letters, for example: "lo", Sahi Editor's autocomplete gives a list of possible options. Select the function from the dropdown.
  6. Now do Ctrl+click on "login" cell. "Function Detail" popup-window gives details of the selected function and its arguments. Click on "Include". This is make "first_script_lib.sah" file available to the scenario file. If other library files contains function with same name, select the required library file.
  7. Enter the function arguments.
  8. Enter all the functions and arguments. The scenario file will look like this
  9. Executing the Scenario File
  10. Executing the Scenario sheet is no different from executing a Sahi script. Click on "Playback" button on the Sahi Editor, and then click on "Run" button.
  11. Play back reports/logs
  12. On execution, Sahi generates logs showing success or failure. Logs are visible from the "Logs" link in Editor. Refer to sahi/userdata/scripts/sahitests/framework folder for some examples.

Suites - Automating playback of multiple scripts

To playback multiple scripts in batch mode, we need to first create a data driven suite file.
  1. Create another script file invalid_login.sah with the following contents.
    _setValue(_textbox("user"), "test");
    _setPassword(_password("password"), "badpassword");
    _click(_submit("Login"));
    _assert(_isVisible(_div("errorMessage")));
    _assertEqual("Invalid username or password", _getText(_div("errorMessage")));
  2. Now we have 2 scripts, scenario1.s.csv and invalid_login.sah. Check the checkboxes adjacent to these script names in Editor's File navigator area
  3. To create a new data driven suite file, hover over the dropdown adjacent to the 'Create Suite' button in Editor and select the 'Data Driven Suite' option.
  4. Now the data driven suite file will look like this:
  5. Save the file.
  6. To execute the suite file, click on "Playback" button on the Sahi Editor, and then click on "Run" button.
  7. Play back reports/logs for suite file

  8. Clicking on any of the script links will take you to the logs of the individual scripts as seen before.

Further Reading

  1. Business Driven Test Automation - IMPORTANT!
  2. Sahi Pro Best Practices - IMPORTANT!
  3. Understanding object identification for resilient scripts
  4. Using Accessor Repository
  5. Analyzing failing Script/Suite
  6. Integrating with Jenkins or other continuous integration