Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 695 lines (492 sloc) 25.554 kb
afb7dc7 @jnicklas It's spelled with a capital C
authored
1 = Capybara
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
2
6b099a4 renamed to capybara
Theo Hultberg and Jonas Nicklas authored
3 * http://github.com/jnicklas/capybara
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
4
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
5 == Description:
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
6
586ce21 @jnicklas Vastly improved README
authored
7 Capybara aims to simplify the process of integration testing Rack applications,
657b9c5 @jnicklas Some updates to README
authored
8 such as Rails, Sinatra or Merb. Capybara simulates how a real user would
9 interact with a web application. It is agnostic about the driver running your
a0e5811 @joliss README and History housekeeping
joliss authored
10 tests and currently comes with Rack::Test and Selenium support built in.
11 HtmlUnit and env.js are supported through external gems.
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
12
f5f444b @joliss Improve README
joliss authored
13 A complete reference is available at
14 {at rubydoc.info}[http://rubydoc.info/github/jnicklas/capybara/master].
8dc27c2 @jnicklas Added note about rdoc.info to README
authored
15
988d4c9 @jnicklas Moved up installation instructions
authored
16 == Install:
17
aec11e2 @jnicklas add note about libffi to readme
authored
18 Install as a gem:
988d4c9 @jnicklas Moved up installation instructions
authored
19
20 sudo gem install capybara
21
ecdc2bd @jnicklas visit now allow fully qualified URLs.
authored
22 On OSX you may have to install libffi, you can install it via MacPorts with:
aec11e2 @jnicklas add note about libffi to readme
authored
23
24 sudo port install libffi
25
27ee5af @jnicklas added note about development to README
authored
26 == Development:
27
28 * Source hosted at {GitHub}[http://github.com/jnicklas/capybara].
e1091da @joliss Make it clearer that general problems should go to the mailing list, not...
joliss authored
29 * Please direct questions, discussion or problems to the {mailing list}[http://groups.google.com/group/ruby-capybara].
30 * If you found a reproducible bug, open a {GitHub Issue}[http://github.com/jnicklas/capybara/issues] to submit a bug report.
38c1cbb @jnicklas Note about submitting specs with patches
authored
31
e1091da @joliss Make it clearer that general problems should go to the mailing list, not...
joliss authored
32 Pull requests are very welcome (and even better than bug reports)! Make sure
33 your patches are well tested, Capybara is a testing tool after all. Please
34 create a topic branch for every separate change you make.
27ee5af @jnicklas added note about development to README
authored
35
c6edcf7 @jnicklas Use bundler in development.
authored
36 Capybara uses bundler in development. To set up a development environment, simply do:
37
f5f444b @joliss Improve README
joliss authored
38 git submodule update --init
c6edcf7 @jnicklas Use bundler in development.
authored
39 gem install bundler --pre
40 bundle install
41
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
42 == Using Capybara with Cucumber
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
43
657b9c5 @jnicklas Some updates to README
authored
44 Capybara is built to work nicely with Cucumber. Support for Capybara is built into
45 cucumber-rails. In your Rails app, just run:
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
46
657b9c5 @jnicklas Some updates to README
authored
47 rails generate cucumber:install --capybara
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
48
6ce66ed @jnicklas Updated setup instructions for cuke-rails 0.2
authored
49 And everything should be set up and ready to go.
586ce21 @jnicklas Vastly improved README
authored
50
6ce66ed @jnicklas Updated setup instructions for cuke-rails 0.2
authored
51 If you want to use Capybara with Cucumber outside Rails (for example with Merb
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
52 or Sinatra), you'll need to require Capybara and set the Rack app manually:
586ce21 @jnicklas Vastly improved README
authored
53
54 require 'capybara/cucumber'
55 Capybara.app = MyRackApp
56
57 Now you can use it in your steps:
58
59 When /I sign in/ do
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
60 within("#session") do
586ce21 @jnicklas Vastly improved README
authored
61 fill_in 'Login', :with => 'user@example.com'
62 fill_in 'Password', :with => 'password'
63 end
64 click_link 'Sign in'
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
65 end
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
66
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
67 Capybara sets up some {tags}[http://wiki.github.com/aslakhellesoy/cucumber/tags]
68 for you to use in Cucumber. Often you'll want to run only some scenarios with a
69 driver that supports JavaScript, Capybara makes this easy: simply tag the
70 scenario (or feature) with <tt>@javascript</tt>:
71
72 @javascript
a0e5811 @joliss README and History housekeeping
joliss authored
73 Scenario: do something Ajaxy
74 When I click the Ajax link
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
75 ...
76
77 You can change which driver Capybara uses for JavaScript:
78
79 Capybara.javascript_driver = :culerity
80
81 There are also explicit <tt>@selenium</tt>, <tt>@culerity</tt> and
82 <tt>@rack_test</tt> tags set up for you.
83
84 == Using Capybara with RSpec
85
0466f37 @joliss Improve documentation on RSpec, Rails, and Rack in README.
joliss authored
86 If you prefer RSpec to using Cucumber, you can use the built in RSpec support
87 by adding the following line (typically to your <tt>spec_helper.rb</tt> file):
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
88
89 require 'capybara/rspec'
90
095a118 @mcmire Clear things up in README again as :type => :request specs become Rails ...
mcmire authored
91 You can now write your specs like so:
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
92
558e7b2 @joliss Use the :request type for RSpec example groups
joliss authored
93 describe "the signup process", :type => :request do
0befc0e @mcmire Modify README to clarify difference between spec/acceptance and spec/req...
mcmire authored
94 before :each do
95 User.make(:email => 'user@example.com', :password => 'caplin')
96 end
a00435d @jnicklas Added :headers option to rack_test driver, closes #328
authored
97
bdde00a @jnicklas Only mix in Capybara into :type => :acceptance examples
authored
98 it "signs me in" do
99 within("#session") do
100 fill_in 'Login', :with => 'user@example.com'
101 fill_in 'Password', :with => 'password'
102 end
103 click_link 'Sign in'
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
104 end
105 end
106
095a118 @mcmire Clear things up in README again as :type => :request specs become Rails ...
mcmire authored
107 Capybara is only included in example groups tagged with
108 <tt>:type => :request</tt> (or <tt>:acceptance</tt> for compatibility with Steak).
558e7b2 @joliss Use the :request type for RSpec example groups
joliss authored
109
095a118 @mcmire Clear things up in README again as :type => :request specs become Rails ...
mcmire authored
110 If you are testing a Rails app and using the <tt>rspec-rails</tt> gem, these
111 <tt>:request</tt> example groups may look familiar to you. That's because they
112 are RSpec versions of Rails integration tests. So, in this case essentially what you are getting are Capybara-enhanced request specs. This means that you can
113 use the Capybara helpers <i>and</i> you have access to things like named route
114 helpers in your tests (so you are able to say, for instance, <tt>visit
115 edit_user_path(user)</tt>, instead of <tt>visit "/users/#{user.id}/edit"</tt>,
116 if you prefer that sort of thing). A good place to put these specs is
117 <tt>spec/requests</tt>, as <tt>rspec-rails</tt> will automatically tag them with
118 <tt>:type => :request</tt>. (In fact, <tt>spec/integration</tt> and
119 <tt>spec/acceptance</tt> will work just as well.)
120
121 <tt>rspec-rails</tt> will also automatically include Capybara in <tt>:controller</tt> and <tt>:mailer</tt> example groups.
bdde00a @jnicklas Only mix in Capybara into :type => :acceptance examples
authored
122
123 RSpec's metadata feature can be used to switch to a different driver. Use
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
124 <tt>:js => true</tt> to switch to the javascript driver, or provide a
125 <tt>:driver</tt> option to switch to one specific driver. For example:
126
127 describe 'some stuff which requires js', :js => true do
128 it 'will use the default js driver'
129 it 'will switch to one specific driver', :driver => :celerity
130 end
131
095a118 @mcmire Clear things up in README again as :type => :request specs become Rails ...
mcmire authored
132 Finally, Capybara also comes with a built in DSL for creating descriptive acceptance tests:
f4897f8 Added DSL for acceptance tests in RSpec
Jonas Nicklas and Nicklas Ramhöj authored
133
0befc0e @mcmire Modify README to clarify difference between spec/acceptance and spec/req...
mcmire authored
134 feature "Signing up" do
f4897f8 Added DSL for acceptance tests in RSpec
Jonas Nicklas and Nicklas Ramhöj authored
135 background do
136 User.make(:email => 'user@example.com', :password => 'caplin')
137 end
138
0befc0e @mcmire Modify README to clarify difference between spec/acceptance and spec/req...
mcmire authored
139 scenario "Signing in with correct credentials" do
f4897f8 Added DSL for acceptance tests in RSpec
Jonas Nicklas and Nicklas Ramhöj authored
140 within("#session") do
141 fill_in 'Login', :with => 'user@example.com'
142 fill_in 'Password', :with => 'caplin'
143 end
144 click_link 'Sign in'
145 end
146 end
147
095a118 @mcmire Clear things up in README again as :type => :request specs become Rails ...
mcmire authored
148 This is, in fact, just a shortcut for making a request spec, where
149 <tt>feature</tt> is an alias for <tt>describe ..., :type => :request</tt>,
150 <tt>background</tt> is an alias for <tt>before</tt>, and <tt>scenario</tt>
151 is an alias for <tt>it</tt>/<tt>specify</tt>.
0befc0e @mcmire Modify README to clarify difference between spec/acceptance and spec/req...
mcmire authored
152
aa46894 Add RSpec support in Capybara itself, closes #187
Anders Törnqvist authored
153 Note that Capybara's built in RSpec support only works with RSpec 2.0 or later.
154 You'll need to roll your own for earlier versions of RSpec.
155
112be58 @joliss added documentation for Test::Unit integration
joliss authored
156 == Using Capybara with Test::Unit
157
158 To use Capybara with Test::Unit, include the DSL (<tt>include Capybara</tt> up
159 until version 0.4.x, <tt>include Capybara::DSL</tt> for newer versions) in
160 whatever test class you are using. For example, if your classes derive from
161 <tt>ActionDispatch::IntegrationTest</tt>, use
162
163 class ActionDispatch::IntegrationTest
164 include Capybara::DSL
165 end
166
1b44e0b @joliss added documentation on driver switching with Test::Unit
joliss authored
167 Test::Unit does not support selecting the driver through test metadata, but you
168 can switch the driver for specific classes using the <tt>setup</tt> and
169 <tt>teardown</tt> methods. See the section "Selecting the Driver".
170
0466f37 @joliss Improve documentation on RSpec, Rails, and Rack in README.
joliss authored
171 == Using Capybara with Ruby on Rails
172
112be58 @joliss added documentation for Test::Unit integration
joliss authored
173 If you are using the Rails framework, add this line to automatically configure
174 Capybara to test against your Rails application:
0466f37 @joliss Improve documentation on RSpec, Rails, and Rack in README.
joliss authored
175
f5f444b @joliss Improve README
joliss authored
176 require 'capybara/rails'
0466f37 @joliss Improve documentation on RSpec, Rails, and Rack in README.
joliss authored
177
178 == Using Capybara with Rack
179
180 If you're using Capybara with a non-Rails Rack application, set
181 <tt>Capybara.app</tt> to your application class:
182
183 Capybara.app = MyRackApp
184
1fa8aee @joliss Better documentation for drivers
joliss authored
185 == Drivers
586ce21 @jnicklas Vastly improved README
authored
186
1fa8aee @joliss Better documentation for drivers
joliss authored
187 Capybara uses the same DSL to drive a variety of browser and headless drivers.
188
189 === Selecting the Driver
190
a0e5811 @joliss README and History housekeeping
joliss authored
191 By default, Capybara uses the <tt>:rack_test</tt> driver, which is fast but does not
1fa8aee @joliss Better documentation for drivers
joliss authored
192 support JavaScript. You can set up a different default driver for your
193 features. For example if you'd prefer to run everything in Selenium, you could
194 do:
586ce21 @jnicklas Vastly improved README
authored
195
196 Capybara.default_driver = :selenium
197
1fa8aee @joliss Better documentation for drivers
joliss authored
198 However, if you are using RSpec or Cucumber, you may instead want to consider
a0e5811 @joliss README and History housekeeping
joliss authored
199 leaving the faster <tt>:rack_test</tt> as the +default_driver+, and marking only those
1fa8aee @joliss Better documentation for drivers
joliss authored
200 tests that require a JavaScript-capable driver using <tt>:js => true</tt> or
201 <tt>@javascript</tt>, respectively. By default, JavaScript tests are run using the
a0e5811 @joliss README and History housekeeping
joliss authored
202 <tt>:selenium</tt> driver. You can change this by setting
203 <tt>Capybara.javascript_driver</tt>.
1fa8aee @joliss Better documentation for drivers
joliss authored
204
1b44e0b @joliss added documentation on driver switching with Test::Unit
joliss authored
205 You can also change the driver temporarily (typically in the Before/setup and
206 After/teardown blocks):
586ce21 @jnicklas Vastly improved README
authored
207
49a9f17 @joliss Better documentation for temporary driver switching
joliss authored
208 Capybara.current_driver = :culerity # temporarily select different driver
209 ... tests ...
210 Capybara.use_default_driver # switch back to default driver
586ce21 @jnicklas Vastly improved README
authored
211
1fa8aee @joliss Better documentation for drivers
joliss authored
212 Note that switching the driver creates a new session, so you may not be able to
213 switch in the middle of a test.
cb809f9 Slight README improvements
CJ Kihlbom and Jonas Nicklas authored
214
a00435d @jnicklas Added :headers option to rack_test driver, closes #328
authored
215 === RackTest
216
217 RackTest is Capybara's default driver. It is written in pure Ruby and does not
218 have any support for executing JavaScript. Since the RackTest driver works
219 directly agains the Rack interface, it does not need any server to be started,
220 it can work directly work against any Rack app. This means that if your
221 application is not a Rack application (Rails, Sinatra and most other Ruby
222 frameworks are Rack applications) then you cannot use this driver. You cannot
223 use the RackTest driver to test a remote application.
224 {capybara-mechanize}[https://github.com/jeroenvandijk/capybara-mechanize]
225 intends to provide a similar driver which works against remote servers, it is a
226 separate project.
227
228 RackTest can be configured with a set of headers like this:
229
230 Capybara.register_driver :rack_test do |app|
231 Capybara::RackTest::Driver.new(app, :browser => :chrome)
232 end
233
234 See the section on adding and configuring drivers.
235
1fa8aee @joliss Better documentation for drivers
joliss authored
236 === Selenium
586ce21 @jnicklas Vastly improved README
authored
237
1fa8aee @joliss Better documentation for drivers
joliss authored
238 At the moment, Capybara supports {Selenium 2.0
239 (Webdriver)}[http://seleniumhq.org/docs/01_introducing_selenium.html#selenium-2-aka-selenium-webdriver],
240 *not* Selenium RC. Provided Firefox is installed, everything is set up for you,
241 and you should be able to start using Selenium right away.
586ce21 @jnicklas Vastly improved README
authored
242
a0e5811 @joliss README and History housekeeping
joliss authored
243 By default Capybara tries to synchronize Ajax requests, so it will wait for
244 Ajax requests to finish after you've interacted with the page. You can switch
021b87f Selenium driver automatically waits for AJAX requests to finish
Jonas Nicklas and Nicklas Ramhöj authored
245 off this behaviour by setting the driver option <tt>:resynchronize</tt> to
246 <tt>false</tt>. See the section on configuring drivers.
247
b7a460c @joliss added some preliminary documentation about transactional fixtures
joliss authored
248 Note: Selenium does not support transactional fixtures; see the section
249 "Transactional Fixtures" below.
250
a0e5811 @joliss README and History housekeeping
joliss authored
251 === HtmlUnit
1fa8aee @joliss Better documentation for drivers
joliss authored
252
a0e5811 @joliss README and History housekeeping
joliss authored
253 There are three different drivers, maintained as external gems, that you can
254 use to drive {HtmlUnit}[http://htmlunit.sourceforge.net/]:
1fa8aee @joliss Better documentation for drivers
joliss authored
255
a0e5811 @joliss README and History housekeeping
joliss authored
256 * {Akephalos}[https://github.com/bernerdschaefer/akephalos] might be the best
257 HtmlUnit driver right now.
586ce21 @jnicklas Vastly improved README
authored
258
a0e5811 @joliss README and History housekeeping
joliss authored
259 * {Celerity}[https://github.com/sobrinho/capybara-celerity] only runs on JRuby,
260 so you'll need to install the celerity gem under JRuby: <tt>jruby -S gem
261 install celerity</tt>
586ce21 @jnicklas Vastly improved README
authored
262
a0e5811 @joliss README and History housekeeping
joliss authored
263 * {Culerity}[https://github.com/sobrinho/capybara-culerity]: Install celerity
264 as noted above, and make sure that JRuby is in your path. Note that Culerity
265 does not seem to be working under Ruby 1.9 at the moment.
586ce21 @jnicklas Vastly improved README
authored
266
b7a460c @joliss added some preliminary documentation about transactional fixtures
joliss authored
267 Note: HtmlUnit does not support transactional fixtures; see the section
268 "Transactional Fixtures" below.
269
1fa8aee @joliss Better documentation for drivers
joliss authored
270 === env.js
bcbfb45 @smparkes envjs blurb
smparkes authored
271
272 The {capybara-envjs driver}[http://github.com/smparkes/capybara-envjs]
273 uses the envjs gem ({GitHub}[http://github.com/smparkes/env-js],
274 {rubygems.org}[http://rubygems.org/gems/envjs]) to interpret
275 JavaScript outside the browser. The driver is installed by installing the capybara-envjs gem:
322ecd3 @jnicklas Explain the difference between find and locate
authored
276
bcbfb45 @smparkes envjs blurb
smparkes authored
277 gem install capybara-envjs
322ecd3 @jnicklas Explain the difference between find and locate
authored
278
bcbfb45 @smparkes envjs blurb
smparkes authored
279 More info about the driver and env.js are available through the links above. The envjs gem only supports
280 Ruby 1.8.7 at this time.
281
b7a460c @joliss added some preliminary documentation about transactional fixtures
joliss authored
282 Note: Envjs does not support transactional fixtures; see the section
283 "Transactional Fixtures" below.
284
7452145 @joliss Removed #dsl anchor from README (does not work with RDoc 1.8 / GitHub)
joliss authored
285 == The DSL
586ce21 @jnicklas Vastly improved README
authored
286
f5f444b @joliss Improve README
joliss authored
287 Capybara's DSL (domain-specific language) is inspired by Webrat. While
288 backwards compatibility is retained in a lot of cases, there are certain
289 important differences. Unlike in Webrat, all searches in Capybara are *case
290 sensitive*. This is because Capybara heavily uses XPath, which doesn't support
291 case insensitivity.
fb71b29 @denro added documentation on the evaluate_script method
denro authored
292
2f8c5e9 @jnicklas Improved README
authored
293 === Navigating
294
f5f444b @joliss Improve README
joliss authored
295 You can use the
dae0ad8 @joliss Added Session#html as alias for Session#body, and some documentation
joliss authored
296 <tt>{visit}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#visit-instance_method]</tt>
f5f444b @joliss Improve README
joliss authored
297 method to navigate to other pages:
2f8c5e9 @jnicklas Improved README
authored
298
299 visit('/projects')
300 visit(post_comments_path(post))
301
302 The visit method only takes a single parameter, the request method is *always*
303 GET.
304
f5f444b @joliss Improve README
joliss authored
305 You can get the {current
306 path}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#current_path-instance_method]
307 of the browsing session for test assertions:
db16a19 @paulelliott Add current_path to dsl
paulelliott authored
308
309 current_path.should == post_comments_path(post)
310
2f8c5e9 @jnicklas Improved README
authored
311 === Clicking links and buttons
312
a0e5811 @joliss README and History housekeeping
joliss authored
313 <em>Full reference: {Capybara::Node::Actions}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions]</em>
314
2f8c5e9 @jnicklas Improved README
authored
315 You can interact with the webapp by following links and buttons. Capybara
316 automatically follows any redirects, and submits forms associated with buttons.
317
318 click_link('id-of-link')
319 click_link('Link Text')
320 click_button('Save')
f5f444b @joliss Improve README
joliss authored
321 click_on('Link Text') # clicks on either links or buttons
322 click_on('Button Value')
2f8c5e9 @jnicklas Improved README
authored
323
324 === Interacting with forms
325
a0e5811 @joliss README and History housekeeping
joliss authored
326 <em>Full reference: {Capybara::Node::Actions}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions]</em>
327
328 There are a number of tools for interacting with form elements:
2f8c5e9 @jnicklas Improved README
authored
329
330 fill_in('First Name', :with => 'John')
331 fill_in('Password', :with => 'Seekrit')
a0e5811 @joliss README and History housekeeping
joliss authored
332 fill_in('Description', :with => 'Really Long Text...')
91ed4cd @dvrensk Documentation clarification and un-typo-ing.
dvrensk authored
333 choose('A Radio Button')
2f8c5e9 @jnicklas Improved README
authored
334 check('A Checkbox')
335 uncheck('A Checkbox')
336 attach_file('Image', '/path/to/image.jpg')
337 select('Option', :from => 'Select Box')
338
339 === Querying
340
a0e5811 @joliss README and History housekeeping
joliss authored
341 <em>Full reference: {Capybara::Node::Matchers}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers]</em>
342
2f8c5e9 @jnicklas Improved README
authored
343 Capybara has a rich set of options for querying the page for the existence of
a0e5811 @joliss README and History housekeeping
joliss authored
344 certain elements, and working with and manipulating those elements.
2f8c5e9 @jnicklas Improved README
authored
345
520a42a @jnicklas Added has_selector? method
authored
346 page.has_selector?('table tr')
347 page.has_selector?(:xpath, '//table/tr')
348 page.has_no_selector?(:content)
349
2f8c5e9 @jnicklas Improved README
authored
350 page.has_xpath?('//table/tr')
351 page.has_css?('table tr.foo')
352 page.has_content?('foo')
4305776 Update README adding explaination for using app_host to call remote web ...
Joseph Wilk and Matt Wynne authored
353
0466f37 @joliss Improve documentation on RSpec, Rails, and Rack in README.
joliss authored
354 You can use these with RSpec's magic matchers:
2f8c5e9 @jnicklas Improved README
authored
355
520a42a @jnicklas Added has_selector? method
authored
356 page.should have_selector('table tr')
357 page.should have_selector(:xpath, '//table/tr')
358 page.should have_no_selector(:content)
359
2f8c5e9 @jnicklas Improved README
authored
360 page.should have_xpath('//table/tr')
361 page.should have_css('table tr.foo')
362 page.should have_content('foo')
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
363 page.should have_no_content('foo')
364
365 Note that <tt>page.should have_no_xpath</tt> is preferred over
366 <tt>page.should_not have_xpath</tt>. Read the section on asynchronous JavaScript
367 for an explanation.
2f8c5e9 @jnicklas Improved README
authored
368
dae0ad8 @joliss Added Session#html as alias for Session#body, and some documentation
joliss authored
369 If all else fails, you can also use the
370 <tt>{page.html}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#html-instance_method]</tt>
371 method to test against the raw HTML:
372
373 page.html.should match /<span>.../i
374
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
375 === Finding
376
a0e5811 @joliss README and History housekeeping
joliss authored
377 <em>Full reference: {Capybara::Node::Finders}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders]</em>
378
379 You can also find specific elements, in order to manipulate them:
2f8c5e9 @jnicklas Improved README
authored
380
381 find_field('First Name').value
382 find_link('Hello').visible?
383 find_button('Send').click
4305776 Update README adding explaination for using app_host to call remote web ...
Joseph Wilk and Matt Wynne authored
384
3070441 @jnicklas Find and locate now do the same thing
authored
385 find(:xpath, "//table/tr").click
386 find("#overlay").find("h1").click
2f8c5e9 @jnicklas Improved README
authored
387 all('a').each { |a| a[:href] }
388
3070441 @jnicklas Find and locate now do the same thing
authored
389 Note that <tt>find</tt> will wait for an element to appear on the page, as explained in the
a0e5811 @joliss README and History housekeeping
joliss authored
390 Ajax section. If the element does not appear it will raise an error.
322ecd3 @jnicklas Explain the difference between find and locate
authored
391
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
392 These elements all have all the Capybara DSL methods available, so you can restrict them
393 to specific parts of the page:
394
3070441 @jnicklas Find and locate now do the same thing
authored
395 find('#navigation').click_link('Home')
396 find('#navigation').should have_button('Sign out')
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
397
398 === Scoping
399
400 Capybara makes it possible to restrict certain actions, such as interacting with
401 forms or clicking links and buttons, to within a specific area of the page. For
f5f444b @joliss Improve README
joliss authored
402 this purpose you can use the generic
dae0ad8 @joliss Added Session#html as alias for Session#body, and some documentation
joliss authored
403 <tt>{within}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#within-instance_method]</tt>
f5f444b @joliss Improve README
joliss authored
404 method. Optionally you can specify which kind of selector to use.
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
405
657b9c5 @jnicklas Some updates to README
authored
406 within("li#employee") do
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
407 fill_in 'Name', :with => 'Jimmy'
408 end
409
657b9c5 @jnicklas Some updates to README
authored
410 within(:xpath, "//li[@id='employee']") do
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
411 fill_in 'Name', :with => 'Jimmy'
412 end
413
e92ef27 @joliss Clarify in README that "within" scopes to the first element
joliss authored
414 Note that <tt>within</tt> will scope the actions to the _first_ (not _any_)
415 element that matches the selector.
416
970fd49 @jnicklas Explain new scoping behaviour in readme
authored
417 There are special methods for restricting the scope to a specific fieldset,
418 identified by either an id or the text of the fieldet's legend tag, and to a
419 specific table, identified by either id or text of the table's caption tag.
420
421 within_fieldset('Employee') do
422 fill_in 'Name', :with => 'Jimmy'
423 end
424
425 within_table('Employee') do
426 fill_in 'Name', :with => 'Jimmy'
427 end
428
2f8c5e9 @jnicklas Improved README
authored
429 === Scripting
430
7458357 @jnicklas Added separate execute_script method, closes #71
authored
431 In drivers which support it, you can easily execute JavaScript:
432
433 page.execute_script("$('body').empty()")
434
435 For simple expressions, you can return the result of the script. Note
436 that this may break with more complicated expressions:
2f8c5e9 @jnicklas Improved README
authored
437
438 result = page.evaluate_script('4 + 4');
439
440 === Debugging
441
442 It can be useful to take a snapshot of the page as it currently is and take a
443 look at it:
586ce21 @jnicklas Vastly improved README
authored
444
445 save_and_open_page
446
b7a460c @joliss added some preliminary documentation about transactional fixtures
joliss authored
447 == Transactional fixtures
448
449 Transactional fixtures only work in the default Rack::Test driver, but not for
450 other drivers like Selenium. Cucumber takes care of this automatically, but
451 with Test::Unit or RSpec, you may have to use the
452 {database_cleaner}[https://github.com/bmabey/database_cleaner] gem. See {this
453 explanation}[https://groups.google.com/d/msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ]
454 (and code for {solution
455 2}[http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-220]
456 and {solution 3}[http://pastie.org/1745020]) for details.
457
a0e5811 @joliss README and History housekeeping
joliss authored
458 == Asynchronous JavaScript (Ajax and friends)
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
459
460 When working with asynchronous JavaScript, you might come across situations
461 where you are attempting to interact with an element which is not yet present
462 on the page. Capybara automatically deals with this by waiting for elements
463 to appear on the page.
464
465 When issuing instructions to the DSL such as:
466
467 click_link('foo')
468 click_link('bar')
469 page.should have_content('baz')
470
471 If clicking on the *foo* link causes triggers an asynchronous process, such as
a0e5811 @joliss README and History housekeeping
joliss authored
472 an Ajax request, which, when complete will add the *bar* link to the page,
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
473 clicking on the *bar* link would be expeced to fail, since that link doesn't
474 exist yet. However Capybara is smart enought to retry finding the link for a
475 brief period of time before giving up and throwing an error. The same is true of
476 the next line, which looks for the content *baz* on the page; it will retry
477 looking for that content for a brief time. You can adjust how long this period
478 is (the default is 2 seconds):
479
480 Capybara.default_wait_time = 5
481
482 Be aware that because of this behaviour, the following two statements are *not*
91ed4cd @dvrensk Documentation clarification and un-typo-ing.
dvrensk authored
483 equivalent, and you should *always* use the latter!
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
484
657b9c5 @jnicklas Some updates to README
authored
485 page.should_not have_xpath('a')
486 page.should have_no_xpath('a')
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
487
488 The former would incorrectly wait for the content to appear, since the
489 asynchronous process has not yet removed the element from the page, it would
490 therefore fail, even though the code might be working correctly. The latter
91ed4cd @dvrensk Documentation clarification and un-typo-ing.
dvrensk authored
491 correctly waits for the element to disappear from the page.
4f2da02 @jnicklas Added a long note about asynchronous JS to README
authored
492
1fa8aee @joliss Better documentation for drivers
joliss authored
493 == Using the DSL in unsupported testing frameworks
586ce21 @jnicklas Vastly improved README
authored
494
31f1ac3 @jnicklas Separate DSL out into a module
authored
495 You can mix the DSL into any context by including +Capybara::DSL+:
496
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
497
6b099a4 renamed to capybara
Theo Hultberg and Jonas Nicklas authored
498 require 'capybara'
499 require 'capybara/dsl'
586ce21 @jnicklas Vastly improved README
authored
500
6b099a4 renamed to capybara
Theo Hultberg and Jonas Nicklas authored
501 Capybara.default_driver = :culerity
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
502
360088a @jnicklas don't suggest including Capybara in the global object, closes #63
authored
503 module MyModule
31f1ac3 @jnicklas Separate DSL out into a module
authored
504 include Capybara::DSL
360088a @jnicklas don't suggest including Capybara in the global object, closes #63
authored
505
506 def login!
507 within("//form[@id='session']") do
508 fill_in 'Login', :with => 'user@example.com'
509 fill_in 'Password', :with => 'password'
510 end
511 click_link 'Sign in'
512 end
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
513 end
514
4305776 Update README adding explaination for using app_host to call remote web ...
Joseph Wilk and Matt Wynne authored
515 == Calling remote servers
516
657b9c5 @jnicklas Some updates to README
authored
517 Normally Capybara expects to be testing an in-process Rack application, but you
518 can also use it to talk to a web server running anywhere on the internets, by
519 setting app_host:
4305776 Update README adding explaination for using app_host to call remote web ...
Joseph Wilk and Matt Wynne authored
520
521 Capybara.current_driver = :selenium
522 Capybara.app_host = 'http://www.google.com'
963190a @jnicklas Removed super outdated note about celerity 0.7.5
authored
523 ...
4305776 Update README adding explaination for using app_host to call remote web ...
Joseph Wilk and Matt Wynne authored
524 visit('/')
525
a0e5811 @joliss README and History housekeeping
joliss authored
526 Note that the default driver (<tt>:rack_test</tt>) does not support running
527 against a remote server. With drivers that support it, you can also visit any
528 URL directly:
5616235 @jnicklas More about remote servers in README
authored
529
530 visit('http://www.google.com')
531
657b9c5 @jnicklas Some updates to README
authored
532 By default Capybara will try to boot a rack application automatically. You
533 might want to switch off Capybara's rack server if you are running against a
534 remote application:
5616235 @jnicklas More about remote servers in README
authored
535
536 Capybara.run_server = false
537
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
538 == Using the sessions manually
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
539
a0e5811 @joliss README and History housekeeping
joliss authored
540 For ultimate control, you can instantiate and use a
541 {Session}[http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session]
542 manually.
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
543
586ce21 @jnicklas Vastly improved README
authored
544 require 'capybara'
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
545
586ce21 @jnicklas Vastly improved README
authored
546 session = Capybara::Session.new(:culerity, my_rack_app)
547 session.within("//form[@id='session']") do
548 session.fill_in 'Login', :with => 'user@example.com'
549 session.fill_in 'Password', :with => 'password'
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
550 end
586ce21 @jnicklas Vastly improved README
authored
551 session.click_link 'Sign in'
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
552
fed2c5b @jnicklas Add section about custom selectors to README
authored
553 == XPath, CSS and selectors
7313511 @jnicklas Added section about selectors to README
authored
554
555 Capybara does not try to guess what kind of selector you are going to give it,
f5f444b @joliss Improve README
joliss authored
556 and will always use CSS by default. If you want to use XPath, you'll need to
557 do:
fb71b29 @denro added documentation on the evaluate_script method
denro authored
558
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
559 within(:xpath, '//ul/li') { ... }
560 find(:xpath, '//ul/li').text
3070441 @jnicklas Find and locate now do the same thing
authored
561 find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value
7313511 @jnicklas Added section about selectors to README
authored
562
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
563 Alternatively you can set the default selector to XPath:
7313511 @jnicklas Added section about selectors to README
authored
564
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
565 Capybara.default_selector = :xpath
566 find('//ul/li').text
7313511 @jnicklas Added section about selectors to README
authored
567
fed2c5b @jnicklas Add section about custom selectors to README
authored
568 Capybara allows you to add custom selectors, which can be very useful if you
569 find yourself using the same kinds of selectors very often:
570
b13e4d3 @jnicklas changed Selector API. Now more like RSpec matchers
authored
571 Capybara.add_selector(:id) do
572 xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
573 end
574
575 Capybara.add_selector(:row) do
576 xpath { |num| ".//tbody/tr[#{num}]" }
577 end
fed2c5b @jnicklas Add section about custom selectors to README
authored
578
b13e4d3 @jnicklas changed Selector API. Now more like RSpec matchers
authored
579 The block given to xpath must always return an XPath expression as a String, or
580 an XPath expression generated through the XPath gem. You can now use these
581 selectors like this:
fed2c5b @jnicklas Add section about custom selectors to README
authored
582
583 find(:id, 'post_123')
584 find(:row, 3)
585
b13e4d3 @jnicklas changed Selector API. Now more like RSpec matchers
authored
586 You can specify an optional match option which will automatically use the
587 selector if it matches the argument:
fed2c5b @jnicklas Add section about custom selectors to README
authored
588
b13e4d3 @jnicklas changed Selector API. Now more like RSpec matchers
authored
589 Capybara.add_selector(:id) do
590 xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
591 match { |value| value.is_a?(Symbol) }
592 end
fed2c5b @jnicklas Add section about custom selectors to README
authored
593
594 Now use it like this:
595
596 find(:post_123)
597
598 This :id selector is already built into Capybara by default, so you don't
599 need to add it yourself.
600
6c5935c @jnicklas Added a note about the XPath // confusion
authored
601 == Beware the XPath // trap
602
603 In XPath the expression // means something very specific, and it might not be what
604 you think. Contrary to common belief, // means "anywhere in the document" not "anywhere
605 in the current context". As an example:
606
fed2c5b @jnicklas Add section about custom selectors to README
authored
607 page.find(:xpath, '//body').all(:xpath, '//script')
6c5935c @jnicklas Added a note about the XPath // confusion
authored
608
609 You might expect this to find all script tags in the body, but actually, it finds all
610 script tags in the entire document, not only those in the body! What you're looking
611 for is the .// expression which means "any descendant of the current node":
612
fed2c5b @jnicklas Add section about custom selectors to README
authored
613 page.find(:xpath, '//body').all(:xpath, './/script')
6c5935c @jnicklas Added a note about the XPath // confusion
authored
614
0d76d06 @jnicklas Add note about within to // section of README
authored
615 The same thing goes for within:
616
fed2c5b @jnicklas Add section about custom selectors to README
authored
617 within(:xpath, '//body') do
618 page.find(:xpath, './/script')
619 within(:xpath, './/table/tbody') do
620 ...
621 end
0d76d06 @jnicklas Add note about within to // section of README
authored
622 end
623
d536eea @jnicklas added section on configuring drivers to readme
authored
624 == Configuring and adding drivers
625
626 Capybara makes it convenient to switch between different drivers. It also exposes
627 an API to tweak those drivers with whatever settings you want, or to add your own
628 drivers. This is how to switch the selenium driver to use chrome:
629
630 Capybara.register_driver :selenium do |app|
f7fa41a @jnicklas Fix new class name for selenium driver
authored
631 Capybara::Selenium::Driver.new(app, :browser => :chrome)
d536eea @jnicklas added section on configuring drivers to readme
authored
632 end
633
634 However, it's also possible to give this a different name, so tests can switch
635 between using different browsers effortlessly:
636
637 Capybara.register_driver :selenium_chrome do |app|
f7fa41a @jnicklas Fix new class name for selenium driver
authored
638 Capybara::Selenium::Driver.new(app, :browser => :chrome)
d536eea @jnicklas added section on configuring drivers to readme
authored
639 end
640
641 Whatever is returned from the block should conform to the API described by
642 Capybara::Driver::Base, it does not however have to inherit from this class.
643 Gems can use this API to add their own drivers to Capybara.
644
6a1a109 @jarib Add Selenium wiki link to the README.
jarib authored
645 The {Selenium wiki}[http://code.google.com/p/selenium/wiki/RubyBindings] has
646 additional info about how the underlying driver can be configured.
647
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
648 == Gotchas:
e06e04e Some more info in README
Jonas Nicklas and Kevin Fitzpatrick authored
649
59ba5d9 @jnicklas Update README, some really outdated stuff here :(
authored
650 * Access to session and request is not possible from the test, Access to
651 response is limited. Some drivers allow access to response headers and HTTP
652 status code, but this kind of functionality is not provided by some drivers,
653 such as Selenium.
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
654
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
655 * Access to Rails specific stuff (such as <tt>controller</tt>) is unavailable,
656 since we're not using Rails' integration testing.
d2265a3 @jnicklas Readme is markdown instead
authored
657
7669134 @jnicklas Added note about time freezing in README
authored
658 * Freezing time: It's common practice to mock out the Time so that features
659 that depend on the current Date work as expected. This can be problematic,
a0e5811 @joliss README and History housekeeping
joliss authored
660 since Capybara's Ajax timing uses the system time, resulting in Capybara
7669134 @jnicklas Added note about time freezing in README
authored
661 never timing out and just hanging when a failure occurs. It's still possible to
662 use plugins which allow you to travel in time, rather than freeze time.
663 One such plugin is {Timecop}[http://github.com/jtrupiano/timecop].
664
8c2a0d9 @trevorturk Document gotcha about visiting absolute URLs
trevorturk authored
665 * When using Rack::Test, beware if attempting to visit absolute URLs. For
666 example, a session might not be shared between visits to <tt>posts_path</tt>
667 and <tt>posts_url</tt>. If testing an absolute URL in an Action Mailer email,
668 set <tt>default_url_options</tt> to match the Rails default of
669 <tt>www.example.com</tt>.
670
cd4198e Back to RDoc for Readme
Dennis Rogenius and Jonas Nicklas authored
671 == License:
8435d19 Initial checkin
Jonas Nicklas and Kevin Fitzpatrick authored
672
673 (The MIT License)
674
675 Copyright (c) 2009 Jonas Nicklas
676
677 Permission is hereby granted, free of charge, to any person obtaining
678 a copy of this software and associated documentation files (the
679 'Software'), to deal in the Software without restriction, including
680 without limitation the rights to use, copy, modify, merge, publish,
681 distribute, sublicense, and/or sell copies of the Software, and to
682 permit persons to whom the Software is furnished to do so, subject to
683 the following conditions:
684
685 The above copyright notice and this permission notice shall be
686 included in all copies or substantial portions of the Software.
687
688 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
689 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
690 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
691 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
692 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
693 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
fb71b29 @denro added documentation on the evaluate_script method
denro authored
694 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.