Hey all.
Short version: the Proc I pass to Watir’s add_checker is run twice on
each page load
Long version:
I’m trying to get decent logging out of my watir scripts, without an
insane amount of extra code.
I settled on using the add_checker method, which takes a Proc, to
handle things like assertions.
Every class in my script inherits from SuperPage (shown below, heavily
heavily chopped down for ease of reading).
Aside from running the Proc twice, this seems to work well.
The throw is commented out, because well, sometimes you want the error
in your page, but you want the thing to continue. Obviously if it’s
not commented out, the proc only runs once. And yes I know having the
page seems like overkill, but I want to be able to retrieve it from an
xml log file later (after I replace my log method)
Thanks --Kyle
require ‘win32ole’
require ‘zlib’
require ‘base64’
require ‘watir’
class SuperPage
def initialize(baseURL,pageURL,pageName,phrases=[“Server Error”])
@pageURL = pageURL
@pageName = pageName
@baseURL = baseURL
@phrases = phrases
setupIE()
end
def logError(message)
log(:Error,message)
#commented out, so log the error but try and continue
#throw :assertion_error
end
def log(type,message)
puts("#{type.to_s}: #{message.to_s}")
end
def buildAssertionChecker()
Proc.new do
|ie|
@phrases.each do
|phrase|
if ie.text.include?phrase
z=Zlib::Deflate.new(9)
logError(“Found #{phrase}, compressed b64 encoded page is
#{Base64.encode64(z.deflate(ie.html,Zlib::FINISH))}”)
end
end
end
end
def setupIE()
begin
@ie=Watir::IE.attach(:url,/localhost/)
rescue
@ie=Watir::IE.new()
@ie.goto(@baseURL)
end
@ie.waitForIE
#add the checking facility
@ie.add_checker(buildAssertionChecker)
end
end