Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/select_spec.rb
1864 views
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, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} do
26
let(:select) { described_class.new(driver.find_element(name: 'selectomatic')) }
27
let(:multi_select) { described_class.new(driver.find_element(id: 'multi')) }
28
let(:single_disabled) { described_class.new(driver.find_element(name: 'single_disabled')) }
29
let(:multi_disabled) { described_class.new(driver.find_element(name: 'multi_disabled')) }
30
31
before { driver.navigate.to url_for('formPage.html') }
32
after { reset_driver! if GlobalTestEnv.rbe? && GlobalTestEnv.browser == :chrome }
33
34
describe '#initialize' do
35
it 'raises exception if not a select element' do
36
expect { described_class.new(driver.find_element(id: 'checky')) }.to raise_exception(ArgumentError)
37
end
38
end
39
40
describe '#multiple?' do
41
it 'detects multiple' do
42
select = described_class.new(driver.find_element(id: 'multi'))
43
expect(select).to be_multiple
44
end
45
46
it 'detects not multiple' do
47
select = described_class.new(driver.find_element(name: 'selectomatic'))
48
expect(select).not_to be_multiple
49
end
50
end
51
52
describe '#options' do
53
it 'lists all' do
54
select = described_class.new(driver.find_element(name: 'selectomatic'))
55
options = select.options
56
expect(options.size).to eq 4
57
expect(options).to include(driver.find_element(id: 'non_multi_option'))
58
end
59
end
60
61
describe '#selected_options' do
62
it 'finds one' do
63
select = described_class.new(driver.find_element(name: 'selectomatic'))
64
expect(select.selected_options).to eq([driver.find_element(id: 'non_multi_option')])
65
end
66
67
it 'finds two' do
68
select = described_class.new(driver.find_element(id: 'multi'))
69
expect(select.selected_options).to include(driver.find_element(css: 'option[value=eggs]'))
70
expect(select.selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
71
end
72
end
73
74
describe '#first_selected_option' do
75
it 'when multiple selected' do
76
select = described_class.new(driver.find_element(id: 'multi'))
77
expect(select.first_selected_option).to eq(driver.find_element(css: 'option[value=eggs]'))
78
end
79
end
80
81
describe '#select_by' do
82
it 'invalid how raises exception' do
83
select = described_class.new(driver.find_element(id: 'multi'))
84
expect { select.select_by(:invalid, 'foo') }.to raise_exception(ArgumentError)
85
end
86
87
context 'when multiple select' do
88
context 'when by text' do
89
it 'already selected stays selected' do
90
multi_select.select_by(:text, 'Sausages')
91
selected_options = multi_select.selected_options
92
93
expect(selected_options.size).to eq 2
94
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
95
end
96
97
it 'not already selected adds to selected' do
98
multi_select.select_by(:text, 'Ham')
99
selected_options = multi_select.selected_options
100
101
expect(selected_options.size).to eq 3
102
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
103
end
104
105
it 'not already selected adds to selected when text multiple words' do
106
multi_select.select_by(:text, 'Onion gravy')
107
selected_options = multi_select.selected_options
108
109
expect(selected_options.size).to eq 3
110
expect(selected_options).to include(driver.find_element(css: 'option[value="onion gravy"]'))
111
end
112
113
it 'errors when option disabled',
114
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
115
expect {
116
multi_disabled.select_by(:text, 'Disabled')
117
}.to raise_exception(Error::UnsupportedOperationError)
118
end
119
120
it 'errors when not found' do
121
expect { multi_select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
122
end
123
end
124
125
context 'when by index' do
126
it 'already selected stays selected' do
127
multi_select.select_by(:index, 0)
128
selected_options = multi_select.selected_options
129
130
expect(selected_options.size).to eq 2
131
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
132
end
133
134
it 'not already selected adds to selected' do
135
multi_select.select_by(:index, 1)
136
selected_options = multi_select.selected_options
137
138
expect(selected_options.size).to eq 3
139
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
140
end
141
142
it 'errors when option disabled',
143
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
144
expect { multi_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)
145
end
146
147
it 'errors when not found' do
148
expect { multi_select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
149
end
150
end
151
152
context 'when by value' do
153
it 'already selected stays selected' do
154
multi_select.select_by(:value, 'sausages')
155
selected_options = multi_select.selected_options
156
157
expect(selected_options.size).to eq 2
158
expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))
159
end
160
161
it 'not already selected adds to selected' do
162
multi_select.select_by(:value, 'ham')
163
selected_options = multi_select.selected_options
164
165
expect(selected_options.size).to eq 3
166
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
167
end
168
169
it 'errors when option disabled',
170
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
171
expect {
172
multi_disabled.select_by(:value, 'disabled')
173
}.to raise_exception(Error::UnsupportedOperationError)
174
end
175
176
it 'errors when not found' do
177
expect { multi_select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
178
end
179
end
180
end
181
182
context 'when single select' do
183
context 'when by text' do
184
it 'already selected stays selected' do
185
select.select_by(:text, 'One')
186
selected_options = select.selected_options
187
188
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
189
end
190
191
it 'not already selected changes selected value' do
192
select.select_by(:text, 'Two')
193
selected_options = select.selected_options
194
195
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
196
end
197
198
it 'not already selected changes selected by complex text' do
199
select.select_by(:text, 'Still learning how to count, apparently')
200
expected_option = driver.find_element(css: 'option[value="still learning how to count, apparently"]')
201
expect(select.selected_options).to eq([expected_option])
202
end
203
204
it 'errors when option disabled',
205
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
206
expect {
207
single_disabled.select_by(:text, 'Disabled')
208
}.to raise_exception(Error::UnsupportedOperationError)
209
end
210
211
it 'errors when not found' do
212
expect { select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
213
end
214
end
215
216
context 'when by index' do
217
it 'already selected stays selected' do
218
select.select_by(:index, 0)
219
selected_options = select.selected_options
220
221
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
222
end
223
224
it 'not already selected changes selected value' do
225
select.select_by(:index, 1)
226
selected_options = select.selected_options
227
228
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
229
end
230
231
it 'errors when option disabled',
232
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
233
expect { single_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)
234
end
235
236
it 'errors when not found' do
237
expect { select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
238
end
239
end
240
241
context 'when by value' do
242
it 'already selected stays selected' do
243
select.select_by(:value, 'one')
244
selected_options = select.selected_options
245
246
expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])
247
end
248
249
it 'not already selected changes selected value' do
250
select.select_by(:value, 'two')
251
selected_options = select.selected_options
252
253
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
254
end
255
256
it 'errors when option disabled',
257
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
258
expect {
259
single_disabled.select_by(:value, 'disabled')
260
}.to raise_exception(Error::UnsupportedOperationError)
261
end
262
263
it 'errors when not found' do
264
expect { select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
265
end
266
end
267
end
268
end
269
270
describe '#deselect_by' do
271
it 'invalid how raises exception' do
272
expect { multi_select.deselect_by(:invalid, 'foo') }.to raise_exception(ArgumentError)
273
end
274
275
it 'raises exception if select not multiple' do
276
select = described_class.new(driver.find_element(name: 'selectomatic'))
277
278
expect { select.deselect_by(:text, 'foo') }.to raise_exception(Error::UnsupportedOperationError)
279
end
280
281
context 'when by text' do
282
it 'already selected is removed from selected' do
283
multi_select.deselect_by(:text, 'Sausages')
284
selected_options = multi_select.selected_options
285
286
expect(selected_options.size).to eq 1
287
expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))
288
end
289
290
it 'not already selected is not selected' do
291
multi_select.deselect_by(:text, 'Ham')
292
selected_options = multi_select.selected_options
293
294
expect(selected_options.size).to eq 2
295
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
296
end
297
298
it 'errors when not found' do
299
expect { multi_select.deselect_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
300
end
301
end
302
303
context 'when by index' do
304
it 'already selected is removed from selected' do
305
multi_select.deselect_by(:index, 0)
306
selected_options = multi_select.selected_options
307
308
expect(selected_options.size).to eq 1
309
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
310
end
311
312
it 'not already selected is not selected' do
313
multi_select.deselect_by(:index, 1)
314
selected_options = multi_select.selected_options
315
316
expect(selected_options.size).to eq 2
317
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
318
end
319
320
it 'errors when not found' do
321
expect { multi_select.deselect_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
322
end
323
end
324
325
context 'when by value' do
326
it 'already selected is removed from selected' do
327
multi_select.deselect_by(:value, 'sausages')
328
selected_options = multi_select.selected_options
329
330
expect(selected_options.size).to eq 1
331
expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))
332
end
333
334
it 'not already selected is not selected' do
335
multi_select.deselect_by(:value, 'ham')
336
selected_options = multi_select.selected_options
337
338
expect(selected_options.size).to eq 2
339
expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))
340
end
341
342
it 'errors when not found' do
343
expect { multi_select.deselect_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
344
end
345
end
346
end
347
348
describe '#select_all' do
349
it 'raises exception if select not multiple' do
350
select = described_class.new(driver.find_element(name: 'selectomatic'))
351
352
expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)
353
end
354
355
it 'raises exception if select contains disabled options',
356
exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do
357
select = described_class.new(driver.find_element(name: 'multi_disabled'))
358
359
expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)
360
end
361
362
it 'selects all options' do
363
multi_select = described_class.new(driver.find_element(id: 'multi'))
364
multi_select.select_all
365
366
selected_options = multi_select.selected_options
367
368
expect(selected_options.size).to eq 4
369
end
370
end
371
372
describe '#deselect_all' do
373
it 'raises exception if select not multiple' do
374
select = described_class.new(driver.find_element(name: 'selectomatic'))
375
376
expect { select.deselect_all }.to raise_exception(Error::UnsupportedOperationError)
377
end
378
379
it 'does not error when select contains disabled options' do
380
select = described_class.new(driver.find_element(name: 'multi_disabled'))
381
382
expect { select.deselect_all }.not_to raise_exception
383
end
384
385
it 'deselects all options' do
386
multi_select = described_class.new(driver.find_element(id: 'multi'))
387
multi_select.deselect_all
388
389
expect(multi_select.selected_options).to be_empty
390
end
391
end
392
end
393
end # Support
394
end # WebDriver
395
end # Selenium
396
397