Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions rb/lib/selenium/webdriver/support/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ def deselect_by_index(index)
def select_option(option)
raise Error::UnsupportedOperationError, 'You may not select a disabled option' unless option.enabled?

unless css_property_and_visible?(option)
raise Error::ElementNotInteractableError,
'You may not select an invisible option'
end

option.click unless option.selected?
end

def deselect_option(option)
unless css_property_and_visible?(option)
raise Error::ElementNotInteractableError,
'You may not deselect an invisible option'
end

option.click if option.selected?
end

Expand Down Expand Up @@ -266,6 +276,15 @@ def find_by_index(index)
def find_by_value(value)
@element.find_elements(xpath: ".//option[@value = #{Escaper.escape value}]")
end

def css_property_and_visible?(element)
css_value_candidates = %w[hidden none 0 0.0].to_set
css_property_candidates = %w[visibility display opacity]

css_property_candidates.none? do |property|
css_value_candidates.include?(element.css_value(property))
end
end
end # Select
end # Support
end # WebDriver
Expand Down
17 changes: 17 additions & 0 deletions rb/spec/integration/selenium/webdriver/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Support
let(:multi_select) { described_class.new(driver.find_element(id: 'multi')) }
let(:single_disabled) { described_class.new(driver.find_element(name: 'single_disabled')) }
let(:multi_disabled) { described_class.new(driver.find_element(name: 'multi_disabled')) }
let(:multi_invisible) { described_class.new(driver.find_element(id: 'invisible_multi_select')) }

before { driver.navigate.to url_for('formPage.html') }
after { reset_driver! if GlobalTestEnv.rbe? && GlobalTestEnv.browser == :chrome }
Expand Down Expand Up @@ -120,6 +121,14 @@ module Support
it 'errors when not found' do
expect { multi_select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
end

it 'errors when option is invisible', :aggregate_failures do
%w[Apples Pears Oranges Lemons].each do |text|
expect {
multi_invisible.select_by(:text, text)
}.to raise_exception(Error::ElementNotInteractableError)
end
end
end

context 'when by index' do
Expand Down Expand Up @@ -298,6 +307,14 @@ module Support
it 'errors when not found' do
expect { multi_select.deselect_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)
end

it 'errors when option is invisible', :aggregate_failures do
%w[Apples Pears Oranges Lemons].each do |text|
expect {
multi_invisible.deselect_by(:text, text)
}.to raise_exception(Error::ElementNotInteractableError)
end
end
end

context 'when by index' do
Expand Down