Wednesday, November 4, 2009

Watir Tutorial: Developing cross-browser Watir-IE/FireWatir-FireFox test case with logging capability

Developing cross-browser Watir-IE/FireWatir-FireFox test case with logging capability



Those who have their test cases written for Watir/IE with logging capability must have bump into this issue. WatirLogger is not available for FireWatir. Suggested solution is - Create your own subclass of Logger instead of using WatirLogger. Or use Log4R.



http://wiki.openqa.org/display/WTR/Firewatir+Compatibility

Which means as a test developer you will have to maintain two set of test cases one for Watir/IE and other for FireWatir/FireFox. And maintaining two sets of scripts for same test scenario is never a good idea. I always prefer to develop a test that works with at least IE and FireFox and not to maintain two different sets.



So if you want to write cross-browser test cases with logging functionality, you have one fo the following three options.

  • Do not use WatirLogger and create your own subclass of Logger.

  • Do not use WatirLogger and use Log4R or any other logger gem available.

  • Temporarily extend the FireWatir to support logging capabilities



In this blogpost I'll explore third option mentioned above.



This is what I tried to extend FireWatir to use with logger and do not have to change my test cases for FireWatir



1. Copy the file logger.rb from Watir gem to <FireWatir-gem-dir>\lib\firewatir and Modify the file to define FireWatirLogger as shown below.


module FireWatir
class FireWatirLogger < Logger
def initialize(filName, logsToKeep, maxLogSize)
super(filName, logsToKeep, maxLogSize)
self.level = Logger::DEBUG
self.datetime_format = "%d-%b-%Y %H:%M:%S"
self.debug("FireWatir starting")
end
end

class DefaultLogger < Logger
def initialize
super(STDERR)
self.level = Logger::WARN
self.datetime_format = "%d-%b-%Y %H:%M:%S"
self.info "Log started"
end
end
end


You can use the same logger.rb without any modification and just include watir/logger, but I choose to copy the logger.rb and modify it. I thought this might be cleaner and less confusing approach.


2. Add this line to firewatir.rb. This is optional and you can explicitly include this in the file you want to use logger, including firefox.rb. So include this in firewatir.rb and you will not have to worry about it later.


require 'firewatir/logger'



3. Add the following lines of code in firefox.rb


a. declare the accessor

# access to the logger object
attr_accessor :logger

b. initialize the logger instant in the constructor

If you choose not to copy and modify the logger from Watir and use it as it is then add this line to FireFox class constructor [initialize method]

@logger = DefaultLogger.new

OR if you followed step #1 then you v=can also use the FireWatirLogger as below

@logger = FireWatirLogger.new



4. define log method for FireFox class in firewatir.rb

def log(what)
@logger.debug(what) if @logger
end



5. Declare global variable $browser_type and set it to IE/FF in your test case or in your top level testlist/test configuration file if you are following unit test framework.

e.g. $browser_type = "FF"



6. Include the related files depending on which browser you want to run this test in


if ($bType == "IE")
require 'watir'
require 'watir/exceptions'
elsif ($bType == "FF")
require 'firewatir'
require 'firewatir/exceptions'
require 'firewatir/logger' #if not included in firewatir.rb
else
$browser.logger.fatal( "Invalid browser type only IE or FF")
exit
end



7. Write method startFireFox_with_logger
In your test case, if you want the same script to run for FF as well as IE, then following trick works for me.
Write the method to start FireFox with logger as you have for Watir

def start_ie_with_logger
$browser = Watir::IE.new()
$browser.logger = Watir::WatirLogger.new( 'debug.txt', 4, 10000 )
$browser.logger.level = Logger::INFO
end

def start_ff_with_logger
$browser = FireWatir::Firefox.new()
$browser.logger = FireWatir::FireWatirLogger.new( 'debug.txt', 4, 10000 )
$browser.logger.level = Logger::INFO
# any other customization ...
end



8. Initialize the browser instance depending on the browser type

if ($bType == "FF")
puts "creating FF browser instance"
start_ff_with_logger
elsif ($bType == "IE")
puts "creating IE browser instance"
start_ie_with_logger
else
puts "cannot create browser instance"
end



9. You can use the logger as you would do when using Watir/IE

$browser.logger.debug("Executing TC_" + numonic + "_Title: Testcase " + name + " title...")
$browser.logger.info($browser.title)
$browser.logger.fatal( "Invalid browser type only IE or FF")


I hope you find this blog post useful. If you got any cool tricks, workarounds, or any issues you experience during test automation with Watir, please do share your experience and thoughts by posting your comments.