Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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 particularly 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.

From http://code.google.com/p/selenium/wiki/PageObjects:

The code presented above shows an important point: the tests, not the PageObjects, should be responsible for making assertions about the state of making assertions about the state of a page ....Of course, as with every guideline there are exceptions...

From http://seleniumhq.org/docs/06_test_design_considerations.html#chapter06-reference:

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.

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.

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.

For example, let's imagine that when you log in 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 every time 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.

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 - on-page methods. This promotes thinner tests and thicker page objects, which generally increases 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.

1 Answer

0 votes
by (62.9k points)

This perplexes me when I see the same assertion could be used across multiple test methods. For example, writing assertion specific method -

public PaymentPage verifyOrderAmount(BigDecimal orderAmount) {      

   Assertion.assertEquals(lblOrderAmount.getText(), orderAmount, "Order Amount is wrong on Payment details page"); 

   return this; 

}

Now I can reuse it in all the tests I need. Instead of repeating the same assertion statement in multiple tests dealing with multiple scenarios. Needless to say, I can chain multiple assertions in a method depending on test i.e -

.verifyOrderAmount(itemPrice)

.verifyBankAmount(discountedItemPrice)

 .verifyCouponCode(flatDiscountCode.getCouponCode()) 

When page object is supposed to represent the services offered by page then, is not assertion point also a service provided by Page.

Browse Categories

...