Skip to content

Commit

Permalink
Add rake tasks for update, remove, and version.
Browse files Browse the repository at this point in the history
  • Loading branch information
kapoorlakshya authored and titusfortner committed May 21, 2019
1 parent 82d784e commit 050e8ce
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 10 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Metrics/MethodLength:
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- 'lib/webdrivers/tasks/*.rake'

Metrics/ClassLength:
Max: 116
Expand Down
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ The default download location is `~/.webdrivers` directory, and this is configur
Webdrivers.install_dir = '/webdrivers/install/dir'
```

Alternatively, you can define the path via the `WD_INSTALL_DIR` environment
variable.

### Version Pinning

If you would like to use a specific (older or beta) version, you can specify it for each driver. Otherwise,
Expand All @@ -64,20 +67,29 @@ Webdrivers::IEdriver.required_version = '3.14.0'
Webdrivers::MSWebdriver.required_version = '17134'
```

You can explicitly trigger the update in your code, but this will happen automatically when the driver is initialized:
You can explicitly trigger the update in your code, but this will happen
automatically when the driver is initialized:

```ruby
Webdrivers::Chromedriver.update
```

### Caching Drivers

You can set Webdrivers to only look for updates if the previous check was longer ago than a specified number of seconds.
You can set Webdrivers to only look for updates if the previous check
was longer ago than a specified number of seconds.

```ruby
Webdrivers.cache_time = 86_400
```

The default in 3.9 is zero, and the default for 4.x will be 24 hours (86,400 seconds)
Alternatively, you can define this value via the `WD_CACHE_TIME` environment
variable.

The default in v3.9 is zero, and the default for 4.x will be
24 hours (86,400 seconds). If you see a warning about the cache time not
being set, manually set the cache time to `86_400` or whatever you want
to turn off the warning.

### Proxy

Expand Down Expand Up @@ -113,6 +125,38 @@ require 'net_http_ssl_fix'

Other solutions are documented on the RubyGems [website](https://guides.rubygems.org/ssl-certificate-update/).

### Rake tasks

Each driver has its own set of `rake` tasks that you can call once before
executing the tests. These are especially useful if you're running tests
in parallel and want to avoid performing an update check per thread.

```bash
$ bundle exec rake -T
rake webdrivers:chromedriver:remove # Force remove chromedriver
rake webdrivers:chromedriver:update[version] # Remove and download updated chromedriver if necessary
rake webdrivers:chromedriver:version # Print current chromedriver version
rake webdrivers:geckodriver:remove # Force remove geckodriver
rake webdrivers:geckodriver:update[version] # Remove and download updated geckodriver if necessary
rake webdrivers:geckodriver:version # Print current geckodriver version
rake webdrivers:iedriver:remove # Force remove IEDriverServer
rake webdrivers:iedriver:update[version] # Remove and download updated IEDriverServer if necessary
rake webdrivers:iedriver:version # Print current IEDriverServer version
```

These tasks respect the `WD_INSTALL_DIR` and `WD_CACHE_TIME` environment
variables which can also be passed through the `rake` command:

```bash
$ bundle exec rake webdrivers:chromedriver:update[2.46] webdrivers:geckodriver:update[0.24.0] WD_CACHE_TIME=86_400 WD_INSTALL_DIR='my_dir'
2019-05-20 19:03:01 INFO Webdrivers Updated to chromedriver 2.46.628388
2019-05-20 19:03:04 INFO Webdrivers Updated to geckodriver 0.24.0
```

Please note that these tasks do not use any of the configurations from your
project (code) and only respect the `ENV` variables and the version (optional)
passed to the `rake` task.

### Logging

The logging level can be configured for debugging purpose:
Expand All @@ -129,7 +173,7 @@ The version of `chromedriver` will depend on the version of Chrome you are using

* For versions >= 70, the downloaded version of `chromedriver` will match the installed version of Google Chrome.
More information [here](http://chromedriver.chromium.org/downloads/version-selection).
* For versions <= 69, `chromedriver` version 2.46 will be downloaded.
* For versions <= 69, `chromedriver` version 2.41 will be downloaded.
* For beta versions, you'll have to set the desired beta version of `chromedriver`
using `Webdrivers::Chromedriver.required_version`.

Expand Down
1 change: 1 addition & 0 deletions lib/webdrivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require 'webdrivers/geckodriver'
require 'webdrivers/iedriver'
require 'webdrivers/mswebdriver'
require 'webdrivers/railtie' if defined?(Rails)
6 changes: 6 additions & 0 deletions lib/webdrivers/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require 'webdrivers'

path = File.expand_path(__dir__)
Dir.glob("#{path}/tasks/*.rake").each { |f| import f }
13 changes: 11 additions & 2 deletions lib/webdrivers/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class NetworkError < StandardError
end

class << self
attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_pass, :install_dir
attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_pass
attr_writer :install_dir

#
# Returns the amount of time (Seconds) the gem waits between two update checks.
#
def cache_time
@cache_time || 0
@cache_time || ENV['WD_CACHE_TIME'].to_i
end

#
Expand All @@ -36,6 +37,14 @@ def cache_time=(value)
@cache_time = value
end

#
# Returns the install (download) directory path for the drivers.
#
# @return [String]
def install_dir
@install_dir || ENV['WD_INSTALL_DIR'] || File.expand_path(File.join(ENV['HOME'], '.webdrivers'))
end

def logger
@logger ||= Webdrivers::Logger.new
end
Expand Down
15 changes: 15 additions & 0 deletions lib/webdrivers/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'webdrivers'
require 'rails'

module Webdrivers
class Railtie < Rails::Railtie
railtie_name :webdrivers

rake_tasks do
path = File.expand_path(__dir__)
Dir.glob("#{path}/tasks/*.rake").each { |f| load f }
end
end
end
2 changes: 1 addition & 1 deletion lib/webdrivers/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def delete(file)
end

def install_dir
Webdrivers.install_dir || File.expand_path(File.join(ENV['HOME'], '.webdrivers'))
Webdrivers.install_dir
end

def cache_version(file_name, version)
Expand Down
46 changes: 46 additions & 0 deletions lib/webdrivers/tasks/chromedriver.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

namespace :webdrivers do
require 'webdrivers/chromedriver'

namespace :chromedriver do
Webdrivers.logger.level = :info

desc 'Print current chromedriver version'
task :version do
gem_ver = Webdrivers::Chromedriver.current_version
if gem_ver
Webdrivers.logger.info "chromedriver #{gem_ver.version}"
else
Webdrivers.logger.warn 'No existing chromedriver found.'
end
end

desc 'Remove and download updated chromedriver if necessary'
task :update, [:version] do |_, args|
args.with_defaults(version: 0)
Webdrivers.cache_time = ENV.fetch('WD_CACHE_TIME', 86_400)
Webdrivers.install_dir = ENV.fetch('WD_INSTALL_DIR', nil)
Webdrivers::Chromedriver.required_version = args.version
Webdrivers::Chromedriver.update
Webdrivers.logger.info "Updated to chromedriver #{Webdrivers::Chromedriver.current_version}"
end

desc 'Force remove chromedriver'
task :remove do
unless File.exist? Webdrivers::Chromedriver.driver_path
Webdrivers.logger.info 'No existing chromedriver to remove.'
next # Return early
end

cur_version = Webdrivers::Chromedriver.current_version
Webdrivers::Chromedriver.remove

if File.exist? Webdrivers::Chromedriver.driver_path # Failed for some reason
Webdrivers.logger.error 'Failed to remove chromedriver. Please try removing manually.'
else
Webdrivers.logger.info "Removed chromedriver #{cur_version}."
end
end
end
end
46 changes: 46 additions & 0 deletions lib/webdrivers/tasks/geckodriver.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

namespace :webdrivers do
require 'webdrivers/geckodriver'

namespace :geckodriver do
Webdrivers.logger.level = :info

desc 'Print current geckodriver version'
task :version do
gem_ver = Webdrivers::Geckodriver.current_version
if gem_ver
Webdrivers.logger.info "geckodriver #{gem_ver.version}"
else
Webdrivers.logger.warn 'No existing geckodriver found.'
end
end

desc 'Remove and download updated geckodriver if necessary'
task :update, [:version] do |_, args|
args.with_defaults(version: 0)
Webdrivers.cache_time = ENV.fetch('WD_CACHE_TIME', 86_400)
Webdrivers.install_dir = ENV.fetch('WD_INSTALL_DIR', nil)
Webdrivers::Geckodriver.required_version = args.version
Webdrivers::Geckodriver.update
Webdrivers.logger.info "Updated to geckodriver #{Webdrivers::Geckodriver.current_version}"
end

desc 'Force remove geckodriver'
task :remove do
unless File.exist? Webdrivers::Geckodriver.driver_path
Webdrivers.logger.info 'No existing geckodriver to remove.'
next # Return early
end

cur_version = Webdrivers::Geckodriver.current_version
Webdrivers::Geckodriver.remove

if File.exist? Webdrivers::Geckodriver.driver_path # Failed for some reason
Webdrivers.logger.error 'Failed to remove geckodriver. Please try removing manually.'
else
Webdrivers.logger.info "Removed geckodriver #{cur_version}."
end
end
end
end
46 changes: 46 additions & 0 deletions lib/webdrivers/tasks/iedriver.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

namespace :webdrivers do
require 'webdrivers/iedriver'

namespace :iedriver do
Webdrivers.logger.level = :info

desc 'Print current IEDriverServer version'
task :version do
gem_ver = Webdrivers::IEdriver.current_version
if gem_ver
Webdrivers.logger.info "IEDriverServer #{gem_ver.version}"
else
Webdrivers.logger.warn 'No existing IEDriverServer found.'
end
end

desc 'Remove and download updated IEDriverServer if necessary'
task :update, [:version] do |_, args|
args.with_defaults(version: 0)
Webdrivers.cache_time = ENV.fetch('WD_CACHE_TIME', 86_400)
Webdrivers.install_dir = ENV.fetch('WD_INSTALL_DIR', nil)
Webdrivers::IEdriver.required_version = args.version
Webdrivers::IEdriver.update
Webdrivers.logger.info "Updated to IEDriverServer #{Webdrivers::IEdriver.current_version}"
end

desc 'Force remove IEDriverServer'
task :remove do
unless File.exist? Webdrivers::IEdriver.driver_path
Webdrivers.logger.info 'No existing IEDriverServer to remove.'
next # Return early
end

cur_version = Webdrivers::IEdriver.current_version
Webdrivers::IEdriver.remove

if File.exist? Webdrivers::IEdriver.driver_path # Failed for some reason
Webdrivers.logger.error 'Failed to remove IEDriverServer. Please try removing manually.'
else
Webdrivers.logger.info "Removed IEDriverServer #{cur_version}."
end
end
end
end
38 changes: 35 additions & 3 deletions spec/webdrivers/chromedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@

expect(Webdrivers::Network).to have_received(:get).twice
end

context 'when required version is 0' do
it 'downloads the latest version' do
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('72.0.3626.7'))
chromedriver.required_version = 0
chromedriver.update
expect(chromedriver.current_version.version).to eq('72.0.3626.7')
end
end

context 'when required version is nil' do
it 'downloads the latest version' do
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('72.0.3626.7'))
chromedriver.required_version = nil
chromedriver.update
expect(chromedriver.current_version.version).to eq('72.0.3626.7')
end
end

context 'when ENV variable WD_CACHE_TIME is set' do
it 'uses cache time value from ENV variable' do
allow(ENV).to receive(:[]).with('WD_CACHE_TIME').and_return('999')
expect(Webdrivers.cache_time).to be(999)
end
end
end

describe '#current_version' do
Expand Down Expand Up @@ -234,19 +259,26 @@

describe '#install_dir' do
it 'uses ~/.webdrivers as default value' do
expect(Webdrivers::System.install_dir).to include('.webdriver')
expect(Webdrivers.install_dir).to include('.webdriver')
end

it 'uses provided value' do
begin
install_dir = File.expand_path(File.join(ENV['HOME'], '.webdrivers2'))
install_dir = File.expand_path(File.join(ENV['HOME'], '.webdrivers2'))
Webdrivers.install_dir = install_dir

expect(Webdrivers::System.install_dir).to eq install_dir
expect(Webdrivers.install_dir).to eq install_dir
ensure
Webdrivers.install_dir = nil
end
end

context 'when ENV variable WD_INSTALL_DIR is set' do
it 'uses path from the ENV variable' do
allow(ENV).to receive(:[]).with('WD_INSTALL_DIR').and_return('custom_dir')
expect(Webdrivers.install_dir).to be('custom_dir')
end
end
end

describe '#driver_path' do
Expand Down

0 comments on commit 050e8ce

Please sign in to comment.