基於Ruby的Watir-WebDriver自動化測試框架


 

Watir-WebDriver

                      —— 軟件測試的自動化時代 QQ群:160409929 

支持哪些瀏覽器?

幾乎所有的瀏覽器: 比如Firefox, Chrome 和IE,除了Safari。

支持網頁上哪些元素?

watir-webdriver支持所有的HTML元素

運行模式是什么?

Watir-WebDriver是基於ruby開發web驅動框架

自動化測試框架

根據不同業務開發相應自動化用例,由Ruby測試框架統一調用分析展示。實現出入口統一,工具類封裝;降低用例開發復雜度,框架統一管理效驗.

頁面元素

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").click

ie.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.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?

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

實例

 按鈕

?  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.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內建了處理這些對話框的方法,並且可以返回對話框中顯示的內容。首先,加載這個擴展:

 

require "watir-webdriver/extensions/alerts"

JAVASCRIPT ALERTS

 

browser.alert do

  browser.button(:value => 'Alert').click

end #=> 'the alert message'

JAVASCRIPT CONFIRMS

 

browser.confirm(true) do

  browser.button(:value => 'Confirm').click

end #=> 'the confirm message'

JAVASCRIPT PROMPT

 

browser.prompt('hello') do

  browser.button(:value => 'Prompt').click

end #=> { :message => 'foo', :default_value => 'bar' }
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)

 

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

:null

:cancel

:help

:backspace

:tab

:clear

:return

:enter

:shift

:left_shift

:control

:left_control

:alt

:left_alt

:pause

:escape

:space

:page_up

:page_down

:end

:home

:left

:arrow_left

:up

:arrow_up

:right

:arrow_right

:down

:arrow_down

:insert

:delete

:semicolon

:equals

:numpad0

:numpad1

:numpad2

:numpad3

:numpad4

:numpad5

:numpad6

:numpad7

:numpad8

:numpad9

:multiply

:add

:separator

:subtract

:decimal

:divide

:f1

:f2

:f3

:f4

:f5

:f6

:f7

:f8

:f9

:f10

:f11

:f12

:meta

:command
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

 


免責聲明!

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



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