Skip to content

Commit

Permalink
Optimize disabled checking to one call where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Apr 26, 2019
1 parent ef5b773 commit 1902f7c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'capybara/selenium/nodes/ie_node'

module Capybara::Selenium::Driver::InternetExplorerDriver
def switch_to_frame(frame)
return super unless frame == :parent
Expand All @@ -10,6 +12,12 @@ def switch_to_frame(frame)
browser.switch_to.default_content
handles.tap(&:pop).each { |fh| browser.switch_to.frame(fh) }
end

private

def build_node(native_node, initial_cache = {})
::Capybara::Selenium::IENode.new(self, native_node, initial_cache)
end
end

module Capybara::Selenium
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/selenium/nodes/chrome_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def click(*)
raise
end

def disabled?
driver.evaluate_script("arguments[0].matches(':disabled, select:disabled *')", self)
end

private

def file_errors
Expand Down
12 changes: 1 addition & 11 deletions lib/capybara/selenium/nodes/firefox_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ def click(keys = [], **options)
end

def disabled?
# Not sure exactly what version of FF fixed the below issue, but it is definitely fixed in 61+
return super unless browser_version < 61.0

return true if super

# workaround for selenium-webdriver/geckodriver reporting elements as enabled when they are nested in disabling elements
if %w[option optgroup].include? tag_name
find_xpath('parent::*[self::optgroup or self::select]')[0].disabled?
else
!find_xpath(DISABLED_BY_FIELDSET_XPATH).empty?
end
driver.evaluate_script("arguments[0].matches(':disabled, select:disabled *')", self)
end

def set_file(value) # rubocop:disable Naming/AccessorMethodName
Expand Down
9 changes: 9 additions & 0 deletions lib/capybara/selenium/nodes/ie_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'capybara/selenium/extensions/html5_drag'

class Capybara::Selenium::IENode < Capybara::Selenium::Node
def disabled?
driver.evaluate_script("arguments[0].msMatchesSelector(':disabled, select:disabled *')", self)
end
end
11 changes: 1 addition & 10 deletions lib/capybara/selenium/nodes/safari_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ def visible_text
end

def disabled?
return true if super

# workaround for safaridriver reporting elements as enabled when they are nested in disabling elements
if %w[option optgroup].include? tag_name
return true if self[:disabled] == 'true'

find_xpath('parent::*[self::optgroup or self::select]')[0].disabled?
else
!find_xpath(DISABLED_BY_FIELDSET_XPATH).empty?
end
driver.evaluate_script("arguments[0].matches(':disabled, select:disabled *')", self)
end

def set_file(value) # rubocop:disable Naming/AccessorMethodName
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# because of the methods being tested. In tests using Capybara this type of behavior should be implemented
# using Capybara provided assertions with builtin waiting behavior.

Capybara::SpecHelper.spec 'node' do
Capybara::SpecHelper.spec 'node', :focus_ do
before do
@session.visit('/with_html')
end
Expand Down

0 comments on commit 1902f7c

Please sign in to comment.