title: page object model: why not include assertions in page methods?
body: <p>First-time poster.  I've been working in UI automation for many years, but was only recently introduced to/instructed to work with the Page Object Model.  Most of it is common sense and includes techniques I've been using already, but there's a particular fine point which I haven't been able to justify in my own mind, despite searching extensively for a well-reasoned explanation.  I'm hoping someone here might enlighten me, as this question has caused some consternation as I try to integrate the POM with my own best practices.</p>
  
  <p>From <a href="http://code.google.com/p/selenium/wiki/PageObjects" rel="noreferrer">http://code.google.com/p/selenium/wiki/PageObjects</a>:</p>
  
  <blockquote>
    <p>The code presented above shows an important point: the tests, not the
    PageObjects, should be responsible for making assertions about the
    state of a page.... Of course, as with every guideline there are
    exceptions...</p>
  </blockquote>
  
  <p>From <a href="http://seleniumhq.org/docs/06_test_design_considerations.html#chapter06-reference" rel="noreferrer">http://seleniumhq.org/docs/06_test_design_considerations.html#chapter06-reference</a>: </p>
  
  <blockquote>
    <p>There is a lot of flexibility in how the page objects may be designed,
    but there are a few basic rules for getting the desired
    maintainability of your test code. Page objects themselves should
    never be make verifications or assertions. This is part of your test
    and should always be within the test’s code, never in an page object.
    The page object will contain the representation of the page, and the
    services the page provides via methods but no code related to what is
    being tested should be within the page object.</p>
    
    <p>There is one, single, verification which can, and should, be within
    the page object and that is to verify that the page, and possibly
    critical elements on the page, were loaded correctly. This
    verification should be done while instantiating the page object.</p>
  </blockquote>
  
  <p>Both of these "guidelines" allow for potential exceptions, but I couldn't disagree more with the basic premise.  I'm accustomed to doing a considerable amount of verification within "page methods", and I think the presence of verification there is a powerful technique for finding issues in a variety of contexts (i.e., verification occurs every time the method is called) rather than only occurring in the limited context of particular tests.</p>
  
  <p>For example, let's imagine that when you login to your AUT, some text appears that says  "logged in as USER".  It's appropriate to have a single test validate this specifically, but why wouldn't you want to verify it <em>every time</em> login is called?  This artifact is not directly related to whether the page "loaded correctly" or not, and it's not related to "what is being tested" in general, so according to the POM guidelines above, it clearly SHOULDN'T be in a page method... but it seems to me that it clearly SHOULD be there, to maximize the power of automation by verifying important artifacts as often as possible, with as little forethought as possible.  Putting verification code in page methods multiplies the power of automation by allowing you to get a lot of verification "for free", without having to worry about it in your tests, and such frequent verification in different contexts often finds issues which you would NOT find if the verification were limited to, say, a single test for that artifact.</p>
  
  <p>In other words, I tend to distinguish between test-specific verification and "general" verification, and I think it's perfectly appropriate/desirable for the latter to be included - extensively - in page methods.  This promotes thinner tests and thicker page objects, which generally <em>increases</em> test maintainability by reusing more code - despite the opposite contention in these guidelines.  Am I missing the point?  What's the real rationale for NOT wanting verification in page methods?  Is the situation I've described actually one of the 'exceptions' described in these guidelines, and therefore actually NOT inconsistent with the POM?  Thanks in advance for your thoughts. -jn-</p>