Skip to content

Commit

Permalink
[Ruby] Fix deselecting options in <select>
Browse files Browse the repository at this point in the history
1. Add UnsupportedOperationError when trying
   to deselect options of non-multiseect

2. Update select_by_index, deselect_by_index to only
   deselect first option, since index is unique.

3. Add tests to select_spec
  • Loading branch information
Glib Briia authored and p0deje committed Mar 4, 2016
1 parent bb4ba4d commit 1e9d89f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
14 changes: 12 additions & 2 deletions rb/lib/selenium/webdriver/support/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def select_by(how, what)
#
# @param [:text, :index, :value] how How to find the option
# @param [String] what What value to find the option by.
# @raise [Error::UnsupportedOperationError] if the element does not support multiple selections.
#
# @see Select#select_by
#
Expand Down Expand Up @@ -181,7 +182,7 @@ def select_by_index(index)
raise Error::NoSuchElementError, "cannot locate element with index: #{index.inspect}"
end

select_options opts
select_option opts.first
end

def select_by_value(value)
Expand All @@ -195,6 +196,9 @@ def select_by_value(value)
end

def deselect_by_text(text)
unless multiple?
raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select'
end
opts = find_by_text text

if opts.empty?
Expand All @@ -205,6 +209,9 @@ def deselect_by_text(text)
end

def deselect_by_value(value)
unless multiple?
raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select'
end
opts = find_by_value value

if opts.empty?
Expand All @@ -215,13 +222,16 @@ def deselect_by_value(value)
end

def deselect_by_index(index)
unless multiple?
raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select'
end
opts = find_by_index index

if opts.empty?
raise Error::NoSuchElementError, "cannot locate option with index: #{index}"
end

deselect_options opts
deselect_option opts.first
end

private
Expand Down
24 changes: 21 additions & 3 deletions rb/spec/unit/selenium/webdriver/support/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ module Support
end

it 'should raise NoSuchElementError if there are no selects to deselect' do
expect(select).to receive(:attribute).with(:multiple).and_return('false')
expect(select).to receive(:find_elements).at_least(3).times.and_return []
expect(multi_select).to receive(:attribute).with(:multiple).and_return('true')
expect(multi_select).to receive(:find_elements).at_least(3).times.and_return []

s = Select.new select
s = Select.new multi_select

expect {
s.deselect_by :index, 12
Expand All @@ -276,6 +276,24 @@ module Support
s.deselect_by :text, 'also not there'
}.to raise_error(Error::NoSuchElementError)
end

it 'should raise UnsupportedOperationError if trying to deselect options in non-multiselect' do
expect(select).to receive(:attribute).with(:multiple).and_return('false')

s = Select.new select

expect {
s.deselect_by :index, 0
}.to raise_error(Error::UnsupportedOperationError)

expect {
s.deselect_by :value, 'not there'
}.to raise_error(Error::UnsupportedOperationError)

expect {
s.deselect_by :text, 'also not there'
}.to raise_error(Error::UnsupportedOperationError)
end
end # Select

describe Select::Escaper do
Expand Down

0 comments on commit 1e9d89f

Please sign in to comment.