capybara 3.14.0 → 3.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +13 -0
  3. data/README.md +2 -1
  4. data/lib/capybara/node/actions.rb +37 -6
  5. data/lib/capybara/node/matchers.rb +10 -2
  6. data/lib/capybara/selector.rb +117 -1
  7. data/lib/capybara/selector/selector.rb +7 -0
  8. data/lib/capybara/selector/xpath_extensions.rb +9 -0
  9. data/lib/capybara/selenium/driver.rb +13 -2
  10. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +15 -0
  11. data/lib/capybara/selenium/extensions/find.rb +2 -1
  12. data/lib/capybara/selenium/node.rb +1 -1
  13. data/lib/capybara/selenium/nodes/firefox_node.rb +1 -1
  14. data/lib/capybara/selenium/nodes/safari_node.rb +145 -0
  15. data/lib/capybara/spec/session/attach_file_spec.rb +46 -27
  16. data/lib/capybara/spec/session/click_button_spec.rb +65 -60
  17. data/lib/capybara/spec/session/element/matches_selector_spec.rb +40 -39
  18. data/lib/capybara/spec/session/fill_in_spec.rb +3 -3
  19. data/lib/capybara/spec/session/find_spec.rb +5 -0
  20. data/lib/capybara/spec/session/has_table_spec.rb +120 -0
  21. data/lib/capybara/spec/session/node_spec.rb +3 -3
  22. data/lib/capybara/spec/session/reset_session_spec.rb +8 -7
  23. data/lib/capybara/spec/session/window/become_closed_spec.rb +20 -17
  24. data/lib/capybara/spec/session/window/window_spec.rb +44 -48
  25. data/lib/capybara/spec/views/form.erb +5 -0
  26. data/lib/capybara/spec/views/tables.erb +67 -0
  27. data/lib/capybara/spec/views/with_html.erb +2 -2
  28. data/lib/capybara/spec/views/with_js.erb +1 -0
  29. data/lib/capybara/version.rb +1 -1
  30. data/spec/capybara_spec.rb +4 -4
  31. data/spec/css_builder_spec.rb +2 -0
  32. data/spec/dsl_spec.rb +13 -17
  33. data/spec/rack_test_spec.rb +77 -85
  34. data/spec/rspec/features_spec.rb +2 -0
  35. data/spec/rspec/shared_spec_matchers.rb +34 -35
  36. data/spec/rspec_spec.rb +11 -13
  37. data/spec/selector_spec.rb +31 -0
  38. data/spec/selenium_spec_chrome.rb +25 -25
  39. data/spec/selenium_spec_firefox.rb +62 -35
  40. data/spec/selenium_spec_firefox_remote.rb +2 -0
  41. data/spec/selenium_spec_safari.rb +148 -0
  42. data/spec/server_spec.rb +40 -44
  43. data/spec/shared_selenium_session.rb +27 -21
  44. data/spec/spec_helper.rb +4 -0
  45. data/spec/xpath_builder_spec.rb +2 -0
  46. metadata +7 -3
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Capybara::SpecHelper.spec '#attach_file' do
4
+ let(:test_file_path) { File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__)) }
5
+ let(:another_test_file_path) { File.expand_path('../fixtures/another_test_file.txt', File.dirname(__FILE__)) }
6
+ let(:test_jpg_file_path) { File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__)) }
7
+ let(:no_extension_file_path) { File.expand_path('../fixtures/no_extension', File.dirname(__FILE__)) }
8
+
4
9
  before do
5
- @test_file_path = File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__))
6
- @another_test_file_path = File.expand_path('../fixtures/another_test_file.txt', File.dirname(__FILE__))
7
- @test_jpg_file_path = File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__))
8
- @no_extension_file_path = File.expand_path('../fixtures/no_extension', File.dirname(__FILE__))
9
10
  @session.visit('/form')
10
11
  end
11
12
 
@@ -38,15 +39,15 @@ Capybara::SpecHelper.spec '#attach_file' do
38
39
 
39
40
  context 'with multipart form' do
40
41
  it 'should set a file path by id' do
41
- @session.attach_file 'form_document', with_os_path_separators(@test_file_path)
42
+ @session.attach_file 'form_document', with_os_path_separators(test_file_path)
42
43
  @session.click_button('Upload Single')
43
- expect(@session).to have_content(File.read(@test_file_path))
44
+ expect(@session).to have_content(File.read(test_file_path))
44
45
  end
45
46
 
46
47
  it 'should set a file path by label' do
47
- @session.attach_file 'Single Document', with_os_path_separators(@test_file_path)
48
+ @session.attach_file 'Single Document', with_os_path_separators(test_file_path)
48
49
  @session.click_button('Upload Single')
49
- expect(@session).to have_content(File.read(@test_file_path))
50
+ expect(@session).to have_content(File.read(test_file_path))
50
51
  end
51
52
 
52
53
  it 'should not break if no file is submitted' do
@@ -55,37 +56,37 @@ Capybara::SpecHelper.spec '#attach_file' do
55
56
  end
56
57
 
57
58
  it 'should send content type text/plain when uploading a text file' do
58
- @session.attach_file 'Single Document', with_os_path_separators(@test_file_path)
59
+ @session.attach_file 'Single Document', with_os_path_separators(test_file_path)
59
60
  @session.click_button 'Upload Single'
60
61
  expect(@session).to have_content('text/plain')
61
62
  end
62
63
 
63
64
  it 'should send content type image/jpeg when uploading an image' do
64
- @session.attach_file 'Single Document', with_os_path_separators(@test_jpg_file_path)
65
+ @session.attach_file 'Single Document', with_os_path_separators(test_jpg_file_path)
65
66
  @session.click_button 'Upload Single'
66
67
  expect(@session).to have_content('image/jpeg')
67
68
  end
68
69
 
69
70
  it 'should not break when uploading a file without extension' do
70
- @session.attach_file 'Single Document', with_os_path_separators(@no_extension_file_path)
71
+ @session.attach_file 'Single Document', with_os_path_separators(no_extension_file_path)
71
72
  @session.click_button 'Upload Single'
72
- expect(@session).to have_content(File.read(@no_extension_file_path))
73
+ expect(@session).to have_content(File.read(no_extension_file_path))
73
74
  end
74
75
 
75
76
  it 'should not break when using HTML5 multiple file input' do
76
- @session.attach_file 'Multiple Documents', with_os_path_separators(@test_file_path)
77
+ @session.attach_file 'Multiple Documents', with_os_path_separators(test_file_path)
77
78
  @session.click_button('Upload Multiple')
78
- expect(@session).to have_content(File.read(@test_file_path))
79
+ expect(@session).to have_content(File.read(test_file_path))
79
80
  expect(@session.body).to include('1 | ') # number of files
80
81
  end
81
82
 
82
83
  it 'should not break when using HTML5 multiple file input uploading multiple files' do
83
84
  @session.attach_file('Multiple Documents',
84
- [@test_file_path, @another_test_file_path].map { |f| with_os_path_separators(f) })
85
+ [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) })
85
86
  @session.click_button('Upload Multiple')
86
- expect(@session.body).to include('2 | ') # number of files
87
- expect(@session.body).to include(File.read(@test_file_path))
88
- expect(@session.body).to include(File.read(@another_test_file_path))
87
+ expect(@session).to have_content('2 | ') # number of files
88
+ expect(@session.body).to include(File.read(test_file_path))
89
+ expect(@session.body).to include(File.read(another_test_file_path))
89
90
  end
90
91
 
91
92
  it 'should not send anything when attaching no files to a multiple upload field' do
@@ -94,26 +95,26 @@ Capybara::SpecHelper.spec '#attach_file' do
94
95
  end
95
96
 
96
97
  it 'should not append files to already attached' do
97
- @session.attach_file 'Multiple Documents', with_os_path_separators(@test_file_path)
98
- @session.attach_file 'Multiple Documents', with_os_path_separators(@another_test_file_path)
98
+ @session.attach_file 'Multiple Documents', with_os_path_separators(test_file_path)
99
+ @session.attach_file 'Multiple Documents', with_os_path_separators(another_test_file_path)
99
100
  @session.click_button('Upload Multiple')
100
- expect(@session.body).to include('1 | ') # number of files
101
- expect(@session.body).to include(File.read(@another_test_file_path))
102
- expect(@session.body).not_to include(File.read(@test_file_path))
101
+ expect(@session).to have_content('1 | ') # number of files
102
+ expect(@session.body).to include(File.read(another_test_file_path))
103
+ expect(@session.body).not_to include(File.read(test_file_path))
103
104
  end
104
105
 
105
106
  it 'should fire change once when uploading multiple files from empty', requires: [:js] do
106
107
  @session.visit('with_js')
107
108
  @session.attach_file('multiple-file',
108
- [@test_file_path, @another_test_file_path].map { |f| with_os_path_separators(f) })
109
+ [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) })
109
110
  expect(@session).to have_css('.file_change', count: 1)
110
111
  end
111
112
 
112
113
  it 'should fire change once for each set of files uploaded', requires: [:js] do
113
114
  @session.visit('with_js')
114
- @session.attach_file('multiple-file', [@test_jpg_file_path].map { |f| with_os_path_separators(f) })
115
+ @session.attach_file('multiple-file', [test_jpg_file_path].map { |f| with_os_path_separators(f) })
115
116
  @session.attach_file('multiple-file',
116
- [@test_file_path, @another_test_file_path].map { |f| with_os_path_separators(f) })
117
+ [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) })
117
118
  expect(@session).to have_css('.file_change', count: 2)
118
119
  end
119
120
  end
@@ -122,7 +123,7 @@ Capybara::SpecHelper.spec '#attach_file' do
122
123
  it 'should raise an error' do
123
124
  msg = 'Unable to find file field "does not exist"'
124
125
  expect do
125
- @session.attach_file('does not exist', with_os_path_separators(@test_file_path))
126
+ @session.attach_file('does not exist', with_os_path_separators(test_file_path))
126
127
  end.to raise_error(Capybara::ElementNotFound, msg)
127
128
  end
128
129
  end
@@ -181,6 +182,24 @@ Capybara::SpecHelper.spec '#attach_file' do
181
182
  end
182
183
  end
183
184
 
185
+ context 'with a block', requires: %i[js] do
186
+ it 'can upload by clicking the file input' do
187
+ @session.attach_file(with_os_path_separators(__FILE__)) do
188
+ @session.find(:file_field, 'form[image]').click
189
+ end
190
+ @session.click_button('awesome')
191
+ expect(extract_results(@session)['image']).to end_with(File.basename(__FILE__))
192
+ end
193
+
194
+ it 'can upload by clicking the label' do
195
+ @session.attach_file(with_os_path_separators(__FILE__)) do
196
+ @session.find(:label, 'Hidden Image').click
197
+ end
198
+ @session.click_button('awesome')
199
+ expect(extract_results(@session)['hidden_image']).to end_with(File.basename(__FILE__))
200
+ end
201
+ end
202
+
184
203
  private
185
204
 
186
205
  def with_os_path_separators(path)
@@ -45,111 +45,113 @@ Capybara::SpecHelper.spec '#click_button' do
45
45
 
46
46
  context 'with value given on a submit button' do
47
47
  context 'on a form with HTML5 fields' do
48
+ let(:results) { extract_results(@session) }
49
+
48
50
  before do
49
51
  @session.click_button('html5_submit')
50
- @results = extract_results(@session)
51
52
  end
52
53
 
53
54
  it 'should serialise and submit search fields' do
54
- expect(@results['html5_search']).to eq('what are you looking for')
55
+ expect(results['html5_search']).to eq('what are you looking for')
55
56
  end
56
57
 
57
58
  it 'should serialise and submit email fields' do
58
- expect(@results['html5_email']).to eq('person@email.com')
59
+ expect(results['html5_email']).to eq('person@email.com')
59
60
  end
60
61
 
61
62
  it 'should serialise and submit url fields' do
62
- expect(@results['html5_url']).to eq('http://www.example.com')
63
+ expect(results['html5_url']).to eq('http://www.example.com')
63
64
  end
64
65
 
65
66
  it 'should serialise and submit tel fields' do
66
- expect(@results['html5_tel']).to eq('911')
67
+ expect(results['html5_tel']).to eq('911')
67
68
  end
68
69
 
69
70
  it 'should serialise and submit color fields' do
70
- expect(@results['html5_color'].upcase).to eq('#FFFFFF')
71
+ expect(results['html5_color'].upcase).to eq('#FFFFFF')
71
72
  end
72
73
  end
73
74
 
74
75
  context 'on an HTML4 form' do
76
+ let(:results) { extract_results(@session) }
77
+
75
78
  before do
76
79
  @session.click_button('awesome')
77
- @results = extract_results(@session)
78
80
  end
79
81
 
80
82
  it 'should serialize and submit text fields' do
81
- expect(@results['first_name']).to eq('John')
83
+ expect(results['first_name']).to eq('John')
82
84
  end
83
85
 
84
86
  it 'should escape fields when submitting' do
85
- expect(@results['phone']).to eq('+1 555 7021')
87
+ expect(results['phone']).to eq('+1 555 7021')
86
88
  end
87
89
 
88
90
  it 'should serialize and submit password fields' do
89
- expect(@results['password']).to eq('seeekrit')
91
+ expect(results['password']).to eq('seeekrit')
90
92
  end
91
93
 
92
94
  it 'should serialize and submit hidden fields' do
93
- expect(@results['token']).to eq('12345')
95
+ expect(results['token']).to eq('12345')
94
96
  end
95
97
 
96
98
  it 'should not serialize fields from other forms' do
97
- expect(@results['middle_name']).to be_nil
99
+ expect(results['middle_name']).to be_nil
98
100
  end
99
101
 
100
102
  it 'should submit the button that was clicked, but not other buttons' do
101
- expect(@results['awesome']).to eq('awesome')
102
- expect(@results['crappy']).to be_nil
103
+ expect(results['awesome']).to eq('awesome')
104
+ expect(results['crappy']).to be_nil
103
105
  end
104
106
 
105
107
  it 'should serialize radio buttons' do
106
- expect(@results['gender']).to eq('female')
108
+ expect(results['gender']).to eq('female')
107
109
  end
108
110
 
109
111
  it "should default radio value to 'on' if none specified" do
110
- expect(@results['valueless_radio']).to eq('on')
112
+ expect(results['valueless_radio']).to eq('on')
111
113
  end
112
114
 
113
115
  it 'should serialize check boxes' do
114
- expect(@results['pets']).to include('dog', 'hamster')
115
- expect(@results['pets']).not_to include('cat')
116
+ expect(results['pets']).to include('dog', 'hamster')
117
+ expect(results['pets']).not_to include('cat')
116
118
  end
117
119
 
118
120
  it "should default checkbox value to 'on' if none specififed" do
119
- expect(@results['valueless_checkbox']).to eq('on')
121
+ expect(results['valueless_checkbox']).to eq('on')
120
122
  end
121
123
 
122
124
  it 'should serialize text areas' do
123
- expect(@results['description']).to eq('Descriptive text goes here')
125
+ expect(results['description']).to eq('Descriptive text goes here')
124
126
  end
125
127
 
126
128
  it 'should serialize select tag with values' do
127
- expect(@results['locale']).to eq('en')
129
+ expect(results['locale']).to eq('en')
128
130
  end
129
131
 
130
132
  it 'should serialize select tag without values' do
131
- expect(@results['region']).to eq('Norway')
133
+ expect(results['region']).to eq('Norway')
132
134
  end
133
135
 
134
136
  it 'should serialize first option for select tag with no selection' do
135
- expect(@results['city']).to eq('London')
137
+ expect(results['city']).to eq('London')
136
138
  end
137
139
 
138
140
  it 'should not serialize a select tag without options' do
139
- expect(@results['tendency']).to be_nil
141
+ expect(results['tendency']).to be_nil
140
142
  end
141
143
 
142
144
  it 'should convert lf to cr/lf in submitted textareas' do
143
- expect(@results['newline']).to eq("\r\nNew line after and before textarea tag\r\n")
145
+ expect(results['newline']).to eq("\r\nNew line after and before textarea tag\r\n")
144
146
  end
145
147
 
146
148
  it 'should not submit disabled fields' do
147
- expect(@results['disabled_text_field']).to be_nil
148
- expect(@results['disabled_textarea']).to be_nil
149
- expect(@results['disabled_checkbox']).to be_nil
150
- expect(@results['disabled_radio']).to be_nil
151
- expect(@results['disabled_select']).to be_nil
152
- expect(@results['disabled_file']).to be_nil
149
+ expect(results['disabled_text_field']).to be_nil
150
+ expect(results['disabled_textarea']).to be_nil
151
+ expect(results['disabled_checkbox']).to be_nil
152
+ expect(results['disabled_radio']).to be_nil
153
+ expect(results['disabled_select']).to be_nil
154
+ expect(results['disabled_file']).to be_nil
153
155
  end
154
156
  end
155
157
  end
@@ -172,57 +174,60 @@ Capybara::SpecHelper.spec '#click_button' do
172
174
  end
173
175
 
174
176
  context 'with fields associated with the form using the form attribute', requires: [:form_attribute] do
177
+ let(:results) { extract_results(@session) }
178
+
175
179
  before do
176
180
  @session.click_button('submit_form1')
177
- @results = extract_results(@session)
178
181
  end
179
182
 
180
183
  it 'should serialize and submit text fields' do
181
- expect(@results['outside_input']).to eq('outside_input')
184
+ expect(results['outside_input']).to eq('outside_input')
182
185
  end
183
186
 
184
187
  it 'should serialize text areas' do
185
- expect(@results['outside_textarea']).to eq('Some text here')
188
+ expect(results['outside_textarea']).to eq('Some text here')
186
189
  end
187
190
 
188
191
  it 'should serialize select tags' do
189
- expect(@results['outside_select']).to eq('Ruby')
192
+ expect(results['outside_select']).to eq('Ruby')
190
193
  end
191
194
 
192
195
  it 'should not serliaze fields associated with a different form' do
193
- expect(@results['for_form2']).to be_nil
196
+ expect(results['for_form2']).to be_nil
194
197
  end
195
198
  end
196
199
 
197
200
  context 'with submit button outside the form defined by <button> tag', requires: [:form_attribute] do
201
+ let(:results) { extract_results(@session) }
202
+
198
203
  before do
199
204
  @session.click_button('outside_button')
200
- @results = extract_results(@session)
201
205
  end
202
206
 
203
207
  it 'should submit the associated form' do
204
- expect(@results['which_form']).to eq('form2')
208
+ expect(results['which_form']).to eq('form2')
205
209
  end
206
210
 
207
211
  it 'should submit the button that was clicked, but not other buttons' do
208
- expect(@results['outside_button']).to eq('outside_button')
209
- expect(@results['unused']).to be_nil
212
+ expect(results['outside_button']).to eq('outside_button')
213
+ expect(results['unused']).to be_nil
210
214
  end
211
215
  end
212
216
 
213
217
  context "with submit button outside the form defined by <input type='submit'> tag", requires: [:form_attribute] do
218
+ let(:results) { extract_results(@session) }
219
+
214
220
  before do
215
221
  @session.click_button('outside_submit')
216
- @results = extract_results(@session)
217
222
  end
218
223
 
219
224
  it 'should submit the associated form' do
220
- expect(@results['which_form']).to eq('form1')
225
+ expect(results['which_form']).to eq('form1')
221
226
  end
222
227
 
223
228
  it 'should submit the button that was clicked, but not other buttons' do
224
- expect(@results['outside_submit']).to eq('outside_submit')
225
- expect(@results['submit_form1']).to be_nil
229
+ expect(results['outside_submit']).to eq('outside_submit')
230
+ expect(results['submit_form1']).to be_nil
226
231
  end
227
232
  end
228
233
 
@@ -303,9 +308,9 @@ Capybara::SpecHelper.spec '#click_button' do
303
308
  it 'should serialize and send GET forms' do
304
309
  @session.visit('/form')
305
310
  @session.click_button('med')
306
- @results = extract_results(@session)
307
- expect(@results['middle_name']).to eq('Darren')
308
- expect(@results['foo']).to be_nil
311
+ results = extract_results(@session)
312
+ expect(results['middle_name']).to eq('Darren')
313
+ expect(results['foo']).to be_nil
309
314
  end
310
315
  end
311
316
 
@@ -357,45 +362,45 @@ Capybara::SpecHelper.spec '#click_button' do
357
362
  context 'with formaction attribute on button' do
358
363
  it 'should submit to the formaction attribute' do
359
364
  @session.click_button('Formaction button')
360
- @results = extract_results(@session)
365
+ results = extract_results(@session)
361
366
  expect(@session.current_path).to eq '/form'
362
- expect(@results['which_form']).to eq 'formaction form'
367
+ expect(results['which_form']).to eq 'formaction form'
363
368
  end
364
369
  end
365
370
 
366
371
  context 'with formmethod attribute on button' do
367
372
  it 'should submit to the formethod attribute' do
368
373
  @session.click_button('Formmethod button')
369
- @results = extract_results(@session)
374
+ results = extract_results(@session)
370
375
  expect(@session.current_path).to eq '/form/get'
371
- expect(@results['which_form']).to eq 'formaction form'
376
+ expect(results['which_form']).to eq 'formaction form'
372
377
  end
373
378
  end
374
379
 
375
380
  it 'should serialize and send valueless buttons that were clicked' do
376
381
  @session.click_button('No Value!')
377
- @results = extract_results(@session)
378
- expect(@results['no_value']).not_to be_nil
382
+ results = extract_results(@session)
383
+ expect(results['no_value']).not_to be_nil
379
384
  end
380
385
 
381
386
  it 'should send button in document order' do
382
387
  @session.click_button('outside_button')
383
- @results = extract_results(@session)
384
- expect(@results.keys).to eq %w[for_form2 outside_button which_form post_count]
388
+ results = extract_results(@session)
389
+ expect(results.keys).to eq %w[for_form2 outside_button which_form post_count]
385
390
  end
386
391
 
387
392
  it 'should not send image buttons that were not clicked' do
388
393
  @session.click_button('Click me!')
389
- @results = extract_results(@session)
390
- expect(@results['okay']).to be_nil
394
+ results = extract_results(@session)
395
+ expect(results['okay']).to be_nil
391
396
  end
392
397
 
393
398
  it 'should serialize and send GET forms' do
394
399
  @session.visit('/form')
395
400
  @session.click_button('med')
396
- @results = extract_results(@session)
397
- expect(@results['middle_name']).to eq('Darren')
398
- expect(@results['foo']).to be_nil
401
+ results = extract_results(@session)
402
+ expect(results['middle_name']).to eq('Darren')
403
+ expect(results['foo']).to be_nil
399
404
  end
400
405
 
401
406
  it 'should follow redirects' do
@@ -1,31 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Capybara::SpecHelper.spec '#match_selector?' do
4
+ let(:element) { @session.find(:xpath, '//span', text: '42') }
5
+
4
6
  before do
5
7
  @session.visit('/with_html')
6
- @element = @session.find('//span', text: '42')
7
8
  end
8
9
 
9
10
  it 'should be true if the element matches the given selector' do
10
- expect(@element).to match_selector(:xpath, '//span')
11
- expect(@element).to match_selector(:css, 'span.number')
12
- expect(@element.matches_selector?(:css, 'span.number')).to be true
11
+ expect(element).to match_selector(:xpath, '//span')
12
+ expect(element).to match_selector(:css, 'span.number')
13
+ expect(element.matches_selector?(:css, 'span.number')).to be true
13
14
  end
14
15
 
15
16
  it 'should be false if the element does not match the given selector' do
16
- expect(@element).not_to match_selector(:xpath, '//div')
17
- expect(@element).not_to match_selector(:css, 'span.not_a_number')
18
- expect(@element.matches_selector?(:css, 'span.not_a_number')).to be false
17
+ expect(element).not_to match_selector(:xpath, '//div')
18
+ expect(element).not_to match_selector(:css, 'span.not_a_number')
19
+ expect(element.matches_selector?(:css, 'span.not_a_number')).to be false
19
20
  end
20
21
 
21
22
  it 'should use default selector' do
22
23
  Capybara.default_selector = :css
23
- expect(@element).not_to match_selector('span.not_a_number')
24
- expect(@element).to match_selector('span.number')
24
+ expect(element).not_to match_selector('span.not_a_number')
25
+ expect(element).to match_selector('span.number')
25
26
  end
26
27
 
27
28
  it 'should work with elements located via a sibling selector' do
28
- sibling = @element.sibling(:css, 'span', text: 'Other span')
29
+ sibling = element.sibling(:css, 'span', text: 'Other span')
29
30
  expect(sibling).to match_selector(:xpath, '//span')
30
31
  expect(sibling).to match_selector(:css, 'span')
31
32
  end
@@ -37,23 +38,23 @@ Capybara::SpecHelper.spec '#match_selector?' do
37
38
 
38
39
  context 'with text' do
39
40
  it 'should discard all matches where the given string is not contained' do
40
- expect(@element).to match_selector('//span', text: '42')
41
- expect(@element).not_to match_selector('//span', text: 'Doesnotexist')
41
+ expect(element).to match_selector('//span', text: '42')
42
+ expect(element).not_to match_selector('//span', text: 'Doesnotexist')
42
43
  end
43
44
  end
44
45
 
45
46
  it 'should have css sugar' do
46
- expect(@element.matches_css?('span.number')).to be true
47
- expect(@element.matches_css?('span.not_a_number')).to be false
48
- expect(@element.matches_css?('span.number', text: '42')).to be true
49
- expect(@element.matches_css?('span.number', text: 'Nope')).to be false
47
+ expect(element.matches_css?('span.number')).to be true
48
+ expect(element.matches_css?('span.not_a_number')).to be false
49
+ expect(element.matches_css?('span.number', text: '42')).to be true
50
+ expect(element.matches_css?('span.number', text: 'Nope')).to be false
50
51
  end
51
52
 
52
53
  it 'should have xpath sugar' do
53
- expect(@element.matches_xpath?('//span')).to be true
54
- expect(@element.matches_xpath?('//div')).to be false
55
- expect(@element.matches_xpath?('//span', text: '42')).to be true
56
- expect(@element.matches_xpath?('//span', text: 'Nope')).to be false
54
+ expect(element.matches_xpath?('//span')).to be true
55
+ expect(element.matches_xpath?('//div')).to be false
56
+ expect(element.matches_xpath?('//span', text: '42')).to be true
57
+ expect(element.matches_xpath?('//span', text: 'Nope')).to be false
57
58
  end
58
59
 
59
60
  it 'should accept selector filters' do
@@ -73,47 +74,47 @@ Capybara::SpecHelper.spec '#match_selector?' do
73
74
  end
74
75
 
75
76
  Capybara::SpecHelper.spec '#not_matches_selector?' do
77
+ let(:element) { @session.find(:css, 'span', text: 42) }
76
78
  before do
77
79
  @session.visit('/with_html')
78
- @element = @session.find(:css, 'span', text: 42)
79
80
  end
80
81
 
81
82
  it 'should be false if the given selector matches the element' do
82
- expect(@element).not_to not_match_selector(:xpath, '//span')
83
- expect(@element).not_to not_match_selector(:css, 'span.number')
84
- expect(@element.not_matches_selector?(:css, 'span.number')).to be false
83
+ expect(element).not_to not_match_selector(:xpath, '//span')
84
+ expect(element).not_to not_match_selector(:css, 'span.number')
85
+ expect(element.not_matches_selector?(:css, 'span.number')).to be false
85
86
  end
86
87
 
87
88
  it 'should be true if the given selector does not match the element' do
88
- expect(@element).to not_match_selector(:xpath, '//abbr')
89
- expect(@element).to not_match_selector(:css, 'p a#doesnotexist')
90
- expect(@element.not_matches_selector?(:css, 'p a#doesnotexist')).to be true
89
+ expect(element).to not_match_selector(:xpath, '//abbr')
90
+ expect(element).to not_match_selector(:css, 'p a#doesnotexist')
91
+ expect(element.not_matches_selector?(:css, 'p a#doesnotexist')).to be true
91
92
  end
92
93
 
93
94
  it 'should use default selector' do
94
95
  Capybara.default_selector = :css
95
- expect(@element).to not_match_selector('p a#doesnotexist')
96
- expect(@element).not_to not_match_selector('span.number')
96
+ expect(element).to not_match_selector('p a#doesnotexist')
97
+ expect(element).not_to not_match_selector('span.number')
97
98
  end
98
99
 
99
100
  context 'with text' do
100
101
  it 'should discard all matches where the given string is contained' do
101
- expect(@element).not_to not_match_selector(:css, 'span.number', text: '42')
102
- expect(@element).to not_match_selector(:css, 'span.number', text: 'Doesnotexist')
102
+ expect(element).not_to not_match_selector(:css, 'span.number', text: '42')
103
+ expect(element).to not_match_selector(:css, 'span.number', text: 'Doesnotexist')
103
104
  end
104
105
  end
105
106
 
106
107
  it 'should have CSS sugar' do
107
- expect(@element.not_matches_css?('span.number')).to be false
108
- expect(@element.not_matches_css?('p a#doesnotexist')).to be true
109
- expect(@element.not_matches_css?('span.number', text: '42')).to be false
110
- expect(@element.not_matches_css?('span.number', text: 'Doesnotexist')).to be true
108
+ expect(element.not_matches_css?('span.number')).to be false
109
+ expect(element.not_matches_css?('p a#doesnotexist')).to be true
110
+ expect(element.not_matches_css?('span.number', text: '42')).to be false
111
+ expect(element.not_matches_css?('span.number', text: 'Doesnotexist')).to be true
111
112
  end
112
113
 
113
114
  it 'should have xpath sugar' do
114
- expect(@element.not_matches_xpath?('//span')).to be false
115
- expect(@element.not_matches_xpath?('//div')).to be true
116
- expect(@element.not_matches_xpath?('//span', text: '42')).to be false
117
- expect(@element.not_matches_xpath?('//span', text: 'Doesnotexist')).to be true
115
+ expect(element.not_matches_xpath?('//span')).to be false
116
+ expect(element.not_matches_xpath?('//div')).to be true
117
+ expect(element.not_matches_xpath?('//span', text: '42')).to be false
118
+ expect(element.not_matches_xpath?('//span', text: 'Doesnotexist')).to be true
118
119
  end
119
120
  end