自动化测试工具都流行做对象映射,例如QTP的Object Repository、RFT的Object Map,如果要在Watir中做对象映射应该怎么做呢?
查找了一下OpenQA上关于Watir的FAQ,发现这种方法:
http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIcreateanapplication%2Fobjectmap%3F
How do I create an application/object map?
Insert the object recognition into a small method
def login_link;$ie.link(:text, 'Log in');end
def username_field;$ie.text_field(:name, 'userid');end
then in your test class do:
login_link.click
username_field.set(username)
但是好像行不通!修改成这样就可以:
require 'watir'
require 'test/unit'
class TC_myTest < Test::Unit::TestCase
def setup
@ie = Watir::IE.new
@ie.goto("http://www.google.cn")
end
def SearchField;@ie.text_field(:name,"q");end
def SearchButton;@ie.button(:name,"btnG");end
def test_1
#@ie = Watir::IE.new
#@ie.goto("http://www.google.cn")
SearchField().set('陈能技')
SearchButton().click()
end
end
后来又google了另外一种做法:
require 'watir'
module Watir
class Map
def initialize(ie)
@ie = ie
Watir::Container.instance_methods.each do |method|
Map.class_eval("def #{method}(k,v); @ie.#{method}(k,v); end") if
method !~/=/
end
end
end
end
ie = Watir::IE.start('http://google.cn')
map = Watir::Map.new(ie)
# If leave out the Map class and instead do
#map = Watir::IE.start('http://google.cn')
# 在这里集中定义测试对象映射
SearchField = map.text_field(:name, 'q')
SearchButton = map.button(:name, 'btnG')
# 使用映射对象
SearchField.set('陈能技')
SearchButton.click()