1
+
# frozen_string_literal: true
2
+
3
+
# Licensed to the Software Freedom Conservancy (SFC) under one
4
+
# or more contributor license agreements. See the NOTICE file
5
+
# distributed with this work for additional information
6
+
# regarding copyright ownership. The SFC licenses this file
7
+
# to you under the Apache License, Version 2.0 (the
8
+
# "License"); you may not use this file except in compliance
9
+
# with the License. You may obtain a copy of the License at
10
+
#
11
+
# http://www.apache.org/licenses/LICENSE-2.0
12
+
#
13
+
# Unless required by applicable law or agreed to in writing,
14
+
# software distributed under the License is distributed on an
15
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+
# KIND, either express or implied. See the License for the
17
+
# specific language governing permissions and limitations
18
+
# under the License.
19
+
20
+
require_relative 'spec_helper'
21
+
22
+
module Selenium
23
+
module WebDriver
24
+
module Support
25
+
describe Select do
26
+
27
+
before { driver.navigate.to url_for('formPage.html') }
28
+
29
+
describe '#initialize' do
30
+
it 'raises exception if not a select element' do
31
+
expect { Select.new(driver.find_element(id: 'checky')) }.to raise_exception(ArgumentError)
32
+
end
33
+
end
34
+
35
+
describe '#multiple?' do
36
+
it 'detects multiple' do
37
+
select = Select.new(driver.find_element(id: 'multi'))
38
+
expect(select).to be_multiple
39
+
end
40
+
41
+
it 'detects not multiple' do
42
+
select = Select.new(driver.find_element(name: 'selectomatic'))
43
+
expect(select).not_to be_multiple
44
+
end
45
+
end
46
+
47
+
describe '#options' do
48
+
it 'lists all' do
49
+
select = Select.new(driver.find_element(name: 'selectomatic'))
50
+
options = select.options
51
+
expect(options.size).to eq 4
52
+
expect(options).to include(driver.find_element(id: 'non_multi_option'))
53
+
end
54
+
end
55
+
56
+
describe '#selected_options' do
57
+
it 'finds one' do
58
+
select = Select.new(driver.find_element(name: 'selectomatic'))
59
+
expect(select.selected_options).to eq([driver.find_element(id: 'non_multi_option')])
60
+
end
61
+
62
+
it 'finds two' do
63
+
select = Select.new(driver.find_element(id: 'multi'))
64
+
expect(select.selected_options).to include(driver.find_element(css: 'option[value=eggs]'))
65
+
expect(select.selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
66
+
end
67
+
end
68
+
69
+
describe '#first_selected_option' do
70
+
it 'when multiple selected' do
71
+
select = Select.new(driver.find_element(id: 'multi'))
72
+
expect(select.first_selected_option).to eq(driver.find_element(css: 'option[value=eggs]'))
73
+
end
74
+
end
75
+
76
+
describe '#select_by' do
77
+
it 'invalid how raises exception' do
78
+
select = Select.new(driver.find_element(id: 'multi'))
79
+
expect { select.select_by(:invalid, 'foo') }.to raise_exception(ArgumentError)
80
+
end
81
+
82
+
context 'when multiple select' do
83
+
let(:multi_select) { Select.new(driver.find_element(id: 'multi')) }
84
+
85
+
context 'when by text' do
86
+
it 'already selected stays selected' do
87
+
multi_select.select_by(:text, 'Sausages')
88
+
selected_options = multi_select.selected_options
89
+
90
+
expect(selected_options.size).to eq 2
91
+
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
92
+
end
93
+
94
+
it 'not already selected adds to selected' do
95
+
multi_select.select_by(:text, 'Ham')
96
+
selected_options = multi_select.selected_options
97
+
98
+
expect(selected_options.size).to eq 3
99
+
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
100
+
end
101
+
102
+
it 'not already selected adds to selected when text multiple words' do
103
+
multi_select.select_by(:text, 'Onion gravy')
104
+
selected_options = multi_select.selected_options
105
+
106
+
expect(selected_options.size).to eq 3
107
+
expect(selected_options).to include(driver.find_element(css: 'option[value="onion gravy"]'))
108
+
end
109
+
110
+
it 'errors when not found' do
111
+
expect { multi_select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
112
+
end
113
+
end
114
+
115
+
context 'when by index' do
116
+
it 'already selected stays selected' do
117
+
multi_select.select_by(:index, 0)
118
+
selected_options = multi_select.selected_options
119
+
120
+
expect(selected_options.size).to eq 2
121
+
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
122
+
end
123
+
124
+
it 'not already selected adds to selected' do
125
+
multi_select.select_by(:index, 1)
126
+
selected_options = multi_select.selected_options
127
+
128
+
expect(selected_options.size).to eq 3
129
+
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
130
+
end
131
+
132
+
it 'errors when not found' do
133
+
expect { multi_select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
134
+
end
135
+
end
136
+
137
+
context 'when by value' do
138
+
it 'already selected stays selected' do
139
+
multi_select.select_by(:value, 'sausages')
140
+
selected_options = multi_select.selected_options
141
+
142
+
expect(selected_options.size).to eq 2
143
+
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
144
+
end
145
+
146
+
it 'not already selected adds to selected' do
147
+
multi_select.select_by(:value, 'ham')
148
+
selected_options = multi_select.selected_options
149
+
150
+
expect(selected_options.size).to eq 3
151
+
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
152
+
end
153
+
154
+
it 'errors when not found' do
155
+
expect { multi_select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
156
+
end
157
+
end
158
+
end
159
+
160
+
context 'when single select' do
161
+
let(:select) { Select.new(driver.find_element(name: 'selectomatic')) }
162
+
163
+
context 'when by text' do
164
+
it 'already selected stays selected' do
165
+
select.select_by(:text, 'One')
166
+
selected_options = select.selected_options
167
+
168
+
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
169
+
end
170
+
171
+
it 'not already selected changes selected value' do
172
+
select.select_by(:text, 'Two')
173
+
selected_options = select.selected_options
174
+
175
+
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
176
+
end
177
+
178
+
it 'not already selected changes selected by complex text' do
179
+
select.select_by(:text, 'Still learning how to count, apparently')
180
+
expected_option = driver.find_element(css: 'option[value="still learning how to count, apparently"]')
181
+
expect(select.selected_options).to eq([expected_option])
182
+
end
183
+
184
+
it 'errors when not found' do
185
+
expect { select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
186
+
end
187
+
end
188
+
189
+
context 'when by index' do
190
+
it 'already selected stays selected' do
191
+
select.select_by(:index, 0)
192
+
selected_options = select.selected_options
193
+
194
+
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
195
+
end
196
+
197
+
it 'not already selected changes selected value' do
198
+
select.select_by(:index, 1)
199
+
selected_options = select.selected_options
200
+
201
+
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
202
+
end
203
+
204
+
it 'errors when not found' do
205
+
expect { select.select_by(:index, 4) }.to raise_exception(Error::NoSuchElementError)
206
+
end
207
+
end
208
+
209
+
context 'when by value' do
210
+
it 'already selected stays selected' do
211
+
select.select_by(:value, 'one')
212
+
selected_options = select.selected_options
213
+
214
+
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
215
+
end
216
+
217
+
it 'not already selected changes selected value' do
218
+
select.select_by(:value, 'two')
219
+
selected_options = select.selected_options
220
+
221
+
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
222
+
end
223
+
224
+
it 'errors when not found' do
225
+
expect { select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
226
+
end
227
+
end
228
+
end
229
+
end
230
+
231
+
describe '#deselect_by' do
232
+
let(:multi_select) { Select.new(driver.find_element(id: 'multi')) }
233
+
234
+
it 'invalid how raises exception' do
235
+
expect { multi_select.deselect_by(:invalid, 'foo') }.to raise_exception(ArgumentError)
236
+
end
237
+
238
+
it 'raises exception if select not multiple' do
239
+
select = Select.new(driver.find_element(name: 'selectomatic'))
240
+
241
+
expect { select.deselect_by(:text, 'foo') }.to raise_exception(Error::UnsupportedOperationError)
242
+
end
243
+
244
+
context 'when by text' do
245
+
it 'already selected is removed from selected' do
246
+
multi_select.deselect_by(:text, 'Sausages')
247
+
selected_options = multi_select.selected_options
248
+
249
+
expect(selected_options.size).to eq 1
250
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))
251
+
end
252
+
253
+
it 'not already selected is not selected' do
254
+
multi_select.deselect_by(:text, 'Ham')
255
+
selected_options = multi_select.selected_options
256
+
257
+
expect(selected_options.size).to eq 2
258
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
259
+
end
260
+
261
+
it 'errors when not found' do
262
+
expect { multi_select.deselect_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
263
+
end
264
+
end
265
+
266
+
context 'when by index' do
267
+
it 'already selected is removed from selected' do
268
+
multi_select.deselect_by(:index, 0)
269
+
selected_options = multi_select.selected_options
270
+
271
+
expect(selected_options.size).to eq 1
272
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
273
+
end
274
+
275
+
it 'not already selected is not selected' do
276
+
multi_select.deselect_by(:index, 1)
277
+
selected_options = multi_select.selected_options
278
+
279
+
expect(selected_options.size).to eq 2
280
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
281
+
end
282
+
283
+
it 'errors when not found' do
284
+
expect { multi_select.deselect_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
285
+
end
286
+
end
287
+
288
+
context 'when by value' do
289
+
it 'already selected is removed from selected' do
290
+
multi_select.deselect_by(:value, 'sausages')
291
+
selected_options = multi_select.selected_options
292
+
293
+
expect(selected_options.size).to eq 1
294
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))
295
+
end
296
+
297
+
it 'not already selected is not selected' do
298
+
multi_select.deselect_by(:value, 'ham')
299
+
selected_options = multi_select.selected_options
300
+
301
+
expect(selected_options.size).to eq 2
302
+
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
303
+
end
304
+
305
+
it 'errors when not found' do
306
+
expect { multi_select.deselect_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
307
+
end
308
+
end
309
+
end
310
+
311
+
describe '#select_all' do
312
+
it 'raises exception if select not multiple' do
313
+
select = Select.new(driver.find_element(name: 'selectomatic'))
314
+
315
+
expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)
316
+
end
317
+
318
+
it 'selects all options' do
319
+
multi_select = Select.new(driver.find_element(id: 'multi'))
320
+
multi_select.select_all
321
+
322
+
selected_options = multi_select.selected_options
323
+
324
+
expect(selected_options.size).to eq 4
325
+
end
326
+
end
327
+
328
+
describe '#deselect_all' do
329
+
it 'raises exception if select not multiple' do
330
+
select = Select.new(driver.find_element(name: 'selectomatic'))
331
+
332
+
expect { select.deselect_all }.to raise_exception(Error::UnsupportedOperationError)
333
+
end
334
+
335
+
it 'deselects all options' do
336
+
multi_select = Select.new(driver.find_element(id: 'multi'))
337
+
multi_select.deselect_all
338
+
339
+
expect(multi_select.selected_options).to be_empty
340
+
end
341
+
end
342
+
end
343
+
end # Support
344
+
end # WebDriver
345
+
end # Selenium
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4