Skip to content

Commit

Permalink
#latest_version now returns latest binary version corresponding to th…
Browse files Browse the repository at this point in the history
…e installed Chrome version.
  • Loading branch information
kapoorlakshya committed Mar 19, 2019
1 parent 645308c commit a9b728a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
sudo: required
dist: trusty
os:
- linux
- osx
language: ruby
cache: bundler
rvm:
- 2.4.1
- jruby-9.2.0.0
addons:
chrome: stable
53 changes: 48 additions & 5 deletions lib/webdrivers/chromedriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def current_version

def latest_version
raise StandardError, "Can not reach site" unless site_available?
Gem::Version.new(get(URI.join(base_url, "LATEST_RELEASE")))

# Latest webdriver release for installed Chrome version
release_file = "LATEST_RELEASE_#{release_version}"
latest_available = get(URI.join(base_url, release_file))
Gem::Version.new(latest_available)
end

private
Expand All @@ -31,18 +35,57 @@ def downloads
Webdrivers.logger.debug "Versions previously located on downloads site: #{@downloads.keys}" if @downloads

@downloads ||= begin
doc = Nokogiri::XML.parse(get(base_url))
doc = Nokogiri::XML.parse(get(base_url))
items = doc.css("Contents Key").collect(&:text)
items.select! {|item| item.include?(platform)}
items.select! { |item| item.include?(platform) }
ds = items.each_with_object({}) do |item, hash|
key = normalize item[/^[^\/]+/]
key = normalize item[/^[^\/]+/]
hash[key] = "#{base_url}/#{item}"
end
Webdrivers.logger.debug "Versions now located on downloads site: #{ds.keys}"
ds
end
end

# Returns release version from the currently installed Chrome version
#
# @example
# 73.0.3683.75 -> 73.0.3683
def release_version
chrome_version.scan(/\d+\.\d+\.\d+/)[0]
end

# Returns currently installed Chrome version
def chrome_version
ver = case platform
when 'win'
chrome_on_windows
when /linux/
chrome_on_linux
when 'mac'
chrome_on_mac
else
raise NotImplementedError, 'Your OS is not supported by webdrivers gem.'
end.chomp

# Google Chrome 73.0.3683.75 -> 73.0.3683.75
ver.scan(/\d+\.\d+\.\d+\.\d+/)[0]
end

def chrome_on_windows
reg = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
ps = "(Get-Item (Get-ItemProperty '#{reg}').'(Default)').VersionInfo.ProductVersion"
`powershell #{ps}`
end

def chrome_on_linux
`google-chrome --version`
end

def chrome_on_mac
loc = Shellwords.escape '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
`#{loc} --version`
end
end
end
end
end
14 changes: 8 additions & 6 deletions spec/chromedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
end

it 'finds latest version' do
old_version = Gem::Version.new("2.30")
future_version = Gem::Version.new("2.90")
old_version = Gem::Version.new('2.30')
future_version = Gem::Version.new('80.00')
latest_version = chromedriver.latest_version

expect(latest_version).to be > old_version
expect(latest_version).to be < future_version
end

it 'downloads latest version by default' do
it 'downloads latest release for current version of Chrome by default' do
chromedriver.remove
chromedriver.download
expect(chromedriver.current_version).to eq chromedriver.latest_version
cur_ver = chromedriver.current_version.version
latest_ver = chromedriver.latest_version.version[0..3] # "72.0.3626.69" - > "72.0"
expect(cur_ver).to eq latest_ver
end

it 'downloads specified version by Float' do
Expand All @@ -36,9 +38,9 @@

it 'downloads specified version by String' do
chromedriver.remove
chromedriver.version = '2.29'
chromedriver.version = '74.0.3729.6'
chromedriver.download
expect(chromedriver.current_version.version).to eq '2.29'
expect(chromedriver.current_version.version).to eq '74.0'
end

it 'removes chromedriver' do
Expand Down

0 comments on commit a9b728a

Please sign in to comment.