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);//提示失败
    }
### Sahi简介及其在IT工具和框架中的定位 Sahi是一种用于Web应用程序测试的开源自动化工具[^3]。它通过模拟真实用户的操作行为来验证网站的功能性和稳定性,从而帮助企业快速发现并修复潜在问题。与ChangeAgent不同的是,Sahi专注于功能测试而非文件链接管理[^1]。 #### 功能特点 Sahi的主要特性包括但不限于以下几点: - **跨浏览器支持**:能够兼容多种主流浏览器(如Chrome、Firefox等),确保应用在不同环境下的表现一致性。 - **易于编写脚本**:提供简单直观的API接口以及JavaScript作为主要编程语言,降低学习成本的同时提高了开发效率。 - **无插件运行模式**:无需安装额外扩展程序即可完成大部分任务,简化部署流程。 以下是利用Python调用selenium库实现类似功能的一个例子: ```python from selenium import webdriver driver = webdriver.Chrome() driver.get("http://example.com") element = driver.find_element_by_id("loginButton") if element.is_displayed(): print("Login button found.") else: print("Element not visible.") driver.quit() ``` 尽管上述代码片段展示的是基于 Selenium 的解决方案,但它可以用来对比说明 Sahi 如何更高效地处理某些特定场景下的交互逻辑[^4]。 #### 应用价值 对于关注于提升产品质量的企业而言,采用合适的测试手段至关重要[^2]。而像Sahi这样的专用框架则能有效缩短迭代周期时间,并减少因人为失误引发的风险概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值