Using Sahi

Using Sahi -

Before you start - Prerequisites
Java 1.5 or above is needed for running Sahi.

Download Sahi
Download Sahi V3 (2009-11-30) from SourceForge

Install Sahi
Unzip sahi.zip to a desired location

Start proxy of Sahi:

Windows: - Go to <sahi_root>/userdata/bin and run start_sahi.bat
Linux - Go to <sahi_root>/userdata/bin and run start_sahi.sh

Note that by default Sahi uses port 9999. This can be modified through sahi.properties

Configure the browser:
You need to change your browser's proxy setting to use Sahi's proxy.

Firefox:

  • Go to Tools > Options >Advanced >Network> Connection Settings >
  • Set to "Manual Proxy Configuration"
  • Set "HTTP Proxy" to "localhost"
  • Set "Port" to "9999". (This setting can be modified through /config/sahi.properties)
  • Do the same for "SSL Proxy" too, if you wish to record and playback https sites
  • Keep "Use the same proxy for all protocol" unchecked as Sahi does not understand protocols other than HTTP
  • NOTE: "No Proxy for" should NOT have localhost in it.

Internet Explorer:

  • Go to Tools > Internet Options > Connections > LAN Settings >
  • In "Proxy server" section, Check "Use a proxy server for your LAN"
  • Click on "Advanced"
  • For HTTP: set "Proxy address to use" to "localhost" and set "Port" to "9999"
  • For Secure: set "Proxy address to use" to "localhost" and set "Port" to "9999" (if you want to test https sites too)
  • Clear out anything in "Do not use proxy server for addresses beginning with:"
  • OK
  • Leave "Bypass proxy server for local addresses" unchecked
  • OK > OK

Recording through Sahi

  • Press ALT and double click on the window which you want to record. Sahi's Controller window will pop up. (If that does not work, press CTRL and ALT keys together and then double click. Make sure popup blockers are turned off. For further trouble shooting have a look at this help link.)
  • On the Controller, go the the Record tab (would be selected by default).
  • Give a name for the script, and click 'Record'
  • Navigate on your website like you normally would. Most actions on the page will now get recorded.
  • Add an assertion:
    o Move the mouse over any html element while pressing Ctrl key. The Accessor field will get populated in the Controller.
    o Click the "Assert" button to generate assertions for the element. They will appear in the "Evaluate Expression" box.
    o Click "Test --->" to check that the assertions are true. You can evaluate any javascript using "Evaluate Expression" and "Test --->". 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.
    o Once satisfied, click on "Append to Script". This will add the assertions to the Script.
  • Click "Stop" to finish recording.

Note that the Controller can be closed and reopened at any time, without disrupting recording.

View the recorded script

The recoded script can be viewed and edited by opening the .sah file in the <sahi_root>/userdata/scripts directory. Sahi scripts are simple text files which use Javascript syntax.

The script can be edited even while recording, so that logical segregation into functions etc. can be done as recording happens.

Playing back

  • Open the Sahi Controller (ALT-DblClick on the page).
  • Enter the script name in the "File:" field (with the help of the autocompletion feature).
  • Enter the Start URL of the test. If you had started recording from http://www.google.co.in , use that URL.
  • Click 'Set'.
  • Wait for the page to reload.
  • 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 "View Logs" link at the bottom right. It will open a window with the results neatly formatted in HTML.

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

That is it! You have successfully recorded and played back a Sahi script!

Help! The Sahi Controller does not come up

The controller will only come up if the web page has been modified by the Sahi proxy

Try the following:

  • Refresh the browser and check
  • Force Reload (override cache),
    Firefox: Ctrl+F5 or Ctrl+Shift+R Internet Explorer: Ctrl + F5
    or Clear the browser cache manually and reload.
  • Make sure popup blockers are turned off.
  • If you are surfing the intranet on IE:
    Go to Tools -> Internet Options -> Connections -> LAN Settings. There, make sure that "Bypass proxy for local addresses" is unchecked.

Configuration Sahi and improve test speed

  • Replace sahi.bat with the one attached.
  • Modify sahi.bat and setup JAVA_HOME, using jdk6 will have better performance.
  • Add the sahi-patch.jar to %SAHI_HOME%/lib
    • You can download sahi.bat and sahi-patch.jar

Sahi script

  • 驗證用戶名和密碼
    function Test($username, $password){ _setValue(_textbox("j_username"), $username); _setValue(_password("j_password"), $password); }
    Test("root", "root")
    _click(_submit("Login"));
  • 包含另一個腳本文件
    _include("path/common_functions.sah");
  • 定義變量名並賦值
    var $variableName = value;
    OR
    var $variableName;
    $variableName = value;
  • 在控制檯打印信息
    function printThroughJava(s){ Packages.java.lang.System.out.println("Through Java: " + s); }
    printThroughJava("Hi there");
    //將在控制檯打印出Through Java: Hi there
  • if條件語句
    var $i = 2;
    if($i == 1){ Packages.java.lang.System.out.println("i = " + $i); }else{ Packages.java.lang.System.out.println("i = " + $i); }
    }
  • for循環語句
    /*This loop will login with user1, password1, user2, password2 etc.
    login and logout are custom functions.*/
    var $max = 10;
    for(var $i=0; $i<$max; $i++){ Packages.java.lang.System.out.println("i = " + $i); }

    * while循環語句
    $i = 0;
    while ($i++ < 10) { Packages.java.lang.System.out.println("i = " + $i); }
    }
  • 捕捉異常
    try{ _click(_link("does not exist")); }catch(e){
    _log("exception occur"); //顯示此行並執行下一句
    //_logException(e);//不顯示任何信息
    Packages.java.lang.System.out.println("excute next step");
    $i = 0;
    while($i++ < 10){ Packages.java.lang.System.out.println("i = " + $i); }
    //_logExceptionAsFailure(e);//提示失敗
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章