appfolio / ae_page_objects

Page Objects for Capybara
MIT License
28 stars 9 forks source link

Routing incorrectly matches documents #63

Closed dtognazzini closed 9 years ago

dtognazzini commented 10 years ago

Consider the following code:

def save!
  node.find("input[type=submit]").click
  window.change_to(Books::ShowPage, Books::NewPage)
end

window.change_to needs to load one of the documents above instead of using an on-demand load with a document proxy to ensure that the window.change_to call fails if the window has a document not in the expected set. If this check is deferred until later, the calling code may never access the returned reference and thus the check would never be made.

However, Rails routes is a precedence-order matching list and the first thing that matches, wins. So, in the above case, if the url is /books/new and we try to load a Books::ShowPage, it'll work because the show route will match the URL and use "new" as the :id parameter for the show named route.

So we have 2 options:

  1. Keep the current load-now behavior and figure out how to match better such that loading ShowPage will fail for /books/new. This would involve running through the normal Rails route matching and somehow comparing the resulting match with whatever would match the ShowPage. A first try might be to run the route matching with the path defined for ShowPage. Unfortunately, generating the path is difficult, 'cause the ShowPage route has a required parameter which would have to be faked in order to get Rails' routes matching to work; this would have to be done for any arbitrary route.

    Another idea, is to run all of the expected document types through the route matching and from the matching ones select whichever has the highest precedence. This would require changes to the Router and/or AePageObjects to support the idea of precedence. This support is a coupling between AePageObjects and the underlying router, in this case Rails'. Of course, the document loading could be further generalized to support this concept.

  2. Keep the current load-now behavior to get the immediate failure when there are no matches, but run the route matching later again from the DocumentProxy when the page is accessed. This works great for explicit casts because it doesn't care about the previous loading that happened with the window.change_to call. Implicit casts works as well, as with implicit casts the contract is that the first specified expected page is used for implicit casts.

    The downside of this method is that it only works when you call window.change_to with multiple pages. It doesn't work with single-arg window.change_to calls:

    new_page  = PageObjects::Books::NewPage.visit
    show_page = new_page.window.change_to(PageObjects::Books::ShowPage)

    This code does not raise an error because Books::ShowPage can recognize Books::NewPage's routes.

Then again, it's not really clear how either solution can handle the single-arg case.

dtognazzini commented 10 years ago

Released in 1.2.0

dtognazzini commented 10 years ago

This fix doesn't support handling mounted Rails engines. Reopening.

ipmsteven commented 10 years ago

@dtognazzini can we add engine test case in page_object_integration_test after revert #73

dtognazzini commented 10 years ago

Yes, we should.