Using Capybara with RSpec

Using Capybara with RSpec

Using Capybara with RSpec

 

Load RSpec 2.x support by adding the following line (typically to your spec_helper.rb file):

require ‘capybara/rspec’

If you are using Rails, put your Capybara specs in spec/features.

If you are not using Rails, tag all the example groups in which you want to use Capybara with :type => :feature.

You can now write your specs like so:

describe “the signin process”, :type => :feature do

before :each do

User.make(:email => ‘[email protected]’, :password => ‘password’)

end

it “signs me in” do

visit ‘/sessions/new’

within(“#session”) do

fill_in ‘Email’, :with => ‘[email protected]

fill_in ‘Password’, :with => ‘password’

end

click_button ‘Sign in’

expect(page).to have_content ‘Success’

end

end

Use :js => true to switch to the Capybara.javascript_driver (:selenium by default), or provide a :driver option to switch to one specific driver. For example:

describe ‘some stuff which requires js’, :js => true do

it ‘will use the default js driver’

it ‘will switch to one specific driver’, :driver => :webkit

end

Capybara also comes with a built in DSL for creating descriptive acceptance tests:

feature “Signing in” do

background do

User.make(:email => ‘[email protected]’, :password => ‘caplin’)

end

 

scenario “Signing in with correct credentials” do

visit ‘/sessions/new’

within(“#session”) do

fill_in ‘Email’, :with => ‘[email protected]

fill_in ‘Password’, :with => ‘caplin’

end

click_button ‘Sign in’

expect(page).to have_content ‘Success’

end

 

given(:other_user) { User.make(:email => ‘[email protected]’, :password => ‘rous’) }

 

scenario “Signing in as another user” do

visit ‘/sessions/new’

within(“#session”) do

fill_in ‘Email’, :with => other_user.email

fill_in ‘Password’, :with => other_user.password

end

click_button ‘Sign in’

expect(page).to have_content ‘Invalid email or password’

end

end

feature is in fact just an alias for describe …, :type => :feature, background is an alias for before, scenario for it, and given/given! aliases for let/let!, respectively.

Finally, Capybara matchers are supported in view specs:

 

RSpec.describe “todos/show.html.erb”, type: :view do

it “displays the todo title” do

assign :todo, Todo.new(title: “Buy milk”)

 

render

 

expect(rendered).to have_css(“header h1”, text: “Buy milk”)

end

end

Using Capybara with RSpec

Capybara naturally fits with RSpec and no special integration is needed.

describe “Create place scenario” do

context “Go to home page” do

it “opens homepage” do

visit(get_homepage)

end

end

context “Click on create object link” do

it “opens create new object form” do

find(:homepage_navigation_create_object).click

end

end

end

Using the DSL to interact with page elements, has one caveat: With every new context, a new Capybara session will be instantiated which is probably not what we want.

Capybara::Session.new :driver helps manage this issue. For example:

session = Capybara::Session.new :selenium # instantiate new session object

session.visit() # use it to call DSL methods

The session represents a single user interaction with the application, which is precisely what we want.

It is a good practice to define a method in spec_helper.rb which will return the session object and use this session object when calling all Capybara methods inside RSpec context (session.visit, session.find, etc.)

 

Make your resume stand out and become a Certified Capybara Testing Professional. Try free practice tests here!

A great career is just a certification away. So, practice and validate your skills to become Certified Capybara Testing Professional

Capybara Setup
Using Capybara with Cucumber

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?