我在使用Watir進行自動化測試方面還是比較新的,並且我遇到了另一個可能非常簡單的問題,我需要向正確的方向向社區伸出一點小凹凸。Watir - 在方法外部記錄
我想在Watir中使用記錄器,如果我將所有內容都保存在方法中,我可以正常工作。如果我沒有定義方法,例如使用循環時,我不能讓記錄器工作。
下面是一個示例代碼我與玩:
$LOAD_PATH << File.dirname(__FILE__)
require 'xls'
require 'watir'
xlFile = XLS.new(Dir.pwd + '/test_XLS_data.xls') #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('Google Search Data','Example') #pull data records from excel
xlFile.close
myData.each do |record|
ie = Watir::IE.start('google.com')
ie.text_field(:name,'q').set(record['SearchString'])
ie.button(:value,/Search/i).click
if ie.contains_text(record['ContainsText'])
puts "Results of search: '#{record['SearchString']}' contains '#{record['ContainsText']}'"
else
puts "Error: could not find text: '#{record['ContainsText']}' in results of search: '#{record['SearchString']}'"
end
sleep 3
ie.close
end
下面是我所做的修改這是目前出現故障:
$LOAD_PATH << File.dirname(__FILE__)
require 'xls'
require 'watir'
require 'example_logger1.rb'
def setup
filePrefix = "xls_log"
#create a logger
$logger = LoggerFactory.start_xml_logger(filePrefix)
$ie.set_logger($logger)
end
xlFile = XLS.new(Dir.pwd + '/test_XLS_data.xls') #grab the data file in the same dirrectory
$logger.log("")
$logger.log("getting data from Excel")
myData = xlFile.getRowRecords('Google Search Data','Example') #pull data records from excel
xlFile.close
myData.each do |record|
ie = Watir::IE.start('google.com')
ie.text_field(:name,'q').set(record['SearchString'])
ie.button(:value,/Search/i).click
if ie.contains_text(record['ContainsText'])
puts "Results of search: '#{record['SearchString']}' contains '#{record['ContainsText']}'"
else
puts "Error: could not find text: '#{record['ContainsText']}' in results of search: '#{record['SearchString']}'"
end
sleep 3
ie.close
end
謝謝!
你究竟在哪裏調用setup()方法?我在發佈的代碼中看不到它。 – Jonathan 2011-03-03 11:06:06