0

我设置了一个 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

我已经尝试过includeing 和extending 两者PageObject::PageFactoryand PageNavigation,但都没有奏效。我还尝试在 TestPage 文件的底部添加World(TestPage)和。World(TestPage.new)那也没用,貌似是因为TestPage是类。

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

4

1 回答 1

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

但是,页面对象无法更改@current_pageCucumber 步骤使用的页面对象。页面对象不知道 Cucumber 实例的@current_page变量。我认为您将不得不手动分配页面:

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

请注意,这假定它open_test2正在返回一个页面对象,它当前正在执行此操作(即该on方法返回一个页面对象)。

于 2014-01-21T13:54:40.133 回答