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>\userdata\bin''' 然后運行 '''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

執行結果

結束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM