Skip to content

Commit

Permalink
Update to match recent Chrome support changes
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Jun 22, 2019
1 parent 8dca81b commit 9e2d150
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 66 deletions.
5 changes: 3 additions & 2 deletions lib/webdrivers/chromedriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def latest_version
# Returns currently installed Chrome/Chromium version.
#
# @return [Gem::Version]
def chrome_version
def browser_version
normalize_version ChromeFinder.version
end
alias chrome_version browser_version

#
# Returns url with domain for calls to get this driver.
Expand Down Expand Up @@ -102,7 +103,7 @@ def download_url
# @example
# 73.0.3683.75 -> 73.0.3683
def release_version
chrome = normalize_version(chrome_version)
chrome = normalize_version(browser_version)
normalize_version(chrome.segments[0..2].join('.'))
end

Expand Down
35 changes: 25 additions & 10 deletions lib/webdrivers/edge_finder.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
# frozen_string_literal: true

module Webdrivers
#
# @api private
#
class EdgeFinder
class << self
def version
location = Selenium::WebDriver::EdgeChrome.path || send("#{System.platform}_location")
version = send("#{System.platform}_version", System.escape_path(location)) if location
version = send("#{System.platform}_version", location)

raise VersionError, 'Failed to find Edge binary or its version.' if version.nil? || version.empty?

Webdrivers.logger.debug "Browser version: #{version}"
version[/\d+\.\d+\.\d+\.\d+/] # Microsoft Edge 73.0.3683.75 -> 73.0.3683.75
end

def location
user_defined_location || send("#{System.platform}_location")
end

private

def user_defined_location
if Selenium::WebDriver::EdgeChrome.path
Webdrivers.logger.debug "Selenium::WebDriver::EdgeChrome.path: #{Selenium::WebDriver::EdgeChrome.path}"
return Selenium::WebDriver::EdgeChrome.path
end

return if ENV['WD_EDGE_CHROME_PATH'].nil?

Webdrivers.logger.debug "WD_EDGE_CHROME_PATH: #{ENV['WD_EDGE_CHROME_PATH']}"
ENV['WD_EDGE_CHROME_PATH']
end

def win_location
envs = %w[LOCALAPPDATA PROGRAMFILES PROGRAMFILES(X86)]
directories = ['\\Microsoft\\Edge Dev\\Application', '\\Microsoft\\Edge SxS\\Application']
Expand All @@ -21,12 +41,7 @@ def win_location
directories.each do |dir|
envs.each do |root|
option = "#{ENV[root]}\\#{dir}\\#{file}"
next unless File.exist?(option)

# Escape space and parenthesis with backticks.
option = option.gsub(/([\s()])/, '`\1') if RUBY_PLATFORM == 'java'

return option
return option if File.exist?(option)
end
end
nil
Expand Down Expand Up @@ -56,11 +71,11 @@ def win_version(location)
end

def linux_version(location)
System.call("#{location} --product-version")&.strip
System.call(location, '--product-version')&.strip
end

def mac_version(location)
System.call("#{location} --version")&.strip
System.call(location, '--version')&.strip
end
end
end
Expand Down
31 changes: 3 additions & 28 deletions lib/webdrivers/edgedriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,12 @@
module Webdrivers
class Edgedriver < Chromedriver
class << self
undef :chrome_version
#
# Returns latest available chromedriver version.
# Returns currently installed Edge version.
#
# @return [Gem::Version]
def latest_version
@latest_version ||= begin
latest_applicable = with_cache(file_name) { latest_point_release(release_version) }

Webdrivers.logger.debug "Latest version available: #{latest_applicable}"
normalize_version(latest_applicable)
end
end

#
# Returns currently installed Chrome/Chromium version.
#
# @return [Gem::Version]
def edge_version
def browser_version
normalize_version EdgeFinder.version
end

Expand Down Expand Up @@ -90,19 +78,6 @@ def download_url
@download_url = url
end

# Returns release version from the currently installed Chrome version
#
# @example
# 73.0.3683.75 -> 73.0.3683
def release_version
edge = normalize_version(edge_version)
normalize_version(edge.segments[0..2].join('.'))
end

def sufficient_binary?
super && current_version && (current_version.segments.first == release_version.segments.first)
end

def downloads
@downloads ||= begin
ds = parse_downloads(Network.get(base_url))
Expand Down
22 changes: 11 additions & 11 deletions spec/webdrivers/chromedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
it 'does not download when offline, binary exists and matches major browser version' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(chromedriver).to receive(:exists?).and_return(true)
allow(chromedriver).to receive(:chrome_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:current_version).and_return(Gem::Version.new('73.0.3683.20'))

chromedriver.update
Expand All @@ -44,7 +44,7 @@

allow(Webdrivers::Network).to receive(:get_response).and_return(client_error)
allow(chromedriver).to receive(:exists?).and_return(true)
allow(chromedriver).to receive(:chrome_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:current_version).and_return(Gem::Version.new('73.0.3683.20'))

chromedriver.update
Expand All @@ -55,7 +55,7 @@
it 'raises ConnectionError when offline, and binary does not match major browser version' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(chromedriver).to receive(:exists?).and_return(true)
allow(chromedriver).to receive(:chrome_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:current_version).and_return(Gem::Version.new('72.0.0.0'))

msg = %r{Can not reach https://chromedriver.storage.googleapis.com}
Expand Down Expand Up @@ -108,7 +108,7 @@

it 'makes a network call if cached driver does not match the browser' do
Webdrivers::System.cache_version('chromedriver', '71.0.3578.137')
allow(chromedriver).to receive(:chrome_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(chromedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(Webdrivers::Network).to receive(:get).and_return('73.0.3683.68')
allow(Webdrivers::System).to receive(:download)

Expand Down Expand Up @@ -155,19 +155,19 @@

describe '#latest_version' do
it 'returns 2.41 if the browser version is less than 70' do
allow(chromedriver).to receive(:chrome_version).and_return('69.0.0')
allow(chromedriver).to receive(:browser_version).and_return('69.0.0')

expect(chromedriver.latest_version).to eq(Gem::Version.new('2.41'))
end

it 'returns the correct point release for a production version greater than 70' do
allow(chromedriver).to receive(:chrome_version).and_return '71.0.3578.9999'
allow(chromedriver).to receive(:browser_version).and_return '71.0.3578.9999'

expect(chromedriver.latest_version).to eq Gem::Version.new('71.0.3578.137')
end

it 'raises VersionError for beta version' do
allow(chromedriver).to receive(:chrome_version).and_return('100.0.0')
allow(chromedriver).to receive(:browser_version).and_return('100.0.0')
msg = 'Unable to find latest point release version for 100.0.0. '\
'You appear to be using a non-production version of Chrome. '\
'Please set `Webdrivers::Chromedriver.required_version = <desired driver version>` '\
Expand All @@ -177,7 +177,7 @@
end

it 'raises VersionError for unknown version' do
allow(chromedriver).to receive(:chrome_version).and_return('72.0.9999.0000')
allow(chromedriver).to receive(:browser_version).and_return('72.0.9999.0000')
msg = 'Unable to find latest point release version for 72.0.9999. '\
'Please set `Webdrivers::Chromedriver.required_version = <desired driver version>` '\
'to a known chromedriver version: https://chromedriver.storage.googleapis.com/index.html'
Expand Down Expand Up @@ -258,14 +258,14 @@
end
end

describe '#chrome_version' do
describe '#browser_version' do
it 'returns a Gem::Version object' do
expect(chromedriver.chrome_version).to be_an_instance_of(Gem::Version)
expect(chromedriver.browser_version).to be_an_instance_of(Gem::Version)
end

it 'returns currently installed Chrome version' do
allow(Webdrivers::ChromeFinder).to receive(:version).and_return('72.0.0.0')
expect(chromedriver.chrome_version).to be Gem::Version.new('72.0.0.0')
expect(chromedriver.browser_version).to be Gem::Version.new('72.0.0.0')
end
end
end
33 changes: 18 additions & 15 deletions spec/webdrivers/edgedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
before(:all) do # rubocop:disable RSpec/BeforeAfterAll
# Skip these tests if version of selenium-webdriver being tested with doesn't
# have Chromium based Edge support
skip unless defined?(Selenium::WebDriver::EdgeChrome)
unless defined?(Selenium::WebDriver::EdgeChrome)
skip "The current selenium-webdriver doesn't include Chromium based Edge support"
end
end

before { edgedriver.remove }
Expand All @@ -27,7 +29,7 @@
it 'does not download when offline, binary exists and matches major browser version' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(edgedriver).to receive(:exists?).and_return(true)
allow(edgedriver).to receive(:edge_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:current_version).and_return(Gem::Version.new('73.0.3683.20'))

edgedriver.update
Expand All @@ -40,8 +42,9 @@

allow(Webdrivers::Network).to receive(:get_response).and_return(client_error)
allow(edgedriver).to receive(:exists?).and_return(true)
allow(edgedriver).to receive(:edge_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:current_version).and_return(Gem::Version.new('73.0.3683.20'))

edgedriver.update

expect(File.exist?(edgedriver.driver_path)).to be false
Expand All @@ -50,7 +53,7 @@
it 'raises ConnectionError when offline, and binary does not match major browser version' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(edgedriver).to receive(:exists?).and_return(true)
allow(edgedriver).to receive(:edge_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:current_version).and_return(Gem::Version.new('72.0.0.0'))

msg = %r{Can not reach https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/}
Expand Down Expand Up @@ -88,14 +91,14 @@
before { allow(edgedriver).to receive(:correct_binary?).and_return(false) }

it 'downloads binary' do
allow(edgedriver).to receive(:edge_version).and_return('76.0.168.154')
allow(edgedriver).to receive(:browser_version).and_return('76.0.168.154')
edgedriver.update

expect(edgedriver.current_version).not_to be_nil
end

it 'raises ConnectionError if offline' do
allow(edgedriver).to receive(:edge_version).and_return('76.0.168.154')
allow(edgedriver).to receive(:browser_version).and_return('76.0.168.154')
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)

msg = %r{Can not reach https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/}
Expand All @@ -105,7 +108,7 @@

it 'makes a network call if cached driver does not match the browser' do
Webdrivers::System.cache_version('msedgedriver', '71.0.3578.137')
allow(edgedriver).to receive(:edge_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:browser_version).and_return(Gem::Version.new('73.0.3683.68'))
allow(edgedriver).to receive(:downloads).and_return(Gem::Version.new('73.0.3683.68') => 'http://some/driver/path')

allow(Webdrivers::System).to receive(:download)
Expand Down Expand Up @@ -145,7 +148,7 @@
it 'returns a Gem::Version instance if binary is on the system' do
allow(edgedriver).to receive(:exists?).and_return(true)
allow(Webdrivers::System).to receive(:call)
.with("#{edgedriver.driver_path} --version")
.with(edgedriver.driver_path, '--version')
.and_return '71.0.3578.137'

expect(edgedriver.current_version).to eq Gem::Version.new('71.0.3578.137')
Expand All @@ -154,13 +157,13 @@

describe '#latest_version' do
it 'returns the correct point release for a production version' do
allow(edgedriver).to receive(:edge_version).and_return '76.0.168.9999'
allow(edgedriver).to receive(:browser_version).and_return '76.0.168.9999'

expect(edgedriver.latest_version).to eq Gem::Version.new('76.0.168.0')
end

it 'raises VersionError for beta version' do
allow(edgedriver).to receive(:edge_version).and_return('100.0.0')
allow(edgedriver).to receive(:browser_version).and_return('100.0.0')
msg = 'Unable to find latest point release version for 100.0.0. '\
'You appear to be using a non-production version of Edge. '\
'Please set `Webdrivers::Edgedriver.required_version = <desired driver version>` '\
Expand All @@ -170,7 +173,7 @@
end

it 'raises VersionError for unknown version' do
allow(edgedriver).to receive(:edge_version).and_return('72.0.9999.0000')
allow(edgedriver).to receive(:browser_version).and_return('72.0.9999.0000')
msg = 'Unable to find latest point release version for 72.0.9999. '\
'Please set `Webdrivers::Edgedriver.required_version = <desired driver version>` '\
'to a known edgedriver version: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/'
Expand All @@ -187,7 +190,7 @@

it 'creates cached file' do
allow(edgedriver).to receive(:downloads).and_return(Gem::Version.new('71.0.3578.137') => 'http://some/driver/path')
allow(edgedriver).to receive(:edge_version).and_return('71.0.3578.137')
allow(edgedriver).to receive(:browser_version).and_return('71.0.3578.137')
edgedriver.latest_version
expect(File.exist?("#{Webdrivers::System.install_dir}/msedgedriver.version")).to eq true
end
Expand All @@ -213,7 +216,7 @@
</p>
HTML
allow(Webdrivers::System).to receive(:valid_cache?)
allow(edgedriver).to receive(:edge_version).and_return('76.0.168.333')
allow(edgedriver).to receive(:browser_version).and_return('76.0.168.333')

expect(edgedriver.latest_version).to eq Gem::Version.new('76.0.168.0')

Expand All @@ -240,7 +243,7 @@

describe '#remove' do
it 'removes existing edgedriver' do
allow(edgedriver).to receive(:edge_version).and_return('76.0.168.154')
allow(edgedriver).to receive(:browser_version).and_return('76.0.168.154')
edgedriver.update

edgedriver.remove
Expand All @@ -255,7 +258,7 @@
describe '#driver_path' do
it 'returns full location of binary' do
expected_bin = "msedgedriver#{'.exe' if Selenium::WebDriver::Platform.windows?}"
expected_path = Webdrivers::System.escape_path("#{File.join(ENV['HOME'])}/.webdrivers/#{expected_bin}")
expected_path = File.absolute_path "#{File.join(ENV['HOME'])}/.webdrivers/#{expected_bin}"
expect(edgedriver.driver_path).to eq(expected_path)
end
end
Expand Down

0 comments on commit 9e2d150

Please sign in to comment.