mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Use chromedrivers is_element_displayed endpoint when available
This commit is contained in:
parent
7b37dc4b35
commit
1ffe240c2c
4 changed files with 59 additions and 5 deletions
|
@ -19,6 +19,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
|
|||
require 'selenium-webdriver'
|
||||
require 'capybara/selenium/logger_suppressor'
|
||||
require 'capybara/selenium/patches/atoms'
|
||||
require 'capybara/selenium/patches/is_displayed'
|
||||
warn "Warning: You're using an unsupported version of selenium-webdriver, please upgrade." if Gem.loaded_specs['selenium-webdriver'].version < Gem::Version.new('3.5.0')
|
||||
rescue LoadError => e
|
||||
raise e unless e.message.match?(/selenium-webdriver/)
|
||||
|
|
|
@ -7,6 +7,7 @@ module Capybara::Selenium::Driver::ChromeDriver
|
|||
def self.extended(base)
|
||||
bridge = base.send(:bridge)
|
||||
bridge.extend Capybara::Selenium::ChromeLogs unless bridge.respond_to?(:log)
|
||||
bridge.extend Capybara::Selenium::IsDisplayed unless bridge.commands(:is_element_displayed)
|
||||
end
|
||||
|
||||
def fullscreen_window(handle)
|
||||
|
|
|
@ -55,10 +55,22 @@ class Capybara::Selenium::ChromeNode < Capybara::Selenium::Node
|
|||
click unless selected_or_disabled
|
||||
end
|
||||
|
||||
def visible?
|
||||
return super unless native_displayed?
|
||||
|
||||
begin
|
||||
bridge.send(:execute, :is_element_displayed, id: native.ref)
|
||||
rescue Selenium::WebDriver::Error::UnknownCommandError
|
||||
# If the is_element_displayed command is unknown, no point in trying again
|
||||
driver.options[:native_displayed] = false
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def perform_legacy_drag(element)
|
||||
return super unless (browser_version < 77.0) && w3c? && !element.obscured?
|
||||
return super if chromedriver_fixed_actions_key_state? || !w3c? || element.obscured?
|
||||
|
||||
# W3C Chrome/chromedriver < 77 doesn't maintain mouse button state across actions API performs
|
||||
# https://bugs.chromium.org/p/chromedriver/issues/detail?id=2981
|
||||
|
@ -78,11 +90,35 @@ private
|
|||
|
||||
def w3c?
|
||||
(defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) ||
|
||||
driver.browser.capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities)
|
||||
capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities)
|
||||
end
|
||||
|
||||
def browser_version
|
||||
caps = driver.browser.capabilities
|
||||
(caps[:browser_version] || caps[:version]).to_f
|
||||
def browser_version(to_float = true)
|
||||
caps = capabilities
|
||||
ver = (caps[:browser_version] || caps[:version])
|
||||
ver = ver.to_f if to_float
|
||||
ver
|
||||
end
|
||||
|
||||
def chromedriver_fixed_actions_key_state?
|
||||
Gem::Version.new(chromedriver_version) >= Gem::Version.new('76.0.3809.68')
|
||||
end
|
||||
|
||||
def chromedriver_supports_displayed_endpoint?
|
||||
Gem::Version.new(chromedriver_version) >= Gem::Version.new('76.0.3809.25')
|
||||
end
|
||||
|
||||
def chromedriver_version
|
||||
capabilities['chrome']['chromedriverVersion'].split(' ')[0]
|
||||
end
|
||||
|
||||
def capabilities
|
||||
driver.browser.capabilities
|
||||
end
|
||||
|
||||
def native_displayed?
|
||||
(driver.options[:native_displayed] != false) &&
|
||||
(w3c? && chromedriver_supports_displayed_endpoint?) &&
|
||||
(!ENV['DISABLE_CAPYBARA_SELENIUM_OPTIMIZATIONS'])
|
||||
end
|
||||
end
|
||||
|
|
16
lib/capybara/selenium/patches/is_displayed.rb
Normal file
16
lib/capybara/selenium/patches/is_displayed.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Capybara
|
||||
module Selenium
|
||||
module IsDisplayed
|
||||
def commands(command)
|
||||
case command
|
||||
when :is_element_displayed
|
||||
[:get, 'session/:session_id/element/:id/displayed']
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue