Sahi (1) —— 快速入门(101 Tutorial)

Sahi (1) —— 快速入门(101 Tutorial)


jvm版本: 1.8.0_65

sahi版本: Sahi Pro 6.1.0

参考来源:

Sahi官网

[Sahi Quick Tutorial](http://sahipro.com/docs/using-sahi/quick-tutorial.html#Download Sahi Pro)

要求

Sahi运行需要Java 1.5 或更高版本

下载

从官网下载Sahi Pro需要注册获取30天的试用期。目前Sahi貌似已不在提供社区版。

install_sahi_pro_v610_20150617.jar

安装

双击“install_sahi_pro_xxx.jar”然后运行安装包

或命令行运行

java -jar install_sahi_pro_xxx.jar

*注意,需要将Sahi安装到目录没有空格的路径下,否则Chrome可能会出错。
河蟹

我们支持正版,想延长试用期的童鞋私聊。

启动Sahi

Sahi Dashboard自动启动Sahi代理,并且可以启动配置好的浏览器。

启动Sahi Dashboard有三种方式:

  1. 双击桌面图标

  2. 通过Windows启动菜单运行

  3. 命令行

    • Windows

      • 进入目录 '''<sahi_root>userdatain''' 然后运行 '''start_dashboard.bat'''
    • Linux/Mac

      • 进入目录 '''<sahi_root>/userdata/bin''' 然后运行 '''./start_dashboard.sh'''

录制Sahi脚本

  1. 以Firefox为例,点击Sahi Dashboard上的浏览器图标,我们可以看到Sahi的会为我们启动它的一个首页

  1. 在新的窗口打开“Sahi Controller”

  2. 在"Sahi Controller"上打开选项卡“Record”

  1. 在“Script Name:”处填上 first_script.sah,然后点击“Record”

  2. 点击“Sample Application”

  1. 打开Sahi的示例程序后,输入用户名“test”和密码“secret”,然后点击“Login”按钮登陆

    最后一步的记录可在“Evaluate Expression”中查看

  2. 登陆后是一个购物车,添加数量2,3,1然后点击“Add”按钮,底部会计算出总价。

断言

一个脚本通常包括网页上的操作以及功能验证,Sahi可以在录制脚本的过程中增加断言,来实现验证。

为总价加一个断言:

  1. 将鼠标移至页面html元素上,然后按住CRTL键

     如果是Mac系统,应用程序窗体需要在focus状态下接受鼠标的事件。
    
  2. “Accessor”字段会出现在Controller上,在这个例子中,我们将鼠标悬浮在“Grand Total”字段上

  3. 点击“Assert”按钮,为元素生成断言

  4. 这些断言会在“Evaluate Expression”中看到

  5. 点击“Test-->” 验证断言是否为真

  6. 一旦断言为真,点击“Append to Script”将断言加入脚本

	用“Evaluate Expression”和“Test-->”可以执行任何javascript脚本,在Controller上的操作不会被录下,只有页面上的直接操作才会被记录。
  1. 点击“登出”

  2. 在Controller上点击“Stop”完成录制

回放

  1. 输入刚才录制好的脚本文件名称到“File: ”字段下

  2. 输入测试的开始地址

     http://sahitest.com/demo/training/login.htm
    
  3. 点击“Play”按钮

脚本中的步骤会被执行,Controller也会在“Statement”里面输出执行的结果,一旦执行完毕,会在底部出现“SUCCSESS”或者“FAILURE”的字样。

*注意:Controller可以随时关闭,不会影响回放。

查看日志

修改脚本

/* --Objects Definitions Above-- */

function login($user, $password){
  _setValue(_textbox("user"), $user);
  _setValue(_password("password"), $password);
  _click(_submit("Login"));
}

function addBooks($qJava, $qRuby, $qPython){
  _setValue(_textbox("q"), $qJava);
  _setValue(_textbox("q[1]"), $qRuby);
  _setValue(_textbox("q[2]"), $qPython);
  _click(_button("Add"));
}

function verifyTotal($total){
  _assertExists(_textbox("total"));
  _assert(_isVisible(_textbox("total")));
  _assertEqual($total, _getValue(_textbox("total")));
}

function logout(){
  _click(_button("Logout"));
}

/* --Functions Above-- */

_navigateTo("http://sahi.co.in/demo/training/");

login("test", "secret");
addBooks("2", "3", "1");
verifyTotal("1550");
logout();

first_script_lib.sah

function login($user, $password){
  _setValue(_textbox("user"), $user);
  _setValue(_password("password"), $password);
  _click(_submit("Login"));
}

function addBooks($qJava, $qRuby, $qPython){
  _setValue(_textbox("q"), $qJava);
  _setValue(_textbox("q[1]"), $qRuby);
  _setValue(_textbox("q[2]"), $qPython);
  _click(_button("Add"));
}

function verifyTotal($total){
  _assertExists(_textbox("total"));
  _assert(_isVisible(_textbox("total")));
  _assertEqual($total, _getValue(_textbox("total")));
}

function logout(){
  _click(_button("Logout"));
}

first_script.sah

_include("first_script_lib.sah");

_navigateTo("http://sahi.co.in/demo/training/");

login("test", "secret");
addBooks("2", "3", "1");
verifyTotal("1550");
logout();

命令行使用

$ ./testrunner.sh first_script.sah http://sahitest.com/demo/training/ firefox

测试套件

invalid_login.sah

_setValue(_textbox("user"), "test");
_setValue(_password("password"), "badpassword");
_click(_submit("Login"));
_assert(_isVisible(_div("errorMessage")));
_assertEqual("Invalid username or password", _getText(_div("errorMessage")));    

tutorial.suite

first_script.sah
invalid_login.sah

命令行运行

$ ./testrunner.sh tutorial.suite http://sahitest.com/demo/training/ chrome

执行结果

结束

原文地址:https://www.cnblogs.com/richaaaard/p/5085148.html