从PageObject类内部访问,访问和if方法

时间:2014-01-21 01:11:57

标签: ruby cucumber watir watir-webdriver page-object-gem

我设置了Cucumber / Watir / PageObject项目。我试图在实际页面对象内的step_definitions之外设置@current_page变量。无论我做什么,我都会收到错误

undefined method `on' for #<TestPage:0x45044d0> (NoMethodError)


test_page.rb

# coding: utf-8

## Test module
class TestPage < AnotherTestPage
  include PageObject

  div(:test_button, id: 'testbutton')

  #
  # Opens test page 2
  #
  # @param [Boolean] test_button defaults to false. If true, the Test button will be selected
  # @return [PageObject] the newly created Test2Page page object
  #
  def open_test2(test_button=false)
    test_button.click if test_button
    on(Test2Page)
  end

end

test_steps.rb

And(/^the Test2 screen is visible$/) do
  @current_page.open_test2
end

我已尝试includeextend同时PageObject::PageFactoryPageNavigation,但都没有效果。我还尝试将World(TestPage)World(TestPage.new)添加到TestPage文件的底部。这也行不通,似乎因为TestPage是一个类。

因此,我的问题是,如何在页面对象内部和步骤定义之外设置@current_page变量

1 个答案:

答案 0 :(得分:1)

要在页面对象中使用on方法,您需要添加PageObject::PageFactory

# Page that calls the on method
class MyPage
  include PageObject
  include PageObject::PageFactory

  def do_stuff
    on(MyPage2)
  end
end

# Page that is returned by the on method
class MyPage2
  include PageObject
end

# Script that calls the methods and shows that the on method works
browser = Watir::Browser.new
page = MyPage.new(browser)
current_page = page.do_stuff
p current_page.class
#=> MyPage2

但是,页面对象无法更改Cucumber步骤使用的@current_page。页面对象不知道Cucumber实例的@current_page变量。我想你必须手动分配页面:

And(/^the Test2 screen is visible$/) do
  @current_page = @current_page.open_test2
end

请注意,这假设open_test2正在返回页面对象,它当前正在返回(即on方法返回页面对象)。