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

接着基於Ruby的watir-webdriver自動化測試方案與實施(二)
繼續 ... ...
 
編寫腳本首先要學會捕獲元素,接下來就要學習頁面元素的捕獲。

頁面元素

attribute_value

獲取當前控件的屬性javascript

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

rand_select

隨機選擇select list中的某一項php

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

popupwin

點擊彈窗上的‘肯定’按鈕html

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

sikuli_image

點擊圖片控件java

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

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

double_click

雙擊事件ios

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

right_click

右擊事件web

exist?

判斷用戶元素是否存在chrome

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

# 返回選中的選項  
puts b.select(:name, 'sex').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.set r.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.set c.set?

# 選擇checkbox  
b.checkbox(:name, 'check_me').set true 
# 清除選擇  
b.checkbox(:name, 'check_me').set false  
 
 

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.text s = b.span :class => 'ss-powered-by' s.exists? s.text

實例

 按鈕windows

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

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

 

 輸入框

?  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) (保存文件的彈出窗口)

alert

if b.alert.exists?  
    puts "ok"
else
    puts "no"
end

puts b.alert.text 

#b.alert.ok 
b.alert.close  

b.close

 圖片

?  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.new profile['browser.download.folderList'] = 2 # custom location  profile['browser.download.dir'] = download_directory profile['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.new profile['download.prompt_for_download'] = false profile['download.default_directory'] = download_directory b = Watir::Browser.new :chrome, :profile => profile

 

 

瀏覽器新窗口

當一個新的瀏覽器窗口打開時,你可使用'use'方法來處理這個新窗口。

 

browser.window(:title => "annoying popup").use do

  browser.button(:id => "close").click end

 

 

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 :chrome 10.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向所見即所得編輯器(應該指的是富文本編輯器)中輸入文字:

 

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

在瀏覽器上執行javascript,經過js腳本去設置編輯器的值

CKEditor

 

require 'watir-webdriver'

b = Watir::Browser.new :firefox b.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.new b.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

相關文章
相關標籤/搜索