在Watir中進行物件對映(Object Map)的方法

TIB發表於2010-01-24

自動化測試工具都流行做物件對映,例如QTPObject RepositoryRFTObject Map,如果要在Watir中做物件對映應該怎麼做呢?

 

 

查詢了一下OpenQA上關於WatirFAQ,發現這種方法:

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了另外一種做法:

 

 

建立Map

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()

 

相關文章