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);//提示失败
    }
posted @ 2014-08-29 10:00  echo1006  阅读(286)  评论(0编辑  收藏  举报