值得收藏的Watir筆記

CharlesCui發表於2007-08-31

開發測試案例(Developing Test Cases)

1.開啟編輯器 2.以.rb為你的副檔名 3.在測試檔案的第一句寫上“require 'watir'”,確保可以訪問Watir工具。 4.開啟瀏覽器並轉到要測試的應用 5.與之互動並設計你的testcase 6.在測試指令碼中使用Watir方法 7.驗證結果 與網頁互動(Interacting With a Web Page)當使用Watir開發測試指令碼的時候,透過給網頁上的物件傳送訊息來與之互動。 ie.text_field(:name , "q").set("bluescorpio") ie.button(:value , "Click Me").click Watir語法(Watir Syntax)

1.使用Watir工具,需要在指令碼中加上 require 'watir'

2.建立一個IE的測試例項 ie = Watir::IE.new 或者在建立的同時直接轉到頁面 ie = Watir::IE.start(") Watir使用start方法同時建立一個瀏覽器例項並轉到一個頁面。

3.頁面導航 ie.goto()

4.操縱Web頁面物件

4.1超連結 4.1.1使用Text屬性點選超連結 ie.link(:text , "Pickaxe").click 對應的HTML程式碼為: 4.1.2使用URL屬性點選超連結 ie.link(:url , ").click 對應的HTML程式碼為:

4.2核取方塊 4.2.1使用name屬性設定核取方塊 ie.checkbox(:name, "checkme").set 4.2.2使用name屬性清除核取方塊 ie.checkbox(:name, "checkme").clear 4.2.3使用name和value屬性設定核取方塊 ie.checkbox(:name, "checkme", "1").set 4.2.4使用name和value屬性清除核取方塊 ie.checkbox(:name, "checkme", "1").clear 對應的HTML程式碼為:

4.3單選框 4.3.1使用name屬性設定單選框 ie.radio(:name, "clickme").set 4.3.2使用name屬性清除單選框 ie.radio(:name, "clickme").clear 4.3.3使用name和id屬性設定單選框 ie.radio(:name, "clickme", "1").set 4.3.4使用name和id屬性清除單選框 ie.radio(:name, "clickme", "1").clear 對應的HTML程式碼為:

4.4下拉框 4.4.1使用name屬性和值來設定下拉框 ie.select_list( :name , "selectme").select("is fun") 4.4.2使用name屬性和值來清除下拉框 ie.select_list( :name , "selectme").clearSelection 對應的HTML程式碼為:

4.5在Web頁面中輸入資料 4.5.1使用文字輸入框的那麼屬性設定輸入內容 ie.text_field(:name, "typeinme").set("Watir World") 4.5.2清空文字輸入框 ie.text_field(:name, "typeinme").clear 對應的HTML程式碼為:

4.6從Web頁面上提交資料 4.6.1按鈕 4.6.1.1透過值或標題屬性點選按鈕 ie.button(:value, "Click Me").click 4.6.1.2透過name屬性點選按鈕 ie.button(:name, "clickme").click 對應的HTML程式碼為: 4.6.2表單 4.6.2.1表單中的按鈕使用value或標題屬性 ie.button(:value, "Submit").click 對應的HTML程式碼為:

4.6.2.2表單中的圖片按鈕使用那麼屬性 ie.button(:name, "doit").click 對應的HTML程式碼為:
4.6.2.3沒有按鈕的表單 Watir can submit a form by identifying it by its name, action and method attributes. 可以透過name、action以及method屬性來提交表單 ie.form(:name, "loginform").submit ie.form(:action, "login").submit 對應的HTML程式碼為:

4.6.3框架 ie.show_frames可以列印出當前頁面框架的數量和名稱 Watir允許透過名稱屬性來訪問框架,如ie.frame("menu") 如果要訪問menu框架中的一個超連結,可以ie.frame("menu").link(:text, "Click Menu Item").click 4.6.4巢狀框架 ie.frame("frame1").frame(:name, "nested_frame") 4.6.5新視窗一些Web應用會彈出新視窗或開啟一個新視窗,可以使用attach方法來訪問並控制新視窗。透過標示新視窗的URL或者title來訪問。 ie2 = Watir::IE.attach(:url, '') ie3 = Watir::IE.attach(:title, 'Test New Window') 也可以使用正規表示式 ie4 = Watir::IE.attach(:title, /Test New/) 注意:不要把新視窗分配到你的ie變數,最好給新視窗一個不同的名字

5.驗證結果比較好的方法是在測試案例中假如驗證點 5.1物件存在使用Watir方法contains_text ie.contains_text("Reached test verification point.") if ie.contains_text("Reached test verification point.") puts: "Test passed. Page contains the text: Reached test verification point." else puts: "Test failed! Page didn't contain text: Reached test verification point." end 5.2使用test::unit Assertions 5.2.1需要test::unit require 'test/unit' 5.2.2建立測試例項 class TC_myTest < Test::Unit::TestCase ...fill in Test Case methods here... end 5.2.3建立測試用例方法在測試類中,需要宣告象下面的方法: def test_myTestCase fill in method body with Watir code and assertion here end 方法必須以test開頭,ruby會隨機執行測試案例,如果需要順序執行,需要在test後加上字母或數字來強迫它順序執行,比如“test_a_mytest” 定義測試方法的類: class TC_myTest < Test::Unit::TestCase def test_ myTestCase Watir code and assertion here... end def test_anotherTestCase Watir code and assertion here... end def test_aTestCase Watir code and assertion here... end end 5.2.4使用Assertions Watir透過在一個asert覆寫Watir方法支援assertions。 assert(ie.contains_text("Reached test verification point.") 5.2.5Setup and Teardown def setup fill in code that will run before every test case here... end def teardown fill in code that will run after every test case here... end

6.Tips and Tricks Running Tests With the Browser Not Visible Run the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b

[@more@]的

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9934490/viewspace-967238/,如需轉載,請註明出處,否則將追究法律責任。

相關文章