Module: Druid::PageFactory

Defined in:
lib/druid/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:

You must also call the navigation_method on each page.

Examples:

Making the PageFactory available to your step definitions

World Druid::PageFactory

Visiting a page for the first time in a Scenario

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

using a page that has already been visited in a Scenario

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

Example routes defined in env.rb

Druid::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.



158
159
160
# File 'lib/druid/page_factory.rb', line 158

def page_object_routes
  @page_object_routes
end

Class Method Details

.routes=(routes) ⇒ Object



160
161
162
163
# File 'lib/druid/page_factory.rb', line 160

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) ⇒ Druid

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 (Druid)

    class that has included the Druid

  • a (Hash)

    hash that contains an element with the key

  • an (block)

    optional block to be called

Returns:

  • (Druid)

    the page you are navigating to



129
130
131
132
133
134
135
# File 'lib/druid/page_factory.rb', line 129

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

#if_page(page_class, &block) ⇒ PageObject Also known as: if

Create a page object if and only if the current page is the same page to be created

Examples:

original:
 on_page(NewProduct).save if @current_page.class == NewProduct
 on_page(EditProduct).save if @current_page.class == EditProduct
new:
 if_page NewProduct do |page|
    page.save
 end
 if_page EditProduct do |page|
    page.update 
 end

Parameters:

  • a (PageObject)

    class that has included the Druid module

  • an (block)

    optional block to be called

Returns:

  • (PageObject)

    the newly created page object



81
82
83
84
# File 'lib/druid/page_factory.rb', line 81

def if_page(page_class, &block)
  return @current_page unless @current_page.class == page_class
  on_page(page_class, false, &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:

  • (PageObject)

    the page you are navigating to



111
112
113
114
115
116
# File 'lib/druid/page_factory.rb', line 111

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 (page_class)

    class that has included the Druid module

  • should (Bool)

    the page be visited? default is false.

  • an

    optional block to be called

Returns:

  • (PageObject)

    the newly created page object



55
56
57
58
59
# File 'lib/druid/page_factory.rb', line 55

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

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

attr_accessor :page

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 (page_class)

    class that has included the Druid module

  • an

    optional block to be called

Returns:

  • (PageObject)

    the newly created page object



41
42
43
# File 'lib/druid/page_factory.rb', line 41

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