capybara-rails-2-2 0.4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. data/History.txt +202 -0
  2. data/README.rdoc +540 -0
  3. data/lib/capybara.rb +231 -0
  4. data/lib/capybara/cucumber.rb +32 -0
  5. data/lib/capybara/driver/base.rb +60 -0
  6. data/lib/capybara/driver/celerity_driver.rb +164 -0
  7. data/lib/capybara/driver/culerity_driver.rb +26 -0
  8. data/lib/capybara/driver/node.rb +74 -0
  9. data/lib/capybara/driver/rack_test_driver.rb +303 -0
  10. data/lib/capybara/driver/selenium_driver.rb +161 -0
  11. data/lib/capybara/dsl.rb +109 -0
  12. data/lib/capybara/node/actions.rb +160 -0
  13. data/lib/capybara/node/base.rb +47 -0
  14. data/lib/capybara/node/document.rb +17 -0
  15. data/lib/capybara/node/element.rb +178 -0
  16. data/lib/capybara/node/finders.rb +201 -0
  17. data/lib/capybara/node/matchers.rb +391 -0
  18. data/lib/capybara/node/simple.rb +116 -0
  19. data/lib/capybara/rails.rb +16 -0
  20. data/lib/capybara/rspec.rb +18 -0
  21. data/lib/capybara/selector.rb +70 -0
  22. data/lib/capybara/server.rb +90 -0
  23. data/lib/capybara/session.rb +281 -0
  24. data/lib/capybara/spec/driver.rb +243 -0
  25. data/lib/capybara/spec/fixtures/capybara.jpg +0 -0
  26. data/lib/capybara/spec/fixtures/test_file.txt +1 -0
  27. data/lib/capybara/spec/public/jquery-ui.js +35 -0
  28. data/lib/capybara/spec/public/jquery.js +19 -0
  29. data/lib/capybara/spec/public/test.js +33 -0
  30. data/lib/capybara/spec/session.rb +110 -0
  31. data/lib/capybara/spec/session/all_spec.rb +78 -0
  32. data/lib/capybara/spec/session/attach_file_spec.rb +70 -0
  33. data/lib/capybara/spec/session/check_spec.rb +65 -0
  34. data/lib/capybara/spec/session/choose_spec.rb +26 -0
  35. data/lib/capybara/spec/session/click_button_spec.rb +252 -0
  36. data/lib/capybara/spec/session/click_link_or_button_spec.rb +36 -0
  37. data/lib/capybara/spec/session/click_link_spec.rb +113 -0
  38. data/lib/capybara/spec/session/current_url_spec.rb +15 -0
  39. data/lib/capybara/spec/session/fill_in_spec.rb +119 -0
  40. data/lib/capybara/spec/session/find_button_spec.rb +18 -0
  41. data/lib/capybara/spec/session/find_by_id_spec.rb +18 -0
  42. data/lib/capybara/spec/session/find_field_spec.rb +26 -0
  43. data/lib/capybara/spec/session/find_link_spec.rb +19 -0
  44. data/lib/capybara/spec/session/find_spec.rb +121 -0
  45. data/lib/capybara/spec/session/first_spec.rb +72 -0
  46. data/lib/capybara/spec/session/has_button_spec.rb +32 -0
  47. data/lib/capybara/spec/session/has_content_spec.rb +106 -0
  48. data/lib/capybara/spec/session/has_css_spec.rb +213 -0
  49. data/lib/capybara/spec/session/has_field_spec.rb +156 -0
  50. data/lib/capybara/spec/session/has_link_spec.rb +37 -0
  51. data/lib/capybara/spec/session/has_select_spec.rb +129 -0
  52. data/lib/capybara/spec/session/has_selector_spec.rb +129 -0
  53. data/lib/capybara/spec/session/has_table_spec.rb +96 -0
  54. data/lib/capybara/spec/session/has_xpath_spec.rb +123 -0
  55. data/lib/capybara/spec/session/headers.rb +19 -0
  56. data/lib/capybara/spec/session/javascript.rb +223 -0
  57. data/lib/capybara/spec/session/response_code.rb +19 -0
  58. data/lib/capybara/spec/session/select_spec.rb +105 -0
  59. data/lib/capybara/spec/session/uncheck_spec.rb +21 -0
  60. data/lib/capybara/spec/session/unselect_spec.rb +61 -0
  61. data/lib/capybara/spec/session/within_spec.rb +160 -0
  62. data/lib/capybara/spec/test_app.rb +117 -0
  63. data/lib/capybara/spec/views/buttons.erb +4 -0
  64. data/lib/capybara/spec/views/fieldsets.erb +29 -0
  65. data/lib/capybara/spec/views/form.erb +348 -0
  66. data/lib/capybara/spec/views/frame_one.erb +8 -0
  67. data/lib/capybara/spec/views/frame_two.erb +8 -0
  68. data/lib/capybara/spec/views/popup_one.erb +8 -0
  69. data/lib/capybara/spec/views/popup_two.erb +8 -0
  70. data/lib/capybara/spec/views/postback.erb +13 -0
  71. data/lib/capybara/spec/views/tables.erb +122 -0
  72. data/lib/capybara/spec/views/with_html.erb +69 -0
  73. data/lib/capybara/spec/views/with_js.erb +39 -0
  74. data/lib/capybara/spec/views/with_scope.erb +36 -0
  75. data/lib/capybara/spec/views/with_simple_html.erb +1 -0
  76. data/lib/capybara/spec/views/within_frames.erb +10 -0
  77. data/lib/capybara/spec/views/within_popups.erb +25 -0
  78. data/lib/capybara/util/save_and_open_page.rb +40 -0
  79. data/lib/capybara/util/timeout.rb +27 -0
  80. data/lib/capybara/version.rb +3 -0
  81. data/spec/basic_node_spec.rb +77 -0
  82. data/spec/capybara_spec.rb +46 -0
  83. data/spec/driver/celerity_driver_spec.rb +13 -0
  84. data/spec/driver/culerity_driver_spec.rb +14 -0
  85. data/spec/driver/rack_test_driver_spec.rb +84 -0
  86. data/spec/driver/remote_culerity_driver_spec.rb +22 -0
  87. data/spec/driver/remote_selenium_driver_spec.rb +16 -0
  88. data/spec/driver/selenium_driver_spec.rb +14 -0
  89. data/spec/dsl_spec.rb +157 -0
  90. data/spec/rspec_spec.rb +47 -0
  91. data/spec/save_and_open_page_spec.rb +159 -0
  92. data/spec/server_spec.rb +85 -0
  93. data/spec/session/celerity_session_spec.rb +24 -0
  94. data/spec/session/culerity_session_spec.rb +26 -0
  95. data/spec/session/rack_test_session_spec.rb +44 -0
  96. data/spec/session/selenium_session_spec.rb +26 -0
  97. data/spec/spec_helper.rb +40 -0
  98. data/spec/string_spec.rb +77 -0
  99. data/spec/timeout_spec.rb +28 -0
  100. metadata +360 -0
data/History.txt ADDED
@@ -0,0 +1,202 @@
1
+ # Version 0.4.1
2
+
3
+ Release date: 2011-01-21
4
+
5
+ ### Added
6
+
7
+ * New click_on alias for click_link_or_button, shorter yet unambiguous. [Jonas Nicklas]
8
+ * Finders now accept :visible => false which will find all elements regardless of Capybara.ignore_hidden_elements [Jonas Nicklas]
9
+ * Configure how the server is started via Capybara.server { |app, port| ... }. [John Firebough]
10
+ * Added :between, :maximum and :minimum options to has_selector and friends [James B. Byrne]
11
+ * New Capybara.string util function which allows matchers on arbitrary strings, mostly for helper and view specs [David Chelimsky and Jonas Nicklas]
12
+ * Server boot timeout is now configurable, via Capybara.server_boot_timeout [Adam Cigánek]
13
+ * Built in support for RSpec [Jonas Nicklas]
14
+ * Capybara.using_driver to switch to a different driver temporarily [Jeff Kreeftmeijer]
15
+ * Added Session#first which is somewhat speedier than Session#all, use it internally for speed boost [John Firebaugh]
16
+
17
+ ### Changed
18
+
19
+ * Session#within now accepts the same arguments as other finders, like Session#all and Session#find [Jonas Nicklas]
20
+
21
+ ### Removed
22
+
23
+ * All deprecations from 0.4.0 have been removed. [Jonas Nicklas]
24
+
25
+ ### Fixed
26
+
27
+ * Don't mangle URLs in save_and_open_page when using self-closing tags [Adam Spiers]
28
+ * Catch correct error when server boot times out [Jonas Nicklas]
29
+ * Celerity driver now properly passes through options, making it configurable [Jonas Nicklas]
30
+ * Better implementation of attributes in C[ue]lerity, should fix issues with attributes with strange names [Jonas Nicklas]
31
+ * Session#find no longer swallows errors [Jonas Nicklas]
32
+ * Fix problems with multiple file inputs [Philip Arndt]
33
+ * Submit multipart forms as multipart under rack-test even if they contain no files [Ryan Kinderman]
34
+ * Matchers like has_select? and has_checked_field? now work with dynamically changed values [John Firebaugh]
35
+ * Preserve order of rack params [Joel Chippindale]
36
+ * RackTest#reset! is more thorough [Joel Chippindale]
37
+
38
+ # Version 0.4.0
39
+
40
+ Release date: 2010-10-22
41
+
42
+ ### Changed
43
+
44
+ * The Selector API was changed slightly, use Capybara.add_selector, see README
45
+
46
+ ### Fixed
47
+
48
+ * Celerity driver is registered properly
49
+ * has_selector? and has_no_selector? added to DSL
50
+ * Multiple selects return correct values under C[cu]lerity
51
+ * Naked query strings are handled correctly by rack-test
52
+
53
+ # Version 0.4.0.rc
54
+
55
+ Release date: 2010-10-12
56
+
57
+ ### Changed
58
+
59
+ * within and find/locate now follow the XPath spec in that //foo finds all nodes in the document, instead of
60
+ only for the context node. See this post for details: http://groups.google.com/group/ruby-capybara/browse_thread/thread/b129067979df21b3
61
+ * within now executes within the first found instance of the selector, not in all of them
62
+ * find now waits for AJAX requests and raises an exception when the element is not found (same as locate used to do)
63
+ * The default selector is now CSS, not XPath
64
+
65
+ ### Deprecated
66
+
67
+ * Session#click has been renamed click_link_or_button and the old click has been deprecated
68
+ * Node#node has been renamed native
69
+ * Node#locate is deprecated in favor of Node#find, which now behaves identically
70
+ * Session#drag is deprecated, please use Node#drag_to(other_node) instead
71
+
72
+ ### Added
73
+
74
+ * Pretty much everything is properly documented now
75
+ * It's now possible to call all session methods on nodes, like `find('#foo').fill_in(...)`
76
+ * Custom selectors can be added with Capybara::Selector.add
77
+ * The :id selector is added by default, use it lile `find(:id, 'foo')` or `find(:foo)`
78
+ * Added Node#has_selector? so any kind of selector can be queried.
79
+ * Added Capybara.configure for less wordy configuration
80
+ * Added within_window to switch between different windows (currently Selenium only)
81
+ * Capybara.server_port to provide a fixed port if wanted (defaults to automatic selection)
82
+
83
+ ### Fixed
84
+
85
+ * CSS selectors with multiple selectors, such as "h1, h2" now work correctly
86
+ * Port is automatically assigned instead of guessing
87
+ * Strip encodings in rack-test, no more warnings!
88
+ * RackTest no longer submits disabled fields
89
+ * Servers no longer output annoying debug information when started
90
+ * TCP port selection is left to Ruby to decide, no more port guessing
91
+ * Select boxes now return option value instead of text if present
92
+ * The default has been changed from localhost to 127.0.0.1, should fix some obscure selenium bugs
93
+ * RackTest now supports complex field names, such as foo[bar][][baz]
94
+
95
+ # Version 0.3.9
96
+
97
+ Release date: 2010-07-03
98
+
99
+ ### Added
100
+
101
+ * status_code which returns the HTTP status code of the last response (no Selenium!)
102
+ * Capybara.save_and_open_page to store tempfiles
103
+ * RackTest and Culerity drivers now clean up after themselves properly
104
+
105
+ ### Fixed
106
+
107
+ * When no rack app is set and the app is called, a more descriptive error is raised
108
+ * select now works with optgroups
109
+ * Don't submit image buttons unless they were clicked under rack-test
110
+ * Support custom field types under Selenium
111
+ * Support input fields without a type, treat them as though they were text fields
112
+ * Redirect now throws an error after 5 redirects, as per RFC
113
+ * Selenium now properly raises an error when Node#trigger is called
114
+ * Node#value now returns the correct value for textareas under rack-test
115
+
116
+ # Version 0.3.8
117
+
118
+ Release date: 2010-05-12
119
+
120
+ ### Added
121
+
122
+ * Within_frame method to execute a block of code within a particular iframe (Selenium only!)
123
+
124
+ ### Fixed
125
+
126
+ * Single quotes are properly escaped with `select` under rack-test and Selenium.
127
+ * The :text option for searches now escapes regexp special characters when a string is given.
128
+ * Selenium now correctly checks already checked checkboxes (same with uncheck)
129
+ * Timing issue which caused Selenium to hang under certain circumstances.
130
+ * Selenium now resolves attributes even if they are given as a Symbol
131
+
132
+ # Version 0.3.7
133
+
134
+ Release date: 2010-04-09
135
+
136
+ This is a drop in compatible maintainance release. It's mostly
137
+ important for driver authors.
138
+
139
+ ### Added
140
+
141
+ * RackTest scans for data-method which rails3 uses to change the request method
142
+
143
+ ### Fixed
144
+
145
+ * Don't hang when starting server on Windoze
146
+
147
+ ### Changed
148
+
149
+ * The driver and session specs are now located inside lib! Driver authors can simply require them.
150
+
151
+ # Version 0.3.6
152
+
153
+ Release date: 2010-03-22
154
+
155
+ This is a maintainance release with minor bug fixes, should be
156
+ drop in compatible.
157
+
158
+ ### Added
159
+
160
+ * It's now possible to load in external drivers
161
+
162
+ ### Fixed
163
+
164
+ * has_content? ignores whitespace
165
+ * Trigger events when choosing radios and checking checkboxes under Selenium
166
+ * Make Capybara.app totally optional when running without server
167
+ * Changed fallback host so it matches the one set up by Rails' integration tests
168
+
169
+ # Version 0.3.5
170
+
171
+ Release date: 2010-02-26
172
+
173
+ This is a mostly backwards compatible release, it does break
174
+ the API in some minor places, which should hopefully not affect
175
+ too many users, please read the release notes carefully!
176
+
177
+ ### Breaking
178
+
179
+ * Relative searching in a node (e.g. find('//p').all('//a')) will now follow XPath standard
180
+ this means that if you want to find descendant nodes only, you'll need to prefix a dot!
181
+ * `visit` now accepts fully qualified URLs for drivers that support it.
182
+ * Capybara will always try to run a rack server, unless you set Capybara.run_sever = false
183
+
184
+ ### Changed
185
+
186
+ * thin is preferred over mongrel and webrick, since it is Ruby 1.9 compatible
187
+ * click_button and click will find <input type="button">, clicking them does nothing in RackTest
188
+
189
+ ### Added
190
+
191
+ * Much improved error messages in a multitude of places
192
+ * More semantic page querying with has_link?, has_button?, etc...
193
+ * Option to ignore hidden elements when querying and interacting with the page
194
+ * Support for multiple selects
195
+
196
+ ### Fixed
197
+
198
+ * find_by_id is no longer broken
199
+ * clicking links where the image's alt attribute contains the text is now possible
200
+ * within_fieldset and within_table work when the default selector is CSS
201
+ * boolean attributes work the same across drivers (return true/false)
202
+
data/README.rdoc ADDED
@@ -0,0 +1,540 @@
1
+ = Capybara
2
+
3
+ * http://github.com/jnicklas/capybara
4
+
5
+ == Description:
6
+
7
+ Capybara aims to simplify the process of integration testing Rack applications,
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
10
+ tests and currently comes bundled with rack-test, Culerity, Celerity and Selenium
11
+ support built in. env.js support is available as the
12
+ {capybara-envjs gem}[http://github.com/smparkes/capybara-envjs].
13
+
14
+ Online documentation is availbable
15
+ {at rdoc.info}[http://rdoc.info/projects/jnicklas/capybara].
16
+
17
+ == Install:
18
+
19
+ Install as a gem:
20
+
21
+ sudo gem install capybara
22
+
23
+ On OSX you may have to install libffi, you can install it via MacPorts with:
24
+
25
+ sudo port install libffi
26
+
27
+ == Development:
28
+
29
+ * Source hosted at {GitHub}[http://github.com/jnicklas/capybara].
30
+ * Please direct questions, discussions at the {mailing list}[http://groups.google.com/group/ruby-capybara].
31
+ * Report issues on {GitHub Issues}[http://github.com/jnicklas/capybara/issues]
32
+
33
+ Pull requests are very welcome! Make sure your patches are well tested, Capybara is
34
+ a testing tool after all. Please create a topic branch for every separate change
35
+ you make.
36
+
37
+ Capybara uses bundler in development. To set up a development environment, simply do:
38
+
39
+ gem install bundler --pre
40
+ bundle install
41
+
42
+ == Using Capybara with Cucumber
43
+
44
+ Capybara is built to work nicely with Cucumber. Support for Capybara is built into
45
+ cucumber-rails. In your Rails app, just run:
46
+
47
+ rails generate cucumber:install --capybara
48
+
49
+ And everything should be set up and ready to go.
50
+
51
+ If you want to use Capybara with Cucumber outside Rails (for example with Merb
52
+ or Sinatra), you'll need to require Capybara and set the Rack app manually:
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
60
+ within("#session") do
61
+ fill_in 'Login', :with => 'user@example.com'
62
+ fill_in 'Password', :with => 'password'
63
+ end
64
+ click_link 'Sign in'
65
+ end
66
+
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
73
+ Scenario: do something AJAXy
74
+ When I click the AJAX link
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
+
86
+ If you prefer RSpec to using Cucumber, you can use the built in RSpec support:
87
+
88
+ require 'capybara/rspec'
89
+ Capybara.app = MyRackApp
90
+
91
+ You can now use it in your examples:
92
+
93
+ describe "the signup process", :type => :acceptance do
94
+ it "signs me in" do
95
+ within("#session") do
96
+ fill_in 'Login', :with => 'user@example.com'
97
+ fill_in 'Password', :with => 'password'
98
+ end
99
+ click_link 'Sign in'
100
+ end
101
+ end
102
+
103
+ Capybara is only included for examples which have the type
104
+ <tt>:acceptance</tt>.
105
+
106
+ RSpec's metadata feature can be used to switch to a different driver. Use
107
+ <tt>:js => true</tt> to switch to the javascript driver, or provide a
108
+ <tt>:driver</tt> option to switch to one specific driver. For example:
109
+
110
+ describe 'some stuff which requires js', :js => true do
111
+ it 'will use the default js driver'
112
+ it 'will switch to one specific driver', :driver => :celerity
113
+ end
114
+
115
+ Note that Capybara's built in RSpec support only works with RSpec 2.0 or later.
116
+ You'll need to roll your own for earlier versions of RSpec.
117
+
118
+ == Default and current driver
119
+
120
+ You can set up a default driver for your features. For example if you'd prefer
121
+ to run Selenium, you could do:
122
+
123
+ require 'capybara/rails'
124
+ require 'capybara/cucumber'
125
+ Capybara.default_driver = :selenium
126
+
127
+ You can change the driver temporarily:
128
+
129
+ Capybara.current_driver = :culerity
130
+ Capybara.use_default_driver
131
+
132
+ You can do this in Before and After blocks to temporarily switch to a different
133
+ driver. Note that switching driver creates a new session, so you may not be able
134
+ to switch in the middle of a Scenario.
135
+
136
+ == Selenium
137
+
138
+ At the moment, Capybara supports Webdriver, also called Selenium 2.0, *not*
139
+ Selenium RC. Provided Firefox is installed, everything is set up for you, and
140
+ you should be able to start using Selenium right away.
141
+
142
+ == Celerity
143
+
144
+ Celerity only runs on JRuby, so you'll need to install the celerity gem under
145
+ JRuby:
146
+
147
+ jruby -S gem install celerity
148
+
149
+ == Culerity
150
+
151
+ Install celerity as noted above, make sure JRuby is in your path. Note that
152
+ Culerity doesn't seem to be working under Ruby 1.9 at the moment.
153
+
154
+ == env.js
155
+
156
+ The {capybara-envjs driver}[http://github.com/smparkes/capybara-envjs]
157
+ uses the envjs gem ({GitHub}[http://github.com/smparkes/env-js],
158
+ {rubygems.org}[http://rubygems.org/gems/envjs]) to interpret
159
+ JavaScript outside the browser. The driver is installed by installing the capybara-envjs gem:
160
+
161
+ gem install capybara-envjs
162
+
163
+ More info about the driver and env.js are available through the links above. The envjs gem only supports
164
+ Ruby 1.8.7 at this time.
165
+
166
+ == The DSL
167
+
168
+ Capybara's DSL is inspired by Webrat. While backwards compatibility is retained
169
+ in a lot of cases, there are certain important differences.
170
+
171
+ Unlike in Webrat, all searches in Capybara are *case sensitive*. This is because
172
+ Capybara heavily uses XPath, which doesn't support case insensitivity.
173
+
174
+ === Navigating
175
+
176
+ You can use the <tt>visit</tt> method to navigate to other pages:
177
+
178
+ visit('/projects')
179
+ visit(post_comments_path(post))
180
+
181
+ The visit method only takes a single parameter, the request method is *always*
182
+ GET.
183
+
184
+ You can get the current path of the browsing session for test assertions:
185
+
186
+ current_path.should == post_comments_path(post)
187
+
188
+ === Clicking links and buttons
189
+
190
+ You can interact with the webapp by following links and buttons. Capybara
191
+ automatically follows any redirects, and submits forms associated with buttons.
192
+
193
+ click_link('id-of-link')
194
+ click_link('Link Text')
195
+ click_button('Save')
196
+ click_link_or_button('Link Text')
197
+ click_link_or_button('Button Value')
198
+
199
+ === Interacting with forms
200
+
201
+ Forms are everywhere in webapps, there are a number of tools for interacting
202
+ with the various form elements:
203
+
204
+ fill_in('First Name', :with => 'John')
205
+ fill_in('Password', :with => 'Seekrit')
206
+ fill_in('Description', :with => 'Really Long Text…')
207
+ choose('A Radio Button')
208
+ check('A Checkbox')
209
+ uncheck('A Checkbox')
210
+ attach_file('Image', '/path/to/image.jpg')
211
+ select('Option', :from => 'Select Box')
212
+
213
+ === Querying
214
+
215
+ Capybara has a rich set of options for querying the page for the existence of
216
+ certain elements, and working with and manipulating those elements.
217
+
218
+ page.has_selector?('table tr')
219
+ page.has_selector?(:xpath, '//table/tr')
220
+ page.has_no_selector?(:content)
221
+
222
+ page.has_xpath?('//table/tr')
223
+ page.has_css?('table tr.foo')
224
+ page.has_content?('foo')
225
+
226
+ You can these use with RSpec's magic matchers:
227
+
228
+ page.should have_selector('table tr')
229
+ page.should have_selector(:xpath, '//table/tr')
230
+ page.should have_no_selector(:content)
231
+
232
+ page.should have_xpath('//table/tr')
233
+ page.should have_css('table tr.foo')
234
+ page.should have_content('foo')
235
+ page.should have_no_content('foo')
236
+
237
+ Note that <tt>page.should have_no_xpath</tt> is preferred over
238
+ <tt>page.should_not have_xpath</tt>. Read the section on asynchronous JavaScript
239
+ for an explanation.
240
+
241
+ === Finding
242
+
243
+ You can also find specific elements, in order to manipulate them:
244
+
245
+ find_field('First Name').value
246
+ find_link('Hello').visible?
247
+ find_button('Send').click
248
+
249
+ find(:xpath, "//table/tr").click
250
+ find("#overlay").find("h1").click
251
+ all('a').each { |a| a[:href] }
252
+
253
+ Note that <tt>find</tt> will wait for an element to appear on the page, as explained in the
254
+ AJAX section. If the element does not appear it will raise an error.
255
+
256
+ These elements all have all the Capybara DSL methods available, so you can restrict them
257
+ to specific parts of the page:
258
+
259
+ find('#navigation').click_link('Home')
260
+ find('#navigation').should have_button('Sign out')
261
+
262
+ === Scoping
263
+
264
+ Capybara makes it possible to restrict certain actions, such as interacting with
265
+ forms or clicking links and buttons, to within a specific area of the page. For
266
+ this purpose you can use the generic <tt>within</tt> method. Optionally you can
267
+ specify which kind of selector to use.
268
+
269
+ within("li#employee") do
270
+ fill_in 'Name', :with => 'Jimmy'
271
+ end
272
+
273
+ within(:xpath, "//li[@id='employee']") do
274
+ fill_in 'Name', :with => 'Jimmy'
275
+ end
276
+
277
+ There are special methods for restricting the scope to a specific fieldset,
278
+ identified by either an id or the text of the fieldet's legend tag, and to a
279
+ specific table, identified by either id or text of the table's caption tag.
280
+
281
+ within_fieldset('Employee') do
282
+ fill_in 'Name', :with => 'Jimmy'
283
+ end
284
+
285
+ within_table('Employee') do
286
+ fill_in 'Name', :with => 'Jimmy'
287
+ end
288
+
289
+ === Scripting
290
+
291
+ In drivers which support it, you can easily execute JavaScript:
292
+
293
+ page.execute_script("$('body').empty()")
294
+
295
+ For simple expressions, you can return the result of the script. Note
296
+ that this may break with more complicated expressions:
297
+
298
+ result = page.evaluate_script('4 + 4');
299
+
300
+ === Debugging
301
+
302
+ It can be useful to take a snapshot of the page as it currently is and take a
303
+ look at it:
304
+
305
+ save_and_open_page
306
+
307
+ == Asynchronous JavaScript (AJAX and friends)
308
+
309
+ When working with asynchronous JavaScript, you might come across situations
310
+ where you are attempting to interact with an element which is not yet present
311
+ on the page. Capybara automatically deals with this by waiting for elements
312
+ to appear on the page.
313
+
314
+ When issuing instructions to the DSL such as:
315
+
316
+ click_link('foo')
317
+ click_link('bar')
318
+ page.should have_content('baz')
319
+
320
+ If clicking on the *foo* link causes triggers an asynchronous process, such as
321
+ an AJAX request, which, when complete will add the *bar* link to the page,
322
+ clicking on the *bar* link would be expeced to fail, since that link doesn't
323
+ exist yet. However Capybara is smart enought to retry finding the link for a
324
+ brief period of time before giving up and throwing an error. The same is true of
325
+ the next line, which looks for the content *baz* on the page; it will retry
326
+ looking for that content for a brief time. You can adjust how long this period
327
+ is (the default is 2 seconds):
328
+
329
+ Capybara.default_wait_time = 5
330
+
331
+ Be aware that because of this behaviour, the following two statements are *not*
332
+ equivalent, and you should *always* use the latter!
333
+
334
+ page.should_not have_xpath('a')
335
+ page.should have_no_xpath('a')
336
+
337
+ The former would incorrectly wait for the content to appear, since the
338
+ asynchronous process has not yet removed the element from the page, it would
339
+ therefore fail, even though the code might be working correctly. The latter
340
+ correctly waits for the element to disappear from the page.
341
+
342
+ == Using the DSL outside cucumber
343
+
344
+ You can mix the DSL into any context, for example you could use it in RSpec
345
+ examples. Just load the DSL and include it anywhere:
346
+
347
+ require 'capybara'
348
+ require 'capybara/dsl'
349
+
350
+ Capybara.default_driver = :culerity
351
+
352
+ module MyModule
353
+ include Capybara
354
+
355
+ def login!
356
+ within("//form[@id='session']") do
357
+ fill_in 'Login', :with => 'user@example.com'
358
+ fill_in 'Password', :with => 'password'
359
+ end
360
+ click_link 'Sign in'
361
+ end
362
+ end
363
+
364
+ == Calling remote servers
365
+
366
+ Normally Capybara expects to be testing an in-process Rack application, but you
367
+ can also use it to talk to a web server running anywhere on the internets, by
368
+ setting app_host:
369
+
370
+ Capybara.current_driver = :selenium
371
+ Capybara.app_host = 'http://www.google.com'
372
+ ...
373
+ visit('/')
374
+
375
+ Note that rack-test does not support running against a remote server. With
376
+ drivers that support it, you can also visit any URL directly:
377
+
378
+ visit('http://www.google.com')
379
+
380
+ By default Capybara will try to boot a rack application automatically. You
381
+ might want to switch off Capybara's rack server if you are running against a
382
+ remote application:
383
+
384
+ Capybara.run_server = false
385
+
386
+ == Using the sessions manually
387
+
388
+ For ultimate control, you can instantiate and use a session manually.
389
+
390
+ require 'capybara'
391
+
392
+ session = Capybara::Session.new(:culerity, my_rack_app)
393
+ session.within("//form[@id='session']") do
394
+ session.fill_in 'Login', :with => 'user@example.com'
395
+ session.fill_in 'Password', :with => 'password'
396
+ end
397
+ session.click_link 'Sign in'
398
+
399
+ == XPath, CSS and selectors
400
+
401
+ Capybara does not try to guess what kind of selector you are going to give it,
402
+ if you want to use XPath with your 'within' declarations for example, you'll need
403
+ to do:
404
+
405
+ within(:xpath, '//ul/li') { ... }
406
+ find(:xpath, '//ul/li').text
407
+ find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value
408
+
409
+ Alternatively you can set the default selector to XPath:
410
+
411
+ Capybara.default_selector = :xpath
412
+ find('//ul/li').text
413
+
414
+ Capybara allows you to add custom selectors, which can be very useful if you
415
+ find yourself using the same kinds of selectors very often:
416
+
417
+ Capybara.add_selector(:id) do
418
+ xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
419
+ end
420
+
421
+ Capybara.add_selector(:row) do
422
+ xpath { |num| ".//tbody/tr[#{num}]" }
423
+ end
424
+
425
+ The block given to xpath must always return an XPath expression as a String, or
426
+ an XPath expression generated through the XPath gem. You can now use these
427
+ selectors like this:
428
+
429
+ find(:id, 'post_123')
430
+ find(:row, 3)
431
+
432
+ You can specify an optional match option which will automatically use the
433
+ selector if it matches the argument:
434
+
435
+ Capybara.add_selector(:id) do
436
+ xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
437
+ match { |value| value.is_a?(Symbol) }
438
+ end
439
+
440
+ Now use it like this:
441
+
442
+ find(:post_123)
443
+
444
+ This :id selector is already built into Capybara by default, so you don't
445
+ need to add it yourself.
446
+
447
+ == Beware the XPath // trap
448
+
449
+ In XPath the expression // means something very specific, and it might not be what
450
+ you think. Contrary to common belief, // means "anywhere in the document" not "anywhere
451
+ in the current context". As an example:
452
+
453
+ page.find(:xpath, '//body').all(:xpath, '//script')
454
+
455
+ You might expect this to find all script tags in the body, but actually, it finds all
456
+ script tags in the entire document, not only those in the body! What you're looking
457
+ for is the .// expression which means "any descendant of the current node":
458
+
459
+ page.find(:xpath, '//body').all(:xpath, './/script')
460
+
461
+ The same thing goes for within:
462
+
463
+ within(:xpath, '//body') do
464
+ page.find(:xpath, './/script')
465
+ within(:xpath, './/table/tbody') do
466
+ ...
467
+ end
468
+ end
469
+
470
+ == Configuring and adding drivers
471
+
472
+ Capybara makes it convenient to switch between different drivers. It also exposes
473
+ an API to tweak those drivers with whatever settings you want, or to add your own
474
+ drivers. This is how to switch the selenium driver to use chrome:
475
+
476
+ Capybara.register_driver :selenium do |app|
477
+ Capybara::Driver::Selenium.new(app, :browser => :chrome)
478
+ end
479
+
480
+ However, it's also possible to give this a different name, so tests can switch
481
+ between using different browsers effortlessly:
482
+
483
+ Capybara.register_driver :selenium_chrome do |app|
484
+ Capybara::Driver::Selenium.new(app, :browser => :chrome)
485
+ end
486
+
487
+ Whatever is returned from the block should conform to the API described by
488
+ Capybara::Driver::Base, it does not however have to inherit from this class.
489
+ Gems can use this API to add their own drivers to Capybara.
490
+
491
+ The {Selenium wiki}[http://code.google.com/p/selenium/wiki/RubyBindings] has
492
+ additional info about how the underlying driver can be configured.
493
+
494
+ == Gotchas:
495
+
496
+ * Access to session and request is not possible from the test, Access to
497
+ response is limited. Some drivers allow access to response headers and HTTP
498
+ status code, but this kind of functionality is not provided by some drivers,
499
+ such as Selenium.
500
+
501
+ * Access to Rails specific stuff (such as <tt>controller</tt>) is unavailable,
502
+ since we're not using Rails' integration testing.
503
+
504
+ * Freezing time: It's common practice to mock out the Time so that features
505
+ that depend on the current Date work as expected. This can be problematic,
506
+ since Capybara's AJAX timing uses the system time, resulting in Capybara
507
+ never timing out and just hanging when a failure occurs. It's still possible to
508
+ use plugins which allow you to travel in time, rather than freeze time.
509
+ One such plugin is {Timecop}[http://github.com/jtrupiano/timecop].
510
+
511
+ * When using Rack::Test, beware if attempting to visit absolute URLs. For
512
+ example, a session might not be shared between visits to <tt>posts_path</tt>
513
+ and <tt>posts_url</tt>. If testing an absolute URL in an Action Mailer email,
514
+ set <tt>default_url_options</tt> to match the Rails default of
515
+ <tt>www.example.com</tt>.
516
+
517
+ == License:
518
+
519
+ (The MIT License)
520
+
521
+ Copyright (c) 2009 Jonas Nicklas
522
+
523
+ Permission is hereby granted, free of charge, to any person obtaining
524
+ a copy of this software and associated documentation files (the
525
+ 'Software'), to deal in the Software without restriction, including
526
+ without limitation the rights to use, copy, modify, merge, publish,
527
+ distribute, sublicense, and/or sell copies of the Software, and to
528
+ permit persons to whom the Software is furnished to do so, subject to
529
+ the following conditions:
530
+
531
+ The above copyright notice and this permission notice shall be
532
+ included in all copies or substantial portions of the Software.
533
+
534
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
535
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
536
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
537
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
538
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
539
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
540
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.