david-capybara 0.3.8

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