Should assertions conditions be done in the page Objects, or in the Test classes?

Probably the best argument against adding assertions to the page object class implementation I have seen so far @martingijsen . Good find. I do find it tempting to have ways of adding assertions to the “one convenient object” though, because often you want to assert if a page is in a certain state or a certain field was populated…because that field ties in the the business logic. and that’s probably the clever point to bring in a business logic class to add that kind of thing to the page object as a subclass. Requires discipline however, which I’ve not seen. The same problem exists when people want to “chain” pages together, chaining is not really the job of the page object, because chaining creates relationships between pages which live at a workflow and error handling level in the web app, not on the page. In back end code navigation lives i na completely separate router module usually, so why do we often insist on baking navigation into our test code at the page-object level? So for me, adding chaining to a page-object breaks the SOLID principle just like adding Assertions does.

3 Likes

The same problem exists when people want to “chain” pages together,

Codewise, it does become more verbose without method chaining. And when the automation grows to a point where you struggle to remember which Page Objects and methods already exist in the code, that also becomes a problem.

2 Likes

At one point this falls into the age old test discussion, if I write a entirely blank test case with no steps, should it fail with a “nothing to test” failure cause? And for me reducing cognitive load trumps, so any way we can use the test code language to help, it must be used to advantage and reduce clutter. I would love to force assertions to be more visible. Chaining creates an assertion in assuming that the new page loads, but it does not really verify that the last page was fully re-populated for example.

I’m using python decorators to that end, and similar syntactic sugar helpers exist in other languages, I encourage designers to use them. But to be honest splitting business logic out of the page object is hard, as is making assertions still stand out and not be forgotten about without relying on whitespace and other visual tricks.