访问 pageobject 小部件时出现 ArgumentError

ArgumentError when accessing pageobject widget

class MainTabsWidget  < PageObject::Elements::UnorderedList
  include  PageObject
  include  PageObject::PageFactory

  link(:first_link, :text => 'First')
  link(:second_link, :text => 'Second')
  link(:third_link, :text => 'Third')
  link(:category_link, :text => 'Category')
  link(:subcat1_link, :text => 'Subcat1')
  link(:subcat2_link, :text => 'Subcat2')

  def goto_tab_item(tab_item)
    items = tab_item.split /\//
    items.each do |item|
      case item
        when 'First'
          first_link
        when 'Second'
          second_link
        when 'Third'
          third_link
        when 'Category'
          category_link
        when 'Subcat1'
          subcat1_link
        when 'Subcat2'
          subcat2_link
      end
    end
  end
end

PageObject.register_widget :main_menu, MainTabsWidget , :ul

我有一个名为 landing page 的页面,它按以下方式使用小部件....

class LandingPage
  include  PageObject
  include  PageObject::PageFactory


  main_menu(:menu_tabs, :id => 'mainMenu')


  def select_menu_item(item)
    menu_tabs_element.go_to_tab_item item
  end
end

这确实在某一时刻奏效了。如果为select_menu_item传递'First',它将点击到first_link指定的页面。如果您指定 'Category/Subcat1',它将单击类别 link,然后单击 subcat1。

然而,事情发生了变化。我们正在从 watir-webdriver gem 过渡到 watir (6.10) gem,现在代码获得参数 exeption

'ArgumentError: wrong number of arguments (0 for 1..2)'

当 menu_tabs_element 以任何方式被引用时,就会发生这种情况。我 运行 没有办法解决这个问题。

堆栈跟踪:

ArgumentError: wrong number of arguments (0 for 1..2)
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/element_locators.rb:10:in `element'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/elements/element.rb:191:in `respond_to_missing?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:53:in `respond_to_missing?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:70:in `respond_to?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:70:in `initialize'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1047:in `new'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1047:in `find_watir_element'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1118:in `block in define_widget_singular_accessor'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/widgets.rb:38:in `block (3 levels) in define_accessors'
./lib/pages/menu.rb:13:in `select_menu_item'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:134:in `block in navigate_through_pages'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:129:in `each'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:129:in `navigate_through_pages'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:67:in `navigate_to'
./spec/refactor/test_spec.rb:6:in `block (2 levels) in <top (required)>'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `instance_exec'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:509:in `block in run_owned_hooks_for'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `each'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `run_owned_hooks_for'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:460:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:537:in `block in run_before_context_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:186:in `block in isolate_for_context_hook'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:182:in `instance_exec'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:182:in `isolate_for_context_hook'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:536:in `run_before_context_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:589:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `map'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1896:in `with_suite_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/reporter.rb:79:in `report'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:112:in `run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:87:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
C:/Ruby22/bin/rspec:23:in `load'
C:/Ruby22/bin/rspec:23:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'

1 example, 1 failure, 0 passed

Finished in 6.087 seconds

Process finished with exit code 1

我不清楚 Watir 中的哪个更改会导致此问题。似乎更可能是页面对象的变化。无论如何,这里有 2 个解决方案。请注意,这是使用 Page-Object v 2.2.4 测试的。

使用小部件

正如 Alexis in Page-Object Issue 263 所指出的,在小部件中包含页面对象访问器方法并不像人们预期的那样简单。在 MainTabsWidget class 中,而不是 include PageObject,你需要做:

extend PageObject::Accessors  # to get the accessor methods
attr_reader :platform         # the accessor methods don't work without a platform

进行此更改后,您的页面对象应该可以正常工作。

使用页面部分

Page-Object 具有可能更适合您需要的页面部分功能。它简化了一些代码(即解决了上述问题)。

您定义了一个 class 来表示您的主要选项卡部分。请注意,您不需要像使用 Widgets 一样注册该部分或进行多行设置。

class MainTabs
  include PageObject
  include PageObject::PageFactory

  link(:first_link, :text => 'First')
  link(:second_link, :text => 'Second')
  link(:third_link, :text => 'Third')
  link(:category_link, :text => 'Category')
  link(:subcat1_link, :text => 'Subcat1')
  link(:subcat2_link, :text => 'Subcat2')

  def select_menu_item(tab_item)
    items = tab_item.split /\//
    items.each do |item|
      case item
        when 'First'
          first_link
        when 'Second'
          second_link
        when 'Third'
          third_link
        when 'Category'
          category_link
        when 'Subcat1'
          subcat1_link
        when 'Subcat2'
          subcat2_link
      end
    end
  end
end

页面对象可以简单地变成:

class LandingPage
  include PageObject
  include PageObject::PageFactory

  page_section(:menu_tabs, MainTabs, :id => 'mainMenu')
end

点击菜单会变成:

page.menu_tabs.select_menu_item('First')

(或者您可以继续在 LandingPage 中定义一个 #select_menu_item 方法以避免下游更改)