Watir(发音类似water),Web ApplicationTestinginRuby.它是Ruby语言下,一个自动化UI测试的框架。对于Watir的测试框架,它对于ActiveX插件,JavaApplets, Flash, 或者其他的插件应用程序基本不支持。如何确定您测试的页面是否Watir支持,只要您在页面中右击出现View Source 选项,那么Waitr就基本支持。Watir还带有AutoIt扩展对Javascript弹出或者其他的弹出窗口。
在Watir中的对象包括Hyperlinks, Checkboxes, Buttons, Forms, Radio buttons, Selection boxes, Text fields, Frames, Images, Tables等等。我们可以使用 ie.show_all_objects来显示页面中的所有元素,当我们无法确定页面对象是否被选中时可以使用flash方法,比如ie.text_field(:name, "test_text").flash。以下的对象属性对于Watir的使用非常有用。比如name,id,index,value,URL(links),Alt,SRC(images)。
以下是Watir对于页面的一些操作:
Hyperlinks
ie.link(:text, "link_name").click
Checkboxes
ie.checkbox(:name, “checkbox_name”, “value”).set
ie.checkbox(:name, "checkbox_name").set
ie.checkbox(:name, "checkme").clear
Radio buttons
ie.radio(:name, "clickme").set
ie.radio(:name, “radio_name”, “value”).set
ie.radio(:name, "clickme").clear
Selection boxes
ie.select_list( :name , "select_list_name").select("select_option")
ie.select_list( :name , "selectme").clearSelection
Text fields
ie.text_field(:name, "field_name").set("Watir World")
ie.text_field(:name, "typeinme").clear
HTML Buttons
ie.button(:value, "Click Me").click
Forms
ie.form(:name, "form_name").submit
ie.form(:action, "action_name").submit
Frames
ie.show_frames# show all the frames existed in the same page
ie.frame("menu").link(:text, "Click Menu Item").click# operate in frame
Images
ie.image(:name, “image_name”).click
Tables
ie.table(:id, “table_id”)
Manipulate row
row = ie.table(:index, 2)[2]
count=1
row.each do |cell|
#fill in code to do an action or verifaction
end
Manipulate cell
cell = ie.table(:index, 2)[2][2]
我们将Table看作一个页面, 我们能在上面检索buttons, images, links, tables等等的对象.
ie.row(:id, 'row1')
ie.cell(:id, 'cell_id')
ie.table(:index, 2)[2][2].table(:index,1)[1][1].text
ie.table(:index, 2)[1][1].button(:name, “button_name”).click
当我们点击按纽或者连接打开新的window,我们能使用attach去识别依靠新的window的URL或者Windowstitle.
ie2 = Watir::IE.attach(:url, 'http://mytestsite')
ie3 = Watir::IE.attach(:title, 'Test New Window')
Watir也支持正则表达式,这对测试将是非常有用的,特别是对页面对象进行复杂验证时。
JavaScript.弹出框比如警告窗口,系统安全弹出框以及其他不属于页面的窗口。我们就得依靠AutoIt来操作了。
// jsPopupWindow.rb
$ie=Watir::IE.new
javascript_page = 'C:\\Watir\\unittests\\html\\JavascriptClick.htm'
$ie.goto(javascript_page)
Thread.new { system("rubyw jscriptExtraAlert.rb")}
proc{ $ie.button(:id, 'btnAlert').click }.call
// jscriptExtraAlert.rb
require 'watir/WindowHelper'
helper = WindowHelper.new
helper.push_alert_button()
// WindowHelper.rb
class WindowHelper
def initialize( )
@autoit = WIN32OLE.new('AutoItX3.Control')
end
def push_alert_button()
@autoit.WinWait "Microsoft Internet Explorer", ""
@autoit.Send "{ENTER}"
end
....
end
Ruby拥有自己的XUNIT框架test::unit,这是框架式自动化测试的基础。
以下是一个简单的实例:
require 'unittests/setup'
require 'test/unit'
class TC_Fields < Test::Unit::TestCase
include Watir
def setup()
gotoTestPage
end
def gotoTestPage() $ie.goto($htmlRoot + "textfields1.html")end
def test_tabbing
$ie.text_field(:name, 'text1').focus
$ie.send_keys('{tab}')
$ie.send_keys('Scooby')
assert('Scooby', $ie.text_field(:name, 'beforetest').value)
end
def test_enter
$ie.text_field(:name, 'text1').focus
$ie.send_keys('{tab}{tab}{tab}{tab}')
$ie.send_keys('Dooby{enter}')
assert($ie.contains_text('PASS'))
end
end