# NoMethodError: undefined method

**URL:** <https://rubytalk.org/t/nomethoderror-undefined-method/63211>\
**Category:** ruby-talk\
**Created:** [31 July 2011 10:52 UTC](https://rubytalk.org/t/nomethoderror-undefined-method/63211 "2011-07-31T10:52:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurang\_Shah](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@Gaurang\_Shah](https://rubytalk.org/u/Gaurang_Shah)\
**Post date:** [31 July 2011 10:52 UTC](https://rubytalk.org/t/nomethoderror-undefined-method/63211/1 "2011-07-31T10:52:19Z")

</div>

I am not able to understand why it gets nil object. would someone let me  
know the solution ??

following are my classes and modules.  
LoginTest.rb  
require 'LoginPage'  
include LoginPage  
require 'BaseClass'  
include BaseClass

require 'rubygems'  
require 'watir'  
require 'test\unit'  
class LoginTest \< Test::Unit::TestCase

&nbsp;&nbsp;def test\_login()  
&nbsp;&nbsp;&nbsp;&nbsp;username\_textbox.set("Gaurang")  
&nbsp;&nbsp;&nbsp;&nbsp;password\_textbox.set("gaurang")  
&nbsp;&nbsp;&nbsp;&nbsp;login\_button.click()  
&nbsp;&nbsp;end

end

LoginPage.rb  
module LoginPage  
&nbsp;&nbsp;def username\_textbox  
&nbsp;&nbsp;&nbsp;&nbsp;return @browser.text\_field(:id,"Email")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def password\_textbox  
&nbsp;&nbsp;&nbsp;&nbsp;return @browser.text\_field(:id,"Passwd")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def login\_button  
&nbsp;&nbsp;&nbsp;&nbsp;return @browser.button(:id,"signIn")  
&nbsp;&nbsp;end  
end

BaseClass.rb  
module BaseClass  
&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;@browser = Watir::Browser.new  
&nbsp;&nbsp;&nbsp;&nbsp;@browser.goto("[http://gmail.com](http://gmail.com)")  
&nbsp;&nbsp;&nbsp;&nbsp;@browser.maximize  
&nbsp;&nbsp;end

end

Now when i run the LoginTest.rb it gives me following error.  
NoMethodError: undefined method `text\_field' for nil:NilClass

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Y\_NOBUOKA](https://avatars.discourse-cdn.com/v4/letter/y/c2a13f/32.png) [@Y\_NOBUOKA](https://rubytalk.org/u/Y_NOBUOKA)\
**Post date:** [31 July 2011 11:05 UTC](https://rubytalk.org/t/nomethoderror-undefined-method/63211/2 "2011-07-31T11:05:02Z")

</div>

Hi,

Do you initialize the instance variable @browser?  
I think you need to call the method BaseClass.setup.

Please retry after rewriting the following code

> def test\_login()  
> &nbsp;&nbsp;&nbsp;username\_textbox.set("Gaurang")  
> &nbsp;&nbsp;&nbsp;password\_textbox.set("gaurang")  
> &nbsp;&nbsp;&nbsp;login\_button.click()  
> end

into

def test\_login()  
&nbsp;&nbsp;&nbsp;&nbsp;setup() # Add this line  
&nbsp;&nbsp;&nbsp;&nbsp;username\_textbox.set("Gaurang")  
&nbsp;&nbsp;&nbsp;&nbsp;password\_textbox.set("gaurang")  
&nbsp;&nbsp;&nbsp;&nbsp;login\_button.click()  
end

Regards,

> **···**
>
> --  
> NOBUOKA Yu

---

<div class="post-metadata">

**Author:** ![Gaurang\_Shah](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@Gaurang\_Shah](https://rubytalk.org/u/Gaurang_Shah)\
**Post date:** [31 July 2011 12:56 UTC](https://rubytalk.org/t/nomethoderror-undefined-method/63211/3 "2011-07-31T12:56:24Z")

</div>

Y. Nobuoka wrote in post #1013959:

> Hi,
> 
> Do you initialize the instance variable @browser?  
> I think you need to call the method BaseClass.setup.
> 
> Please retry after rewriting the following code
> 
> > def test\_login()  
> > &nbsp;&nbsp;&nbsp;username\_textbox.set("Gaurang")  
> > &nbsp;&nbsp;&nbsp;password\_textbox.set("gaurang")  
> > &nbsp;&nbsp;&nbsp;login\_button.click()  
> > end
> 
> into
> 
> def test\_login()  
> &nbsp;&nbsp;&nbsp;&nbsp;setup() # Add this line  
> &nbsp;&nbsp;&nbsp;&nbsp;username\_textbox.set("Gaurang")  
> &nbsp;&nbsp;&nbsp;&nbsp;password\_textbox.set("gaurang")  
> &nbsp;&nbsp;&nbsp;&nbsp;login\_button.click()  
> end
> 
> Regards,

actually the some of method is of unit test and gets invoked  
automatically. And sorry, as i have put the wrong code. the code is  
something like this..  
require 'rubygems'  
require 'LoginPage'

gem 'test-unit'  
require 'test/unit'  
require 'BaseClass'

class LoginTest \< Test::Unit::TestCase  
&nbsp;&nbsp;&nbsp;&nbsp;extend BaseClassClass  
&nbsp;&nbsp;&nbsp;&nbsp;include LoginPage

&nbsp;&nbsp;&nbsp;&nbsp;def test\_third()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "third"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username\_textbox.set("gaurang")  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

I am not including the BaseClass rather I am extending it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [31 July 2011 15:10 UTC](https://rubytalk.org/t/nomethoderror-undefined-method/63211/4 "2011-07-31T15:10:16Z")

</div>

So, show the \*exact\* error you are getting, together with the backtrace.  
And point out the actual line of code where the exception is raised.

Also, try adding a 'puts "in setup"' into your setup method, to see if  
it is being called.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
