Skip to content

Commit

Permalink
added continue_navigation_to method to PageFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
cheezy committed Jan 4, 2012
1 parent bd7e01e commit 9c8cf6f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -3,6 +3,7 @@
* Added #button_elements to find all buttons on a page that match locators
* Added #navigation_method to Accessors to gahter method name to use with navigation
* Added #navigate_to method to PageFactory to navigate to a page through previous pages
* Added #continue_navigation_to method to PageFactory which begins at @current_page
* Added routes to PageFactory to collect routes through the site

=== Version 0.5.5 / 2011-12-27
Expand Down
34 changes: 31 additions & 3 deletions lib/page-object/page_factory.rb
Expand Up @@ -81,18 +81,46 @@ def on_page(page_class, visit=false, &block)
# @return [PageObject] the page you are navigating to
#
def navigate_to(page_cls, how = {:using => :default}, &block)
path = path_for how
navigate_through_pages(path[0..path.index(page_cls)-1])
on_page(page_cls, &block)
end

#
# Same as navigate_to except it will start at the @current_page
# instead the beginning of the path.
#
# @param [PageObject] a class that has included the PageObject
# module and which has the navigation_method defined
# @param [Hash] a hash that contains an element with the key
# :using. This will be used to lookup the route. It has a
# default value of :default.
# @param [block] an optional block to be called
# @return [PageObject] the page you are navigating to
#
def continue_navigation_to(page_cls, how = {:using => :default}, &block)
path = path_for how
navigate_through_pages(path[path.index(@current_page)+1..path.index(page_cls)-1])
on_page(page_cls, &block)
end

private

def path_for(how)
path = PageObject::PageFactory.page_object_routes[how[:using]]
fail("PageFactory route :#{how[:using].to_s} not found") unless path
path[0..path.index(page_cls)-1].each do |cls|
path
end

def navigate_through_pages(pages)
pages.each do |cls|
page = on_page(cls)
method = cls.page_object_navigation_method
fail("Navigation method not specified on #{cls}. Please call the ") unless page.respond_to? method
page.send method
end
on_page(page_cls, &block)
end


class << self
attr_accessor :page_object_routes

Expand Down
11 changes: 11 additions & 0 deletions spec/page-object/page_factory_spec.rb
Expand Up @@ -79,4 +79,15 @@ class TestWorld
fake_page.should_receive(:respond_to?).with(:a_method).and_return(false)
expect { @world.navigate_to(AnotherPage) }.to raise_error
end

it "should know how to continue routng from a location" do
PageObject::PageFactory.routes = {:default => [FactoryTestPage, AnotherPage, YetAnotherPage]}
fake_page = double('a_page')
FactoryTestPage.should_not_receive(:new)
AnotherPage.should_receive(:new).and_return(fake_page)
fake_page.should_receive(:respond_to?).with(:b_method).and_return(true)
fake_page.should_receive(:b_method)
@world.instance_variable_set :@current_page, FactoryTestPage
@world.continue_navigation_to(YetAnotherPage).class.should == YetAnotherPage
end
end

0 comments on commit 9c8cf6f

Please sign in to comment.