[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

watir checker being run twice?

Kyle Schmitt

7/23/2007 5:21:00 PM

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

10 Answers

Paul Rogers

7/23/2007 6:11:00 PM

0

On Jul 23, 11:20 am, "Kyle Schmitt" <kyleaschm...@gmail.com> wrote:
> 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

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

Paul Rogers

7/23/2007 6:14:00 PM

0

On Jul 23, 12:11 pm, Paul Rogers <pmr16...@gmail.com> wrote:
> On Jul 23, 11:20 am, "Kyle Schmitt" <kyleaschm...@gmail.com> wrote:
>
>
>
> > 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
>
> 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

Kyle Schmitt

7/23/2007 6:28:00 PM

0

On 7/23/07, Paul Rogers <pmr16366@gmail.com> wrote:
> > 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
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).

Paul Rogers

7/23/2007 6:57:00 PM

0

On Jul 23, 12:28 pm, "Kyle Schmitt" <kyleaschm...@gmail.com> wrote:
> On 7/23/07, Paul Rogers <pmr16...@gmail.com> wrote:> > 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
>
> 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).

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

Kyle Schmitt

7/23/2007 7:29:00 PM

0

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

Paul Rogers

7/23/2007 7:38:00 PM

0

On Jul 23, 1:28 pm, "Kyle Schmitt" <kyleaschm...@gmail.com> wrote:
> 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

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

Kyle Schmitt

7/23/2007 8:37:00 PM

0

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

Paul Rogers

7/23/2007 8:53:00 PM

0

On Jul 23, 2:37 pm, "Kyle Schmitt" <kyleaschm...@gmail.com> 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

Kyle Schmitt

7/23/2007 9:03:00 PM

0

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

Kyle Schmitt

7/23/2007 9:05:00 PM

0

I'm going to repost this to the watir mailing list now. :)

On 7/23/07, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> In that case it happens once :/
> For my code it happens twice.
>