Skip to content

Creating and using page objects

cheezy edited this page Jul 28, 2011 · 10 revisions

Alister Scott blogged about an idea for creating a factory to create instances of page objects. In the past I have blogged about page objects returning page objects. After a lot of contemplation, I am completely in the Alister camp. page-object now supports this approach as well.

PageFactory

A module named PageFactory provides this ability. This method has two methods.

def visit_page(page_class, &block)
def on_page(page_class, visit=false, &block)

Let's take a look at how you would use these method in your cucumber scripts.

Given /^I am on the registration page$/ do
  visit_page RegistrationPage
end

This call will cause the browser to open the page specified by the call to page_url in the class.

class RegistrationPage
  include PageObject

  page_url "http://mysite.com/registration"
  ...
end

If you wish to perform some activity on that page when you navigate to it you can pass a block to the method.

When /^I register on the registration page$/ do
  visit_page RegistrationPage do |page|
    page.register_user
  end
end

If you are already on a page and wish to interact with it you can use the on_page method.

Then /^I should be able to cancel my order$/ do
  on_page CheckoutPage do |page|
    page.cancel_order
  end
end