@@ -38,9 +38,7 @@ module Support
38
38
it 'raises ArgumentError if passed a non-select Element' do
39
39
link = instance_double ( Element , tag_name : 'a' )
40
40
41
- expect {
42
- Select . new link
43
- } . to raise_error ( ArgumentError )
41
+ expect { Select . new link } . to raise_error ( ArgumentError )
44
42
end
45
43
46
44
it 'indicates whether a select is multiple correctly' do
@@ -59,236 +57,169 @@ module Support
59
57
60
58
it 'returns all options' do
61
59
options = [ ]
62
-
63
- expect ( multi_select ) . to receive ( :find_elements )
64
- . with ( tag_name : 'option' )
65
- . once
66
- . and_return ( options )
60
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( options )
67
61
68
62
expect ( Select . new ( multi_select ) . options ) . to eql ( options )
63
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
69
64
end
70
65
71
66
it 'returns all selected options' do
72
67
bad_option = instance_double ( Element , selected? : false )
73
68
good_option = instance_double ( Element , selected? : true )
74
-
75
- expect ( multi_select ) . to receive ( :find_elements )
76
- . with ( tag_name : 'option' )
77
- . once
78
- . and_return ( [ bad_option , good_option ] )
69
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ bad_option , good_option ] )
79
70
80
71
opts = Select . new ( multi_select ) . selected_options
81
72
82
73
expect ( opts . size ) . to eq ( 1 )
83
74
expect ( opts . first ) . to eq ( good_option )
75
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
84
76
end
85
77
86
78
it 'returns the first selected option' do
87
79
first_option = instance_double ( Element , selected? : true )
88
80
second_option = instance_double ( Element , selected? : true )
89
-
90
- expect ( multi_select ) . to receive ( :find_elements )
91
- . with ( tag_name : 'option' )
92
- . once
93
- . and_return ( [ first_option , second_option ] )
81
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
94
82
95
83
option = Select . new ( multi_select ) . first_selected_option
96
84
expect ( option ) . to eq ( first_option )
85
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
97
86
end
98
87
99
88
it 'raises a NoSuchElementError if nothing is selected' do
100
89
option = instance_double ( Element , selected? : false )
90
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ option ] )
101
91
102
- expect ( multi_select ) . to receive ( :find_elements )
103
- . with ( tag_name : 'option' )
104
- . once
105
- . and_return ( [ option ] )
106
-
107
- expect {
108
- Select . new ( multi_select ) . first_selected_option
109
- } . to raise_error ( Error ::NoSuchElementError )
92
+ expect { Select . new ( multi_select ) . first_selected_option } . to raise_error ( Error ::NoSuchElementError )
110
93
end
111
94
112
95
it 'allows options to be selected by visible text' do
113
- option = instance_double ( Element , selected? : false , enabled? : true )
114
-
115
- expect ( multi_select ) . to receive ( :find_elements )
116
- . with ( xpath : './/option[normalize-space(.) = "fish"]' )
117
- . once
118
- . and_return ( [ option ] )
119
-
120
- expect ( option ) . to receive ( :click ) . once
96
+ option = instance_double ( Element , selected? : false , enabled? : true , click : nil )
97
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ option ] )
121
98
122
99
Select . new ( multi_select ) . select_by ( :text , 'fish' )
100
+ expect ( option ) . to have_received ( :click )
101
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[normalize-space(.) = "fish"]' )
123
102
end
124
103
125
104
it 'allows options to be selected by index' do
126
- first_option = instance_double ( Element , selected? : true , enabled? : true )
127
- second_option = instance_double ( Element , selected? : false , enabled? : true )
105
+ first_option = instance_double ( Element , selected? : true , enabled? : true , click : nil )
106
+ second_option = instance_double ( Element , selected? : false , enabled? : true , click : nil )
128
107
129
108
allow ( first_option ) . to receive ( :property ) . with ( :index ) . and_return 0
130
- expect ( first_option ) . not_to receive ( :click )
131
-
132
109
allow ( second_option ) . to receive ( :property ) . with ( :index ) . and_return 1
133
- expect ( second_option ) . to receive ( :click ) . once
134
-
135
- allow ( multi_select ) . to receive ( :find_elements )
136
- . with ( tag_name : 'option' )
137
- . and_return ( [ first_option , second_option ] )
110
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
138
111
139
112
Select . new ( multi_select ) . select_by ( :index , 1 )
140
113
expect ( first_option ) . to have_received ( :property ) . with ( :index )
141
114
expect ( second_option ) . to have_received ( :property ) . with ( :index )
142
115
expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
116
+ expect ( first_option ) . not_to have_received ( :click )
117
+ expect ( second_option ) . to have_received ( :click ) . once
143
118
end
144
119
145
120
it 'allows options to be selected by returned value' do
146
- first_option = instance_double ( Element , selected? : false , enabled? : true )
147
- allow ( multi_select ) . to receive ( :find_elements )
148
- . with ( xpath : './/option[@value = "b"]' )
149
- . and_return ( [ first_option ] )
150
-
151
- expect ( first_option ) . to receive ( :click ) . once
121
+ first_option = instance_double ( Element , selected? : false , enabled? : true , click : nil )
122
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option ] )
152
123
153
124
Select . new ( multi_select ) . select_by ( :value , 'b' )
125
+
126
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[@value = "b"]' )
127
+ expect ( first_option ) . to have_received ( :click ) . once
154
128
expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[@value = "b"]' )
155
129
end
156
130
157
131
it 'can deselect all when select supports multiple selections' do
158
- first_option = instance_double ( Element , selected? : true )
159
- second_option = instance_double ( Element , selected? : false )
160
-
161
- expect ( multi_select ) . to receive ( :find_elements )
162
- . with ( tag_name : 'option' )
163
- . once
164
- . and_return ( [ first_option , second_option ] )
165
-
166
- expect ( first_option ) . to receive ( :click ) . once
167
- expect ( second_option ) . not_to receive ( :click )
132
+ first_option = instance_double ( Element , selected? : true , click : nil )
133
+ second_option = instance_double ( Element , selected? : false , click : nil )
134
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
168
135
169
136
Select . new ( multi_select ) . deselect_all
137
+
138
+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
139
+ expect ( first_option ) . to have_received ( :click ) . once
140
+ expect ( second_option ) . not_to have_received ( :click )
170
141
end
171
142
172
143
it 'can not deselect all when select does not support multiple selections' do
173
- expect {
174
- Select . new ( select ) . deselect_all
175
- } . to raise_error ( Error ::UnsupportedOperationError )
144
+ expect { Select . new ( select ) . deselect_all } . to raise_error ( Error ::UnsupportedOperationError )
176
145
end
177
146
178
147
it 'can deselect options by visible text' do
179
- first_option = instance_double ( Element , selected? : true )
180
- second_option = instance_double ( Element , selected? : false )
181
-
182
- allow ( multi_select ) . to receive ( :find_elements )
183
- . with ( xpath : './/option[normalize-space(.) = "b"]' )
184
- . and_return ( [ first_option , second_option ] )
185
-
186
- expect ( first_option ) . to receive ( :click ) . once
187
- expect ( second_option ) . not_to receive ( :click )
148
+ first_option = instance_double ( Element , selected? : true , click : nil )
149
+ second_option = instance_double ( Element , selected? : false , click : nil )
150
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
188
151
189
152
Select . new ( multi_select ) . deselect_by ( :text , 'b' )
153
+
190
154
expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[normalize-space(.) = "b"]' )
155
+ expect ( first_option ) . to have_received ( :click ) . once
156
+ expect ( second_option ) . not_to have_received ( :click )
191
157
end
192
158
193
159
it 'can deselect options by index' do
194
- first_option = instance_double ( Element , selected? : true )
195
- second_option = instance_double ( Element )
196
-
197
- allow ( multi_select ) . to receive ( :find_elements )
198
- . with ( tag_name : 'option' )
199
- . and_return ( [ first_option , second_option ] )
160
+ first_option = instance_double ( Element , selected? : true , click : nil )
161
+ second_option = instance_double ( Element , click : nil )
200
162
163
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
201
164
allow ( first_option ) . to receive ( :property ) . with ( :index ) . and_return ( 2 )
202
165
allow ( second_option ) . to receive ( :property ) . with ( :index ) . and_return ( 1 )
203
166
204
- expect ( first_option ) . to receive ( :click ) . once
205
- expect ( second_option ) . not_to receive ( :click )
206
-
207
167
Select . new ( multi_select ) . deselect_by ( :index , 2 )
168
+
169
+ expect ( first_option ) . to have_received ( :click ) . once
170
+ expect ( second_option ) . not_to have_received ( :click )
208
171
expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
209
172
end
210
173
211
174
it 'can deselect options by returned value' do
212
- first_option = instance_double ( Element , selected? : true )
213
- second_option = instance_double ( Element , selected? : false )
214
-
215
- allow ( multi_select ) . to receive ( :find_elements )
216
- . with ( xpath : './/option[@value = "b"]' )
217
- . and_return ( [ first_option , second_option ] )
218
-
219
- expect ( first_option ) . to receive ( :click ) . once
220
- expect ( second_option ) . not_to receive ( :click )
175
+ first_option = instance_double ( Element , selected? : true , click : nil )
176
+ second_option = instance_double ( Element , selected? : false , click : nil )
177
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
221
178
222
179
Select . new ( multi_select ) . deselect_by ( :value , 'b' )
180
+
181
+ expect ( first_option ) . to have_received ( :click ) . once
182
+ expect ( second_option ) . not_to have_received ( :click )
223
183
expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[@value = "b"]' )
224
184
end
225
185
226
186
it 'should fall back to slow lookups when "get by visible text fails" and there is a space' do
227
- first_option = instance_double ( Element , selected? : false , enabled? : true , text : 'foo bar' )
228
-
229
- xpath1 = './/option[normalize-space(.) = "foo bar"]'
230
- xpath2 = './/option[contains(., "foo")]'
231
-
232
- allow ( select ) . to receive ( :find_elements ) . with ( xpath : xpath1 ) . and_return ( [ ] )
233
- allow ( select ) . to receive ( :find_elements ) . with ( xpath : xpath2 ) . and_return ( [ first_option ] )
234
-
235
- expect ( first_option ) . to receive ( :click ) . once
187
+ first_option = instance_double ( Element , selected? : false , enabled? : true , text : 'foo bar' , click : nil )
188
+ allow ( select ) . to receive ( :find_elements ) . and_return ( [ ] , [ first_option ] )
236
189
237
190
Select . new ( select ) . select_by ( :text , 'foo bar' )
238
- expect ( select ) . to have_received ( :find_elements ) . with ( xpath : xpath1 ) . once
239
- expect ( select ) . to have_received ( :find_elements ) . with ( xpath : xpath2 ) . once
191
+
192
+ expect ( first_option ) . to have_received ( :click ) . once
193
+ expect ( select ) . to have_received ( :find_elements ) . with ( xpath : './/option[normalize-space(.) = "foo bar"]' ) . once
194
+ expect ( select ) . to have_received ( :find_elements ) . with ( xpath : './/option[contains(., "foo")]' ) . once
240
195
end
241
196
242
197
it 'should raise NoSuchElementError if there are no selects to select' do
243
- expect ( select ) . to receive ( :find_elements ) . at_least ( 3 ) . times . and_return [ ]
198
+ allow ( select ) . to receive ( :find_elements ) . and_return [ ]
244
199
245
200
select_element = Select . new select
246
201
247
- expect {
248
- select_element . select_by :index , 12
249
- } . to raise_error ( Error ::NoSuchElementError )
250
-
251
- expect {
252
- select_element . select_by :value , 'not there'
253
- } . to raise_error ( Error ::NoSuchElementError )
254
-
255
- expect {
256
- select_element . select_by :text , 'also not there'
257
- } . to raise_error ( Error ::NoSuchElementError )
202
+ expect { select_element . select_by :index , 12 } . to raise_error ( Error ::NoSuchElementError )
203
+ expect { select_element . select_by :value , 'not there' } . to raise_error ( Error ::NoSuchElementError )
204
+ expect { select_element . select_by :text , 'also not there' } . to raise_error ( Error ::NoSuchElementError )
258
205
end
259
206
260
207
it 'should raise NoSuchElementError if there are no selects to deselect' do
261
- expect ( multi_select ) . to receive ( :find_elements ) . at_least ( 3 ) . times . and_return [ ]
208
+ allow ( multi_select ) . to receive ( :find_elements ) . and_return [ ]
262
209
263
210
select_element = Select . new multi_select
264
211
265
- expect {
266
- select_element . deselect_by :index , 12
267
- } . to raise_error ( Error ::NoSuchElementError )
268
-
269
- expect {
270
- select_element . deselect_by :value , 'not there'
271
- } . to raise_error ( Error ::NoSuchElementError )
272
-
273
- expect {
274
- select_element . deselect_by :text , 'also not there'
275
- } . to raise_error ( Error ::NoSuchElementError )
212
+ expect { select_element . deselect_by :index , 12 } . to raise_error ( Error ::NoSuchElementError )
213
+ expect { select_element . deselect_by :value , 'not there' } . to raise_error ( Error ::NoSuchElementError )
214
+ expect { select_element . deselect_by :text , 'also not there' } . to raise_error ( Error ::NoSuchElementError )
276
215
end
277
216
278
217
it 'should raise UnsupportedOperationError if trying to deselect options in non-multiselect' do
279
218
select_element = Select . new select
280
219
281
- expect {
282
- select_element . deselect_by :index , 0
283
- } . to raise_error ( Error ::UnsupportedOperationError )
284
-
285
- expect {
286
- select_element . deselect_by :value , 'not there'
287
- } . to raise_error ( Error ::UnsupportedOperationError )
288
-
289
- expect {
290
- select_element . deselect_by :text , 'also not there'
291
- } . to raise_error ( Error ::UnsupportedOperationError )
220
+ expect { select_element . deselect_by :index , 0 } . to raise_error ( Error ::UnsupportedOperationError )
221
+ expect { select_element . deselect_by :value , 'not there' } . to raise_error ( Error ::UnsupportedOperationError )
222
+ expect { select_element . deselect_by :text , 'also not there' } . to raise_error ( Error ::UnsupportedOperationError )
292
223
end
293
224
end # Select
294
225
0 commit comments