webdrivers 3.8.0 → 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
- require 'rubocop/rake_task'
4
-
5
- RuboCop::RakeTask.new
6
-
7
- RSpec::Core::RakeTask.new(:spec)
8
-
9
- task default: %w[spec rubocop]
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
data/appveyor.yml ADDED
@@ -0,0 +1,23 @@
1
+ build: off
2
+ cache:
3
+ - vendor/bundle
4
+ environment:
5
+ matrix:
6
+ - RUBY_VERSION: 24
7
+ RAKE_TASK: spec
8
+ - RUBY_VERSION: 24
9
+ RAKE_TASK: rubocop
10
+ - RUBY_VERSION: 25
11
+ RAKE_TASK: spec
12
+ - RUBY_VERSION: 26
13
+ RAKE_TASK: spec
14
+ install:
15
+ - set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH%
16
+ - bundle config --local path vendor/bundle
17
+ - bundle install
18
+ before_test:
19
+ - ruby -v
20
+ - gem -v
21
+ - bundle -v
22
+ test_script:
23
+ - bundle exec rake %RAKE_TASK%
data/lib/webdrivers.rb CHANGED
@@ -1,26 +1,28 @@
1
- require 'webdrivers/selenium'
2
- require 'webdrivers/logger'
3
- require 'webdrivers/common'
4
- require 'webdrivers/chromedriver'
5
- require 'webdrivers/geckodriver'
6
- require 'webdrivers/iedriver'
7
- require 'webdrivers/mswebdriver'
8
-
9
- module Webdrivers
10
- class << self
11
- attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_pass, :install_dir
12
-
13
- def logger
14
- @logger ||= Webdrivers::Logger.new
15
- end
16
-
17
- def configure
18
- yield self
19
- end
20
-
21
- def net_http_ssl_fix
22
- raise 'Webdrivers.net_http_ssl_fix is no longer available.' \
23
- ' Please see https://github.com/titusfortner/webdrivers#ssl_connect-errors.'
24
- end
25
- end
26
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'webdrivers/selenium'
4
+ require 'webdrivers/logger'
5
+ require 'webdrivers/common'
6
+ require 'webdrivers/chromedriver'
7
+ require 'webdrivers/geckodriver'
8
+ require 'webdrivers/iedriver'
9
+ require 'webdrivers/mswebdriver'
10
+
11
+ module Webdrivers
12
+ class << self
13
+ attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_pass, :install_dir
14
+
15
+ def logger
16
+ @logger ||= Webdrivers::Logger.new
17
+ end
18
+
19
+ def configure
20
+ yield self
21
+ end
22
+
23
+ def net_http_ssl_fix
24
+ raise 'Webdrivers.net_http_ssl_fix is no longer available.' \
25
+ ' Please see https://github.com/titusfortner/webdrivers#ssl_connect-errors.'
26
+ end
27
+ end
28
+ end
@@ -1,140 +1,161 @@
1
- require 'nokogiri'
2
- require 'shellwords'
3
-
4
- module Webdrivers
5
- class Chromedriver < Common
6
- class << self
7
- def current_version
8
- Webdrivers.logger.debug 'Checking current version'
9
- return nil unless downloaded?
10
-
11
- ver = `#{binary} --version`
12
- Webdrivers.logger.debug "Current #{binary} version: #{ver}"
13
-
14
- # Matches 2.46, 2.46.628411 and 73.0.3683.75
15
- normalize ver[/\d+\.\d+(\.\d+)?(\.\d+)?/]
16
- end
17
-
18
- def latest_version
19
- raise StandardError, 'Can not reach site' unless site_available?
20
-
21
- # Versions before 70 do not have a LATEST_RELEASE file
22
- return Gem::Version.new('2.46') if release_version < '70.0.3538'
23
-
24
- # Latest webdriver release for installed Chrome version
25
- release_file = "LATEST_RELEASE_#{release_version}"
26
- latest_available = get(URI.join(base_url, release_file))
27
- Webdrivers.logger.debug "Latest version available: #{latest_available}"
28
- Gem::Version.new(latest_available)
29
- end
30
-
31
- private
32
-
33
- def file_name
34
- platform == 'win' ? 'chromedriver.exe' : 'chromedriver'
35
- end
36
-
37
- def base_url
38
- 'https://chromedriver.storage.googleapis.com'
39
- end
40
-
41
- def downloads
42
- Webdrivers.logger.debug "Versions previously located on downloads site: #{@downloads.keys}" if @downloads
43
-
44
- @downloads ||= begin
45
- doc = Nokogiri::XML.parse(get(base_url))
46
- items = doc.css('Contents Key').collect(&:text)
47
- items.select! { |item| item.include?(platform) }
48
- ds = items.each_with_object({}) do |item, hash|
49
- key = normalize item[%r{^[^/]+}]
50
- hash[key] = "#{base_url}/#{item}"
51
- end
52
- Webdrivers.logger.debug "Versions now located on downloads site: #{ds.keys}"
53
- ds
54
- end
55
- end
56
-
57
- # Returns release version from the currently installed Chrome version
58
- #
59
- # @example
60
- # 73.0.3683.75 -> 73.0.3683
61
- def release_version
62
- chrome_version[/\d+\.\d+\.\d+/]
63
- end
64
-
65
- # Returns currently installed Chrome version
66
- def chrome_version
67
- ver = case platform
68
- when 'win'
69
- chrome_on_windows
70
- when /linux/
71
- chrome_on_linux
72
- when 'mac'
73
- chrome_on_mac
74
- else
75
- raise NotImplementedError, 'Your OS is not supported by webdrivers gem.'
76
- end.chomp
77
-
78
- raise StandardError, 'Failed to find Chrome binary or its version.' if ver.nil? || ver.empty?
79
-
80
- # Google Chrome 73.0.3683.75 -> 73.0.3683.75
81
- ver[/\d+\.\d+\.\d+\.\d+/]
82
- end
83
-
84
- def chrome_on_windows
85
- if browser_binary
86
- Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
87
- return `powershell (Get-ItemProperty '#{browser_binary}').VersionInfo.ProductVersion`.strip
88
- end
89
-
90
- # Workaround for Google Chrome when using Jruby on Windows.
91
- # @see https://github.com/titusfortner/webdrivers/issues/41
92
- if RUBY_PLATFORM == 'java' && platform == 'win'
93
- ver = 'powershell (Get-Item -Path ((Get-ItemProperty "HKLM:\\Software\\Microsoft' \
94
- "\\Windows\\CurrentVersion\\App` Paths\\chrome.exe\").\\'(default)\\'))" \
95
- '.VersionInfo.ProductVersion'
96
- return `#{ver}`.strip
97
- end
98
-
99
- # Default to Google Chrome
100
- reg = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
101
- executable = `powershell (Get-ItemProperty '#{reg}' -Name '(default)').'(default)'`.strip
102
- Webdrivers.logger.debug "Browser executable: '#{executable}'"
103
- ps = "(Get-Item (Get-ItemProperty '#{reg}').'(default)').VersionInfo.ProductVersion"
104
- `powershell #{ps}`.strip
105
- end
106
-
107
- def chrome_on_linux
108
- if browser_binary
109
- Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
110
- return `#{Shellwords.escape browser_binary} --product-version`.strip
111
- end
112
-
113
- # Default to Google Chrome
114
- executable = `which google-chrome`.strip
115
- Webdrivers.logger.debug "Browser executable: '#{executable}'"
116
- `#{executable} --product-version`.strip
117
- end
118
-
119
- def chrome_on_mac
120
- if browser_binary
121
- Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
122
- return `#{Shellwords.escape browser_binary} --version`.strip
123
- end
124
-
125
- # Default to Google Chrome
126
- executable = Shellwords.escape '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
127
- Webdrivers.logger.debug "Browser executable: #{executable}"
128
- `#{executable} --version`.strip
129
- end
130
-
131
- #
132
- # Returns user defined browser executable path from Selenium::WebDrivers::Chrome#path.
133
- #
134
- def browser_binary
135
- # For Chromium, Brave, or whatever else
136
- Selenium::WebDriver::Chrome.path
137
- end
138
- end
139
- end
140
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+ require 'shellwords'
5
+
6
+ module Webdrivers
7
+ class Chromedriver < Common
8
+ class << self
9
+ def current_version
10
+ Webdrivers.logger.debug 'Checking current version'
11
+ return nil unless downloaded?
12
+
13
+ ver = `#{binary} --version`
14
+ Webdrivers.logger.debug "Current #{binary} version: #{ver}"
15
+
16
+ # Matches 2.46, 2.46.628411 and 73.0.3683.75
17
+ normalize_version ver[/\d+\.\d+(\.\d+)?(\.\d+)?/]
18
+ end
19
+
20
+ def latest_version
21
+ return @latest_version if @latest_version
22
+
23
+ raise StandardError, 'Can not reach site' unless site_available?
24
+
25
+ # Versions before 70 do not have a LATEST_RELEASE file
26
+ return normalize_version('2.46') if release_version < normalize_version('70.0.3538')
27
+
28
+ latest_applicable = latest_point_release(release_version)
29
+
30
+ Webdrivers.logger.debug "Latest version available: #{latest_applicable}"
31
+ @latest_version = normalize_version(latest_applicable)
32
+ end
33
+
34
+ private
35
+
36
+ def latest_point_release(version)
37
+ release_file = "LATEST_RELEASE_#{version}"
38
+ begin
39
+ normalize_version(get(URI.join(base_url, release_file)))
40
+ rescue Net::HTTPServerException
41
+ latest_release = normalize_version(get(URI.join(base_url, 'LATEST_RELEASE')))
42
+ Webdrivers.logger.debug "Unable to find a driver for: #{version}"
43
+
44
+ msg = version > latest_release ? 'you appear to be using a non-production version of Chrome; ' : ''
45
+ msg = "#{msg}please set `Webdrivers::Chromedriver.version = <desired driver version>` to an known "\
46
+ 'chromedriver version: https://chromedriver.storage.googleapis.com/index.html'
47
+ raise StandardError, msg
48
+ end
49
+ end
50
+
51
+ def platform
52
+ if Selenium::WebDriver::Platform.linux?
53
+ 'linux64'
54
+ elsif Selenium::WebDriver::Platform.mac?
55
+ 'mac64'
56
+ else
57
+ 'win32'
58
+ end
59
+ end
60
+
61
+ def file_name
62
+ Selenium::WebDriver::Platform.windows? ? 'chromedriver.exe' : 'chromedriver'
63
+ end
64
+
65
+ def base_url
66
+ 'https://chromedriver.storage.googleapis.com'
67
+ end
68
+
69
+ def download_url
70
+ return @download_url if @download_url
71
+
72
+ url = "#{base_url}/#{desired_version}/chromedriver_#{platform}.zip"
73
+ Webdrivers.logger.debug "chromedriver URL: #{url}"
74
+ @download_url = url
75
+ end
76
+
77
+ # Returns release version from the currently installed Chrome version
78
+ #
79
+ # @example
80
+ # 73.0.3683.75 -> 73.0.3683
81
+ def release_version
82
+ chrome = normalize_version(chrome_version)
83
+ normalize_version(chrome.segments[0..2].join('.'))
84
+ end
85
+
86
+ # Returns currently installed Chrome version
87
+ def chrome_version
88
+ ver = case platform
89
+ when 'win32'
90
+ chrome_on_windows
91
+ when 'linux64'
92
+ chrome_on_linux
93
+ when 'mac64'
94
+ chrome_on_mac
95
+ else
96
+ raise NotImplementedError, 'Your OS is not supported by webdrivers gem.'
97
+ end.chomp
98
+
99
+ raise StandardError, 'Failed to find Chrome binary or its version.' if ver.nil? || ver.empty?
100
+
101
+ Webdrivers.logger.debug "Browser version: #{ver}"
102
+ normalize_version ver[/\d+\.\d+\.\d+\.\d+/] # Google Chrome 73.0.3683.75 -> 73.0.3683.75
103
+ end
104
+
105
+ def chrome_on_windows
106
+ if browser_binary
107
+ Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
108
+ return `powershell (Get-ItemProperty '#{browser_binary}').VersionInfo.ProductVersion`.strip
109
+ end
110
+
111
+ # Workaround for Google Chrome when using Jruby on Windows.
112
+ # @see https://github.com/titusfortner/webdrivers/issues/41
113
+ if RUBY_PLATFORM == 'java'
114
+ ver = 'powershell (Get-Item -Path ((Get-ItemProperty "HKLM:\\Software\\Microsoft' \
115
+ "\\Windows\\CurrentVersion\\App` Paths\\chrome.exe\").\\'(default)\\'))" \
116
+ '.VersionInfo.ProductVersion'
117
+ return `#{ver}`.strip
118
+ end
119
+
120
+ # Default to Google Chrome
121
+ reg = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
122
+ executable = `powershell (Get-ItemProperty '#{reg}' -Name '(default)').'(default)'`.strip
123
+ Webdrivers.logger.debug "Browser executable: '#{executable}'"
124
+ ps = "(Get-Item (Get-ItemProperty '#{reg}').'(default)').VersionInfo.ProductVersion"
125
+ `powershell #{ps}`.strip
126
+ end
127
+
128
+ def chrome_on_linux
129
+ if browser_binary
130
+ Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
131
+ return `#{Shellwords.escape browser_binary} --product-version`.strip
132
+ end
133
+
134
+ # Default to Google Chrome
135
+ executable = `which google-chrome`.strip
136
+ Webdrivers.logger.debug "Browser executable: '#{executable}'"
137
+ `#{executable} --product-version`.strip
138
+ end
139
+
140
+ def chrome_on_mac
141
+ if browser_binary
142
+ Webdrivers.logger.debug "Browser executable: '#{browser_binary}'"
143
+ return `#{Shellwords.escape browser_binary} --version`.strip
144
+ end
145
+
146
+ # Default to Google Chrome
147
+ executable = Shellwords.escape '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
148
+ Webdrivers.logger.debug "Browser executable: #{executable}"
149
+ `#{executable} --version`.strip
150
+ end
151
+
152
+ #
153
+ # Returns user defined browser executable path from Selenium::WebDrivers::Chrome#path.
154
+ #
155
+ def browser_binary
156
+ # For Chromium, Brave, or whatever else
157
+ Selenium::WebDriver::Chrome.path
158
+ end
159
+ end
160
+ end
161
+ end
@@ -1,205 +1,221 @@
1
- require 'rubygems/package'
2
- require 'zip'
3
-
4
- module Webdrivers
5
- class Common
6
- class << self
7
- attr_accessor :version
8
-
9
- def update
10
- unless site_available?
11
- return current_version.nil? ? nil : binary
12
- end
13
-
14
- # Newer not specified or latest not found, so use existing
15
- return binary if desired_version.nil? && File.exist?(binary)
16
-
17
- # Can't find desired and no existing binary
18
- if desired_version.nil?
19
- msg = "Unable to find the latest version of #{file_name}; try downloading manually from #{base_url} and place in #{install_dir}"
20
- raise StandardError, msg
21
- end
22
-
23
- if correct_binary?
24
- Webdrivers.logger.debug 'Expected webdriver version found'
25
- return binary
26
- end
27
-
28
- remove # Remove outdated exe
29
- download # Fetch latest
30
- end
31
-
32
- def desired_version
33
- if version.is_a?(Gem::Version)
34
- version
35
- elsif version.nil?
36
- latest_version
37
- else
38
- Gem::Version.new(version.to_s)
39
- end
40
- end
41
-
42
- def latest_version
43
- raise StandardError, 'Can not reach site' unless site_available?
44
-
45
- downloads.keys.max
46
- end
47
-
48
- def remove
49
- Webdrivers.logger.debug "Deleting #{binary}"
50
- FileUtils.rm_f binary
51
- end
52
-
53
- def download
54
- raise StandardError, 'Can not reach site' unless site_available?
55
-
56
- url = downloads[desired_version]
57
- filename = File.basename url
58
-
59
- FileUtils.mkdir_p(install_dir) unless File.exist?(install_dir)
60
- Dir.chdir install_dir do
61
- FileUtils.rm_f filename
62
- File.open(filename, 'wb') do |file|
63
- file.print get(url)
64
- end
65
- raise "Could not download #{url}" unless File.exist? filename
66
-
67
- Webdrivers.logger.debug "Successfully downloaded #{filename}"
68
- dcf = decompress_file(filename)
69
- Webdrivers.logger.debug 'Decompression Complete'
70
- if dcf
71
- Webdrivers.logger.debug "Deleting #{filename}"
72
- FileUtils.rm_f filename
73
- end
74
- if respond_to? :extract_file
75
- Webdrivers.logger.debug "Extracting #{dcf}"
76
- extract_file(dcf)
77
- end
78
- end
79
- raise "Could not decompress #{filename} to get #{binary}" unless File.exist?(binary)
80
-
81
- FileUtils.chmod 'ugo+rx', binary
82
- Webdrivers.logger.debug "Completed download and processing of #{binary}"
83
- binary
84
- end
85
-
86
- def install_dir
87
- Webdrivers.install_dir || File.expand_path(File.join(ENV['HOME'], '.webdrivers'))
88
- end
89
-
90
- def binary
91
- File.join install_dir, file_name
92
- end
93
-
94
- protected
95
-
96
- def get(url, limit = 10)
97
- raise StandardError, 'Too many HTTP redirects' if limit.zero?
98
-
99
- response = http.get_response(URI(url))
100
- Webdrivers.logger.debug "Get response: #{response.inspect}"
101
-
102
- case response
103
- when Net::HTTPSuccess
104
- response.body
105
- when Net::HTTPRedirection
106
- location = response['location']
107
- Webdrivers.logger.debug "Redirected to #{location}"
108
- get(location, limit - 1)
109
- else
110
- response.value
111
- end
112
- end
113
-
114
- def http
115
- if using_proxy
116
- Net::HTTP.Proxy(Webdrivers.proxy_addr, Webdrivers.proxy_port,
117
- Webdrivers.proxy_user, Webdrivers.proxy_pass)
118
- else
119
- Net::HTTP
120
- end
121
- end
122
-
123
- private
124
-
125
- def using_proxy
126
- Webdrivers.proxy_addr && Webdrivers.proxy_port
127
- end
128
-
129
- def downloaded?
130
- result = File.exist? binary
131
- Webdrivers.logger.debug "File is already downloaded: #{result}"
132
- result
133
- end
134
-
135
- def site_available?
136
- Webdrivers.logger.debug "Looking for Site: #{base_url}"
137
- get(base_url)
138
- Webdrivers.logger.debug "Found Site: #{base_url}"
139
- true
140
- rescue StandardError => e
141
- Webdrivers.logger.debug e
142
- false
143
- end
144
-
145
- def platform
146
- if Selenium::WebDriver::Platform.linux?
147
- "linux#{Selenium::WebDriver::Platform.bitsize}"
148
- elsif Selenium::WebDriver::Platform.mac?
149
- 'mac'
150
- else
151
- 'win'
152
- end
153
- end
154
-
155
- def decompress_file(filename)
156
- case filename
157
- when /tar\.gz$/
158
- Webdrivers.logger.debug 'Decompressing tar'
159
- untargz_file(filename)
160
- when /tar\.bz2$/
161
- Webdrivers.logger.debug 'Decompressing bz2'
162
- system "tar xjf #{filename}"
163
- filename.gsub('.tar.bz2', '')
164
- when /\.zip$/
165
- Webdrivers.logger.debug 'Decompressing zip'
166
- unzip_file(filename)
167
- else
168
- Webdrivers.logger.debug 'No Decompression needed'
169
- nil
170
- end
171
- end
172
-
173
- def untargz_file(filename)
174
- tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(filename))
175
-
176
- File.open(file_name, 'w+b') do |ucf|
177
- tar_extract.each { |entry| ucf << entry.read }
178
- File.basename ucf
179
- end
180
- end
181
-
182
- def unzip_file(filename)
183
- Zip::File.open("#{Dir.pwd}/#{filename}") do |zip_file|
184
- zip_file.each do |f|
185
- @top_path ||= f.name
186
- f_path = File.join(Dir.pwd, f.name)
187
- FileUtils.rm_rf(f_path) if File.exist?(f_path)
188
- FileUtils.mkdir_p(File.dirname(f_path)) unless File.exist?(File.dirname(f_path))
189
- zip_file.extract(f, f_path)
190
- end
191
- end
192
- @top_path
193
- end
194
-
195
- # Already have latest version downloaded?
196
- def correct_binary?
197
- desired_version == current_version && File.exist?(binary)
198
- end
199
-
200
- def normalize(string)
201
- Gem::Version.new(string)
202
- end
203
- end
204
- end
205
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems/package'
4
+ require 'zip'
5
+
6
+ module Webdrivers
7
+ class Common
8
+ class << self
9
+ attr_accessor :version
10
+
11
+ def update
12
+ unless site_available?
13
+ return current_version.nil? ? nil : binary
14
+ end
15
+
16
+ # Newer not specified or latest not found, so use existing
17
+ return binary if desired_version.nil? && File.exist?(binary)
18
+
19
+ # Can't find desired and no existing binary
20
+ if desired_version.nil?
21
+ msg = "Unable to find the latest version of #{file_name}; try downloading manually " \
22
+ "from #{base_url} and place in #{install_dir}"
23
+ raise StandardError, msg
24
+ end
25
+
26
+ if correct_binary?
27
+ Webdrivers.logger.debug 'Expected webdriver version found'
28
+ return binary
29
+ end
30
+
31
+ remove # Remove outdated exe
32
+ download # Fetch latest
33
+ end
34
+
35
+ def desired_version
36
+ if version.is_a?(Gem::Version)
37
+ version
38
+ elsif version.nil?
39
+ latest_version
40
+ else
41
+ normalize_version(version)
42
+ end
43
+ end
44
+
45
+ def latest_version
46
+ return @latest_version if @latest_version
47
+
48
+ raise StandardError, 'Can not reach site' unless site_available?
49
+
50
+ @latest_version = downloads.keys.max
51
+ end
52
+
53
+ def remove
54
+ max_attempts = 3
55
+ attempts_made = 0
56
+ delay = 0.5
57
+ Webdrivers.logger.debug "Deleting #{binary}"
58
+ @download_url = nil
59
+
60
+ begin
61
+ attempts_made += 1
62
+ File.delete binary if File.exist? binary
63
+ rescue Errno::EACCES # Solves an intermittent file locking issue on Windows
64
+ sleep(delay)
65
+ retry if File.exist?(binary) && attempts_made <= max_attempts
66
+ raise
67
+ end
68
+ end
69
+
70
+ def download
71
+ raise StandardError, 'Can not reach site' unless site_available?
72
+
73
+ filename = File.basename download_url
74
+
75
+ FileUtils.mkdir_p(install_dir) unless File.exist?(install_dir)
76
+ Dir.chdir install_dir do
77
+ df = Tempfile.open(['', filename], binmode: true) do |file|
78
+ file.print get(download_url)
79
+ file
80
+ end
81
+
82
+ raise "Could not download #{download_url}" unless File.exist? df.to_path
83
+
84
+ Webdrivers.logger.debug "Successfully downloaded #{df.to_path}"
85
+
86
+ decompress_file(df.to_path, filename)
87
+ Webdrivers.logger.debug 'Decompression Complete'
88
+ Webdrivers.logger.debug "Deleting #{df.to_path}"
89
+ df.close!
90
+ end
91
+ raise "Could not decompress #{download_url} to get #{binary}" unless File.exist?(binary)
92
+
93
+ FileUtils.chmod 'ugo+rx', binary
94
+ Webdrivers.logger.debug "Completed download and processing of #{binary}"
95
+ binary
96
+ end
97
+
98
+ def install_dir
99
+ Webdrivers.install_dir || File.expand_path(File.join(ENV['HOME'], '.webdrivers'))
100
+ end
101
+
102
+ def binary
103
+ File.join install_dir, file_name
104
+ end
105
+
106
+ protected
107
+
108
+ def get(url, limit = 10)
109
+ raise StandardError, 'Too many HTTP redirects' if limit.zero?
110
+
111
+ response = http.get_response(URI(url))
112
+ Webdrivers.logger.debug "Get response: #{response.inspect}"
113
+
114
+ case response
115
+ when Net::HTTPSuccess
116
+ response.body
117
+ when Net::HTTPRedirection
118
+ location = response['location']
119
+ Webdrivers.logger.debug "Redirected to #{location}"
120
+ get(location, limit - 1)
121
+ else
122
+ response.value
123
+ end
124
+ end
125
+
126
+ def http
127
+ if using_proxy
128
+ Net::HTTP.Proxy(Webdrivers.proxy_addr, Webdrivers.proxy_port,
129
+ Webdrivers.proxy_user, Webdrivers.proxy_pass)
130
+ else
131
+ Net::HTTP
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def download_url
138
+ @download_url ||= downloads[desired_version]
139
+ end
140
+
141
+ def using_proxy
142
+ Webdrivers.proxy_addr && Webdrivers.proxy_port
143
+ end
144
+
145
+ def downloaded?
146
+ result = File.exist? binary
147
+ Webdrivers.logger.debug "File is already downloaded: #{result}"
148
+ result
149
+ end
150
+
151
+ def site_available?
152
+ Webdrivers.logger.debug "Looking for Site: #{base_url}"
153
+ get(base_url)
154
+ Webdrivers.logger.debug "Found Site: #{base_url}"
155
+ true
156
+ rescue StandardError => e
157
+ Webdrivers.logger.debug e
158
+ false
159
+ end
160
+
161
+ def platform
162
+ if Selenium::WebDriver::Platform.linux?
163
+ "linux#{Selenium::WebDriver::Platform.bitsize}"
164
+ elsif Selenium::WebDriver::Platform.mac?
165
+ 'mac'
166
+ else
167
+ 'win'
168
+ end
169
+ end
170
+
171
+ def decompress_file(filename, target)
172
+ case filename
173
+ when /tar\.gz$/
174
+ Webdrivers.logger.debug 'Decompressing tar'
175
+ untargz_file(filename)
176
+ when /tar\.bz2$/
177
+ Webdrivers.logger.debug 'Decompressing bz2'
178
+ system "tar xjf #{filename}"
179
+ filename.gsub('.tar.bz2', '')
180
+ when /\.zip$/
181
+ Webdrivers.logger.debug 'Decompressing zip'
182
+ unzip_file(filename)
183
+ else
184
+ Webdrivers.logger.debug 'No Decompression needed'
185
+ FileUtils.cp(filename, File.join(Dir.pwd, target))
186
+ end
187
+ end
188
+
189
+ def untargz_file(filename)
190
+ tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(filename))
191
+
192
+ File.open(file_name, 'w+b') do |ucf|
193
+ tar_extract.each { |entry| ucf << entry.read }
194
+ File.basename ucf
195
+ end
196
+ end
197
+
198
+ def unzip_file(filename)
199
+ Zip::File.open(filename) do |zip_file|
200
+ zip_file.each do |f|
201
+ @top_path ||= f.name
202
+ f_path = File.join(Dir.pwd, f.name)
203
+ FileUtils.rm_rf(f_path) if File.exist?(f_path)
204
+ FileUtils.mkdir_p(File.dirname(f_path)) unless File.exist?(File.dirname(f_path))
205
+ zip_file.extract(f, f_path)
206
+ end
207
+ end
208
+ @top_path
209
+ end
210
+
211
+ # Already have latest version downloaded?
212
+ def correct_binary?
213
+ desired_version == current_version && File.exist?(binary)
214
+ end
215
+
216
+ def normalize_version(version)
217
+ Gem::Version.new(version.to_s)
218
+ end
219
+ end
220
+ end
221
+ end