Last active
December 30, 2015 09:39
-
-
Save dougvk/7810988 to your computer and use it in GitHub Desktop.
Page Object Delegator - an improvement on http://robots.thoughtbot.com/better-acceptance-tests-with-page-objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageObjectDelegator | |
include Capybara::DSL | |
include Rails.application.routes.url_helpers | |
def initialize(*args) | |
# Create a `path` => `page object model` hash | |
@pages = {} | |
@pages[foo_bar_path] = FooBarPage.new("Foo Bar") | |
@pages[baz_path] = BazPage.new("Baz") | |
end | |
# Send the named route you wish to check that you're on, | |
# e.g. @page_delegator.on_page :foo_bar. | |
def on_page?(page) | |
@pages[send("#{page}_path")].on_page? | |
end | |
def method_missing(method, *args) | |
# If method not found, delegate to corresponding page object model | |
# based on the current page. | |
@pages[page.current_path].send(method, *args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now, whenever you need to call your page object, you can use your page delegator to forward the call to the corresponding page object