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.
Class Attribute Summary collapse
-
.page_object_routes ⇒ Object
Returns the value of attribute page_object_routes.
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#if_page(page_class, &block) ⇒ PageObject
(also: #if)
Create a page object if and only if the current page is the same page to be created.
-
#navigate_to(page_cls, how = {:using => :default}, &block) ⇒ PageObject
Navigate to a specific page following a predefined path.
-
#on_page(page_class, visit = false, &block) ⇒ PageObject
(also: #on)
Create a page object.
-
#visit_page(page_class, &block) ⇒ PageObject
(also: #visit)
attr_accessor :page.
Class Attribute Details
.page_object_routes ⇒ Object
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.
129 130 131 132 133 134 135 |
# File 'lib/druid/page_factory.rb', line 129 def (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
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(page_cls, how = {:using => :default}, &block) ⇒ PageObject
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.
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.
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.
41 42 43 |
# File 'lib/druid/page_factory.rb', line 41 def visit_page(page_class, &block) on_page page_class, true, &block end |