[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

aidy

6/4/2006 2:23:00 PM

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

5 Answers

Daniel Schierbeck

6/4/2006 2:53:00 PM

0

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

aidy

6/4/2006 3:20:00 PM

0

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

Marcin Mielzynski

6/4/2006 3:56:00 PM

0

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

aidy

6/4/2006 7:18:00 PM

0

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

Marcin Mielzynski

6/4/2006 8:10:00 PM

0

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