@@ -38,9 +38,7 @@ module Support
3838 it 'raises ArgumentError if passed a non-select Element' do
3939 link = instance_double ( Element , tag_name : 'a' )
4040
41- expect {
42- Select . new link
43- } . to raise_error ( ArgumentError )
41+ expect { Select . new link } . to raise_error ( ArgumentError )
4442 end
4543
4644 it 'indicates whether a select is multiple correctly' do
@@ -59,236 +57,169 @@ module Support
5957
6058 it 'returns all options' do
6159 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 )
6761
6862 expect ( Select . new ( multi_select ) . options ) . to eql ( options )
63+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
6964 end
7065
7166 it 'returns all selected options' do
7267 bad_option = instance_double ( Element , selected? : false )
7368 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 ] )
7970
8071 opts = Select . new ( multi_select ) . selected_options
8172
8273 expect ( opts . size ) . to eq ( 1 )
8374 expect ( opts . first ) . to eq ( good_option )
75+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
8476 end
8577
8678 it 'returns the first selected option' do
8779 first_option = instance_double ( Element , selected? : true )
8880 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 ] )
9482
9583 option = Select . new ( multi_select ) . first_selected_option
9684 expect ( option ) . to eq ( first_option )
85+ expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
9786 end
9887
9988 it 'raises a NoSuchElementError if nothing is selected' do
10089 option = instance_double ( Element , selected? : false )
90+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ option ] )
10191
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 )
11093 end
11194
11295 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 ] )
12198
12299 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"]' )
123102 end
124103
125104 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 )
128107
129108 allow ( first_option ) . to receive ( :property ) . with ( :index ) . and_return 0
130- expect ( first_option ) . not_to receive ( :click )
131-
132109 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 ] )
138111
139112 Select . new ( multi_select ) . select_by ( :index , 1 )
140113 expect ( first_option ) . to have_received ( :property ) . with ( :index )
141114 expect ( second_option ) . to have_received ( :property ) . with ( :index )
142115 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
143118 end
144119
145120 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 ] )
152123
153124 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
154128 expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[@value = "b"]' )
155129 end
156130
157131 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 ] )
168135
169136 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 )
170141 end
171142
172143 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 )
176145 end
177146
178147 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 ] )
188151
189152 Select . new ( multi_select ) . deselect_by ( :text , 'b' )
153+
190154 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 )
191157 end
192158
193159 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 )
200162
163+ allow ( multi_select ) . to receive ( :find_elements ) . and_return ( [ first_option , second_option ] )
201164 allow ( first_option ) . to receive ( :property ) . with ( :index ) . and_return ( 2 )
202165 allow ( second_option ) . to receive ( :property ) . with ( :index ) . and_return ( 1 )
203166
204- expect ( first_option ) . to receive ( :click ) . once
205- expect ( second_option ) . not_to receive ( :click )
206-
207167 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 )
208171 expect ( multi_select ) . to have_received ( :find_elements ) . with ( tag_name : 'option' )
209172 end
210173
211174 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 ] )
221178
222179 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 )
223183 expect ( multi_select ) . to have_received ( :find_elements ) . with ( xpath : './/option[@value = "b"]' )
224184 end
225185
226186 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 ] )
236189
237190 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
240195 end
241196
242197 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 [ ]
244199
245200 select_element = Select . new select
246201
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 )
258205 end
259206
260207 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 [ ]
262209
263210 select_element = Select . new multi_select
264211
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 )
276215 end
277216
278217 it 'should raise UnsupportedOperationError if trying to deselect options in non-multiselect' do
279218 select_element = Select . new select
280219
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 )
292223 end
293224 end # Select
294225
0 commit comments