Question

I have a Cucumber/Watir/PageObject project set up. I am trying to set the @current_page variable outside of the step_definitions inside of the actual page-object. No matter what I do, I get the error

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

I've tried includeing and extending both PageObject::PageFactory and PageNavigation, and neither worked. I also tried adding World(TestPage) and World(TestPage.new) to the bottom of the TestPage file. That also did not work, seemingly because TestPage is a class.

My question therefore is, how can I set the @current_page variable inside of my PageObject and outside of the step definitions

Was it helpful?

Solution

To use the on method in the page object, you need to include the 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

However, there is no way for the page object to change the @current_page that is used by the Cucumber steps. The page objects have no knowledge of the Cucumber instance's @current_page variable. I think you will have to manually assign the page:

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

Note that this assumes that open_test2 is returning a page object, which it currently does (ie the on method returns a page object).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top