Example Logging

Submitted by Jonathan Kohl

See attached file: example_logger1.rb

These are examples of how to integrate logging with Watir. Watir uses "logger", which is included in the Ruby library when you install Ruby. -Jonathan Kohl

If you make improvements to this example, please share them here!

Example 1:
----------
This example logs to a default log file that catches any log method calls in
Watir itself or in any test cases, and logs results to an XML file. This is a
very simple example that does minimal XML logging by creating the tags in methods
within the example_logger1.rb file.

example_logger1.rb uses inheritance from watir::logger and from logger. A
default logger is created which writes all "log" method calls to a text file
prefixed by "test_logger1". The example test: test_logger1.rb uses the default
"core" logger, and also creates an XML Logger object which logs the test results
to an XML log file also prefixed by "test_logger1".

When "test_logger1" is run, it will create two log files in the same directory:
test_logger1_<date>.txt
test_logger1_<date>.xml


require 'watir'
require 'test/unit' 

$LOAD_PATH << File.dirname(__FILE__)
require 'example_logger1'

class TC_google_logging < Test::Unit::TestCase
  
  def start
    #open the IE browser
    $ie = Watir::IE.new
    filePrefix = "test_logger1"
    #create a logger 
    $logger = LoggerFactory.start_xml_logger(filePrefix) 
    $ie.set_logger($logger)
  end
  
  def test_a_simplesearch
    
    #call start method...
    start #fires up the IE browser and a logger object
    
    #variables
    test_site = 'http://www.google.com'
    #
    $logger.log("")
    $logger.log("## Beginning of test: Google search") #logs only to corelogger file 
    $logger.log("Step 1: Go to the Google site: www.google.com")
    $ie.goto(test_site)
    $logger.log(" Action: entered " + test_site + " in the address bar.")
    
    $logger.log("Step 2: Enter 'pickaxe' in the search text field")
    $ie.text_field(:name, "q").set("pickaxe")
    $logger.log("  Action: entered pickaxe in the search field")
    
    $logger.log("Step 3: Click the 'Google Search' button")
    $ie.button(:name, "btnG").click
    $logger.log("  Action: clicked the Google Search button.")
    
    $logger.log("Expected Result: ")
    $logger.log(" - A Google page with results should be shown. 'Programming Ruby' should be high on the list.")
    
    $logger.log("Actual Result: Check that the 'Programming Ruby' link actually appears on the page by using an assertion")
    
    if $ie.text.include?("Programming Ruby") 
      $logger.log("Passed. Found test string 'Programming Ruby' ")
      $logger.log_results("test_a_simplesearch", "pickaxe", "Programming Ruby", "TEST PASSED.") #logs to both the XML file and corelogger
    else
      $logger.log("*FAILED*.")
      $logger.log_results("test_a_simplesearch", "pickaxe", "Programming Ruby", "TEST FAILED.")  #logs to both the XML file and corelogger    
    end
    
    $logger.log "## End of test: google search\n"
    
  end # end of test_simplesearch
  
  def test_b_googlenews
    
    #variables
    test_site = 'http://news.google.com'
    
    $logger.log("## Beginning of test: Google News")
    
    $logger.log("Step 1: go to the Google news site: news.google.com")
    $ie.goto(test_site)
    $logger.log("  Action: entered " + test_site + " in the address bar.")
    
    $logger.log("Step 2: Select Canada from the Top Stories drop-down list")
    $ie.select_list( :index , 1).select("Canada English")
    $logger.log("  Action: selected Canada from the drop-down list.")
    
    $logger.log("Step 3: click the 'Go' button")
    $ie.button(:caption, "Go").click
    $logger.log("  Action: clicked the Go button.")
    
    $logger.log("Expected Result: ")
    $logger.log(" - The Google News Canada site should be displayed")
    
    $logger.log(" Actual Result: Check that 'Canada' appears on the page by using an assertion")
    
    if $ie.text.include?("Canada") 
      $logger.log("TEST PASSED. Found test string 'Canada' ")
      $logger.log_results("test_b_googlenews", "Canada English", "Canada", "TEST PASSED.") #logs to both the XML file and corelogger
    else
      $logger.log("TEST FAILED.")
      $logger.log_results("test_b_googlenews", "Canada English", "Canada", "TEST FAILED.")  #logs to both the XML file and corelogger    
    end
    
    $logger.log '## End of test: Google news selection'
    
    $logger.end_log  #close XML log file
    
  end # end of test_googlenews
  
end  #end of class TC_google_logging


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 17, 2009

    William Zhang says:

    for the  example_logger1.rb Line 9 and Line 15     time = ...

    for the  example_logger1.rb

    Line 9 and Line 15

        time = Time.now.strftime("%m %d %Y %H %M %s")  

    should be replaced as 

        time = Time.now.strftime("%m %d %Y %H %M %S")  

    =============

    line 16

        logger = XMLLogger.new(File.join(File.dirname(_FILE_), "#

    Unknown macro: {fileNamePrefix}

    _#

    Unknown macro: {time}

    .txt") ,2, 1000000, "#

    _#

    Unknown macro: {time}

    .XML")

     should be replaced as

        logger = XMLLogger.new(File.join(File.dirname(_FILE_), "#

    Unknown macro: {fileNamePrefix}

    _#

    .txt") ,2, 1000000, File.join(File.dirname(_FILE_), "#

    Unknown macro: {fileNamePrefix}

    _#

    Unknown macro: {time}

    .XML") )