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).