Module: PageObject::PageFactory

Defined in:
lib/page-object/page_factory.rb

Overview

Module to facilitate to creating of page objects in step definitions. You can make the methods below available to all of your step definitions by adding this module to World. This idea was first discussed in Alister Scott’s blog entry watirmelon.com/2011/06/07/removing-local-page-references-from-cucumber-steps/.

If you plan to use the navigate_to method you will need to ensure you setup the possible routes ahead of time. You must always have a default route in order for this to work. Here is an example of how you define routes:

Examples:

Making the PageFactory available to your step definitions

World PageObject::PageFactory

Visiting a page for the first time in a Scenario

visit_page MyPageObject do |page|
  page.name = 'Cheezy'
end

using a page that has already been visited in a Scenario

on_page MyPageObject do |page|
  page.name.should == 'Cheezy'
end

Example routes defined in env.rb

PageObject::PageFactory.routes = {
  :default => [[PageOne,:method1], [PageTwoA,:method2], [PageThree,:method3]],
  :another_route => [[PageOne,:method1], [PageTwoB,:method2b], [PageThree,:method3]]
}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.page_object_routesObject

Returns the value of attribute page_object_routes.



136
137
138
# File 'lib/page-object/page_factory.rb', line 136

def page_object_routes
  @page_object_routes
end

Class Method Details

.routes=(routes) ⇒ Object



138
139
140
141
# File 'lib/page-object/page_factory.rb', line 138

def routes=(routes)
  raise("You must provide a :default route for PageFactory routes") unless routes[:default]
  @page_object_routes = routes
end

Instance Method Details

#continue_navigation_to(page_cls, how = {:using => :default}, &block) ⇒ PageObject

Same as navigate_to except it will start at the @current_page instead the beginning of the path.

module and which has the navigation_method defined :using. This will be used to lookup the route. It has a default value of :default.

Parameters:

  • a (PageObject)

    class that has included the PageObject

  • a (Hash)

    hash that contains an element with the key

  • an (block)

    optional block to be called

Returns:



107
108
109
110
111
112
113
# File 'lib/page-object/page_factory.rb', line 107

def continue_navigation_to(page_cls, how = {:using => :default}, &block)
  path = path_for how
  from_index = find_index_for(path, @current_page.class)+1
  to_index = find_index_for(path, page_cls)-1
  navigate_through_pages(path[from_index..to_index])
  on_page(page_cls, &block)
end

Navigate to a specific page following a predefined path.

This method requires a lot of setup. See the documentation for this class. Once the setup is complete you can navigate to a page traversing through all other pages along the way. It will call the method you specified in the routes for each page as it navigates. Using the example setup defined in the documentation above you can call the method two ways:

module and which has the navigation_method defined :using. This will be used to lookup the route. It has a default value of :default.

Examples:

page.navigate_to(PageThree)  # will use the default path
page.navigate_to(PageThree, :using => :another_route)

Parameters:

  • a (PageObject)

    class that has included the PageObject

  • a (Hash)

    hash that contains an element with the key

  • an (block)

    optional block to be called

Returns:



88
89
90
91
92
93
# File 'lib/page-object/page_factory.rb', line 88

def navigate_to(page_cls, how = {:using => :default}, &block)
  path = path_for how
  to_index = find_index_for(path, page_cls)-1
  navigate_through_pages(path[0..to_index])
  on_page(page_cls, &block)
end

#on_page(page_class, visit = false, &block) ⇒ PageObject Also known as: on

Create a page object.

Parameters:

  • a (PageObject)

    class that has included the PageObject module

  • a (Boolean)

    boolean indicating if the page should be visited? default is false.

  • an (block)

    optional block to be called

Returns:



57
58
59
60
61
# File 'lib/page-object/page_factory.rb', line 57

def on_page(page_class, visit=false, &block)
  @current_page = page_class.new(@browser, visit)
  block.call @current_page if block
  @current_page
end

#visit_page(page_class, &block) ⇒ PageObject Also known as: visit

Create and navigate to a page object. The navigation will only work if the ‘page_url’ method was call on the page object.

Parameters:

  • a (PageObject)

    class that has included the PageObject module

  • an

    optional block to be called

Returns:



42
43
44
# File 'lib/page-object/page_factory.rb', line 42

def visit_page(page_class, &block)
  on_page page_class, true, &block
end