# Assertions in a Page Object?

**URL:** <https://club.ministryoftesting.com/t/assertions-in-a-page-object/12765>\
**Category:** Archive\
**Tags:** automation\
**Created:** [22 January 2018 16:07 UTC](https://club.ministryoftesting.com/t/assertions-in-a-page-object/12765 "2018-01-22T16:07:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burdettelamar](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/burdettelamar/32/2536_2.png) [@burdettelamar](https://club.ministryoftesting.com/u/burdettelamar)\
**Post date:** [22 January 2018 16:07 UTC](https://club.ministryoftesting.com/t/assertions-in-a-page-object/12765/1 "2018-01-22T16:07:02Z")

</div>

About assertions, the [Selenium wiki](https://github.com/SeleniumHQ/selenium/wiki/PageObjects) says this:

> [T]he tests, not the PageObjects, should be responsible for making assertions about the state of a page.

There’s a fine point here: the key concept is _responsible for_.

This means that only the test itself should _cause_ an assertion to be made. But it does not mean that the assertion code must _be in the test_.

For example, a test might want to verify that the user data displayed in the web page is as expected. The test can do the assertion itself, thus:

```auto
# Assume that expected_user is already defined.
# Get actual user from page.
actual_user = user_page.get_user
# Use method in User class to verify.
User.assert_equal(expected_user, actual_user)

```

If the verification code is refactored into the page object (where it also becomes reusable), the test code can be simpler:

```auto
user_page.assert_user_equal(expected_user)

```

without having to fetch and pass the actual user data.

Cleaner and DRYer, right?

Moreover, when the expected data is static (e.g., table headers), that data would normally be defined in the page object itself, not the test.

So a test might verify table headers with something like:

```auto
user_page.assert_table_headers_correct

```

without having to pass any data (expected or actual) to the page object.

Now that’s _really_ clean and DRY.

---

<div class="post-metadata">

**Author:** ![g33klady](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/g33klady/32/734_2.png) [@g33klady](https://club.ministryoftesting.com/u/g33klady)\
**Post date:** [22 January 2018 20:25 UTC](https://club.ministryoftesting.com/t/assertions-in-a-page-object/12765/2 "2018-01-22T20:25:18Z")

</div>

So the test framework (for the asserts) is in the Page Object itself or are you using other means to do those checks? How does this bubble up to test runners, like NUnit?

---

<div class="post-metadata">

**Author:** ![burdettelamar](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/burdettelamar/32/2536_2.png) [@burdettelamar](https://club.ministryoftesting.com/u/burdettelamar)\
**Post date:** [22 January 2018 21:21 UTC](https://club.ministryoftesting.com/t/assertions-in-a-page-object/12765/3 "2018-01-22T21:21:56Z")

</div>

The example web UI testing that’s in my RubyTest project targets GitHub’s own web UI.

In that example framework, the base assert methods (corresponding to NUnit assert methods) are in the [`Log`](https://github.com/BurdetteLamar/RubyTest/blob/master/lib/log/log.rb) class. These methods are usually called either by a data object (such as [`Label`](https://github.com/BurdetteLamar/RubyTest/blob/master/examples/github/data/label.rb)) or a page object (such as [`LabelsPage`](https://github.com/BurdetteLamar/RubyTest/blob/master/examples/github/ui/pages/labels_page.rb)).

The tests use Ruby gem [`minitest`](https://rubygems.org/gems/minitest/versions/5.8.4), and are launched by Rakefiles (Ruby ‘makefiles’).

In the examples, there’s a Tester Tour that includes an [example web UI test](https://github.com/BurdetteLamar/RubyTest/blob/master/examples/github/tester_tour/md_files/WebUi.md/#web-ui-test). The tour begins [here](https://github.com/BurdetteLamar/RubyTest/blob/master/examples/github/TesterTour.md#tester-tour).
