Martin Fowler favors having no assertions in page objects however is not definitive on the subject:
Advocates of including assertions in page objects say that this helps avoid duplication of assertions in test scripts, makes it easier to provide better error messages, and supports a more TellDontAsk style API.
Advocates of assertion-free page objects say that including assertions mixes the responsibilities of providing access to page data with assertion logic, and leads to a bloated page object.
An assertion library DSL depends on the domain knowledge of the application under test (aka business domain) but the question is whether you attach the TellDontAsk API to the Page Objects or to the test data models.
Example in Protractor to favor no assertions in page objects
But rather in a User
test data model
// `julie` could eventually be loaded from a csv or JSON test data file but
// ultimately you end up creating a new instance of a User class or type
var julie = new User({userName: 'julie', fullName: 'Julie Ralph', ...});
julie.login();
User.prototype.login = function(user) {
var loginPage = new LoginPage(); // you could go with singletons too
loginPage.get();
// below could be within loginPage.get to avoid duplication
expect(loginPage).toBeLoaded();
loginPage.setUserName(user.userName);
// below assertion could be within loginPage.setUserName
expect(loginPage.getUserName()).toEqual(user.userName);
// and so on...
};
Example in Protractor that favors assertions within page objects
var loginPage = new LoginPage();
loginPage.login(julie);
LoginPage.prototype.login = function(user) {
var loginPage = this;
loginPage.get();
expect(loginPage).toBeLoaded();
loginPage.setUserName(user.userName);
expect(loginPage.getUserName()).toEqual(user.userName);
// and so on...
};
Either way you can still alias or delegate one from the other, i.e.
User.prototype.login =
LoginPage.prototype.login.bind(LoginPage.prototype);
Then the test engineers get to choose any of these 2 DSLs:
julie.login();
loginPage.login(julie);
Would like to know experiences of marking this decision and if you have strong positions on this subject.