Skip to content

Commit f207270

Browse files
committed
[rb] do not allow Select class to select disabled options
1 parent 3b691c4 commit f207270

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

rb/lib/selenium/webdriver/support/select.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ def deselect_by_index(index)
218218
end
219219

220220
def select_option(option)
221+
raise Error::UnsupportedOperationError, 'You may not select a disabled option' unless option.enabled?
222+
221223
option.click unless option.selected?
222224
end
223225

rb/spec/integration/selenium/webdriver/select_spec.rb

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module Selenium
2323
module WebDriver
2424
module Support
2525
describe Select do
26+
let(:select) { Select.new(driver.find_element(name: 'selectomatic')) }
27+
let(:multi_select) { Select.new(driver.find_element(id: 'multi')) }
28+
let(:single_disabled) { Select.new(driver.find_element(name: 'single_disabled')) }
29+
let(:multi_disabled) { Select.new(driver.find_element(name: 'multi_disabled')) }
2630

2731
before { driver.navigate.to url_for('formPage.html') }
2832

@@ -85,8 +89,6 @@ module Support
8589
end
8690

8791
context 'when multiple select' do
88-
let(:multi_select) { Select.new(driver.find_element(id: 'multi')) }
89-
9092
context 'when by text' do
9193
it 'already selected stays selected' do
9294
multi_select.select_by(:text, 'Sausages')
@@ -112,6 +114,12 @@ module Support
112114
expect(selected_options).to include(driver.find_element(css: 'option[value="onion gravy"]'))
113115
end
114116

117+
it 'errors when option disabled' do
118+
expect {
119+
multi_disabled.select_by(:text, 'Disabled')
120+
}.to raise_exception(Error::UnsupportedOperationError)
121+
end
122+
115123
it 'errors when not found' do
116124
expect { multi_select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
117125
end
@@ -134,6 +142,10 @@ module Support
134142
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
135143
end
136144

145+
it 'errors when option disabled' do
146+
expect { multi_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)
147+
end
148+
137149
it 'errors when not found' do
138150
expect { multi_select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
139151
end
@@ -156,15 +168,19 @@ module Support
156168
expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))
157169
end
158170

171+
it 'errors when option disabled' do
172+
expect {
173+
multi_disabled.select_by(:value, 'disabled')
174+
}.to raise_exception(Error::UnsupportedOperationError)
175+
end
176+
159177
it 'errors when not found' do
160178
expect { multi_select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
161179
end
162180
end
163181
end
164182

165183
context 'when single select' do
166-
let(:select) { Select.new(driver.find_element(name: 'selectomatic')) }
167-
168184
context 'when by text' do
169185
it 'already selected stays selected' do
170186
select.select_by(:text, 'One')
@@ -186,6 +202,12 @@ module Support
186202
expect(select.selected_options).to eq([expected_option])
187203
end
188204

205+
it 'errors when option disabled' do
206+
expect {
207+
single_disabled.select_by(:text, 'Disabled')
208+
}.to raise_exception(Error::UnsupportedOperationError)
209+
end
210+
189211
it 'errors when not found' do
190212
expect { select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
191213
end
@@ -206,8 +228,12 @@ module Support
206228
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
207229
end
208230

231+
it 'errors when option disabled' do
232+
expect { single_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)
233+
end
234+
209235
it 'errors when not found' do
210-
expect { select.select_by(:index, 4) }.to raise_exception(Error::NoSuchElementError)
236+
expect { select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)
211237
end
212238
end
213239

@@ -226,6 +252,12 @@ module Support
226252
expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])
227253
end
228254

255+
it 'errors when option disabled' do
256+
expect {
257+
single_disabled.select_by(:value, 'disabled')
258+
}.to raise_exception(Error::UnsupportedOperationError)
259+
end
260+
229261
it 'errors when not found' do
230262
expect { select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)
231263
end
@@ -234,8 +266,6 @@ module Support
234266
end
235267

236268
describe '#deselect_by' do
237-
let(:multi_select) { Select.new(driver.find_element(id: 'multi')) }
238-
239269
it 'invalid how raises exception' do
240270
expect { multi_select.deselect_by(:invalid, 'foo') }.to raise_exception(ArgumentError)
241271
end
@@ -320,6 +350,12 @@ module Support
320350
expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)
321351
end
322352

353+
it 'raises exception if select contains disabled options' do
354+
select = Select.new(driver.find_element(name: 'multi_disabled'))
355+
356+
expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)
357+
end
358+
323359
it 'selects all options' do
324360
multi_select = Select.new(driver.find_element(id: 'multi'))
325361
multi_select.select_all
@@ -337,6 +373,12 @@ module Support
337373
expect { select.deselect_all }.to raise_exception(Error::UnsupportedOperationError)
338374
end
339375

376+
it 'does not error when select contains disabled options' do
377+
select = Select.new(driver.find_element(name: 'multi_disabled'))
378+
379+
expect { select.deselect_all }.not_to raise_exception
380+
end
381+
340382
it 'deselects all options' do
341383
multi_select = Select.new(driver.find_element(id: 'multi'))
342384
multi_select.deselect_all

0 commit comments

Comments
 (0)