0

我真的很困惑。我在自动化测试套件中使用下面的 Ruby 类:

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

我将其子类化为:

class BrandFlightsPage < FlightSearchPage


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

当我实例化子类时,传入 @browser,一个 watir-webdriver 对象,@browser.title 抛出:

nil:NilClass (NoMethodError) 的未定义方法“标题”

但是,如果我取消注释 FlightSearchPage 中的 page_title 方法,它会按预期工作。我也尝试过构造函数中的变量赋值。

我绕着圈子试图追踪它,但我就是看不到它。

这是堆栈跟踪:

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>'
4

1 回答 1

0

这是因为

 attr_accessor :page_title

已经创建了一个 getter一个 setter。

我会把这个

@page_title   = @browser.title

在您的初始化方法中..

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

或(更好)删除attr_accessor并添加一个方法

   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

实例化浏览器:

这被放置在一个名为 env.rb 的文件中,该文件在每次测试之前运行:

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

它在黄瓜世界的当前实例的上下文中运行

我不明白的是,当使用除此之外的访问器实现时,所有其他浏览器方法都按预期工作。

于 2012-08-21T20:47:18.380 回答