can call a method, but not a method within a user-defined class

Discussion in 'Ruby' started by aidy, Jun 4, 2006.

  1. aidy

    aidy Guest

    I have two ruby files.

    In the client file, I can do this:

    require 'browser'
    start_browser("http://gbahevm07l15:9081/wps/portal")

    In the browser file I have this method

    def start_browser (url)
    ......
    @ie.goto(url)
    ......
    end

    However if I envelop this method within a user defined class

    class Browser
    def start_browser (url)
    ......
    end
    end

    And in the client file, do this:

    require 'browser'

    Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")

    I receive this error

    'undefined method `start_browser' for main:Object (NoMethodError)'

    and do not know why.

    Could anyone help

    Thanks

    Aidy
     
    aidy, Jun 4, 2006
    #1
    1. Advertising

  2. aidy wrote:
    > class Browser
    > def start_browser (url)
    > ......
    > end
    > end
    >
    > And in the client file, do this:
    >
    > require 'browser'
    >
    > Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
    >
    > I receive this error
    >
    > 'undefined method `start_browser' for main:Object (NoMethodError)'


    Please post more code -- it's impossible to help you unless we have
    something to work with.


    Daniel
     
    Daniel Schierbeck, Jun 4, 2006
    #2
    1. Advertising

  3. aidy

    aidy Guest

    Hi,

    I apologise, as I was not clear at all.

    browser.rb

    require 'watir'

    class Browser
    def start_browser (url)
    @ie = Watir::IE.new
    @ie.goto(url)
    @ie.maximize()
    end
    end

    logon.rb

    class Login
    def login (username, password)
    @ie.link:)text, 'Log in').click
    @ie.text_field:)name, 'userid').set(username)
    @ie.text_field:)name, 'password').set(password)
    @ie.button:)value,'Log in').click
    end
    end

    test_1.rb

    require 'browser'
    require 'logon'

    Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
    Login.new.login("aidy","12345")


    the error I actually get is this

    'login': undefined method `link' for nil:NilClass (NoMethodError)

    Now, if I remove the classes: Browser + Login, but keep the methods.

    And in test_1.rb, I do this


    start_browser("http://gbahevm07l15:9081/wps/portal")
    login("aidy","12345")

    the test runs fine.

    Thank You

    Aidy
     
    aidy, Jun 4, 2006
    #3
  4. aidy wrote:

    > class Browser
    > def start_browser (url)
    > @ie = Watir::IE.new
    > @ie.goto(url)
    > @ie.maximize()
    > end
    > end
    >
    > logon.rb
    >
    > class Login
    > def login (username, password)
    > @ie.link:)text, 'Log in').click
    > @ie.text_field:)name, 'userid').set(username)
    > @ie.text_field:)name, 'password').set(password)
    > @ie.button:)value,'Log in').click
    > end
    > end
    >


    >
    > 'login': undefined method `link' for nil:NilClass (NoMethodError)
    >
    > Now, if I remove the classes: Browser + Login, but keep the methods.



    The problem is that in:

    def start_browser (url)
    > @ie = Watir::IE.new


    def login (username, password)
    > @ie.link:)text, 'Log in').click


    you are not referring the same instance variable (@ie in Login class is
    not even initialized). They belong to separate classes and thus separate
    instances.

    Two solutions come to my mind right now:

    put start_browser and login methods in the same class or
    use class variables to share them between Browser and Login class instances.

    lopex
     
    Marcin Mielżyński, Jun 4, 2006
    #4
  5. aidy

    aidy Guest

    Hi Marcin
    > use class variables to share them between Browser and Login class instances.


    Thanks for your patience, but is this a valid way of sharing class
    variables?

    browser.rb

    require 'watir'
    class Browser
    def start_browser(url)
    @@ie = Watir::IE.new
    @@ie.goto(url)
    @@ie.maximize()
    end
    end

    logon.rb

    require 'browser'
    class Login < Browser
    def login(username, password)
    @@ie.link:)text, 'Log in').click
    @@ie.text_field:)name, 'userid').set(username)
    @@ie.text_field:)name, 'password').set(password)
    @@ie.button:)value,'Log in').click
    end
    end


    test_case.rb

    require 'browser'
    require 'logon'
    Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
    Login.new.login("aidy","12345")

    Cheers

    Aidy
     
    aidy, Jun 4, 2006
    #5
  6. aidy wrote:

    >
    > require 'watir'
    > class Browser
    > def start_browser(url)
    > @@ie = Watir::IE.new
    > @@ie.goto(url)
    > @@ie.maximize()
    > end
    > end
    >
    > logon.rb
    >
    > require 'browser'
    > class Login < Browser
    > def login(username, password)
    > @@ie.link:)text, 'Log in').click
    > @@ie.text_field:)name, 'userid').set(username)
    > @@ie.text_field:)name, 'password').set(password)
    > @@ie.button:)value,'Log in').click
    > end
    > end


    In this case it is not necessary since you've introduced inheritance
    between Browser and Login classes. In this case Browser instance
    variables are inherited by Login and thus they are the same variables. I
    meant a case where these two classes were not related:

    class A

    def A.ie
    @@ie
    end

    def meth1
    @@ie = 'Foo'
    end

    end

    class B
    def meth2
    A.ie # here @@ie A class class variable is referenced
    end
    end

    so:

    a=A.new
    a.meth1
    b=B.new
    b.meth2

    will work as you expected (although it is not a good programming pattern ;)

    As I mentioned before, the best way is to put start_browser and login
    methods in one class since they share same context (@ie instance here).
    Furthermore I suggest to call start_browser in initialize method to make
    it run automatically on instance creation.

    hope that helps

    lopex
     
    Marcin Mielżyński, Jun 4, 2006
    #6
    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. Ian
    Replies:
    2
    Views:
    366
    Ulrich Petri
    Sep 6, 2003
  2. Oodini
    Replies:
    1
    Views:
    1,843
    Keith Thompson
    Sep 27, 2005
  3. Travis
    Replies:
    6
    Views:
    571
    Markus Schoder
    Jun 28, 2007
  4. Replies:
    1
    Views:
    541
    Sion Arrowsmith
    Jul 10, 2008
  5. Alok
    Replies:
    3
    Views:
    288
Loading...

Share This Page