Question

I'm really confused about this. I'm using the Ruby class below in an automated test suite:

class FlightSearchPage

  attr_accessor :page_title

  def initialize(browser, page)
    @browser      = browser
    @start_url    = page
  end

  def method_missing(sym, *args, &block)
    @browser.send sym, *args, &block
  end

  @page_title   = @browser.title

  #def page_title
  #  @browser.title
  #end

end

I'm subclassing it with:

class BrandFlightsPage < FlightSearchPage


    def initialize(browser, page, brand)
      super(browser, page)
      [...snip...]
    end
end

When I instantiate the subclass, passing in @browser, a watir-webdriver object, @browser.title throws:

undefined method `title' for nil:NilClass (NoMethodError)

But if I uncomment the page_title method in FlightSearchPage, it works as expected. I've also tried it with the variable assignment inside the constructor.

I've gone around in circles trying to track this down, but I just can't see it.

Here's the stacktrace:

undefined method `title' for nil:NilClass (NoMethodError)
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/flight_search_page.rb:13:in `<class:FlightSearchPage>'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/flight_search_page.rb:1:in `<top (required)>'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/brand_flight_search_page.rb:1:in `<top (required)>'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/features/step_definitions/steps.rb:1:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load_code_file'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:171:in `load_file'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:175:in `load_step_definitions'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:40:in `run!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:43:in `execute!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:20:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/bin/cucumber:14:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p0/bin/cucumber:19:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p0/bin/cucumber:19:in `<main>'
Was it helpful?

Solution

This is because

 attr_accessor :page_title

already creates a getter and a setter.

i would put this

@page_title   = @browser.title

inside your initialize method..

class FlightSearchPage

  attr_accessor :page_title

  def initialize(browser, page)
    @browser      = browser
    @page_title   = @browser.title
    @start_url    = page
  end

  def method_missing(sym, *args, &block)
    @browser.send sym, *args, &block
  end

end

or (better) remove the attr_accessor and just add a method

   class FlightSearchPage

      def initialize(browser, page)
        @browser      = browser
        @start_url    = page
      end

      def method_missing(sym, *args, &block)
        @browser.send sym, *args, &block
      end

      def page_title
        return @browser.title unless @browser
        "browser not set, no title"
      end

    end

Instantiating the browser:

This is placed in a file called env.rb which is run before each test:

Before do
  @browser = Watir::Browser.new
end

It's run in the context of the current instance of cucumber's World

What I don't understand is that all other browser methods are working as expected when implemented with accessors except this one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top