您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Watir
Watir Webdriver生成文件型log的实例
作者:网络转载 发布时间:[ 2013/12/31 14:09:11 ] 推荐标签:Watir log

我们想获得很好的log日志,这需要我们动手去写,这里有个生成文件log的实例:

首先我们有个主log生成器, logfactory.rb

require 'logger'

# default logger
class LoggerFactory
 
  # start logger
  def LoggerFactory.start_default_logger(fileNamePrefix)
   
    # time = Time.now.strftime("%m %d %Y %H %M %s") 
 time = Time.now.strftime("%Y%m%d%H%M%S")

    # logger = CoreLogger.new(File.join(File.dirname(__FILE__), "#{fileNamePrefix}_#{time}.txt") ,logs to keep, maxlogsize)
 logger = CoreLogger.new(File.join(File.expand_path(".") + "\logs\", "#{fileNamePrefix}_#{time}.txt") ,2, 1000000)
    return logger
  end 
end

class  CoreLogger < Logger
 
  # extend the logger, overide it
  def initialize(fileName, logsToKeep, maxLogSize)
    super(fileName , logsToKeep, maxLogSize)
    self.level = Logger::INFO   # set to INFO level
    self.datetime_format = "%d-%b-%Y %H:%M:%S"
    self.info("Logger starting...")         
  end
 
  #overloaded "log" from logger.rb to something more intuitive.
  def log(message)
    puts "log #{message} "         #optional. comment out if you don't want to see logging in the console
    info(message)                   #calls info in logger.rb -- would be good to use different logging levels
  end
end 

然后在我们的case中引入:

require 'logfactory'

可以进行使用了:

def login_action
# Navigate to target URL
$browser.goto(TEST_SITE)
$logger.log("Passed: Step " + $stepcounter.to_s() + ": Navigate to URL:" + TEST_SITE)
$stepcounter=$stepcounter + 1

# wait till the user name element show up
Watir::Wait.until {$browser.text.include? "USER NAME:"}
  
# enter user name
    $browser.text_field(:name, "UserName").set(LOGINID)
    $logger.log("Passed: Step " + $stepcounter.to_s() + ": Enter '" + LOGINID + "' in the User Name text field")
    $stepcounter=$stepcounter + 1
    # enter user password
    $browser.text_field(:name, "UserPassword").set(PASSWORD)
    $logger.log("Passed: Step " + $stepcounter.to_s() + ": Enter '" + PASSWORD + "' in the Password text field")
$stepcounter=$stepcounter + 1
    # click Login button
    $browser.button(:value, "Login").click
    $logger.log("Passed: Step " + $stepcounter.to_s() + ": Click the 'Login' button")
$stepcounter=$stepcounter + 1
# if the duplicate login session pop up show up
if $browser.button(:value, 'OK').exist? # Yes, pop up is here
$browser.button(:value, 'OK').click # Click OK button
$logger.log("Passed: Step " + $stepcounter.to_s() + ": click the 'OK' button")
$stepcounter=$stepcounter + 1
end

# check login sucessfully or not
# Watir::Wait.until {$browser.title.include?('Home')}
    $browser.div(:id, "content").wait_until_present

if $browser.div(:id, "content").text.include?"Home"
$logger.log("Passed: Step " + $stepcounter.to_s() + ": Login successfully")
$stepcounter=$stepcounter + 1
else
$logger.log("Failed: Step " + $stepcounter.to_s() + ": Login failed")
$stepcounter=$stepcounter + 1
end
 end
  
  def logout_action
 
 # click the logout link
   $browser.link(:text, "Log Out").click
   $logger.log("Passed: Step " + $stepcounter.to_s() + ": Click the 'Log Out' link")

      # close the browser
      $browser.close
  end

生成的结果如:
# Logfile created on 2012-11-21 16:17:52 +0800 by logger.rb/31641
I, [21-Nov-2012 16:17:52#5096]  INFO -- : Logger starting...
I, [21-Nov-2012 16:17:52#5096]  INFO -- :
I, [21-Nov-2012 16:17:54#5096]  INFO -- : ------------------------------------------
I, [21-Nov-2012 16:17:54#5096]  INFO -- : ## Beginning of test case 01            ##
I, [21-Nov-2012 16:17:54#5096]  INFO -- : ------------------------------------------
I, [21-Nov-2012 16:17:55#5096]  INFO -- : Passed: Step 1000: Navigate to URL:http://10.32.152.113:8080/
I, [21-Nov-2012 16:17:57#5096]  INFO -- : Passed: Step 1001: Enter 'test@gmail.com' in the User Name text field
I, [21-Nov-2012 16:17:58#5096]  INFO -- : Passed: Step 1002: Enter 'password123' in the Password text field
I, [21-Nov-2012 16:17:58#5096]  INFO -- : Passed: Step 1003: Click the 'Login' button
I, [21-Nov-2012 16:17:59#5096]  INFO -- : Passed: Step 1004: click the 'OK' button
I, [21-Nov-2012 16:18:00#5096]  INFO -- : Passed: Step 1005: Login successfully
I, [21-Nov-2012 16:18:00#5096]  INFO -- : ------------------------------------------
I, [21-Nov-2012 16:18:00#5096]  INFO -- : ## End of test case 01                  ##
I, [21-Nov-2012 16:18:00#5096]  INFO -- : ------------------------------------------

软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd