Watir and Inheritance

Discussion in 'Ruby' started by Mario Ruiz, Oct 23, 2007.

  1. Mario Ruiz

    Mario Ruiz Guest

    I'm trying to do this:
    -----------------------------
    require 'Watir'

    class Navigator<Watir::IE
    def initialize(maxim)
    @maxim=maxim
    @iex=Watir::IE.new
    end
    def go(url)
    @iex.goto(url)
    if @maxim==1 then
    @iex.maximize()
    end
    end
    end

    naveg=Navigator.new(1)
    naveg.go("http://www.google.es")

    puts naveg.url
    --------------------------------
    I want my 'naveg' object can display the methods and properties of
    wakir, url for example. What am I doing wrong?
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Oct 23, 2007
    #1
    1. Advertising

  2. Mario Ruiz

    Kyle Schmitt Guest

    Without looking at this too indepth...
    you are inheriting from Watir::IE, and then making a new instance of
    it inside of it? Ehh?

    Start off with changing
    class Navigator<Watir::IE
    to
    class Navigator

    Now your class will work (but won't be inheriting). I don't know what
    you really want to do with your class :) but hey, have fun playing
    with it!

    --Kyle

    On 10/23/07, Mario Ruiz <> wrote:
    > I'm trying to do this:
    > -----------------------------
    > require 'Watir'
    >
    > class Navigator<Watir::IE
    > def initialize(maxim)
    > @maxim=maxim
    > @iex=Watir::IE.new
    > end
    > def go(url)
    > @iex.goto(url)
    > if @maxim==1 then
    > @iex.maximize()
    > end
    > end
    > end
    >
    > naveg=Navigator.new(1)
    > naveg.go("http://www.google.es")
    >
    > puts naveg.url
    > --------------------------------
    > I want my 'naveg' object can display the methods and properties of
    > wakir, url for example. What am I doing wrong?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Kyle Schmitt, Oct 23, 2007
    #2
    1. Advertising

  3. Mario Ruiz

    Drew Olson Guest

    Mario Ruiz wrote:
    > I want my 'naveg' object can display the methods and properties of
    > wakir, url for example. What am I doing wrong?


    You want to call the url method on the @iex object, not the navigator
    object. Try this:

    require 'Watir'

    class Navigator<Watir::IE
    def method_misssing(name,*args)
    if @iex.respond_to? name
    @iex.send(name,*args)
    end
    end
    def initialize(maxim)
    @maxim=maxim
    @iex=Watir::IE.new
    end
    def go(url)
    @iex.goto(url)
    if @maxim==1 then
    @iex.maximize()
    end
    end
    end

    naveg=Navigator.new(1)
    naveg.go("http://www.google.es")

    puts naveg.url
    --
    Posted via http://www.ruby-forum.com/.
    Drew Olson, Oct 23, 2007
    #3
  4. Mario Ruiz

    Mario Ruiz Guest

    If I write the class without inheriting, how can I get the 'url' outside
    of the class?
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Oct 24, 2007
    #4
  5. Mario Ruiz

    Mario Ruiz Guest

    Drew Olson wrote:
    > You want to call the url method on the @iex object, not the navigator
    > object. Try this:

    ...

    This is not working.
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Oct 24, 2007
    #5
  6. On 10/23/07, Mario Ruiz <> wrote:
    > I'm trying to do this:
    > -----------------------------
    > require 'Watir'
    >
    > class Navigator<Watir::IE
    > def initialize(maxim)
    > @maxim=maxim
    > @iex=Watir::IE.new
    > end
    > def go(url)
    > @iex.goto(url)
    > if @maxim==1 then
    > @iex.maximize()
    > end
    > end
    > end
    >
    > naveg=Navigator.new(1)
    > naveg.go("http://www.google.es")
    >
    > puts naveg.url
    > --------------------------------
    > I want my 'naveg' object can display the methods and properties of
    > wakir, url for example. What am I doing wrong?


    I think what you are doing wrong is having a Watir::IE object inside
    your Navigator class. If you inherit from IE you have all methods in
    IE available so you don't need to wrap another instance of IE inside.
    Can you try this (not tested):

    require 'Watir'

    class Navigator<Watir::IE
    def initialize(maxim)
    @maxim=maxim
    end
    def go(url)
    goto(url)
    if @maxim==1 then
    maximize()
    end
    end
    end

    naveg=Navigator.new(1)
    naveg.go("http://www.google.es")

    puts naveg.url

    Hope this helps,

    Jesus.
    Jesús Gabriel y Galán, Oct 24, 2007
    #6
  7. Mario Ruiz

    Mario Ruiz Guest

    Jesús Gabriel y Galán wrote:...

    I'm sorry but it doesn't work because the class doesn't understand the
    IE methods without the suffix.
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Oct 24, 2007
    #7
  8. Mario Ruiz

    Mario Ruiz Guest

    By the way the error is:
    c:/ruby/lib/ruby/site_ruby/1.8/Watir.rb:1341:in `url': undefined method
    `LocationURL' for nil:NilClass (NoMethodError)
    from tempexample.rb:22
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Oct 24, 2007
    #8
  9. Mario Ruiz

    Kyle Schmitt Guest

    Watir's ie object allows you to get at things like the url using
    public methods. Unless the idea really is extending or or replacing
    them, inheritance shouldn't be necessary (or probably advised). Well,
    that is unless the whole point is to play around with Watir to learn
    about it, more than get work done ;)

    On 10/24/07, Jes=FAs Gabriel y Gal=E1n <> wrote:
    > On 10/23/07, Mario Ruiz <> wrote:
    > > I'm trying to do this:
    > > -----------------------------
    > > require 'Watir'
    > >
    > > class Navigator<Watir::IE
    > > def initialize(maxim)
    > > @maxim=3Dmaxim
    > > @iex=3DWatir::IE.new
    > > end
    > > def go(url)
    > > @iex.goto(url)
    > > if @maxim=3D=3D1 then
    > > @iex.maximize()
    > > end
    > > end
    > > end
    > >
    > > naveg=3DNavigator.new(1)
    > > naveg.go("http://www.google.es")
    > >
    > > puts naveg.url
    > > --------------------------------
    > > I want my 'naveg' object can display the methods and properties of
    > > wakir, url for example. What am I doing wrong?

    >
    > I think what you are doing wrong is having a Watir::IE object inside
    > your Navigator class. If you inherit from IE you have all methods in
    > IE available so you don't need to wrap another instance of IE inside.
    > Can you try this (not tested):
    >
    > require 'Watir'
    >
    > class Navigator<Watir::IE
    > def initialize(maxim)
    > @maxim=3Dmaxim
    > end
    > def go(url)
    > goto(url)
    > if @maxim=3D=3D1 then
    > maximize()
    > end
    > end
    > end
    >
    > naveg=3DNavigator.new(1)
    > naveg.go("http://www.google.es")
    >
    > puts naveg.url
    >
    > Hope this helps,
    >
    > Jesus.
    >
    >
    Kyle Schmitt, Oct 24, 2007
    #9
  10. Mario Ruiz

    Mario Ruiz Guest

    Mario Ruiz, Oct 24, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,135
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    815
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,036
  4. Rouslan Korneychuk
    Replies:
    8
    Views:
    596
    Rouslan Korneychuk
    Feb 10, 2011
  5. Anukul Singhal
    Replies:
    1
    Views:
    164
    Charley Baker
    May 15, 2008
Loading...

Share This Page