您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Watir
10分鐘學(xué)會自動化測試框架--Cucumber + Watir
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/3/26 13:05:08 ] 推薦標(biāo)簽:Watir 測試框架 自動化

    測試成功,沒有看到任何Watir的影子。

 

(三)單獨使用Watir

    聽說有非常fashionable的office lady用Watir來完成日常例行并且繁瑣的網(wǎng)頁點擊工作的(當(dāng)然不是測試),聽說而已,但是Watir的確可以完成諸如此類的網(wǎng)頁模擬操作,接下類我們用Watir來完成google搜索功能,新建watir_google.rb文件并加入以下內(nèi)容:
復(fù)制代碼

1require 'watir-webdriver'
2browser = Watir::Browser.new :chrome
3browser.goto"www.google.com/"
4browser.text_field(:name=> "q").set"ThoughtWorks"
5browser.button(:name=> "btnG").click

復(fù)制代碼

  當(dāng)執(zhí)行到第2行時,一個瀏覽器窗口會自動打開,之后訪問google主頁(第3行),設(shè)置搜索關(guān)鍵詞"ThoughtWorks",后點擊搜索按鈕,單獨運行Watir成功,沒有任何Cucumber的影子。

 

(四)用Cucumber+Watir寫自動化測試

    由上文可知,Cucumber只是用接近自然語言的方式來描述業(yè)務(wù)行為,而Watir則只是對人為操作網(wǎng)頁的模擬。當(dāng)使用Cucumber+Watir實現(xiàn)自動化測試時,通過正則表達式匹配將Cucumber的行為描述與Watir的網(wǎng)頁操作步驟耦合起來即可。同樣以Google搜索為例,搜索關(guān)鍵字后,我們希望獲得搜索結(jié)果,先用Cucumber完成搜索行為描述:
復(fù)制代碼

1Feature:Googlesearch
2Scenario: search for keyword
3Given I amongoogle home page
4WhenI searchfor'ThoughtWorks'
5ThenI should be able toviewthe search result of 'ThoughtWorks'

復(fù)制代碼

   對應(yīng)的Watir代碼如下:
復(fù)制代碼

1require "rubygems"
2require "watir-webdriver"
3require 'rspec'

4Given /^I amongoogle home page$/do
5@browser = Watir::Browser.new :chrome
6@browser.goto("www.google.com")
7end
8
9When/^I searchfor'([^"]*)'$/ do |search_text|
10@browser.text_field(:name => "q").set(search_text)
11@browser.button(:name => "btnK").click
12end
13
14Then /^I should be able to view the search result of '([^"]*)'$/do|result_text|
15@browser.text.should include(result_text)
16end

復(fù)制代碼

    運行cucumber,一個新的瀏覽器被打開,顯示結(jié)果與(三)中相同。

(五)自動化測試的設(shè)計模式:Page對象

   在上面的例子中,我們在所有的step中均直接對@browser對象進行操作,對于這樣簡單的例子并無不妥,但是對于動則幾十個甚至上百個頁面的網(wǎng)站來說便顯得過于混亂,既然要面向?qū)ο,我們希望將不同的頁面也用對象來封裝,于是引入Page對象,既可完成對頁面的邏輯封裝,又實現(xiàn)了分層重用。此時位于high-level的Cucumber文件無需變動,我們只需要定義一個Page對象來封裝Google頁面(google-page.rb):
復(fù)制代碼

1require "rubygems"
2require "watir-webdriver"
3require "rspec"
4
5class GooglePage
6def initialize
7@browser = Watir::Browser.new :chrome
8@browser.goto("www.google.com")
9end
10
11def search str
12@browser.text_field(:name=> "q").set(str)
13@browser.button(:name=> "btnK").click
14end
15
16def has_text text
17@browser.text.should include(text)
18end
19end

上一頁1234下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd