Implementation:Teamcapybara Capybara Capybara Feature DSL
| Knowledge Sources | |
|---|---|
| Domains | Testing, DSL |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for writing feature specs using Capybara's Feature DSL aliases, provided by capybara/rspec/features and capybara/dsl.
Description
lib/capybara/rspec/features.rb uses RSpec.configure to create feature, xfeature, ffeature as aliases for describe (with type: :feature metadata), and scenario, xscenario, fscenario as aliases for it. A shared context adds background (alias for before) and given/given! (aliases for let/let!).
lib/capybara/dsl.rb defines the Capybara::DSL module which provides the page method (returning Capybara.current_session) and dynamically delegates all Session::DSL_METHODS to the current page.
Usage
Use these aliases in RSpec feature or system specs after requiring capybara/rspec. The aliases are automatically available in feature spec contexts.
Code Reference
Source Location
- Repository: capybara
- File: lib/capybara/rspec/features.rb (L1-23), lib/capybara/dsl.rb (L6-56)
Signature
# Feature DSL aliases (from rspec/features.rb)
feature(description, *args, &block) # alias for RSpec.describe with type: :feature
scenario(description, *args, &block) # alias for it
background(&block) # alias for before
given(name, &block) # alias for let
given!(name, &block) # alias for let!
# DSL module (from dsl.rb)
module Capybara::DSL
def page
Capybara.current_session # -> Capybara::Session
end
# All Session::DSL_METHODS delegated to page
# visit, find, all, fill_in, click_on, within, etc.
end
Import
require 'capybara/rspec' # includes both features.rb and dsl.rb
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| description | String | Yes | Description of the feature or scenario |
| block | Block | Yes | Test body containing Capybara interactions |
Outputs
| Name | Type | Description |
|---|---|---|
| page | Capybara::Session | The current session, accessible via page method |
| RSpec examples | RSpec::Core::Example | Executable test examples |
Usage Examples
Feature Spec with DSL
feature 'Shopping Cart' do
given(:user) { create(:user) }
background do
sign_in(user)
end
scenario 'adding an item to cart' do
visit '/products'
click_on 'Add to Cart'
expect(page).to have_text('Item added')
end
scenario 'removing an item from cart', js: true do
visit '/cart'
click_on 'Remove'
expect(page).to have_no_text('Item')
end
end