Kelp
Kelp is a collection of helpers that makes it easier to write step definitions for Cucumber. The Kelp gem is hosted on Rubygems, so you can install it with:
$ gem install kelp
The name "Kelp" is a contraction of "Cuke Helpers". It was chosen because it's short, easy to remember, and is in keeping with the theme of greenish plants. Kelp is licensed under the MIT License.
Documentation is
available on rdoc.info.
Please use the issue tracker
to report any bugs or feature requests. Visit the #kelp
channel on
irc.freenode.net
to chat.
Motivation
Kelp was developed as an attempt to get away from the bad habit of nesting steps inside one another. While Cucumber does allow this behavior, it is frowned upon by much of the Cucumber community. It was also partly motivated by a desire to move away from imperative steps, toward a more declarative style. Some of Capybara's methods were perceived as being too low-level, especially when it comes to filling in or verifying several different fields in a form, or checking the presence or absence of multiple strings or regular expressions.
As of version 1.1.0, cucumber-rails
no longer provides
a generator for web_steps.rb
; since Kelp does provide such a generator, it
may serve the niche market of cucumber-rails users who preferred to use the
imperative step definitions that it provided. Kelp's intent is not to encourage
this behavior; nearly all of the imperative steps it provides are one-liners,
which in many cases are more succinct and readable than the step definition
that wraps them.
It is this developer's opinion that imperative web_steps.rb
-style steps can
be the easiest route to a working Cucumber scenario, but that they can also
be a fast track to unmaintainability. Consider them a starting point; the baby
steps you might take when writing your first few scenarios, before you discover
the higher-level declarative steps that need to be reusably encapsulated.
Usage
To use Kelp's helpers in your Cucumber step definitions, simply require
the
helper module you're interested in:
require 'kelp/visibility'
Then add the relevant modules to Cucumber's World
:
World(Kelp::Visibility)
Or, to include all available helpers:
require 'kelp/attribute'
require 'kelp/checkbox'
require 'kelp/dropdown'
require 'kelp/field'
require 'kelp/navigation'
require 'kelp/scoping'
require 'kelp/visibility'
World(Kelp::Attribute)
World(Kelp::Checkbox)
World(Kelp::Dropdown)
World(Kelp::Field)
World(Kelp::Navigation)
World(Kelp::Scoping)
World(Kelp::Visibility)
Many of the provided helpers are designed to make it easier to do things you might otherwise be tempted to do with nested step definitions. For example, if you need to verify the presence of several text strings on a webpage, you might have a step definition like this:
Then /^I should see the login page$/ do
Then %{I should see "Welcome"}
And %{I should see "Thanks for visiting"}
And %{I should see "Login"}
end
Using the provided helper method should_see
, you can do this instead:
Then /^I should see the login page$/ do
should_see "Welcome"
should_see "Thanks for visiting"
should_see "Login"
end
Or even this:
Then /^I should see the login page$/ do
should_see [
"Welcome",
"Thanks for visiting",
"Login"
]
end
Following links, filling in fields, and pressing buttons can all be easily done with Ruby code instead of nested steps. Thus this:
When %{I follow "Login"}
And %{I fill in "Username" with "skroob"}
And %{I fill in "Password" with "12345"}
And %{I press "Log me in"}
translates to this:
follow "Login"
fill_in_fields \
"Username" => "skroob",
"Password" => "12345"
press "Log me in"
Several methods also accept keywords to define the scope of an action. For instance, if you want to look within an element with id="greeting"`, do:
should_see "Welcome", :within => "#greeting"
At the moment, the :within
keyword is the only accepted scope; the locator
you pass to this should be in whatever format your Capybara.default_selector
is set to. Other keywords like :before
or :after
may be supported in future
revisions.
See the kelp documentation. for more information.
Rails generator
Kelp provides a generator for Rails projects, which writes step definitions
to features/step_definitions/web_steps.rb
. This file provides all of the same
step definitions formerly provided by
cucumber-rails, with several
enhancements. If you have made customizations to your web_steps.rb
, they will
be overwritten! Consider yourself warned.
To generate web_steps.rb
, run this for Rails 2.x:
$ script/generate kelp
Or this for Rails 3.x:
$ rails generate kelp:steps
Note: If you are upgrading from a version of Kelp prior to 0.1.9, you should
remove kelp_steps.rb
from your features/step_definitions
directory; these
are obsolete, and conflict with the newer step definitions in web_steps.rb
.
Development
If you'd like to hack on Kelp, first fork the repository, then clone your fork:
$ git clone git://github.com/your_username/kelp.git
Install bundler:
$ gem install bundler
Then install Kelp's dependencies:
$ cd /path/to/kelp
$ bundle install
It's a good idea to use RVM with a new gemset to keep things tidy.
If you make changes that you'd like to share, push them into your Kelp fork, then submit a pull request.
Testing
Kelp comes with a Rakefile
, so you can run the RSpec tests and generate an
rcov coverage report via:
$ rake spec
This will write an HTML report to coverage/index.html
. Finally, there are
some Cucumber self-tests included, mainly for testing the web_steps.rb
file used by the Rails generator. Run via:
$ rake cucumber
This self-test includes high-level scenarios that exercise more detailed
scenarios in examples/sinatra_app/features
, ensuring that the generated step
definitions perform as expected.
Future plans
- Support Webrat
Copyright
The MIT License
Copyright (c) 2010 Eric Pierce
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.