访问pageobject小部件时的ArgumentError

时间:2017-12-05 21:30:03

标签: ruby automated-tests watir pageobjects page-object-gem

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

我有一个名为登录页面的页面,它以下列方式使用窗口小部件....

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',它将单击类别链接,然后单击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

1 个答案:

答案 0 :(得分:1)

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

使用小工具

正如Alexis in Page-Object Issue 263所指出的,包括小部件中的页面对象访问器方法并不像人们期望的那样直截了当。在MainTabsWidget课程中,您需要执行以下操作:

。而不是include PageObject
extend PageObject::Accessors  # to get the accessor methods
attr_reader :platform         # the accessor methods don't work without a platform

通过此更改,您的页面对象应该可以正常工作。

使用页面部分

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

您可以定义一个类来表示主要标签部分。请注意,您不需要像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')

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