基於Ruby的watir-webdriver自動化測試方案與實施(三)

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   使用   sp   for   

接著基於Ruby的watir-webdriver自動化測試方案與實施(二)http://www.cnblogs.com/Javame/p/4159468.html繼續 ... ... 編寫指令碼首先要學會捕獲元素,接下來就要學習頁面元素的捕獲。頁面元素attribute_value

擷取當前控制項的屬性

Value = ie.link(:id=>‘xxx’).attribute_value("href")
rand_select

隨機播放select list中的某一項

ie.select_list(:name=>’’).rand_select
popupwin

點擊彈窗上的‘確定’按鈕

ie.popupwin.button(:name=>"確定").click
sikuli_image

點擊圖片控制項

ie.sikuli_image(:image=>"1.png").clickie.sikuli_image(:image=>"1.png;2.png").click#可以指定多張圖片來識別
double_click

雙擊事件

ie .sikuli_image(:image=>"1.png").double_click
right_click

右擊事件

exist?

判斷使用者元素是否存在

edit = ie.text_field(:name,"username")                                                                        if edit.exist?()                                      #The highlighted                                     edit.flash                                                              ie.text_field(:name, "password").set(pwd)                                                 ie.button(:class, "x-login-submit").click                             end                   end
Text Fields
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘t = b.text_field :id => ‘entry_0‘t.exists?t.set ‘your name‘t.value
Select Lists – Combos
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘s = b.select_list :id => ‘entry_1‘s.select ‘Ruby‘s.selected_options
Radios
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘r = b.label(:text => ‘What is ruby?‘).parent.radio :value => ‘A gem‘r.exists?r.setr.set?
Checkboxes
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘c = b.label(:text => ‘What versions of ruby?‘).parent.checkbox :value => ‘1.9.2‘c.exists?c.setc.set?
Buttons
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘btn = b.button :value, ‘Submit‘btn.exists?btn.click
Links
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘l = b.link :text => ‘Google Docs‘l.exists?l.click
Divs & Spans
require ‘watir-webdriver‘b = Watir::Browser.start ‘bit.ly/watir-webdriver-demo‘d = b.div :class => ‘ss-form-desc ss-no-ignore-whitespace‘d.exists?d.texts = b.span :class => ‘ss-powered-by‘s.exists?s.text
執行個體

 按鈕

?  ie.button(:name=>"",:id=>"",:index=>n,:type=>"").click

?  ie.button(:name=>"",:id=>"",:index=>n,:type=>"").doclick

 

 輸入框

?  ie.text_field(:name=>"").set "變數"

?  ie.text_field(:name=>"").value 取text_field值不是用text而是value!

 

 下拉框

?  ie.select_list(:name=>"").select "下拉框值"

?  ie.select_list(:name=>"").select "#1" #表示第一項內容

?  ie.select_list(:name=>"").rand_select

?  ie.select_list(:name=>"").getSelectedItems|getAllContents->返回Array

 

 單選框

?  ie.radio(:id=>"",:name=>"",:index=>n).set(選中當前radio)

?  ie.radio(:id=>"",:name=>"",:index=>n).clear(取消選中當前radio)

         ie.div(:class=>"iradio_minimal-blue checked").radios[1]

 

 複選框

?  ie.check_box(:id=>"",:name=>"",:index=>n).set(true|false)(true表示選中,false表示不選中)

?  ie.check_box(:id=>"",:name=>"",:index=>n).clear(取消選中當前checkbox)

 

 連結

?  ie.link(:text=>"").click/doclick

?  ie.link(:text=>"").href(返回當前link指向的連結)

 cell (TD標籤,用時一般需要先找到上層控制項如table、div等)

?  ie.table(:class=>"",:index=>n).cell(:class=>"",:index=>n).text

?  ie.table(:index=>n).rows 行  列 .text (行、列從1開始)

?  ie.div(:class=>"",:index=>n).cell(:class=>"",:index=>n).text

 span

?  ie.table(:id=>"").span(:class=>"").text

 

 彈出框

?  ie.popupwin.get_static_text (返回當前提示框的文本)

?  ie.popupwin.button(:name=>"確定").click/doclick (前一個點擊按鈕必須用doclick)

?  ie.file_dialog(:index=>1/2).set_file(file_path_download,true) (儲存檔案的快顯視窗)

 

 圖片

?  ie.image(:src=>/word3a_nor.gif/).click/doclick

 

back

後退

ie.back

 

forward

前進

ie.forward

   

refresh

重新整理頁面

ie.refresh

 

在Watir-WebDriver中處理frame是非常簡單的,就跟處理其他頁面元素一樣:

 

b.frame(:id => "content_ifr").send_keys "hello world"

 

檔案的下載

最簡單最好的處理檔案下載對話方塊的方式就是完全的避免對話方塊彈出。

 

可以在代碼裡告訴瀏覽器自動的將檔案下載到指定目錄,然後在測試案例中訪問該目錄進行驗證。

 

Firefox download_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows? profile = Selenium::WebDriver::Firefox::Profile.newprofile[‘browser.download.folderList‘] = 2 # custom locationprofile[‘browser.download.dir‘] = download_directoryprofile[‘browser.helperApps.neverAsk.saveToDisk‘] = "text/csv,application/pdf" b = Watir::Browser.new :firefox, :profile => profile

 

關於Firefox的所有配置項可以通過在地址欄中輸入‘about:config‘進行查看。

 

If you want to know a way to work out the file types (eg. application/pdf) then you can read the following blog post for an step by step guide. 如果你想知道如何處理特定類型的檔案,請閱讀這篇博文。

 

Chrome download_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if  Selenium::WebDriver::Platform.windows? profile = Selenium::WebDriver::Chrome::Profile.newprofile[‘download.prompt_for_download‘] = falseprofile[‘download.default_directory‘] = download_directory b = Watir::Browser.new :chrome, :profile => profile

 

 

瀏覽器新視窗

當一個新的瀏覽器視窗開啟時,你可以使用‘use‘方法來處理這個新視窗。

 

browser.window(:title => "annoying popup").use do  browser.button(:id => "close").clickend

 

 

JS彈出框

在web應用中,JavaScript對話方塊是十分常見的。

 

Watir-WebDriver內建了處理這些對話方塊的方法,並且可以返回對話方塊中顯示的內容。首先,載入這個擴充:

 

View Code

可選方法

 

如果你使用上面的方法時遇到了麻煩,你可以自行覆蓋JavaScript functions,這樣一來原來應該顯示的對話方塊就可以在觸發時不顯示了。

 

# 使alert方法返回空

browser.execute_script("window.alert = function() {}")

 

# 使prompt返回特定的字串,用來類比使用者的輸入

browser.execute_script("window.prompt = function() {return ‘my name‘}")

 

# 使prompt方法返回null用來類比使用者點擊了Cancel(取消)按鈕

browser.execute_script("window.prompt = function() {return null}")

 

# 使confirm方法返回true用來類比使用者點擊了OK(確定)按鈕

browser.execute_script("window.confirm = function() {return true}")

 

# 使confirm方法返回false用來類比使用者點擊了Cancel(取消)按鈕

browser.execute_script("window.confirm = function() {return false}")

 

頁面效能

Watir-WebDriver-Performance gem 提供在訪問頁面的同時進行頁面效能度量的功能,其使用的是W3C頁面效能度量指標。這是一個完美的捕獲響應效能指標的解決方案,其使用方法非常直觀和簡單,不過目前只支援Chrome和IE9l瀏覽器。

 

require ‘watir-webdriver‘require ‘watir-webdriver-performance‘b = Watir::Browser.new :chrome10.times do  b.goto ‘http://17test.info‘  load_secs = b.performance.summary[:response_time]/1000 puts "Load Time: #{load_secs} seconds."end

 

其統計結果如下:

 

Load Time: 3.701 seconds.

 

截屏

Watir-WebDriver內建的功能很贊也很好用。

 

browser.driver.save_screenshot ‘screenshot.png‘

 

The great thing about this is it gives you a screen shot of the entire page, not just above the fold. 功能最棒的地方在於它能捕獲到整個頁面,而不是螢幕上顯示的那部分。

 

如果你正在使用Cucumber,那麼你可以簡單的將下面的代碼添加到env.rb檔案中,這樣你可以在html的報告中插入:

 

After do |scenario|  browser.driver.save_screenshot ‘screenshot.png‘  embed ‘screenshot.png‘, ‘image/png‘end

 

類比特殊按鍵

使用.send_keys方法可以類比特殊的鍵盤按鍵(比如shift),其參數是你所需要類比的按鍵的符號表示(symbolic)。

 

b.send_keys :enter

 

也可以這樣做:

 

b.element.send_keys [:control, ‘a‘], :backspace

 

你還可以修改click方法的行為,使得點擊可以配合按鍵一起進行:

 

b.element.click(:shift, :control)

 

支援的按鍵鍵名列表如下:

View Code富文字編輯器

有兩種方法可以通過Watir-WebDriver向所見即所得 (WYSIWYG)編輯器(應該指的是富文字編輯器)中輸入文字:

 

定位編輯器所在的iFrame,然後使用.send_keys方法(缺點是瀏覽器必須在前台運行)

在瀏覽器上執行javascript,通過js指令碼去設定編輯器的值

CKEditor

 

require ‘watir-webdriver‘b = Watir::Browser.new :firefoxb.goto ‘http://ckeditor.com/demo‘b.execute_script("CKEDITOR.instances[‘editor1‘].setData(‘hello world‘);")b.frame(:title => ‘Rich text editor, editor1, press ALT 0 for help.‘).send_keys ‘hello world again‘TinyMCE Editor require ‘watir-webdriver‘b = Watir::Browser.newb.goto ‘http://tinymce.moxiecode.com/tryit/full.php‘b.execute_script("tinyMCE.get(‘content‘).execCommand(‘mceSetContent‘,false, ‘hello world‘ );")b.frame(:id => "content_ifr").send_keys ‘hello world again‘

 

QA

 

基於Ruby的watir-webdriver自動化測試方案與實施(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.