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:
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) ⇒ PageObject
Same as navigate_to except it will start at the @current_page instead the beginning of the path.
-
#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
Create a page object.
-
#visit_page(page_class, &block) ⇒ PageObject
Create and navigate to a page object.
Class Attribute Details
.page_object_routes ⇒ Object
Returns the value of attribute page_object_routes.
130 131 132 |
# File 'lib/page-object/page_factory.rb', line 130 def page_object_routes @page_object_routes end |
Class Method Details
.routes=(routes) ⇒ Object
132 133 134 135 |
# File 'lib/page-object/page_factory.rb', line 132 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.
101 102 103 104 105 106 107 |
# File 'lib/page-object/page_factory.rb', line 101 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 |
#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.
82 83 84 85 86 87 |
# File 'lib/page-object/page_factory.rb', line 82 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
Create a page object.
54 55 56 57 58 |
# File 'lib/page-object/page_factory.rb', line 54 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
Create and navigate to a page object. The navigation will only work if the ‘page_url’ method was call on the page object.
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 |