rcarvalho-capybara 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 +78 -0
  9. data/lib/capybara/driver/rack_test_driver.rb +303 -0
  10. data/lib/capybara/driver/selenium_driver.rb +165 -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 +182 -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 +17 -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 +343 -0
@@ -0,0 +1,78 @@
1
+ shared_examples_for "all" do
2
+ describe '#all' do
3
+ before do
4
+ @session.visit('/with_html')
5
+ end
6
+
7
+ it "should find all elements using the given locator" do
8
+ @session.all('//p').should have(3).elements
9
+ @session.all('//h1').first.text.should == 'This is a test'
10
+ @session.all("//input[@id='test_field']").first[:value].should == 'monkey'
11
+ end
12
+
13
+ it "should return an empty array when nothing was found" do
14
+ @session.all('//div[@id="nosuchthing"]').should be_empty
15
+ end
16
+
17
+ it "should accept an XPath instance" do
18
+ @session.visit('/form')
19
+ @xpath = XPath::HTML.fillable_field('Name')
20
+ @result = @session.all(@xpath).map { |r| r.value }
21
+ @result.should include('Smith', 'John', 'John Smith')
22
+ end
23
+
24
+ context "with css selectors" do
25
+ it "should find all elements using the given selector" do
26
+ @session.all(:css, 'h1').first.text.should == 'This is a test'
27
+ @session.all(:css, "input[id='test_field']").first[:value].should == 'monkey'
28
+ end
29
+
30
+ it "should find all elements when given a list of selectors" do
31
+ @session.all(:css, 'h1, p').should have(4).elements
32
+ end
33
+ end
34
+
35
+ context "with xpath selectors" do
36
+ it "should find the first element using the given locator" do
37
+ @session.all(:xpath, '//h1').first.text.should == 'This is a test'
38
+ @session.all(:xpath, "//input[@id='test_field']").first[:value].should == 'monkey'
39
+ end
40
+ end
41
+
42
+ context "with css as default selector" do
43
+ before { Capybara.default_selector = :css }
44
+ it "should find the first element using the given locator" do
45
+ @session.all('h1').first.text.should == 'This is a test'
46
+ @session.all("input[id='test_field']").first[:value].should == 'monkey'
47
+ end
48
+ after { Capybara.default_selector = :xpath }
49
+ end
50
+
51
+ context "with visible filter" do
52
+ after { Capybara.ignore_hidden_elements = false }
53
+ it "should only find visible nodes" do
54
+ @session.all("//a[@title='awesome title']").should have(2).elements
55
+ @session.all("//a[@title='awesome title']", :visible => true).should have(1).elements
56
+ Capybara.ignore_hidden_elements = true
57
+ @session.all("//a[@title='awesome title']").should have(1).elements
58
+ end
59
+
60
+ it "should only find invisible nodes" do
61
+ Capybara.ignore_hidden_elements = true
62
+ @session.all("//a[@title='awesome title']", :visible => false).should have(2).elements
63
+ end
64
+ end
65
+
66
+ context "within a scope" do
67
+ before do
68
+ @session.visit('/with_scope')
69
+ end
70
+
71
+ it "should find any element using the given locator" do
72
+ @session.within(:xpath, "//div[@id='for_bar']") do
73
+ @session.all('.//li').should have(2).elements
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,70 @@
1
+ shared_examples_for "attach_file" do
2
+
3
+ describe "#attach_file" do
4
+ before do
5
+ @session.visit('/form')
6
+ end
7
+
8
+ context "with normal form" do
9
+ it "should set a file path by id" do
10
+ @session.attach_file "form_image", __FILE__
11
+ @session.click_button('awesome')
12
+ extract_results(@session)['image'].should == File.basename(__FILE__)
13
+ end
14
+
15
+ it "should set a file path by label" do
16
+ @session.attach_file "Image", __FILE__
17
+ @session.click_button('awesome')
18
+ extract_results(@session)['image'].should == File.basename(__FILE__)
19
+ end
20
+ end
21
+
22
+ context "with multipart form" do
23
+ before do
24
+ @test_file_path = File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__))
25
+ @test_jpg_file_path = File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__))
26
+ end
27
+
28
+ it "should set a file path by id" do
29
+ @session.attach_file "form_document", @test_file_path
30
+ @session.click_button('Upload')
31
+ @session.body.should include(File.read(@test_file_path))
32
+ end
33
+
34
+ it "should set a file path by label" do
35
+ @session.attach_file "Document", @test_file_path
36
+ @session.click_button('Upload')
37
+ @session.body.should include(File.read(@test_file_path))
38
+ end
39
+
40
+ it "should not break if no file is submitted" do
41
+ @session.click_button('Upload')
42
+ @session.body.should include('No file uploaded')
43
+ end
44
+
45
+ it "should send content type text/plain when uploading a text file" do
46
+ @session.attach_file "Document", @test_file_path
47
+ @session.click_button 'Upload'
48
+ @session.body.should include('text/plain')
49
+ end
50
+
51
+ it "should send content type image/jpeg when uploading an image" do
52
+ @session.attach_file "Document", @test_jpg_file_path
53
+ @session.click_button 'Upload'
54
+ @session.body.should include('image/jpeg')
55
+ end
56
+
57
+ it "should not break when using HTML5 multiple file input" do
58
+ @session.attach_file "Multiple Documents", @test_file_path
59
+ @session.click_button('Upload Multiple')
60
+ @session.body.should include(File.read(@test_file_path))
61
+ end
62
+ end
63
+
64
+ context "with a locator that doesn't exist" do
65
+ it "should raise an error" do
66
+ running { @session.attach_file('does not exist', 'foo.txt') }.should raise_error(Capybara::ElementNotFound)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,65 @@
1
+ shared_examples_for "check" do
2
+
3
+ describe "#check" do
4
+ before do
5
+ @session.visit('/form')
6
+ end
7
+
8
+ describe "'checked' attribute" do
9
+ it "should be true if checked" do
10
+ @session.check("Terms of Use")
11
+ @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_true
12
+ end
13
+
14
+ it "should be false if unchecked" do
15
+ @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_false
16
+ end
17
+ end
18
+
19
+ describe "checking" do
20
+ it "should not change an already checked checkbox" do
21
+ @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
22
+ @session.check('form_pets_dog')
23
+ @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
24
+ end
25
+
26
+ it "should check an unchecked checkbox" do
27
+ @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
28
+ @session.check('form_pets_cat')
29
+ @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_true
30
+ end
31
+ end
32
+
33
+ describe "unchecking" do
34
+ it "should not change an already unchecked checkbox" do
35
+ @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
36
+ @session.uncheck('form_pets_cat')
37
+ @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
38
+ end
39
+
40
+ it "should uncheck a checked checkbox" do
41
+ @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
42
+ @session.uncheck('form_pets_dog')
43
+ @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_false
44
+ end
45
+ end
46
+
47
+ it "should check a checkbox by id" do
48
+ @session.check("form_pets_cat")
49
+ @session.click_button('awesome')
50
+ extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
51
+ end
52
+
53
+ it "should check a checkbox by label" do
54
+ @session.check("Cat")
55
+ @session.click_button('awesome')
56
+ extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
57
+ end
58
+
59
+ context "with a locator that doesn't exist" do
60
+ it "should raise an error" do
61
+ running { @session.check('does not exist') }.should raise_error(Capybara::ElementNotFound)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ shared_examples_for "choose" do
2
+
3
+ describe "#choose" do
4
+ before do
5
+ @session.visit('/form')
6
+ end
7
+
8
+ it "should choose a radio button by id" do
9
+ @session.choose("gender_male")
10
+ @session.click_button('awesome')
11
+ extract_results(@session)['gender'].should == 'male'
12
+ end
13
+
14
+ it "should choose a radio button by label" do
15
+ @session.choose("Both")
16
+ @session.click_button('awesome')
17
+ extract_results(@session)['gender'].should == 'both'
18
+ end
19
+
20
+ context "with a locator that doesn't exist" do
21
+ it "should raise an error" do
22
+ running { @session.choose('does not exist') }.should raise_error(Capybara::ElementNotFound)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,252 @@
1
+ shared_examples_for "click_button" do
2
+ describe '#click_button' do
3
+ before do
4
+ @session.visit('/form')
5
+ end
6
+
7
+ context "with multiple values with the same name" do
8
+ it "should use the latest given value" do
9
+ @session.check('Terms of Use')
10
+ @session.click_button('awesome')
11
+ extract_results(@session)['terms_of_use'].should == '1'
12
+ end
13
+ end
14
+
15
+ context "with value given on a submit button" do
16
+ context "on a form with HTML5 fields" do
17
+ before do
18
+ @session.click_button('html5_submit')
19
+ @results = extract_results(@session)
20
+ end
21
+
22
+ it "should serialise and submit search fields" do
23
+ @results['html5_search'].should == 'what are you looking for'
24
+ end
25
+
26
+ it "should serialise and submit email fields" do
27
+ @results['html5_email'].should == 'person@email.com'
28
+ end
29
+
30
+ it "should serialise and submit url fields" do
31
+ @results['html5_url'].should == 'http://www.example.com'
32
+ end
33
+
34
+ it "should serialise and submit tel fields" do
35
+ @results['html5_tel'].should == '911'
36
+ end
37
+
38
+ it "should serialise and submit color fields" do
39
+ @results['html5_color'].should == '#FFF'
40
+ end
41
+ end
42
+
43
+ context "on an HTML4 form" do
44
+ before do
45
+ @session.click_button('awesome')
46
+ @results = extract_results(@session)
47
+ end
48
+
49
+ it "should serialize and submit text fields" do
50
+ @results['first_name'].should == 'John'
51
+ end
52
+
53
+ it "should escape fields when submitting" do
54
+ @results['phone'].should == '+1 555 7021'
55
+ end
56
+
57
+ it "should serialize and submit password fields" do
58
+ @results['password'].should == 'seeekrit'
59
+ end
60
+
61
+ it "should serialize and submit hidden fields" do
62
+ @results['token'].should == '12345'
63
+ end
64
+
65
+ it "should not serialize fields from other forms" do
66
+ @results['middle_name'].should be_nil
67
+ end
68
+
69
+ it "should submit the button that was clicked, but not other buttons" do
70
+ @results['awesome'].should == 'awesome'
71
+ @results['crappy'].should be_nil
72
+ end
73
+
74
+ it "should serialize radio buttons" do
75
+ @results['gender'].should == 'female'
76
+ end
77
+
78
+ it "should serialize check boxes" do
79
+ @results['pets'].should include('dog', 'hamster')
80
+ @results['pets'].should_not include('cat')
81
+ end
82
+
83
+ it "should serialize text areas" do
84
+ @results['description'].should == 'Descriptive text goes here'
85
+ end
86
+
87
+ it "should serialize select tag with values" do
88
+ @results['locale'].should == 'en'
89
+ end
90
+
91
+ it "should serialize select tag without values" do
92
+ @results['region'].should == 'Norway'
93
+ end
94
+
95
+ it "should serialize first option for select tag with no selection" do
96
+ @results['city'].should == 'London'
97
+ end
98
+
99
+ it "should not serialize a select tag without options" do
100
+ @results['tendency'].should be_nil
101
+ end
102
+
103
+ it "should not submit disabled fields" do
104
+ @results['disabled_text_field'].should be_nil
105
+ @results['disabled_textarea'].should be_nil
106
+ @results['disabled_checkbox'].should be_nil
107
+ @results['disabled_radio'].should be_nil
108
+ @results['disabled_select'].should be_nil
109
+ @results['disabled_file'].should be_nil
110
+ end
111
+ end
112
+ end
113
+
114
+ context "with id given on a submit button" do
115
+ it "should submit the associated form" do
116
+ @session.click_button('awe123')
117
+ extract_results(@session)['first_name'].should == 'John'
118
+ end
119
+
120
+ it "should work with partial matches" do
121
+ @session.click_button('Go')
122
+ @session.body.should include('You landed')
123
+ end
124
+ end
125
+
126
+ context "with alt given on an image button" do
127
+ it "should submit the associated form" do
128
+ @session.click_button('oh hai thar')
129
+ extract_results(@session)['first_name'].should == 'John'
130
+ end
131
+
132
+ it "should work with partial matches" do
133
+ @session.click_button('hai')
134
+ extract_results(@session)['first_name'].should == 'John'
135
+ end
136
+ end
137
+
138
+ context "with value given on an image button" do
139
+ it "should submit the associated form" do
140
+ @session.click_button('okay')
141
+ extract_results(@session)['first_name'].should == 'John'
142
+ end
143
+
144
+ it "should work with partial matches" do
145
+ @session.click_button('kay')
146
+ extract_results(@session)['first_name'].should == 'John'
147
+ end
148
+ end
149
+
150
+ context "with id given on an image button" do
151
+ it "should submit the associated form" do
152
+ @session.click_button('okay556')
153
+ extract_results(@session)['first_name'].should == 'John'
154
+ end
155
+ end
156
+
157
+ context "with text given on a button defined by <button> tag" do
158
+ it "should submit the associated form" do
159
+ @session.click_button('Click me')
160
+ extract_results(@session)['first_name'].should == 'John'
161
+ end
162
+
163
+ it "should work with partial matches" do
164
+ @session.click_button('Click')
165
+ extract_results(@session)['first_name'].should == 'John'
166
+ end
167
+
168
+ it "should prefer exact matches over partial matches" do
169
+ @session.click_button('Just an input')
170
+ extract_results(@session)['button'].should == 'button_second'
171
+ end
172
+ end
173
+
174
+ context "with id given on a button defined by <button> tag" do
175
+ it "should submit the associated form" do
176
+ @session.click_button('click_me_123')
177
+ extract_results(@session)['first_name'].should == 'John'
178
+ end
179
+
180
+ it "should serialize and send GET forms" do
181
+ @session.visit('/form')
182
+ @session.click_button('med')
183
+ @results = extract_results(@session)
184
+ @results['middle_name'].should == 'Darren'
185
+ @results['foo'].should be_nil
186
+ end
187
+ end
188
+
189
+ context "with value given on a button defined by <button> tag" do
190
+ it "should submit the associated form" do
191
+ @session.click_button('click_me')
192
+ extract_results(@session)['first_name'].should == 'John'
193
+ end
194
+
195
+ it "should work with partial matches" do
196
+ @session.click_button('ck_me')
197
+ extract_results(@session)['first_name'].should == 'John'
198
+ end
199
+
200
+ it "should prefer exact matches over partial matches" do
201
+ @session.click_button('Just a button')
202
+ extract_results(@session)['button'].should == 'Just a button'
203
+ end
204
+ end
205
+
206
+ context "with a locator that doesn't exist" do
207
+ it "should raise an error" do
208
+ running do
209
+ @session.click_button('does not exist')
210
+ end.should raise_error(Capybara::ElementNotFound)
211
+ end
212
+ end
213
+
214
+ it "should serialize and send valueless buttons that were clicked" do
215
+ @session.click_button('No Value!')
216
+ @results = extract_results(@session)
217
+ @results['no_value'].should_not be_nil
218
+ end
219
+
220
+ it "should not send image buttons that were not clicked" do
221
+ @session.click_button('Click me!')
222
+ @results = extract_results(@session)
223
+ @results['okay'].should be_nil
224
+ end
225
+
226
+ it "should serialize and send GET forms" do
227
+ @session.visit('/form')
228
+ @session.click_button('med')
229
+ @results = extract_results(@session)
230
+ @results['middle_name'].should == 'Darren'
231
+ @results['foo'].should be_nil
232
+ end
233
+
234
+ it "should follow redirects" do
235
+ @session.click_button('Go FAR')
236
+ @session.current_url.should match(%r{/landed$})
237
+ @session.body.should include('You landed')
238
+ end
239
+
240
+ it "should post pack to the same URL when no action given" do
241
+ @session.visit('/postback')
242
+ @session.click_button('With no action')
243
+ @session.body.should include('Postback')
244
+ end
245
+
246
+ it "should post pack to the same URL when blank action given" do
247
+ @session.visit('/postback')
248
+ @session.click_button('With blank action')
249
+ @session.body.should include('Postback')
250
+ end
251
+ end
252
+ end