In this post I will explain how to use rspec expectations in cucumber project that uses page object gem.

In order to be able to use rspec expectations in cucumber project, you first have to include rspec gem in your Gemfile. After that, you have to put in


following line

require ‘rspec/expectations’

You are good to go.

But where I should use rspec matchers, in page class or in step definitions? If you use rspec matcher in page class definition, you will get following exception:

You have to move rspec matchers in step definitions, something like this:


When you think about it, this is logical decision. Page object class should represent page that you interact with with methods that interact with page elements. It should not contain checks of those elements. Step definition contain Then word, so their purpose is to check page elements. For me, that was not obvious so I hope that this post would save you some time.