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

A

aidy

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
 
D

Daniel Schierbeck

aidy said:
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
 
A

aidy

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
 
M

Marcin Mielżyński

aidy said:
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
 
A

aidy

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
 
M

Marcin Mielżyński

aidy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top