// 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
對RUBY和WATIR框架進行進一步的分析。主要針對WATIR源碼進行簡單的分析。
一般的WATIR自動化測試都是以對瀏覽器的初始化開始的:
#Jaycer.D.Woo#
#open the IE browser
ie = Watir::IE.new
# print some comments
ie.goto test_site
這是簡單的初始化IE并連接到URL(test_site)。
這里用的是IEController的navigate屬性轉到相應的URL(與.NET中的nevigate同質):
# * url - string - the URL to navigate to
def goto( url )
@ie.navigate(url)
wait()
sleep 0.2
return @down_load_time
end
一般對PAGE中的object操作:
Text fields
ie.text_field(:name, "field_name").set("Watir World")
對應的源碼處理:
def text_field(how , what=nil)
return TextField.new(self, how, what)
end
在CLASS TextField中有:
def initialize( ieController, how , what )
@ieController = ieController
@how = how
@what = what
if(how != :from_object) then
@o = @ieController.getObject(@how, @what, supported_types)
else
@o = what
end
super( @o )
end
TextField的初始化方法還有supported_types,size,maxLength,assert_not_readonly,verify_contains,dragContentsTo,append,set,clear等方法。