Capybara with Given/When/Then steps in acceptance testing

Capybara with Given/When/Then steps in acceptance testing

Capybara with Given/When/Then steps in acceptance testing.Many of us use cucumber to write acceptance tests. We must to say it’s very nice tool that allows you to write features in English plain text. The main goal is provide interface that should be clear for Customer(non-programmer) and Developer.

Cucumber example:

It’s very readable. But cucumber has one big downside:All steps are global and a instance variable from one step is accessible in another step. In big project it can easy lead to mess. Also you can read some rants about cucumber: Previously we also use cucumber as tool for user stories. Lately we found that our customers NEVER read cucumber features. So do we need it anymore? Let’s check alternatives.As you probably know, cucumber use internally Capybara for steps implementation. And there is even more – latest version of Capybara comes with a built in DSL for creating descriptive acceptance tests!So we decided to switch to pure rspec+capybara testing. We moved our features to spec/acceptance directory. Let’s look at rspec+capybara example.

RSpec+Capybara example

Thus approach has one big benefit:You don’t need anymore to jump between feature and step files to guess where is implementation for a step. It’s nice but there are also two downsides.First downsides is that RSpec documentation output will be too poor and it’s not enough informative:Second downside is that scenario does not have anymore exact boundary blocks or steps. It makes harder to understand the scenario flow.Real user story often has big scenarios and we realized that cucumber steps were cool.There were two ways:
  • switch back to cucumber
  • go further and invent
Railswarians never go back :)

Our Solution

If you decided to switch from Cucumber to Capybara acceptance testing try rspec-example steps gem.With this gem you may use Given/When/Then steps into rspec example!

RSpec+Capybara+Steps example

And documentation output will be:Now it’s much better. Isn’t it? :)

RSpec Example Steps features

  • Does not hack RSpec Example. It only extends it.
  • Brings Given/When/Then/And/But steps support into rspec example block that allow you to better organize scenario
  • Adds shared_steps DSL
  • Adds ability to mark step as pending
  • Reports with color about passed/pending/failed step into documentation formating output



See README for more details.Just add to spec/spec_helper.rbAnd enjoy Given/When/Then step usage!