Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add RSpec support in Capybara itself, closes #187

Just the basics of including Capybara and setting
up some metadata to switch between drivers.
  • Loading branch information...
commit aa46894ce79af589fa5148bba2533e05a7f6e910 1 parent 05d9697
Anders Törnqvist authored
Showing with 101 additions and 19 deletions.
  1. +46 −19 README.rdoc
  2. +14 −0 lib/capybara/rspec.rb
  3. +41 −0 spec/rspec_spec.rb
View
65 README.rdoc
@@ -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
@@ -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*
View
14 lib/capybara/rspec.rb
@@ -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
View
41 spec/rspec_spec.rb
@@ -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.
Something went wrong with that request. Please try again.