Watir checker being run twice?

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

On Jul 23, 11:20 am, “Kyle S.” [email protected] wrote:

Thanks --Kyle
@pageName = pageName

      z=Zlib::Deflate.new(9)
rescue
  @ie=Watir::IE.new()
  @ie.goto(@baseURL)
end
@ie.waitForIE
#add the checking facility
@ie.add_checker(buildAssertionChecker)

end

end

You’d do better to post to the watir list.
This might be due to a number of reasons - if you can post some html I
might be better able to help you.
Which version of watir are you using?

Paul

On 7/23/07, Paul R. [email protected] wrote:

If the phrases are different for each page, then maybe delete the
checker when youve finished with it

Paul
Paul, I’m using Watir 1.5.1.1192.
As far as the error checker, it’s defined and set in the setupIE
method, which is only run once. Each page has it’s own @ie object, so
when pages aren’t using the default list of things to look for, they
shouldn’t conflict.
I wanted it setup this way, because there are times the test needs to
go to different pages and back again (SuperPage has a browseTo method
that I excluded from the previous post, so there is no watir/ie object
in the main test).

On Jul 23, 12:11 pm, Paul R. [email protected] wrote:

insane amount of extra code.
not commented out, the proc only runs once. And yes I know having the
class SuperPage
log(:Error,message)
|ie|

end

end

You’d do better to post to the watir list.
This might be due to a number of reasons - if you can post some html I
might be better able to help you.
Which version of watir are you using?

Paul

actually looking at it again, if you always use the same phrases, then
you are basically asking watir to run a new error_checker, but which
is checking for exactly the same thing.
If the phrases are different for each page, then maybe delete the
checker when youve finished with it

Paul

On Jul 23, 12:28 pm, “Kyle S.” [email protected] wrote:

If the phrases are different for each page, then maybe delete the
go to different pages and back again (SuperPage has a browseTo method
that I excluded from the previous post, so there is no watir/ie object
in the main test).

If you inherit from superpage then setupIE ( and hence the error
checker) will get run every time you create a subclass of superpage -
which is probably where you get multiple error checkers
if you alter buildErrorCHecker to something like ( I havent tstted
it )

def buildAssertionChecker()
Proc.new do
|ie|
puts “checker for #{self.class.name}”
@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

then you will see which page each checker is being created for

Paul

Wow am I confused now.

I added two such a lines to buildAssertionChecker, and one line to
setupIE: see below.
So it tells me when the proc is created and who its’ created for
when the proc is used, and who is using it
and when the proc is added to the @ie object.

Obviously I got tons of messages out, and they say that each of those
procs is created once, and is added to the class specific @ie once.

def buildAssertionChecker()
puts “Creating the checker for #{self.class.name}”
Proc.new do
|ie|
puts “checker for #{self.class.name}”
@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
puts “adding the checker to the ie object for #{self.class.name}”
@ie.add_checker(buildAssertionChecker)
end

Yes it seems to be
I just did this…
require ‘super_page.rb’
m =
SuperPage.new(“http://localhost:1948”,“Section/Page1aspx?selected=true”,“X”)

It ran the checker twice…
–Kyle

On Jul 23, 1:28 pm, “Kyle S.” [email protected] wrote:

#{Base64.encode64(z.deflate(ie.html,Zlib::FINISH))}")
@ie.goto(@baseURL)
end
@ie.waitForIE
#add the checking facility
puts “adding the checker to the ie object for #{self.class.name}”
@ie.add_checker(buildAssertionChecker)
end

but if you use an existing @ie, it will already have a checker. Im not
using the version of watir that you are, but I use lots of checkers
and dont see this problem.
If you do the simplest thing ( 1 page, 1 ie and 1 checker ) does it
run twice?

Paul

On Jul 23, 2:37 pm, “Kyle S.” [email protected] wrote:

Yes it seems to be
I just did this…
require ‘super_page.rb’
m = SuperPage.new(“http://localhost:1948”,“Section/Page1aspx?selected=true”,“X”)

It ran the checker twice…
–Kyle

I meant if you do this
p = Proc.new{ |ie| puts"YES" }
ie = Watir::IE.start
ie.add_checker( p )
ie.goto( ‘www.gmail.com’)

does watir report it twice?
There may be instances, ( iframes may be one of them ) where the
checker does run more than once
Paul

I’m going to repost this to the watir mailing list now. :slight_smile:

In that case it happens once :confused:
For my code it happens twice.