// 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等方法。