[英]Watir - Logging outside of a method
I'm still fairly new in using Watir for automation testing and I've hit another possibly insanely easy problem that I need to reach out to the community for a little bump in the right direction. 在使用Watir进行自动化测试时,我还是一个新手,我遇到了另一个可能非常简单的问题,我需要联系社区以向正确的方向发展。
I'm trying to use logger in Watir, which I can get to work fine if I keep everything within methods. 我正在尝试在Watir中使用记录器,如果将所有内容都保留在方法中,则可以正常工作。 If I don't have a method defined, for instance when using a loop, I can't get logger to work. 如果我没有定义方法,例如在使用循环时,则无法使记录器正常工作。
Here is an example code I'm playing with: 这是我正在使用的示例代码:
$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
Here are the modifications I made which are currently failing: 这是我所做的当前失败的修改:
$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
Thanks! 谢谢!
It looks like you have a scoping problem. 看来您有范围问题。 When you define a variable inside of a method or loop, it's a local variable that exists only within that method or loop. 当您在方法或循环内定义变量时,它是仅存在于该方法或循环内的局部变量。 Once the method or loop exits, the variable is gone. 一旦方法或循环退出,变量就消失了。 You'll need to declare $logger outside of the setup method for it to be available outside of that method. 您需要在setup方法之外声明$ logger,以使其在该方法之外可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.