Newb question. Undefined Method error

So I’m trying to run this script


#!/usr/bin/env ruby

#sample ruby script using the selenium client

require “rubygems”
gem “selenium-webdriver”, “>=2.17.0”
require “selenium/webdriver”
begin
@browser = Selenium::Client::Driver.new
:host => “localhost”,
:port => 4444,
:browser => “*firefox”,
:url => “http://www.google.com”,
:timeout_in_second => 60
@browser.start_new_browser_session
@browser.open “/”
@browser.type “q”, “Selenium seleniumhq.org
@browser.click “btnG”, :wait_for => :page
puts @browser.text? (“seleniumhq.org”)
ensure
@browser.close_current_browser_session
end

And I get this response when I run it.

seleniumtest_script.rb:24:in ensure in <main>': undefined method close_current_browser_session’ for nil:NilClass (NoMethodError)
from seleniumtest_script.rb:24:in `’

So my question is, what am I missing?
Thanks in advance.

On Sat, Jan 21, 2012 at 19:17, Daniel K. [email protected] wrote:

So I’m trying to run this script
[snip]
And I get this response when I run it.

seleniumtest_script.rb:24:in ensure in <main>': undefined method close_current_browser_session’ for nil:NilClass (NoMethodError)
from seleniumtest_script.rb:24:in `’

So my question is, what am I missing?

Assuming that what you’ve posted is indeed seleniumtest_script.rb, and
that line 24 is indeed the one where you do
@browser.close_current_browser_session” (even though you have fewer
than 24 lines of code there!), @browser is nil. Any time you see
“undefined method `some_method’ for nil:NilClass” that means that
you’ve tryied to call some_method on an object that’s actually nil.

Now the question is, why is it nil? Most likely your call to
@browser = Selenium::Client::Driver.new” (with the args on further
lines) is failing.

So now the question is, why is that failing? That is left as an
exercise for the reader. :wink:

-Dave