Skip to content

Commit

Permalink
Add RSpec support in Capybara itself, closes teamcapybara#187
Browse files Browse the repository at this point in the history
Just the basics of including Capybara and setting
up some metadata to switch between drivers.
  • Loading branch information
Anders Törnqvist committed Dec 10, 2010
1 parent 05d9697 commit aa46894
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 19 deletions.
65 changes: 46 additions & 19 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,52 @@ Now you can use it in your steps:
click_link 'Sign in'
end

Capybara sets up some {tags}[http://wiki.github.com/aslakhellesoy/cucumber/tags]
for you to use in Cucumber. Often you'll want to run only some scenarios with a
driver that supports JavaScript, Capybara makes this easy: simply tag the
scenario (or feature) with <tt>@javascript</tt>:

@javascript
Scenario: do something AJAXy
When I click the AJAX link
...

You can change which driver Capybara uses for JavaScript:

Capybara.javascript_driver = :culerity

There are also explicit <tt>@selenium</tt>, <tt>@culerity</tt> and
<tt>@rack_test</tt> tags set up for you.

== Using Capybara with RSpec

If you prefer RSpec to using Cucumber, you can use the built in RSpec support:

require 'capybara/rspec'
Capybara.app = MyRackApp

You can now use it in your examples:

it "signs me in" do
within("#session") do
fill_in 'Login', :with => 'user@example.com'
fill_in 'Password', :with => 'password'
end
click_link 'Sign in'
end

RSpec's metadata feature can be used to switch to a different driver. Use the
<tt>:js => true</tt> to switch to the javascript driver, or provide a
<tt>:driver</tt> 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 => :celerity
end

Note that Capybara's built in RSpec support only works with RSpec 2.0 or later.
You'll need to roll your own for earlier versions of RSpec.

== Default and current driver

You can set up a default driver for your features. For example if you'd prefer
Expand All @@ -82,25 +128,6 @@ You can do this in Before and After blocks to temporarily switch to a different
driver. Note that switching driver creates a new session, so you may not be able
to switch in the middle of a Scenario.

== Cucumber and Tags

Capybara sets up some {tags}[http://wiki.github.com/aslakhellesoy/cucumber/tags]
for you to use in Cucumber. Often you'll want to run only some scenarios with a
driver that supports JavaScript, Capybara makes this easy: simply tag the
scenario (or feature) with <tt>@javascript</tt>:

@javascript
Scenario: do something AJAXy
When I click the AJAX link
...

You can change which driver Capybara uses for JavaScript:

Capybara.javascript_driver = :culerity

There are also explicit <tt>@selenium</tt>, <tt>@culerity</tt> and
<tt>@rack_test</tt> tags set up for you.

== Selenium

At the moment, Capybara supports Webdriver, also called Selenium 2.0, *not*
Expand Down
14 changes: 14 additions & 0 deletions lib/capybara/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'capybara'
require 'capybara/dsl'

Rspec.configure do |config|
config.include Capybara
config.after do
Capybara.reset_sessions!
Capybara.use_default_driver
end
config.before do
Capybara.current_driver = Capybara.javascript_driver if example.metadata[:js]
Capybara.current_driver = example.metadata[:driver] if example.metadata[:driver]
end
end
41 changes: 41 additions & 0 deletions spec/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'
require 'capybara/rspec'

Capybara.app = TestApp

describe 'capybara/rspec' do
it "should include Capybara in rpsec" do
visit('/foo')
page.body.should include('Another World')
end

context "resetting session" do
it "sets a cookie in one example..." do
visit('/set_cookie')
page.body.should include('Cookie set to test_cookie')
end

it "...then it is not availbable in the next" do
visit('/get_cookie')
page.body.should_not include('test_cookie')
end
end

context "setting the current driver" do
it "sets the current driver in one example..." do
Capybara.current_driver = :selenium
end

it "...then it has returned to the default in the next example" do
Capybara.current_driver.should == :rack_test
end
end

it "switches to the javascript driver when giving it as metadata", :js => true do
Capybara.current_driver.should == :selenium
end

it "switches to the given driver when giving it as metadata", :driver => :culerity do
Capybara.current_driver.should == :culerity
end
end

0 comments on commit aa46894

Please sign in to comment.