Skip to content

Commit

Permalink
[rb] remove unnecessary methods from driver classes
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 20, 2019
1 parent b5ac3a3 commit 3490d4a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 96 deletions.
38 changes: 1 addition & 37 deletions rb/lib/selenium/webdriver/chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::DownloadsFiles

def initialize(opts = {})
opts[:desired_capabilities] = create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.send(browser)

opts[:url] ||= service_url(opts)

Expand All @@ -61,42 +61,6 @@ def quit
def execute_cdp(cmd, **params)
@bridge.send_command(cmd: cmd, params: params)
end

private

def create_capabilities(opts)
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.chrome }
options = opts.delete(:options) { Options.new }

profile = opts.delete(:profile)
if profile
profile = profile.as_json

options.args ||= []
if options.args.none?(&/user-data-dir/.method(:match?))
options.add_argument("--user-data-dir=#{profile['directory']}")
end

if profile['extensions']
WebDriver.logger.deprecate 'Using Selenium::WebDriver::Chrome::Profile#extensions',
'Selenium::WebDriver::Chrome::Options#add_extension'
profile['extensions'].each do |extension|
options.add_encoded_extension(extension)
end
end
end

detach = opts.delete(:detach)
options.add_option(:detach, true) if detach

options = options.as_json
caps.merge!(options) unless options[Options::KEY].empty?

caps[:proxy] = opts.delete(:proxy) if opts.key?(:proxy)
caps[:proxy] ||= opts.delete('proxy') if opts.key?('proxy')

caps
end
end # Driver
end # Chrome
end # WebDriver
Expand Down
11 changes: 10 additions & 1 deletion rb/lib/selenium/webdriver/chrome/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Selenium
module WebDriver
module Chrome
class Options < WebDriver::Common::Options
attr_accessor :profile

KEY = 'goog:chromeOptions'

Expand Down Expand Up @@ -54,6 +55,8 @@ class Options < WebDriver::Common::Options
# options = Selenium::WebDriver::Chrome::Options.new(args: ['start-maximized', 'user-data-dir=/tmp/temp_profile'])
# driver = Selenium::WebDriver.for(:chrome, options: options)
#
# @param [Profile] :profile An instance of a Firefox::Profile Class
# @param [Array] :encoded_extensions List of extensions that do not need to be Base64 encoded
# @param [Hash] opts the pre-defined options to create the Chrome::Options with
# @option opts [Array<String>] :args List of command-line arguments to use when starting Chrome
# @option opts [String] :binary Path to the Chrome executable to use
Expand All @@ -70,9 +73,10 @@ class Options < WebDriver::Common::Options
# @option opts [Array<String>] :window_types A list of window types to appear in the list of window handles
#

def initialize(encoded_extensions: nil, **opts)
def initialize(profile: nil, encoded_extensions: nil, **opts)
super(opts)

@profile = profile
@options[:encoded_extensions] = encoded_extensions if encoded_extensions
@options[:extensions]&.each(&method(:validate_extension))
end
Expand Down Expand Up @@ -182,6 +186,11 @@ def add_emulation(**opts)
def as_json(*)
options = super

if @profile
options['args'] ||= []
options['args'] << "--user-data-dir=#{@profile[:directory]}"
end

options['binary'] ||= binary_path if binary_path
extensions = options['extensions'] || []
encoded_extensions = options.delete(:encoded_extensions) || []
Expand Down
8 changes: 0 additions & 8 deletions rb/lib/selenium/webdriver/edge_chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ class Driver < Selenium::WebDriver::Chrome::Driver
def browser
:edge_chrome
end

private

def create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.edge_chrome
opts[:options] ||= Options.new
super(opts)
end
end # Driver
end # Chrome
end # WebDriver
Expand Down
13 changes: 1 addition & 12 deletions rb/lib/selenium/webdriver/edge_html/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::TakesScreenshot

def initialize(opts = {})
opts[:desired_capabilities] = create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.edge

opts[:url] ||= service_url(opts)

Expand All @@ -53,17 +53,6 @@ def quit
ensure
@service&.stop
end

private

def create_capabilities(opts)
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.edge }
options = opts.delete(:options) { Options.new }
options = options.as_json
caps.merge!(options) unless options.empty?

caps
end
end # Driver
end # Edge
end # WebDriver
Expand Down
13 changes: 1 addition & 12 deletions rb/lib/selenium/webdriver/firefox/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::TakesScreenshot

def initialize(opts = {})
opts[:desired_capabilities] = create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.firefox

opts[:url] ||= service_url(opts)

Expand All @@ -55,17 +55,6 @@ def quit
ensure
@service&.stop
end

private

def create_capabilities(opts)
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.firefox }
options = opts.delete(:options) { Options.new }
options = options.as_json
caps.merge!(options) unless options.empty?

caps
end
end # Driver
end # Firefox
end # WebDriver
Expand Down
14 changes: 1 addition & 13 deletions rb/lib/selenium/webdriver/ie/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::TakesScreenshot

def initialize(opts = {})
opts[:desired_capabilities] = create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.internet_explorer

opts[:url] ||= service_url(opts)

Expand All @@ -54,18 +54,6 @@ def quit
ensure
@service&.stop
end

private

def create_capabilities(opts)
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.internet_explorer }
options = opts.delete(:options) { Options.new }
options = options.as_json
caps.merge!(options) unless options.empty?

caps
end

end # Driver
end # IE
end # WebDriver
Expand Down
12 changes: 1 addition & 11 deletions rb/lib/selenium/webdriver/safari/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver
include DriverExtensions::TakesScreenshot

def initialize(opts = {})
opts[:desired_capabilities] = create_capabilities(opts)
opts[:desired_capabilities] ||= Remote::Capabilities.safari

opts[:url] ||= service_url(opts)

Expand All @@ -55,16 +55,6 @@ def quit
ensure
@service&.stop
end

private

def create_capabilities(opts = {})
caps = opts.delete(:desired_capabilities) { Remote::Capabilities.safari }
options = opts.delete(:options) { Options.new }
caps.merge!(options.as_json)
caps
end

end # Driver
end # Safari
end # WebDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ module EdgeChrome

expect(profile).to receive(:layout_on_disk).and_return 'ignored'

expect(profile.as_json).to eq(directory: 'ignored',
extensions: [Base64.strict_encode64('test')])
expect(profile.as_json).to eq('directory' => 'ignored',
'extensions' => [Base64.strict_encode64('test')])
end

it "raises an error if the extension doesn't exist" do
Expand Down

0 comments on commit 3490d4a

Please sign in to comment.