2009-11-19 17:14:34 -05:00
|
|
|
|
= capybara
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-16 16:02:16 -05:00
|
|
|
|
* http://github.com/jnicklas/capybara
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Description:
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
Capybara aims to simplify the process of integration testing Rack applications,
|
2009-11-18 18:15:46 -05:00
|
|
|
|
such as Rails, Sinatra or Merb. It is inspired by and aims to replace Webrat as
|
|
|
|
|
a DSL for interacting with a webapplication. It is agnostic about the driver
|
2009-11-18 18:00:10 -05:00
|
|
|
|
running your tests and currently comes bundled with rack-test, Culerity and
|
|
|
|
|
Selenium support built in.
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-24 18:18:33 -05:00
|
|
|
|
== Install:
|
|
|
|
|
|
|
|
|
|
Capybara is hosted on Gemcutter, install it with:
|
|
|
|
|
|
|
|
|
|
sudo gem install capybara
|
|
|
|
|
|
2009-11-24 18:16:17 -05:00
|
|
|
|
== Development:
|
|
|
|
|
|
|
|
|
|
* Source hosted at {GitHub}[http://github.com/jnicklas/capybara].
|
|
|
|
|
* Please direct questions, discussions at the {mailing list}[http://groups.google.com/group/ruby-capybara].
|
2009-11-24 18:17:20 -05:00
|
|
|
|
* Report issues on {GitHub Issues}[http://github.com/jnicklas/capybara/issues]
|
|
|
|
|
* Pull requests are very welcome!
|
2009-11-24 18:16:17 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Using Capybara with Cucumber
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
Capybara is built to work nicely with Cucumber. The API is very similar to
|
|
|
|
|
Webrat, so if you know Webrat you should feel right at home. Remove any
|
2009-11-19 17:14:34 -05:00
|
|
|
|
references to Webrat from your <tt>env.rb</tt>, if you're using Rails, make sure to set
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
Cucumber::Rails::World.use_transactional_fixtures = false
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
Capybara uses DatabaseCleaner to truncate the database. Require Capybara in your
|
|
|
|
|
env.rb. For Rails do this:
|
|
|
|
|
|
|
|
|
|
require 'capybara/rails'
|
|
|
|
|
require 'capybara/cucumber'
|
|
|
|
|
|
|
|
|
|
For other frameworks, you'll need to set the Rack app manually:
|
|
|
|
|
|
|
|
|
|
require 'capybara/cucumber'
|
|
|
|
|
Capybara.app = MyRackApp
|
|
|
|
|
|
|
|
|
|
Now you can use it in your steps:
|
|
|
|
|
|
|
|
|
|
When /I sign in/ do
|
|
|
|
|
within("//form[@id='session']") do
|
|
|
|
|
fill_in 'Login', :with => 'user@example.com'
|
|
|
|
|
fill_in 'Password', :with => 'password'
|
|
|
|
|
end
|
|
|
|
|
click_link 'Sign in'
|
2009-11-14 20:13:07 -05:00
|
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Default and current driver
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-18 18:15:46 -05:00
|
|
|
|
You can set up a default driver for your features. For example if you'd prefer
|
|
|
|
|
to run Selenium, you could do:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
|
|
require 'capybara/rails'
|
|
|
|
|
require 'capybara/cucumber'
|
|
|
|
|
Capybara.default_driver = :selenium
|
|
|
|
|
|
|
|
|
|
You can change the driver temporarily:
|
|
|
|
|
|
|
|
|
|
Capybara.current_driver = :culerity
|
|
|
|
|
Capybara.use_default_driver
|
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Cucumber and Tags
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-20 13:15:19 -05:00
|
|
|
|
Capybara sets up some {tags}[http://wiki.github.com/aslakhellesoy/cucumber/tags]
|
2009-11-18 18:15:46 -05:00
|
|
|
|
for you to use in Cucumber. Often you'll want to use run only some scenarios
|
|
|
|
|
with a driver that supports JavaScript, Capybara makes this easy: simply tag the
|
2009-11-19 17:14:34 -05:00
|
|
|
|
scenario (or feature) with <tt>@javascript</tt>:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
|
|
@javascript
|
|
|
|
|
Scenario: do something AJAXy
|
|
|
|
|
When I click the AJAX link
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
You can change which driver Capybara uses for JavaScript:
|
|
|
|
|
|
|
|
|
|
Capybara.javascript_driver = :culerity
|
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
There are also explicit <tt>@selenium</tt>, <tt>@culerity</tt> and <tt>@rack_test</tt> tags set up
|
2009-11-18 18:15:46 -05:00
|
|
|
|
for you.
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== The API
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
|
|
Navigation:
|
|
|
|
|
|
|
|
|
|
visit – The only way to get to anywhere.
|
|
|
|
|
|
|
|
|
|
Scoping:
|
|
|
|
|
|
|
|
|
|
within – Takes a block which executes in the given scope
|
2009-11-24 15:51:10 -05:00
|
|
|
|
within_fieldset – Execute the block in the fieldset given by id or legend
|
|
|
|
|
within_table – Execute the block in the table given by id or caption
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
|
|
Interaction:
|
|
|
|
|
|
|
|
|
|
click_link
|
|
|
|
|
click_button
|
2009-11-20 13:15:19 -05:00
|
|
|
|
fill_in
|
2009-11-18 18:00:10 -05:00
|
|
|
|
choose
|
|
|
|
|
check
|
2009-11-20 12:25:18 -05:00
|
|
|
|
uncheck
|
2009-11-18 18:00:10 -05:00
|
|
|
|
attach_file
|
|
|
|
|
select
|
|
|
|
|
|
|
|
|
|
Querying:
|
|
|
|
|
|
|
|
|
|
body
|
|
|
|
|
has_xpath? – Checks if given XPath exists, takes text and count options
|
|
|
|
|
has_css? – Checks if given CSS exists, takes text and count options
|
2009-11-18 18:15:46 -05:00
|
|
|
|
has_content? – Checks if the given content is on the page
|
2009-11-18 18:00:10 -05:00
|
|
|
|
find_field
|
|
|
|
|
find_link
|
|
|
|
|
find_button
|
|
|
|
|
field_labeled
|
|
|
|
|
|
2009-12-12 07:37:29 -05:00
|
|
|
|
Scripting:
|
|
|
|
|
|
|
|
|
|
evaluate_script – Returns the value of the executed javascript (only works on javascript supported drivers)
|
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
Debugging:
|
|
|
|
|
|
|
|
|
|
save_and_open_page
|
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Using the DSL outside cucumber
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
|
|
You can mix the DSL into any context, for example you could use it in RSpec
|
|
|
|
|
examples. Just load the dsl and include it anywhere:
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-16 16:02:16 -05:00
|
|
|
|
require 'capybara'
|
|
|
|
|
require 'capybara/dsl'
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-16 16:02:16 -05:00
|
|
|
|
include Capybara
|
|
|
|
|
Capybara.default_driver = :culerity
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
|
|
|
|
within("//form[@id='session']") do
|
|
|
|
|
fill_in 'Login', :with => 'user@example.com'
|
|
|
|
|
fill_in 'Password', :with => 'password'
|
|
|
|
|
end
|
|
|
|
|
click_link 'Sign in'
|
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Using the sessions manually
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
For ultimate control, you can instantiate and use a session manually.
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
require 'capybara'
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
2009-11-18 18:00:10 -05:00
|
|
|
|
session = Capybara::Session.new(:culerity, my_rack_app)
|
|
|
|
|
session.within("//form[@id='session']") do
|
|
|
|
|
session.fill_in 'Login', :with => 'user@example.com'
|
|
|
|
|
session.fill_in 'Password', :with => 'password'
|
2009-11-14 20:13:07 -05:00
|
|
|
|
end
|
2009-11-18 18:00:10 -05:00
|
|
|
|
session.click_link 'Sign in'
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
2009-11-29 16:04:53 -05:00
|
|
|
|
== XPath and CSS
|
2009-11-26 17:48:18 -05:00
|
|
|
|
|
|
|
|
|
Capybara does not try to guess what kind of selector you are going to give it,
|
|
|
|
|
if you want to use CSS with your 'within' declarations for example, you'll need
|
|
|
|
|
to do:
|
2009-12-12 07:37:29 -05:00
|
|
|
|
|
2009-11-26 17:48:18 -05:00
|
|
|
|
within(:css, 'ul li') { ... }
|
|
|
|
|
|
|
|
|
|
Alternatively you can set the default selector to CSS, which may help if you are
|
|
|
|
|
moving from Webrat and used CSS a lot, or simply generally prefer CSS:
|
|
|
|
|
|
|
|
|
|
Capybara.default_selector = :css
|
|
|
|
|
within('ul li') { ... }
|
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== Gotchas:
|
2009-11-14 20:13:07 -05:00
|
|
|
|
|
2009-11-22 05:07:20 -05:00
|
|
|
|
* Install JRuby and the 'celerity' gem, version 0.7.4 (0.7.5 has a bug with
|
2009-11-24 18:10:30 -05:00
|
|
|
|
password fields) under JRuby for Culerity support.
|
2009-11-20 13:15:19 -05:00
|
|
|
|
|
2009-11-18 18:15:46 -05:00
|
|
|
|
* Everything is *case sensitive*. Capybara heavily relies on XPath, which
|
|
|
|
|
doesn't support case insensitive searches.
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
* The <tt>have_tag</tt> and <tt>have_text</tt> matchers in RSpec-Rails are not
|
|
|
|
|
supported. You should use <tt>page.should have_css('#header p')</tt>,
|
|
|
|
|
<tt>page.should have_xpath('//ul/li')</tt> and <tt>page.should
|
|
|
|
|
have_content('Monkey')</tt> instead.
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-18 18:15:46 -05:00
|
|
|
|
* Domain names (including subdomains) don't work under rack-test. Since it's a
|
|
|
|
|
pain to set up subdomains for the other drivers anyway, you should consider an
|
|
|
|
|
alternate solution. You might use
|
2009-11-22 05:07:20 -05:00
|
|
|
|
{default_url_options}[https://gist.github.com/643a758320a2926bd2ed] in Rails
|
2009-11-18 18:15:46 -05:00
|
|
|
|
for example.
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
* The <tt>set_hidden_field</tt> method from Webrat is not implemented, since it doesn't
|
2009-11-18 18:15:46 -05:00
|
|
|
|
work in any of the browser based drivers (Culerity, Selenium)
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
2009-11-18 18:15:46 -05:00
|
|
|
|
* Access to session, request and response from the test is not possible. Maybe
|
|
|
|
|
we'll do response headers at some point in the future, but the others really
|
|
|
|
|
shouldn't be touched in an integration test anyway.
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
* Access to Rails specific stuff (such as <tt>controller</tt>) is unavailable,
|
|
|
|
|
since we're not using Rails' integration testing.
|
2009-11-18 18:15:46 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
* <tt><a href="#"></tt> Will cause problems under rack-test, please do
|
|
|
|
|
<tt><a href="/same/url#"></tt> instead. You can achieve this in Rails with
|
|
|
|
|
<tt>link_to('foo', :anchor => '')</tt>
|
2009-11-18 18:15:46 -05:00
|
|
|
|
|
2009-11-26 17:48:34 -05:00
|
|
|
|
== Contributors:
|
|
|
|
|
|
|
|
|
|
The following people have dedicated their time and effort to Capybara:
|
|
|
|
|
|
|
|
|
|
* Jonas Nicklas
|
|
|
|
|
* Dennis Rogenius
|
|
|
|
|
* Rob Holland
|
2009-11-30 15:40:39 -05:00
|
|
|
|
* Wincent Colaiuta
|
2009-12-07 14:19:00 -05:00
|
|
|
|
* Andrea Fazzi
|
2009-11-26 17:48:34 -05:00
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
|
== License:
|
2009-11-04 17:00:05 -05:00
|
|
|
|
|
|
|
|
|
(The MIT License)
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2009 Jonas Nicklas
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
|
'Software'), to deal in the Software without restriction, including
|
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
2009-12-12 07:37:29 -05:00
|
|
|
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|