2010-07-09 14:39:25 -04:00
|
|
|
= Capybara
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
Capybara helps you test Rails and Rack applications by simulating how a real
|
|
|
|
user would interact with your app. It is agnostic about the driver running your
|
|
|
|
tests and comes with Rack::Test and Selenium support built in. WebKit is
|
|
|
|
supported through an external gem.
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
<b>Need help?</b> Ask on the mailing list: http://groups.google.com/group/ruby-capybara
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
== Setup
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
To install, type
|
2010-07-17 13:07:50 -04:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
sudo gem install capybara
|
2009-11-24 18:18:33 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
If you are using Rails, add this line to your test helper file:
|
2009-11-24 18:18:33 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
require 'capybara/rails'
|
2009-11-24 18:18:33 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
If you are not using Rails, set Capybara.app to your rack app:
|
2010-01-17 10:31:40 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
Capybara.app = MyRackApp
|
2010-01-17 10:31:40 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
== Development
|
2009-11-24 18:16:17 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
If you found a _reproducible_ bug, open a {GitHub
|
|
|
|
Issue}[http://github.com/jnicklas/capybara/issues] to submit a bug report.
|
2010-02-20 16:03:29 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
Even better, send a pull request! Make sure all changes are well tested,
|
|
|
|
Capybara is a testing tool after all. Topic branches are good.
|
2009-11-24 18:16:17 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
To set up a development environment, simply do:
|
2010-07-21 17:32:14 -04:00
|
|
|
|
2011-02-04 09:06:30 -05:00
|
|
|
git submodule update --init
|
2011-08-26 20:29:58 -04:00
|
|
|
gem install bundler
|
2010-07-21 17:32:14 -04:00
|
|
|
bundle install
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
== Using Capybara with Cucumber
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
The <tt>cucumber-rails</tt> gem comes with Capybara support built-in. If you
|
|
|
|
are not using Rails, manually load the <tt>capybara/cucumber</tt> module:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
require 'capybara/cucumber'
|
|
|
|
Capybara.app = MyRackApp
|
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
You can use the Capybara DSL in your steps, like so:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
When /I sign in/ do
|
2010-07-16 16:21:35 -04:00
|
|
|
within("#session") do
|
2009-11-18 18:00:10 -05:00
|
|
|
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
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
You can switch to the <tt>Capybara.javascript_driver</tt> (<tt>:selenium</tt>
|
|
|
|
by default) by tagging scenarios (or features) with <tt>@javascript</tt>:
|
2010-12-10 08:54:46 -05:00
|
|
|
|
|
|
|
@javascript
|
2011-03-14 10:27:46 -04:00
|
|
|
Scenario: do something Ajaxy
|
|
|
|
When I click the Ajax link
|
2010-12-10 08:54:46 -05:00
|
|
|
...
|
|
|
|
|
2011-12-19 16:42:08 -05:00
|
|
|
There are also explicit <tt>@selenium</tt> and <tt>@rack_test</tt>
|
|
|
|
tags set up for you.
|
2010-12-10 08:54:46 -05:00
|
|
|
|
|
|
|
== Using Capybara with RSpec
|
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
Load RSpec 2.x support by adding the following line (typically to your
|
|
|
|
<tt>spec_helper.rb</tt> file):
|
2010-12-10 08:54:46 -05:00
|
|
|
|
|
|
|
require 'capybara/rspec'
|
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
If you are using Rails, put your Capybara specs in <tt>spec/requests</tt> or
|
|
|
|
<tt>spec/integration</tt>.
|
|
|
|
|
|
|
|
If you are not using Rails, tag all the example groups in which you want to use
|
|
|
|
Capybara with <tt>:type => :request</tt>.
|
|
|
|
|
2011-04-28 18:08:05 -04:00
|
|
|
You can now write your specs like so:
|
2010-12-10 08:54:46 -05:00
|
|
|
|
2011-02-03 17:18:07 -05:00
|
|
|
describe "the signup process", :type => :request do
|
2011-04-18 15:34:06 -04:00
|
|
|
before :each do
|
|
|
|
User.make(:email => 'user@example.com', :password => 'caplin')
|
|
|
|
end
|
2011-04-25 05:35:57 -04:00
|
|
|
|
2011-01-09 07:57:41 -05:00
|
|
|
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'
|
2010-12-10 08:54:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
Use <tt>:js => true</tt> to switch to the <tt>Capybara.javascript_driver</tt>
|
|
|
|
(<tt>:selenium</tt> by default), or provide a <tt>:driver</tt> option to switch
|
|
|
|
to one specific driver. For example:
|
2010-12-10 08:54:46 -05:00
|
|
|
|
|
|
|
describe 'some stuff which requires js', :js => true do
|
|
|
|
it 'will use the default js driver'
|
2011-12-19 16:42:08 -05:00
|
|
|
it 'will switch to one specific driver', :driver => :webkit
|
2010-12-10 08:54:46 -05:00
|
|
|
end
|
|
|
|
|
2011-04-28 18:08:05 -04:00
|
|
|
Finally, Capybara also comes with a built in DSL for creating descriptive acceptance tests:
|
2011-02-11 08:44:58 -05:00
|
|
|
|
2011-04-18 15:34:06 -04:00
|
|
|
feature "Signing up" do
|
2011-02-11 08:44:58 -05:00
|
|
|
background do
|
|
|
|
User.make(:email => 'user@example.com', :password => 'caplin')
|
|
|
|
end
|
|
|
|
|
2011-04-18 15:34:06 -04:00
|
|
|
scenario "Signing in with correct credentials" do
|
2011-02-11 08:44:58 -05:00
|
|
|
within("#session") do
|
|
|
|
fill_in 'Login', :with => 'user@example.com'
|
|
|
|
fill_in 'Password', :with => 'caplin'
|
|
|
|
end
|
|
|
|
click_link 'Sign in'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
<tt>feature</tt> is in fact just an alias for <tt>describe ..., :type =>
|
|
|
|
:request</tt>, <tt>background</tt> is an alias for <tt>before</tt>, and
|
|
|
|
<tt>scenario</tt> for <tt>it</tt>.
|
2010-12-10 08:54:46 -05:00
|
|
|
|
2011-04-14 09:46:22 -04:00
|
|
|
== Using Capybara with Test::Unit
|
|
|
|
|
2011-12-02 10:07:03 -05:00
|
|
|
To use Capybara with Test::Unit, include <tt>Capybara::DSL</tt> in whatever
|
|
|
|
test class you are using. For example, if your classes derive from
|
2011-04-14 09:46:22 -04:00
|
|
|
<tt>ActionDispatch::IntegrationTest</tt>, use
|
|
|
|
|
|
|
|
class ActionDispatch::IntegrationTest
|
|
|
|
include Capybara::DSL
|
|
|
|
end
|
|
|
|
|
2011-04-14 09:53:58 -04:00
|
|
|
Test::Unit does not support selecting the driver through test metadata, but you
|
|
|
|
can switch the driver for specific classes using the <tt>setup</tt> and
|
|
|
|
<tt>teardown</tt> methods. See the section "Selecting the Driver".
|
|
|
|
|
2011-09-26 09:44:33 -04:00
|
|
|
== Using Capybara with MiniTest::Spec
|
|
|
|
|
|
|
|
Include Capybara::DSL like in Test::Unit. Be sure to include it in the proper class,
|
|
|
|
which could be something other than ActionDispatch::IntegrationTest.
|
|
|
|
|
|
|
|
The capybara_minitest_spec gem ({Github}[https://github.com/ordinaryzelig/capybara_minitest_spec],
|
|
|
|
{rubygems.org}[https://rubygems.org/gems/capybara_minitest_spec]) provides MiniTest::Spec
|
|
|
|
expectations for Capybara. For example:
|
|
|
|
|
|
|
|
page.must_have_content('Important!')
|
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
== Drivers
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
Capybara uses the same DSL to drive a variety of browser and headless drivers.
|
|
|
|
|
|
|
|
=== Selecting the Driver
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
By default, Capybara uses the <tt>:rack_test</tt> driver, which is fast but does not
|
2011-02-18 19:18:57 -05:00
|
|
|
support JavaScript. You can set up a different default driver for your
|
|
|
|
features. For example if you'd prefer to run everything in Selenium, you could
|
|
|
|
do:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
Capybara.default_driver = :selenium
|
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
However, if you are using RSpec or Cucumber, you may instead want to consider
|
2011-03-14 10:27:46 -04:00
|
|
|
leaving the faster <tt>:rack_test</tt> as the +default_driver+, and marking only those
|
2011-02-18 19:18:57 -05:00
|
|
|
tests that require a JavaScript-capable driver using <tt>:js => true</tt> or
|
|
|
|
<tt>@javascript</tt>, respectively. By default, JavaScript tests are run using the
|
2011-03-14 10:27:46 -04:00
|
|
|
<tt>:selenium</tt> driver. You can change this by setting
|
|
|
|
<tt>Capybara.javascript_driver</tt>.
|
2011-02-18 19:18:57 -05:00
|
|
|
|
2011-04-14 09:53:58 -04:00
|
|
|
You can also change the driver temporarily (typically in the Before/setup and
|
|
|
|
After/teardown blocks):
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-12-19 16:42:08 -05:00
|
|
|
Capybara.current_driver = :webkit # temporarily select different driver
|
2011-04-02 11:57:55 -04:00
|
|
|
... tests ...
|
|
|
|
Capybara.use_default_driver # switch back to default driver
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
Note that switching the driver creates a new session, so you may not be able to
|
|
|
|
switch in the middle of a test.
|
2009-12-14 16:07:51 -05:00
|
|
|
|
2011-04-25 05:35:57 -04:00
|
|
|
=== RackTest
|
|
|
|
|
|
|
|
RackTest is Capybara's default driver. It is written in pure Ruby and does not
|
|
|
|
have any support for executing JavaScript. Since the RackTest driver works
|
2011-09-16 11:44:38 -04:00
|
|
|
directly against the Rack interface, it does not need any server to be started,
|
2011-04-25 05:35:57 -04:00
|
|
|
it can work directly work against any Rack app. This means that if your
|
|
|
|
application is not a Rack application (Rails, Sinatra and most other Ruby
|
|
|
|
frameworks are Rack applications) then you cannot use this driver. You cannot
|
|
|
|
use the RackTest driver to test a remote application.
|
|
|
|
{capybara-mechanize}[https://github.com/jeroenvandijk/capybara-mechanize]
|
|
|
|
intends to provide a similar driver which works against remote servers, it is a
|
|
|
|
separate project.
|
|
|
|
|
|
|
|
RackTest can be configured with a set of headers like this:
|
|
|
|
|
|
|
|
Capybara.register_driver :rack_test do |app|
|
|
|
|
Capybara::RackTest::Driver.new(app, :browser => :chrome)
|
|
|
|
end
|
|
|
|
|
|
|
|
See the section on adding and configuring drivers.
|
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
=== Selenium
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
At the moment, Capybara supports {Selenium 2.0
|
|
|
|
(Webdriver)}[http://seleniumhq.org/docs/01_introducing_selenium.html#selenium-2-aka-selenium-webdriver],
|
|
|
|
*not* Selenium RC. Provided Firefox is installed, everything is set up for you,
|
|
|
|
and you should be able to start using Selenium right away.
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-09-02 10:28:14 -04:00
|
|
|
Capybara can block and wait for Ajax requests to finish after you've interacted
|
|
|
|
with the page. To enable this behaviour, set the <tt>:resynchronize</tt> driver
|
|
|
|
option to <tt>true</tt>. This should normally not be necessary, since
|
|
|
|
Capybara's automatic reloading should take care of any asynchronicity problems.
|
|
|
|
See the section on Asynchronous JavaScript for details.
|
2011-02-22 09:53:14 -05:00
|
|
|
|
2011-04-14 09:39:34 -04:00
|
|
|
Note: Selenium does not support transactional fixtures; see the section
|
|
|
|
"Transactional Fixtures" below.
|
|
|
|
|
2011-08-07 10:54:53 -04:00
|
|
|
=== Capybara-webkit
|
|
|
|
|
2011-10-10 14:39:19 -04:00
|
|
|
The {capybara-webkit driver}[https://github.com/thoughtbot/capybara-webkit] is for true headless
|
|
|
|
testing. It uses QtWebKit to start a rendering engine process. It can execute JavaScript as well.
|
2011-08-07 10:54:53 -04:00
|
|
|
It is significantly faster than drivers like Selenium since it does not load an entire browser.
|
|
|
|
|
|
|
|
You can install it with:
|
|
|
|
|
|
|
|
gem install capybara-webkit
|
|
|
|
|
|
|
|
And you can use it by:
|
|
|
|
|
|
|
|
Capybara.javascript_driver = :webkit
|
|
|
|
|
2011-03-01 15:52:07 -05:00
|
|
|
== The DSL
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-12-21 12:02:33 -05:00
|
|
|
A complete reference is available at
|
|
|
|
{at rubydoc.info}[http://rubydoc.info/github/jnicklas/capybara/master].
|
|
|
|
|
|
|
|
Note: All searches in Capybara are <b>case sensitive</b>. This is because
|
|
|
|
Capybara heavily uses XPath, which doesn't support case insensitivity.
|
2009-12-12 07:37:29 -05:00
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
=== Navigating
|
|
|
|
|
2011-02-04 09:06:30 -05:00
|
|
|
You can use the
|
2011-02-17 17:55:16 -05:00
|
|
|
<tt>{visit}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#visit-instance_method]</tt>
|
2011-02-04 09:06:30 -05:00
|
|
|
method to navigate to other pages:
|
2009-12-25 12:00:27 -05:00
|
|
|
|
|
|
|
visit('/projects')
|
|
|
|
visit(post_comments_path(post))
|
|
|
|
|
|
|
|
The visit method only takes a single parameter, the request method is *always*
|
|
|
|
GET.
|
|
|
|
|
2011-02-04 09:06:30 -05:00
|
|
|
You can get the {current
|
|
|
|
path}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#current_path-instance_method]
|
|
|
|
of the browsing session for test assertions:
|
2010-04-07 11:20:59 -04:00
|
|
|
|
|
|
|
current_path.should == post_comments_path(post)
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
=== Clicking links and buttons
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
<em>Full reference: {Capybara::Node::Actions}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions]</em>
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
You can interact with the webapp by following links and buttons. Capybara
|
|
|
|
automatically follows any redirects, and submits forms associated with buttons.
|
|
|
|
|
|
|
|
click_link('id-of-link')
|
|
|
|
click_link('Link Text')
|
|
|
|
click_button('Save')
|
2011-02-04 09:06:30 -05:00
|
|
|
click_on('Link Text') # clicks on either links or buttons
|
|
|
|
click_on('Button Value')
|
2009-12-25 12:00:27 -05:00
|
|
|
|
|
|
|
=== Interacting with forms
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
<em>Full reference: {Capybara::Node::Actions}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions]</em>
|
|
|
|
|
|
|
|
There are a number of tools for interacting with form elements:
|
2009-12-25 12:00:27 -05:00
|
|
|
|
|
|
|
fill_in('First Name', :with => 'John')
|
|
|
|
fill_in('Password', :with => 'Seekrit')
|
2011-03-14 10:27:46 -04:00
|
|
|
fill_in('Description', :with => 'Really Long Text...')
|
2010-03-13 16:53:51 -05:00
|
|
|
choose('A Radio Button')
|
2009-12-25 12:00:27 -05:00
|
|
|
check('A Checkbox')
|
|
|
|
uncheck('A Checkbox')
|
|
|
|
attach_file('Image', '/path/to/image.jpg')
|
|
|
|
select('Option', :from => 'Select Box')
|
|
|
|
|
|
|
|
=== Querying
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
<em>Full reference: {Capybara::Node::Matchers}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers]</em>
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
Capybara has a rich set of options for querying the page for the existence of
|
2011-03-14 10:27:46 -04:00
|
|
|
certain elements, and working with and manipulating those elements.
|
2009-12-25 12:00:27 -05:00
|
|
|
|
2010-10-12 07:26:24 -04:00
|
|
|
page.has_selector?('table tr')
|
|
|
|
page.has_selector?(:xpath, '//table/tr')
|
|
|
|
page.has_no_selector?(:content)
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
page.has_xpath?('//table/tr')
|
|
|
|
page.has_css?('table tr.foo')
|
|
|
|
page.has_content?('foo')
|
2011-10-16 19:46:19 -04:00
|
|
|
page.has_text?('foo')
|
2010-02-03 07:18:20 -05:00
|
|
|
|
2011-01-31 19:43:16 -05:00
|
|
|
You can use these with RSpec's magic matchers:
|
2009-12-25 12:00:27 -05:00
|
|
|
|
2010-10-12 07:26:24 -04:00
|
|
|
page.should have_selector('table tr')
|
|
|
|
page.should have_selector(:xpath, '//table/tr')
|
|
|
|
page.should have_no_selector(:content)
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
page.should have_xpath('//table/tr')
|
|
|
|
page.should have_css('table tr.foo')
|
2011-10-16 19:46:19 -04:00
|
|
|
page.should have_text('foo')
|
|
|
|
page.should have_no_text('foo')
|
|
|
|
|
|
|
|
Note that there are 2 matchers for checking content/text. <tt>page.has_text?('foo')</tt>
|
|
|
|
will check only for text that is displayable, whereas <tt>page.has_content?('foo')</tt> will
|
|
|
|
check for the content within any nodes (including the head section and within script tags).
|
|
|
|
Most of the time you'll want the behaviour of <tt>page.has_text?('foo')</tt>, so go with that
|
|
|
|
unless you have a specific reason to use <tt>page.has_content?('foo')</tt> instead.
|
2010-01-01 13:58:10 -05:00
|
|
|
|
|
|
|
Note that <tt>page.should have_no_xpath</tt> is preferred over
|
|
|
|
<tt>page.should_not have_xpath</tt>. Read the section on asynchronous JavaScript
|
|
|
|
for an explanation.
|
2009-12-25 12:00:27 -05:00
|
|
|
|
2011-02-17 17:55:16 -05:00
|
|
|
If all else fails, you can also use the
|
|
|
|
<tt>{page.html}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#html-instance_method]</tt>
|
|
|
|
method to test against the raw HTML:
|
|
|
|
|
|
|
|
page.html.should match /<span>.../i
|
|
|
|
|
2010-07-10 11:10:54 -04:00
|
|
|
=== Finding
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
<em>Full reference: {Capybara::Node::Finders}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders]</em>
|
|
|
|
|
|
|
|
You can also find specific elements, in order to manipulate them:
|
2009-12-25 12:00:27 -05:00
|
|
|
|
|
|
|
find_field('First Name').value
|
|
|
|
find_link('Hello').visible?
|
|
|
|
find_button('Send').click
|
2010-02-03 07:18:20 -05:00
|
|
|
|
2010-07-19 14:18:16 -04:00
|
|
|
find(:xpath, "//table/tr").click
|
|
|
|
find("#overlay").find("h1").click
|
2009-12-25 12:00:27 -05:00
|
|
|
all('a').each { |a| a[:href] }
|
|
|
|
|
2010-07-19 14:18:16 -04:00
|
|
|
Note that <tt>find</tt> will wait for an element to appear on the page, as explained in the
|
2011-03-14 10:27:46 -04:00
|
|
|
Ajax section. If the element does not appear it will raise an error.
|
2010-07-10 10:56:20 -04:00
|
|
|
|
2010-07-10 11:10:54 -04:00
|
|
|
These elements all have all the Capybara DSL methods available, so you can restrict them
|
|
|
|
to specific parts of the page:
|
|
|
|
|
2010-07-19 14:18:16 -04:00
|
|
|
find('#navigation').click_link('Home')
|
|
|
|
find('#navigation').should have_button('Sign out')
|
2010-07-10 11:10:54 -04:00
|
|
|
|
|
|
|
=== Scoping
|
|
|
|
|
|
|
|
Capybara makes it possible to restrict certain actions, such as interacting with
|
|
|
|
forms or clicking links and buttons, to within a specific area of the page. For
|
2011-02-04 09:06:30 -05:00
|
|
|
this purpose you can use the generic
|
2011-02-17 17:55:16 -05:00
|
|
|
<tt>{within}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#within-instance_method]</tt>
|
2011-02-04 09:06:30 -05:00
|
|
|
method. Optionally you can specify which kind of selector to use.
|
2010-07-10 11:10:54 -04:00
|
|
|
|
2010-10-06 15:07:47 -04:00
|
|
|
within("li#employee") do
|
2010-07-10 11:10:54 -04:00
|
|
|
fill_in 'Name', :with => 'Jimmy'
|
|
|
|
end
|
|
|
|
|
2010-10-06 15:07:47 -04:00
|
|
|
within(:xpath, "//li[@id='employee']") do
|
2010-07-10 11:10:54 -04:00
|
|
|
fill_in 'Name', :with => 'Jimmy'
|
|
|
|
end
|
|
|
|
|
2011-04-30 20:33:41 -04:00
|
|
|
Note that <tt>within</tt> will scope the actions to the _first_ (not _any_)
|
|
|
|
element that matches the selector.
|
|
|
|
|
2010-07-10 11:10:54 -04:00
|
|
|
There are special methods for restricting the scope to a specific fieldset,
|
|
|
|
identified by either an id or the text of the fieldet's legend tag, and to a
|
|
|
|
specific table, identified by either id or text of the table's caption tag.
|
|
|
|
|
|
|
|
within_fieldset('Employee') do
|
|
|
|
fill_in 'Name', :with => 'Jimmy'
|
|
|
|
end
|
|
|
|
|
|
|
|
within_table('Employee') do
|
|
|
|
fill_in 'Name', :with => 'Jimmy'
|
|
|
|
end
|
|
|
|
|
2009-12-25 12:00:27 -05:00
|
|
|
=== Scripting
|
|
|
|
|
2010-06-29 16:41:33 -04:00
|
|
|
In drivers which support it, you can easily execute JavaScript:
|
|
|
|
|
|
|
|
page.execute_script("$('body').empty()")
|
|
|
|
|
|
|
|
For simple expressions, you can return the result of the script. Note
|
|
|
|
that this may break with more complicated expressions:
|
2009-12-25 12:00:27 -05:00
|
|
|
|
|
|
|
result = page.evaluate_script('4 + 4');
|
|
|
|
|
|
|
|
=== Debugging
|
|
|
|
|
|
|
|
It can be useful to take a snapshot of the page as it currently is and take a
|
|
|
|
look at it:
|
2009-11-18 18:00:10 -05:00
|
|
|
|
|
|
|
save_and_open_page
|
|
|
|
|
2011-04-14 09:39:34 -04:00
|
|
|
== Transactional fixtures
|
|
|
|
|
|
|
|
Transactional fixtures only work in the default Rack::Test driver, but not for
|
|
|
|
other drivers like Selenium. Cucumber takes care of this automatically, but
|
|
|
|
with Test::Unit or RSpec, you may have to use the
|
|
|
|
{database_cleaner}[https://github.com/bmabey/database_cleaner] gem. See {this
|
|
|
|
explanation}[https://groups.google.com/d/msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ]
|
|
|
|
(and code for {solution
|
2011-11-09 22:19:13 -05:00
|
|
|
2}[http://opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-220]
|
2011-04-14 09:39:34 -04:00
|
|
|
and {solution 3}[http://pastie.org/1745020]) for details.
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
== Asynchronous JavaScript (Ajax and friends)
|
2010-01-01 13:58:10 -05:00
|
|
|
|
|
|
|
When working with asynchronous JavaScript, you might come across situations
|
|
|
|
where you are attempting to interact with an element which is not yet present
|
2011-09-02 10:28:14 -04:00
|
|
|
on the page. Capybara automatically deals with this by waiting for elements
|
2010-01-01 13:58:10 -05:00
|
|
|
to appear on the page.
|
|
|
|
|
|
|
|
When issuing instructions to the DSL such as:
|
|
|
|
|
|
|
|
click_link('foo')
|
|
|
|
click_link('bar')
|
2011-10-16 19:46:19 -04:00
|
|
|
page.should have_text('baz')
|
2010-01-01 13:58:10 -05:00
|
|
|
|
2011-09-02 10:28:14 -04:00
|
|
|
If clicking on the *foo* link triggers an asynchronous process, such as
|
2011-03-14 10:27:46 -04:00
|
|
|
an Ajax request, which, when complete will add the *bar* link to the page,
|
2011-08-25 18:16:11 -04:00
|
|
|
clicking on the *bar* link would be expected to fail, since that link doesn't
|
2010-01-01 13:58:10 -05:00
|
|
|
exist yet. However Capybara is smart enought to retry finding the link for a
|
|
|
|
brief period of time before giving up and throwing an error. The same is true of
|
|
|
|
the next line, which looks for the content *baz* on the page; it will retry
|
|
|
|
looking for that content for a brief time. You can adjust how long this period
|
|
|
|
is (the default is 2 seconds):
|
|
|
|
|
|
|
|
Capybara.default_wait_time = 5
|
|
|
|
|
2011-09-08 22:07:52 -04:00
|
|
|
Be aware that because of this behaviour, the following two statements are *not*
|
|
|
|
equivalent, and you should *always* use the latter!
|
|
|
|
|
|
|
|
!page.has_xpath?('a')
|
|
|
|
page.has_no_xpath?('a')
|
|
|
|
|
|
|
|
The former would immediately fail because the content has not yet been removed.
|
|
|
|
Only the latter would wait for the asynchronous process to remove the content
|
|
|
|
from the page.
|
|
|
|
|
|
|
|
Capybara's Rspec matchers, however, are smart enough to handle either form.
|
|
|
|
The two following statements are functionally equivalent:
|
|
|
|
|
|
|
|
page.should_not have_xpath('a')
|
|
|
|
page.should have_no_xpath('a')
|
|
|
|
|
2011-09-02 10:28:14 -04:00
|
|
|
Capybara's waiting behaviour is quite advanced, and can deal with situations
|
|
|
|
such as the following line of code:
|
|
|
|
|
2011-10-16 19:46:19 -04:00
|
|
|
find('#sidebar').find('h1').should have_text('Something')
|
2011-09-02 10:28:14 -04:00
|
|
|
|
|
|
|
Even if JavaScript causes <tt>#sidebar</tt> to disappear off the page, Capybara
|
|
|
|
will automatically reload it and any elements it contains. So if an AJAX
|
|
|
|
request causes the contents of <tt>#sidebar</tt> to change, which would update
|
|
|
|
the text of the <tt>h1</tt> to "Something", and this happened, this test would
|
|
|
|
pass. If you do not want this behaviour, you can set
|
|
|
|
<tt>Capybara.automatic_reload</tt> to <tt>false</tt>.
|
|
|
|
|
2011-02-18 19:18:57 -05:00
|
|
|
== Using the DSL in unsupported testing frameworks
|
2009-11-18 18:00:10 -05:00
|
|
|
|
2011-12-08 14:59:17 -05:00
|
|
|
You can mix the DSL into any context by including <tt>Capybara::DSL</tt>:
|
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
|
|
|
|
2011-12-19 16:42:08 -05:00
|
|
|
Capybara.default_driver = :webkit
|
2009-11-14 20:13:07 -05:00
|
|
|
|
2010-04-09 09:17:47 -04:00
|
|
|
module MyModule
|
2011-04-11 01:58:42 -04:00
|
|
|
include Capybara::DSL
|
2010-04-09 09:17:47 -04:00
|
|
|
|
|
|
|
def login!
|
|
|
|
within("//form[@id='session']") do
|
|
|
|
fill_in 'Login', :with => 'user@example.com'
|
|
|
|
fill_in 'Password', :with => 'password'
|
|
|
|
end
|
|
|
|
click_link 'Sign in'
|
|
|
|
end
|
2009-11-14 20:13:07 -05:00
|
|
|
end
|
|
|
|
|
2010-02-03 07:18:20 -05:00
|
|
|
== Calling remote servers
|
|
|
|
|
2010-10-06 15:07:47 -04:00
|
|
|
Normally Capybara expects to be testing an in-process Rack application, but you
|
|
|
|
can also use it to talk to a web server running anywhere on the internets, by
|
|
|
|
setting app_host:
|
2010-02-03 07:18:20 -05:00
|
|
|
|
|
|
|
Capybara.current_driver = :selenium
|
|
|
|
Capybara.app_host = 'http://www.google.com'
|
2010-09-16 15:48:03 -04:00
|
|
|
...
|
2010-02-03 07:18:20 -05:00
|
|
|
visit('/')
|
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
Note that the default driver (<tt>:rack_test</tt>) does not support running
|
|
|
|
against a remote server. With drivers that support it, you can also visit any
|
|
|
|
URL directly:
|
2010-02-05 13:54:56 -05:00
|
|
|
|
|
|
|
visit('http://www.google.com')
|
|
|
|
|
2010-10-06 15:07:47 -04:00
|
|
|
By default Capybara will try to boot a rack application automatically. You
|
|
|
|
might want to switch off Capybara's rack server if you are running against a
|
|
|
|
remote application:
|
2010-02-05 13:54:56 -05:00
|
|
|
|
|
|
|
Capybara.run_server = false
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
== Using the sessions manually
|
2009-11-14 20:13:07 -05:00
|
|
|
|
2011-03-14 10:27:46 -04:00
|
|
|
For ultimate control, you can instantiate and use a
|
|
|
|
{Session}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/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
|
|
|
|
2011-12-19 16:42:08 -05:00
|
|
|
session = Capybara::Session.new(:webkit, my_rack_app)
|
2009-11-18 18:00:10 -05:00
|
|
|
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
|
|
|
|
2010-08-27 15:07:26 -04:00
|
|
|
== XPath, CSS and selectors
|
2009-11-26 17:48:18 -05:00
|
|
|
|
|
|
|
Capybara does not try to guess what kind of selector you are going to give it,
|
2011-02-04 09:06:30 -05:00
|
|
|
and will always use CSS by default. If you want to use XPath, you'll need to
|
|
|
|
do:
|
2009-12-12 07:37:29 -05:00
|
|
|
|
2010-07-16 16:21:35 -04:00
|
|
|
within(:xpath, '//ul/li') { ... }
|
|
|
|
find(:xpath, '//ul/li').text
|
2010-07-19 14:18:16 -04:00
|
|
|
find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value
|
2009-11-26 17:48:18 -05:00
|
|
|
|
2010-07-16 16:21:35 -04:00
|
|
|
Alternatively you can set the default selector to XPath:
|
2009-11-26 17:48:18 -05:00
|
|
|
|
2010-07-16 16:21:35 -04:00
|
|
|
Capybara.default_selector = :xpath
|
|
|
|
find('//ul/li').text
|
2009-11-26 17:48:18 -05:00
|
|
|
|
2010-08-27 15:07:26 -04:00
|
|
|
Capybara allows you to add custom selectors, which can be very useful if you
|
|
|
|
find yourself using the same kinds of selectors very often:
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
Capybara.add_selector(:id) do
|
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:row) do
|
|
|
|
xpath { |num| ".//tbody/tr[#{num}]" }
|
|
|
|
end
|
2010-08-27 15:07:26 -04:00
|
|
|
|
2011-09-29 16:04:51 -04:00
|
|
|
Capybara.add_selector(:flash_type) do
|
|
|
|
css { |type| "#flash.#{type}" }
|
|
|
|
end
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
The block given to xpath must always return an XPath expression as a String, or
|
|
|
|
an XPath expression generated through the XPath gem. You can now use these
|
|
|
|
selectors like this:
|
2010-08-27 15:07:26 -04:00
|
|
|
|
|
|
|
find(:id, 'post_123')
|
|
|
|
find(:row, 3)
|
2011-09-29 16:04:51 -04:00
|
|
|
find(:flash_type, :notice)
|
2010-08-27 15:07:26 -04:00
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
You can specify an optional match option which will automatically use the
|
|
|
|
selector if it matches the argument:
|
2010-08-27 15:07:26 -04:00
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
Capybara.add_selector(:id) do
|
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
|
|
|
match { |value| value.is_a?(Symbol) }
|
|
|
|
end
|
2010-08-27 15:07:26 -04:00
|
|
|
|
|
|
|
Now use it like this:
|
|
|
|
|
|
|
|
find(:post_123)
|
|
|
|
|
|
|
|
This :id selector is already built into Capybara by default, so you don't
|
|
|
|
need to add it yourself.
|
|
|
|
|
2010-07-10 11:15:57 -04:00
|
|
|
== Beware the XPath // trap
|
|
|
|
|
|
|
|
In XPath the expression // means something very specific, and it might not be what
|
|
|
|
you think. Contrary to common belief, // means "anywhere in the document" not "anywhere
|
|
|
|
in the current context". As an example:
|
|
|
|
|
2010-08-27 15:07:26 -04:00
|
|
|
page.find(:xpath, '//body').all(:xpath, '//script')
|
2010-07-10 11:15:57 -04:00
|
|
|
|
|
|
|
You might expect this to find all script tags in the body, but actually, it finds all
|
|
|
|
script tags in the entire document, not only those in the body! What you're looking
|
|
|
|
for is the .// expression which means "any descendant of the current node":
|
|
|
|
|
2010-08-27 15:07:26 -04:00
|
|
|
page.find(:xpath, '//body').all(:xpath, './/script')
|
2010-07-10 11:15:57 -04:00
|
|
|
|
2010-07-11 07:10:57 -04:00
|
|
|
The same thing goes for within:
|
|
|
|
|
2010-08-27 15:07:26 -04:00
|
|
|
within(:xpath, '//body') do
|
|
|
|
page.find(:xpath, './/script')
|
|
|
|
within(:xpath, './/table/tbody') do
|
|
|
|
...
|
|
|
|
end
|
2010-07-11 07:10:57 -04:00
|
|
|
end
|
|
|
|
|
2010-09-16 15:55:57 -04:00
|
|
|
== Configuring and adding drivers
|
|
|
|
|
|
|
|
Capybara makes it convenient to switch between different drivers. It also exposes
|
|
|
|
an API to tweak those drivers with whatever settings you want, or to add your own
|
|
|
|
drivers. This is how to switch the selenium driver to use chrome:
|
|
|
|
|
|
|
|
Capybara.register_driver :selenium do |app|
|
2011-04-25 05:36:15 -04:00
|
|
|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
|
2010-09-16 15:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
However, it's also possible to give this a different name, so tests can switch
|
|
|
|
between using different browsers effortlessly:
|
|
|
|
|
|
|
|
Capybara.register_driver :selenium_chrome do |app|
|
2011-04-25 05:36:15 -04:00
|
|
|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
|
2010-09-16 15:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Whatever is returned from the block should conform to the API described by
|
|
|
|
Capybara::Driver::Base, it does not however have to inherit from this class.
|
|
|
|
Gems can use this API to add their own drivers to Capybara.
|
|
|
|
|
2011-01-07 10:44:21 -05:00
|
|
|
The {Selenium wiki}[http://code.google.com/p/selenium/wiki/RubyBindings] has
|
|
|
|
additional info about how the underlying driver can be configured.
|
|
|
|
|
2009-11-19 17:14:34 -05:00
|
|
|
== Gotchas:
|
2009-11-14 20:13:07 -05:00
|
|
|
|
2010-07-16 16:21:35 -04:00
|
|
|
* Access to session and request is not possible from the test, Access to
|
|
|
|
response is limited. Some drivers allow access to response headers and HTTP
|
|
|
|
status code, but this kind of functionality is not provided by some drivers,
|
|
|
|
such as Selenium.
|
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
|
|
|
|
2010-07-17 07:21:23 -04:00
|
|
|
* Freezing time: It's common practice to mock out the Time so that features
|
|
|
|
that depend on the current Date work as expected. This can be problematic,
|
2011-03-14 10:27:46 -04:00
|
|
|
since Capybara's Ajax timing uses the system time, resulting in Capybara
|
2010-07-17 07:21:23 -04:00
|
|
|
never timing out and just hanging when a failure occurs. It's still possible to
|
|
|
|
use plugins which allow you to travel in time, rather than freeze time.
|
|
|
|
One such plugin is {Timecop}[http://github.com/jtrupiano/timecop].
|
|
|
|
|
2010-11-22 05:53:53 -05:00
|
|
|
* When using Rack::Test, beware if attempting to visit absolute URLs. For
|
|
|
|
example, a session might not be shared between visits to <tt>posts_path</tt>
|
|
|
|
and <tt>posts_url</tt>. If testing an absolute URL in an Action Mailer email,
|
|
|
|
set <tt>default_url_options</tt> to match the Rails default of
|
|
|
|
<tt>www.example.com</tt>.
|
|
|
|
|
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.
|