pr0d1r2-capybara 0.3.7

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 (87) hide show
  1. data/History.txt +71 -0
  2. data/README.rdoc +388 -0
  3. data/lib/capybara.rb +57 -0
  4. data/lib/capybara/cucumber.rb +32 -0
  5. data/lib/capybara/driver/base.rb +48 -0
  6. data/lib/capybara/driver/celerity_driver.rb +143 -0
  7. data/lib/capybara/driver/culerity_driver.rb +25 -0
  8. data/lib/capybara/driver/rack_test_driver.rb +271 -0
  9. data/lib/capybara/driver/selenium_driver.rb +161 -0
  10. data/lib/capybara/dsl.rb +60 -0
  11. data/lib/capybara/node.rb +60 -0
  12. data/lib/capybara/rails.rb +17 -0
  13. data/lib/capybara/save_and_open_page.rb +33 -0
  14. data/lib/capybara/searchable.rb +54 -0
  15. data/lib/capybara/server.rb +114 -0
  16. data/lib/capybara/session.rb +262 -0
  17. data/lib/capybara/spec/driver.rb +162 -0
  18. data/lib/capybara/spec/fixtures/capybara.jpg +0 -0
  19. data/lib/capybara/spec/fixtures/test_file.txt +1 -0
  20. data/lib/capybara/spec/public/jquery-ui.js +35 -0
  21. data/lib/capybara/spec/public/jquery.js +19 -0
  22. data/lib/capybara/spec/public/test.js +33 -0
  23. data/lib/capybara/spec/session.rb +81 -0
  24. data/lib/capybara/spec/session/all_spec.rb +69 -0
  25. data/lib/capybara/spec/session/attach_file_spec.rb +64 -0
  26. data/lib/capybara/spec/session/check_spec.rb +67 -0
  27. data/lib/capybara/spec/session/choose_spec.rb +26 -0
  28. data/lib/capybara/spec/session/click_button_spec.rb +236 -0
  29. data/lib/capybara/spec/session/click_link_spec.rb +108 -0
  30. data/lib/capybara/spec/session/click_spec.rb +24 -0
  31. data/lib/capybara/spec/session/current_url_spec.rb +8 -0
  32. data/lib/capybara/spec/session/fill_in_spec.rb +108 -0
  33. data/lib/capybara/spec/session/find_button_spec.rb +16 -0
  34. data/lib/capybara/spec/session/find_by_id_spec.rb +16 -0
  35. data/lib/capybara/spec/session/find_field_spec.rb +22 -0
  36. data/lib/capybara/spec/session/find_link_spec.rb +17 -0
  37. data/lib/capybara/spec/session/find_spec.rb +57 -0
  38. data/lib/capybara/spec/session/has_button_spec.rb +32 -0
  39. data/lib/capybara/spec/session/has_content_spec.rb +106 -0
  40. data/lib/capybara/spec/session/has_css_spec.rb +107 -0
  41. data/lib/capybara/spec/session/has_field_spec.rb +96 -0
  42. data/lib/capybara/spec/session/has_link_spec.rb +33 -0
  43. data/lib/capybara/spec/session/has_select_spec.rb +89 -0
  44. data/lib/capybara/spec/session/has_table_spec.rb +96 -0
  45. data/lib/capybara/spec/session/has_xpath_spec.rb +123 -0
  46. data/lib/capybara/spec/session/headers.rb +19 -0
  47. data/lib/capybara/spec/session/javascript.rb +204 -0
  48. data/lib/capybara/spec/session/locate_spec.rb +59 -0
  49. data/lib/capybara/spec/session/select_spec.rb +77 -0
  50. data/lib/capybara/spec/session/uncheck_spec.rb +21 -0
  51. data/lib/capybara/spec/session/unselect_spec.rb +54 -0
  52. data/lib/capybara/spec/session/within_spec.rb +153 -0
  53. data/lib/capybara/spec/test_app.rb +75 -0
  54. data/lib/capybara/spec/views/buttons.erb +4 -0
  55. data/lib/capybara/spec/views/fieldsets.erb +29 -0
  56. data/lib/capybara/spec/views/form.erb +234 -0
  57. data/lib/capybara/spec/views/frame_one.erb +8 -0
  58. data/lib/capybara/spec/views/frame_two.erb +8 -0
  59. data/lib/capybara/spec/views/postback.erb +13 -0
  60. data/lib/capybara/spec/views/tables.erb +122 -0
  61. data/lib/capybara/spec/views/with_html.erb +42 -0
  62. data/lib/capybara/spec/views/with_js.erb +39 -0
  63. data/lib/capybara/spec/views/with_scope.erb +36 -0
  64. data/lib/capybara/spec/views/with_simple_html.erb +1 -0
  65. data/lib/capybara/spec/views/within_frames.erb +10 -0
  66. data/lib/capybara/version.rb +3 -0
  67. data/lib/capybara/wait_until.rb +28 -0
  68. data/lib/capybara/xpath.rb +179 -0
  69. data/spec/capybara_spec.rb +18 -0
  70. data/spec/driver/celerity_driver_spec.rb +16 -0
  71. data/spec/driver/culerity_driver_spec.rb +12 -0
  72. data/spec/driver/rack_test_driver_spec.rb +11 -0
  73. data/spec/driver/remote_culerity_driver_spec.rb +23 -0
  74. data/spec/driver/remote_selenium_driver_spec.rb +18 -0
  75. data/spec/driver/selenium_driver_spec.rb +11 -0
  76. data/spec/dsl_spec.rb +140 -0
  77. data/spec/save_and_open_page_spec.rb +43 -0
  78. data/spec/searchable_spec.rb +66 -0
  79. data/spec/server_spec.rb +53 -0
  80. data/spec/session/celerity_session_spec.rb +27 -0
  81. data/spec/session/culerity_session_spec.rb +25 -0
  82. data/spec/session/rack_test_session_spec.rb +33 -0
  83. data/spec/session/selenium_session_spec.rb +25 -0
  84. data/spec/spec_helper.rb +19 -0
  85. data/spec/wait_until_spec.rb +28 -0
  86. data/spec/xpath_spec.rb +180 -0
  87. metadata +259 -0
@@ -0,0 +1,71 @@
1
+ # Versiob 0.3.7
2
+
3
+ Release date: 2010-04-09
4
+
5
+ This is a drop in compatible maintainance release. It's mostly
6
+ important for driver authors.
7
+
8
+ ### Added
9
+
10
+ * RackTest scans for data-method which rails3 uses to change the request method
11
+
12
+ ### Fixed
13
+
14
+ * Don't hang when starting server on Windoze
15
+
16
+ ### Changed
17
+
18
+ * The driver and session specs are now located inside lib! Driver authors can simply require them.
19
+
20
+ # Version 0.3.6
21
+
22
+ Release date: 2010-03-22
23
+
24
+ This is a maintainance release with minor bug fixes, should be
25
+ drop in compatible.
26
+
27
+ ### Added
28
+
29
+ * It's now possible to load in external drivers
30
+
31
+ ### Fixed
32
+
33
+ * has_content? ignores whitespace
34
+ * Trigger events when choosing radios and checking checkboxes under Selenium
35
+ * Make Capybara.app totally optional when running without server
36
+ * Changed fallback host so it matches the one set up by Rails' integration tests
37
+
38
+ # Version 0.3.5
39
+
40
+ Release date: 2010-02-26
41
+
42
+ This is a mostly backwards compatible release, it does break
43
+ the API in some minor places, which should hopefully not affect
44
+ too many users, please read the release notes carefully!
45
+
46
+ ### Breaking
47
+
48
+ * Relative searching in a node (e.g. find('//p').all('//a')) will now follow XPath standard
49
+ this means that if you want to find descendant nodes only, you'll need to prefix a dot!
50
+ * `visit` now accepts fully qualified URLs for drivers that support it.
51
+ * Capybara will always try to run a rack server, unless you set Capybara.run_sever = false
52
+
53
+ ### Changed
54
+
55
+ * thin is preferred over mongrel and webrick, since it is Ruby 1.9 compatible
56
+ * click_button and click will find <input type="button">, clicking them does nothing in RackTest
57
+
58
+ ### Added
59
+
60
+ * Much improved error messages in a multitude of places
61
+ * More semantic page querying with has_link?, has_button?, etc...
62
+ * Option to ignore hidden elements when querying and interacting with the page
63
+ * Support for multiple selects
64
+
65
+ ### Fixed
66
+
67
+ * find_by_id is no longer broken
68
+ * clicking links where the image's alt attribute contains the text is now possible
69
+ * within_fieldset and within_table work when the default selector is CSS
70
+ * boolean attributes work the same across drivers (return true/false)
71
+
@@ -0,0 +1,388 @@
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. It is inspired by and aims to replace Webrat as
9
+ a DSL for interacting with a webapplication. It is agnostic about the driver
10
+ running your tests and currently comes bundled with rack-test, Culerity,
11
+ Celerity and Selenium support built in.
12
+
13
+ == Install:
14
+
15
+ Install as a gem:
16
+
17
+ sudo gem install capybara
18
+
19
+ On OSX you may have to install libffi, you can install it via MacPorts with:
20
+
21
+ sudo port install libffi
22
+
23
+ == Development:
24
+
25
+ * Source hosted at {GitHub}[http://github.com/jnicklas/capybara].
26
+ * Please direct questions, discussions at the {mailing list}[http://groups.google.com/group/ruby-capybara].
27
+ * Report issues on {GitHub Issues}[http://github.com/jnicklas/capybara/issues]
28
+
29
+ Pull requests are very welcome! Make sure your patches are well tested, Capybara is
30
+ a testing tool after all.
31
+
32
+ == Using Capybara with Cucumber
33
+
34
+ Capybara is built to work nicely with Cucumber. The API is very similar to
35
+ Webrat, so if you know Webrat you should feel right at home. Support for
36
+ Capybara is built into cucumber-rails 0.2. In your Rails app, just run:
37
+
38
+ script/generate cucumber --capybara
39
+
40
+ And everything should be set up and ready to go.
41
+
42
+ If you want to use Capybara with Cucumber outside Rails (for example with Merb
43
+ or Sinatra), you'll need require capybara and set the Rack app manually:
44
+
45
+ require 'capybara/cucumber'
46
+ Capybara.app = MyRackApp
47
+
48
+ Now you can use it in your steps:
49
+
50
+ When /I sign in/ do
51
+ within("//form[@id='session']") do
52
+ fill_in 'Login', :with => 'user@example.com'
53
+ fill_in 'Password', :with => 'password'
54
+ end
55
+ click_link 'Sign in'
56
+ end
57
+
58
+ Please note that while Capybara uses XPath selectors by default, Cucumber explicitly
59
+ changes this to CSS in `env.rb`. See "XPath and CSS" below.
60
+
61
+ == Default and current driver
62
+
63
+ You can set up a default driver for your features. For example if you'd prefer
64
+ to run Selenium, you could do:
65
+
66
+ require 'capybara/rails'
67
+ require 'capybara/cucumber'
68
+ Capybara.default_driver = :selenium
69
+
70
+ You can change the driver temporarily:
71
+
72
+ Capybara.current_driver = :culerity
73
+ Capybara.use_default_driver
74
+
75
+ You can do this in Before and After blocks to temporarily switch to a different
76
+ driver. Note that switching driver creates a new session, so you may not be able
77
+ to switch in the middle of a Scenario.
78
+
79
+ == Cucumber and Tags
80
+
81
+ Capybara sets up some {tags}[http://wiki.github.com/aslakhellesoy/cucumber/tags]
82
+ for you to use in Cucumber. Often you'll want to run only some scenarios with a
83
+ driver that supports JavaScript, Capybara makes this easy: simply tag the
84
+ scenario (or feature) with <tt>@javascript</tt>:
85
+
86
+ @javascript
87
+ Scenario: do something AJAXy
88
+ When I click the AJAX link
89
+ ...
90
+
91
+ You can change which driver Capybara uses for JavaScript:
92
+
93
+ Capybara.javascript_driver = :culerity
94
+
95
+ There are also explicit <tt>@selenium</tt>, <tt>@culerity</tt> and
96
+ <tt>@rack_test</tt> tags set up for you.
97
+
98
+ == Selenium
99
+
100
+ At the moment, Capybara supports Webdriver, also called Selenium 2.0, *not*
101
+ Selenium RC. Provided Firefox is installed, everything is set up for you, and
102
+ you should be able to start using Selenium right away.
103
+
104
+ If desired, you can change Selenium browser to :chrome or :ie:
105
+
106
+ require "selenium-webdriver"
107
+ Selenium::WebDriver.for :chrome
108
+
109
+ == Celerity
110
+
111
+ Celerity only runs on JRuby, so you'll need to install the celerity gem under
112
+ JRuby:
113
+
114
+ jruby -S gem install celerity
115
+
116
+ Note that some specs currently fail on celerity 0.7.5, due to a bug in recent
117
+ versions of HTMLUnit. It is recommended you use celerity 0.7.4 for the time
118
+ being.
119
+
120
+ == Culerity
121
+
122
+ Install celerity as noted above, make sure JRuby is in your path. Note that
123
+ Culerity doesn't seem to be working under Ruby 1.9 at the moment.
124
+
125
+ == The DSL
126
+
127
+ Capybara's DSL is inspired by Webrat. While backwards compatibility is retained
128
+ in a lot of cases, there are certain important differences.
129
+
130
+ Unlike in Webrat, all searches in Capybara are *case sensitive*. This is because
131
+ Capybara heavily uses XPath, which doesn't support case insensitivity.
132
+
133
+ === Navigating
134
+
135
+ You can use the <tt>visit</tt> method to navigate to other pages:
136
+
137
+ visit('/projects')
138
+ visit(post_comments_path(post))
139
+
140
+ The visit method only takes a single parameter, the request method is *always*
141
+ GET.
142
+
143
+ === Clicking links and buttons
144
+
145
+ You can interact with the webapp by following links and buttons. Capybara
146
+ automatically follows any redirects, and submits forms associated with buttons.
147
+
148
+ click_link('id-of-link')
149
+ click_link('Link Text')
150
+ click_button('Save')
151
+ click('Link Text') # Click either a link or a button
152
+ click('Button Value')
153
+
154
+ === Interacting with forms
155
+
156
+ Forms are everywhere in webapps, there are a number of tools for interacting
157
+ with the various form elements:
158
+
159
+ fill_in('First Name', :with => 'John')
160
+ fill_in('Password', :with => 'Seekrit')
161
+ fill_in('Description', :with => 'Really Long Text…')
162
+ choose('A Radio Button')
163
+ check('A Checkbox')
164
+ uncheck('A Checkbox')
165
+ attach_file('Image', '/path/to/image.jpg')
166
+ select('Option', :from => 'Select Box')
167
+
168
+ === Scoping
169
+
170
+ Capybara makes it possible to restrict certain actions, such as interacting with
171
+ forms or clicking links and buttons, to within a specific area of the page. For
172
+ this purpose you can use the generic <tt>within</tt> method. Optionally you can
173
+ specify which kind of selector (CSS or XPath to use).
174
+
175
+ within("//li[@id='employee']") do
176
+ fill_in 'Name', :with => 'Jimmy'
177
+ end
178
+
179
+ within(:css, "li#employee") do
180
+ fill_in 'Name', :with => 'Jimmy'
181
+ end
182
+
183
+ You can choose which kind of selector Capybara uses by default, by setting
184
+ <tt>Capybara.default_selector</tt>.
185
+
186
+ There are special methods for restricting the scope to a specific fieldset,
187
+ identified by either an id or the text of the fieldet's legend tag, and to a
188
+ specific table, identified by either id or text of the table's caption tag.
189
+
190
+ within_fieldset('Employee') do
191
+ fill_in 'Name', :with => 'Jimmy'
192
+ end
193
+
194
+ within_table('Employee') do
195
+ fill_in 'Name', :with => 'Jimmy'
196
+ end
197
+
198
+ === Querying
199
+
200
+ Capybara has a rich set of options for querying the page for the existence of
201
+ certain elements, and working with and manipulating those elements.
202
+
203
+ page.has_xpath?('//table/tr')
204
+ page.has_css?('table tr.foo')
205
+ page.has_content?('foo')
206
+
207
+ You can use with RSpecs magic matchers:
208
+
209
+ page.should have_xpath('//table/tr')
210
+ page.should have_css('table tr.foo')
211
+ page.should have_content('foo')
212
+ page.should have_no_content('foo')
213
+
214
+ Note that <tt>page.should have_no_xpath</tt> is preferred over
215
+ <tt>page.should_not have_xpath</tt>. Read the section on asynchronous JavaScript
216
+ for an explanation.
217
+
218
+ You can also find specific elements, in order to manipulate them:
219
+
220
+ find_field('First Name').value
221
+ find_link('Hello').visible?
222
+ find_button('Send').click
223
+
224
+ find('//table/tr').click
225
+ locate("//*[@id='overlay'").find("//h1").click
226
+ all('a').each { |a| a[:href] }
227
+
228
+ === Scripting
229
+
230
+ In drivers which support it, you can easily evaluate JavaScript:
231
+
232
+ result = page.evaluate_script('4 + 4');
233
+
234
+ === Debugging
235
+
236
+ It can be useful to take a snapshot of the page as it currently is and take a
237
+ look at it:
238
+
239
+ save_and_open_page
240
+
241
+ == Asynchronous JavaScript (AJAX and friends)
242
+
243
+ When working with asynchronous JavaScript, you might come across situations
244
+ where you are attempting to interact with an element which is not yet present
245
+ on the page. Capybara automatically deals with this by waiting for elements
246
+ to appear on the page.
247
+
248
+ When issuing instructions to the DSL such as:
249
+
250
+ click_link('foo')
251
+ click_link('bar')
252
+ page.should have_content('baz')
253
+
254
+ If clicking on the *foo* link causes triggers an asynchronous process, such as
255
+ an AJAX request, which, when complete will add the *bar* link to the page,
256
+ clicking on the *bar* link would be expeced to fail, since that link doesn't
257
+ exist yet. However Capybara is smart enought to retry finding the link for a
258
+ brief period of time before giving up and throwing an error. The same is true of
259
+ the next line, which looks for the content *baz* on the page; it will retry
260
+ looking for that content for a brief time. You can adjust how long this period
261
+ is (the default is 2 seconds):
262
+
263
+ Capybara.default_wait_time = 5
264
+
265
+ Be aware that because of this behaviour, the following two statements are *not*
266
+ equivalent, and you should *always* use the latter!
267
+
268
+ page.should_not have_xpath('//a')
269
+ page.should have_no_xpath('//a')
270
+
271
+ The former would incorrectly wait for the content to appear, since the
272
+ asynchronous process has not yet removed the element from the page, it would
273
+ therefore fail, even though the code might be working correctly. The latter
274
+ correctly waits for the element to disappear from the page.
275
+
276
+ == Using the DSL outside cucumber
277
+
278
+ You can mix the DSL into any context, for example you could use it in RSpec
279
+ examples. Just load the DSL and include it anywhere:
280
+
281
+ require 'capybara'
282
+ require 'capybara/dsl'
283
+
284
+ Capybara.default_driver = :culerity
285
+
286
+ module MyModule
287
+ include Capybara
288
+
289
+ def login!
290
+ within("//form[@id='session']") do
291
+ fill_in 'Login', :with => 'user@example.com'
292
+ fill_in 'Password', :with => 'password'
293
+ end
294
+ click_link 'Sign in'
295
+ end
296
+ end
297
+
298
+ == Calling remote servers
299
+
300
+ 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:
301
+
302
+ Capybara.current_driver = :selenium
303
+ Capybara.app_host = 'http://www.google.com'
304
+ ...
305
+ visit('/')
306
+
307
+ Note that rack-test does not support running against a remote server. With drivers that support it, you can also visit any URL directly:
308
+
309
+ visit('http://www.google.com')
310
+
311
+ 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:
312
+
313
+ Capybara.run_server = false
314
+
315
+ == Using the sessions manually
316
+
317
+ For ultimate control, you can instantiate and use a session manually.
318
+
319
+ require 'capybara'
320
+
321
+ session = Capybara::Session.new(:culerity, my_rack_app)
322
+ session.within("//form[@id='session']") do
323
+ session.fill_in 'Login', :with => 'user@example.com'
324
+ session.fill_in 'Password', :with => 'password'
325
+ end
326
+ session.click_link 'Sign in'
327
+
328
+ == XPath and CSS
329
+
330
+ Capybara does not try to guess what kind of selector you are going to give it,
331
+ if you want to use CSS with your 'within' declarations for example, you'll need
332
+ to do:
333
+
334
+ within(:css, 'ul li') { ... }
335
+ find(:css, 'ul li').text
336
+ locate(:css, 'input#name').value
337
+
338
+ Alternatively you can set the default selector to CSS, which may help if you are
339
+ moving from Webrat and used CSS a lot, or simply generally prefer CSS:
340
+
341
+ Capybara.default_selector = :css
342
+ within('ul li') { ... }
343
+ find('ul li').text
344
+ locate('input#name').value
345
+
346
+ == Gotchas:
347
+
348
+ * Domain names (including subdomains) don't work under rack-test. Since it's a
349
+ pain to set up subdomains for the other drivers anyway, you should consider an
350
+ alternate solution. You might use
351
+ {default_url_options}[https://gist.github.com/643a758320a2926bd2ed] in Rails
352
+ for example.
353
+
354
+ * Access to session, request and response from the test is not possible. Maybe
355
+ we'll do response headers at some point in the future, but the others really
356
+ shouldn't be touched in an integration test anyway.
357
+
358
+ * Access to Rails specific stuff (such as <tt>controller</tt>) is unavailable,
359
+ since we're not using Rails' integration testing.
360
+
361
+ * <tt><a href="#"></tt> Will cause problems under rack-test, please do
362
+ <tt><a href="/same/url#"></tt> instead. You can achieve this in Rails with
363
+ <tt>link_to('foo', :anchor => '')</tt>
364
+
365
+ == License:
366
+
367
+ (The MIT License)
368
+
369
+ Copyright (c) 2009 Jonas Nicklas
370
+
371
+ Permission is hereby granted, free of charge, to any person obtaining
372
+ a copy of this software and associated documentation files (the
373
+ 'Software'), to deal in the Software without restriction, including
374
+ without limitation the rights to use, copy, modify, merge, publish,
375
+ distribute, sublicense, and/or sell copies of the Software, and to
376
+ permit persons to whom the Software is furnished to do so, subject to
377
+ the following conditions:
378
+
379
+ The above copyright notice and this permission notice shall be
380
+ included in all copies or substantial portions of the Software.
381
+
382
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
383
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
384
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
385
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
386
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
387
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
388
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.