Friday, January 10, 2014

Using Capybara and RSpec assertions in Page Objects

On my Rails project, I am using RSpec and Capybara to write functional tests.  I did not want to specify any HTML elements in my Capybara feature files, since that makes the feature files brittle to HTML / CSS changes. It also violates the DRY principle and basic code hygiene.

So, I decided to refactor out my HTML centric Capybara code into separate Page Objects. If you are unfamiliar with Page Objects, then read the following:

The Page Object pattern for encapsulating HTML centric DSL is a common pattern followed while writing UI level functional tests in ThoughtWorks.

The problem I was facing when I refactored my code into Page objects was that I was unable to use the RSpec 'expect' syntax in Page Objects. Turns out all I had to do in my page objects was:

include RSpec::Matchers

Here is the full code from my project on Github.

The appropriate feature files and page objects in a GIST:


Unknown said...

You link your post to Martin Fowler's article where he strongly recommends not to use assertions inside page objects and the hole post is about using them.

Gurpreet said...

hmmm... I like the way my page object is designed.

Plus maybe you missed this point in Martin's post: "One form of assertions is fine even for people like me who generally favor a no-assertion style. These assertions are those that check the invariants of a page or the application at this point, rather than specific things that a test is probing".

Think about this one.. and then again look at my assertions.

The assertion methods like validate_capsule_data is a reusable method whose HTML elements I would like to keep inside the object. I don't want it replicated outside by everyone who wishes to validate the content of this page.