1. BDD(ROR) using
Cucumber/Rspec
:acceptance=> :capybara
By: Bindesh Vijayan
2. Cucumber
● Cucumber is a tool that executes
plain-text functional descriptions
as automated tests.
● While Cucumber can be thought of as
a “testing” tool, the intent of the
tool is to support BDD
● This means that the “tests” (plain
text feature descriptions with
scenarios) are typically written
before anything else and verified by
business analysts, domain experts,
etc. non technical stakeholders.
3. 4 steps in defining a
feature
● Cucumber begins by defining a
“Feature” that we are trying to
implement
● After that we specify what we want to
do using “In Order”
● The we specify a user who will be
interacting with that feature using
“As a”
● And then we finally specify what do
we expect using “I want”
5. Defining the scenario
for login
● After the feature is specified
you need to specify “Scenario”
for Cucumber
● Scenario consists of the
following clauses
Given
When
Then
6. Lets extend our
user.feature
Scenario: Logging into the
system
Given when i visit the login page
And I enter a valid username and
password
When I press login button
Then I should see a success message
7. User.feature in a nutshell
Feature: User login
In order to login
As a User
I want to see a login page with username and
password
Scenario: Logging into the system
Given when i visit the login page
And I enter a valid username and password
When I press login button
Then I should see a success message
8. Cucumber on terminal
● Installing
gem install cucumber
● Cucumber help
cucumber –help
● Running cucumber tests
cucumber features/user.feature
9. Specifying steps
● Once you run cucumber it will report the
missing steps with examples of how you
can write them
● Create a new folder name step_definitions
and add a new file called
<object>_steps.rb
● Copy paste the code from the output
● Re run, the Feature would pass but the
scenario would fail
10. Cucumber steps
● Cucumber internally uses
capybara
● You can use any other acceptance
test framework e.g. Webrat
● For this session we will stick
to capybara
11. Capybara DSL to simulate
user steps
● Navigating
visit('/projects')
visit(post_comments_path(post))
● Clicking links and buttons
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click_on('Link Text') # clicks on
either links or buttons
click_on('Button Value')
12. Interacting with forms
fill_in('First Name', :with => 'John')
fill_in('Password', :with => 'Seekrit')
fill_in('Description', :with => 'Really
Long Text...')
choose('A Radio Button')
check('A Checkbox')
uncheck('A Checkbox')
attach_file('Image', '/path/to/image.jpg')
select('Option', :from => 'Select Box')
14. Scoping
● Capybara makes it possible to
restrict certain actions, such as
interacting with forms or clicking
links and buttons, to within a
specific area of the page
● You can use within DSL to specify
a scope
e.g. within(“#form-group”)
● You can also further restrict it
to a table
e.g. within_table(“customer-table”)
15. A note about Capybara
drivers
● Drivers are the internal selectors, if
you may want to call it, used by
Capybara
● By default capybara uses rack_test,
which is faster than others
● The limitation it doesn't support
Javascript and can't access external
HTTP resources like remote APIs or Oauth
services
● You can change the default driver by
specifying this line in your helper
Capybara.default_driver = :selenium
16. Rspec and Capybara
● Instead of using Cucumber you can use
Rspec and Capybara
● We use the same basic principle that we
used with Rspec
● Only difference is that you now use
capybara dsl insidel your rspec “it” blocks
17. Configuring rspec with capybara
#Gem file
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end
#spec_helper
require 'rspec/rails'
# Add this to load Capybara integration:
require 'capybara/rspec'
require 'capybara/rails