lib/watir/capabilities.rb in watir-7.1.0 vs lib/watir/capabilities.rb in watir-7.2.0
- old
+ new
@@ -1,30 +1,37 @@
+# frozen_string_literal: true
+
module Watir
class Capabilities
- attr_reader :options
+ attr_reader :options, :selenium_browser, :selenium_args
def initialize(browser = nil, options = {})
- if browser.is_a?(Hash)
- options = browser
- browser = nil
- end
+ @options, @browser = case browser
+ when Selenium::WebDriver::Driver
+ return
+ when ::Symbol, String
+ [options.dup, browser&.to_sym]
+ when Hash
+ [browser.dup, infer_browser(browser)]
+ when nil
+ [{}, infer_browser]
+ else
+ raise ArgumentError,
+ "expected Driver, String, Symbol or Hash, but received: #{browser.class}"
+ end
+ validate_options
- @options = options.dup
- Watir.logger.info "Creating Browser instance of #{browser} with user provided options: #{@options.inspect}"
-
- if @options.key?(:capabilities) && @options.key?(:options)
- raise(ArgumentError, ':capabilities and :options are not both allowed')
- end
- raise(ArgumentError, ':url and :service are not both allowed') if @options.key?(:service) && @options.key?(:url)
-
- @browser = browser.nil? && infer_browser || browser
-
@selenium_browser = @options.key?(:url) ? :remote : @browser
end
def to_args
- [@selenium_browser, process_arguments]
+ Watir.logger.info "Creating Browser instance of #{@browser} with user provided options: #{@options.inspect}"
+ @selenium_args = process_arguments
+ raise ArgumentError, "#{@options} are unrecognized arguments for Browser constructor" unless @options.empty?
+
+ Watir.logger.info "Selenium options generated by Watir: #{@selenium_args.inspect}"
+ [@selenium_browser, @selenium_args]
end
private
def process_arguments
@@ -37,13 +44,19 @@
service = process_service
selenium_opts[:service] = service if service
end
selenium_opts[:http_client] = process_http_client
- selenium_opts[:capabilities] = @options.delete(:capabilities) || process_browser_options
+ if @options.key?(:capabilities)
+ Watir.logger.deprecate(':capabilities argument in Browser constructor',
+ ':options argument with Selenium Options instance or Hash',
+ id: :capabilities)
+ selenium_opts[:capabilities] = @options.delete(:capabilities)
+ else
+ selenium_opts[:options] = process_browser_options
+ end
- Watir.logger.info "Creating Browser instance with Watir processed options: #{selenium_opts.inspect}"
selenium_opts
end
def process_http_client
http_client = @options.delete(:http_client) || Watir::HttpClient.new
@@ -62,11 +75,10 @@
end
end
def process_browser_options
browser_options = @options.delete(:options).dup || {}
- vendor_caps = process_vendor_capabilities(browser_options)
options = if browser_options.is_a? Selenium::WebDriver::Options
browser_options
else
convert_timeouts(browser_options)
@@ -74,13 +86,15 @@
end
options.unhandled_prompt_behavior ||= :ignore
process_proxy_options(options)
browser_specific_options(options)
- raise ArgumentError, "#{@options} are unrecognized arguments for Browser constructor" unless @options.empty?
- vendor_caps << options
+ vendor_opts = process_vendor_options(browser_options)
+ vendor_opts.each { |opts| options.add_option(opts) }
+
+ options
end
def process_proxy_options(options)
proxy = @options.delete(:proxy)
return if proxy.nil?
@@ -92,25 +106,25 @@
end
options.proxy = proxy
end
- def process_vendor_capabilities(opts)
+ def process_vendor_options(opts)
return [] unless opts.is_a? Hash
vendor = opts.select { |key, _val| key.to_s.include?(':') && opts.delete(key) }
vendor.map { |k, v| Selenium::WebDriver::Remote::Capabilities.new(k => v) }
end
def convert_timeouts(browser_options)
browser_options[:timeouts] ||= {}
- browser_options[:timeouts].keys.each do |key|
+ browser_options[:timeouts].each_key do |key|
raise(ArgumentError, 'do not set implicit wait, Watir handles waiting automatically') if key.to_s == 'implicit'
Watir.logger.deprecate('using timeouts directly in options',
":#{key}_timeout",
- id: 'timeouts')
+ id: :timeouts)
end
if browser_options.key?(:page_load_timeout)
browser_options[:timeouts][:page_load] = browser_options.delete(:page_load_timeout) * 1000
end
@@ -149,18 +163,27 @@
else
raise TypeError, "#{service} needs to be Selenium Service or Hash instance"
end
end
- def infer_browser
- if @options.key?(:browser)
- @options.delete(:browser)
- elsif @options.key?(:capabilities)
- @options[:capabilities].browser_name.tr(' ', '_').downcase.to_sym
- elsif @options.key?(:options)
- @options[:options].class.to_s.split('::')[-2].downcase.to_sym
- else
- :chrome
+ def infer_browser(options = nil)
+ options ||= {}
+ inferred = if options.key?(:capabilities)
+ options[:capabilities].browser_name.tr(' ', '_').downcase.to_sym
+ elsif options[:options].is_a?(Selenium::WebDriver::Options)
+ options[:options].class.to_s.split('::')[-2].downcase.to_sym
+ elsif options.key?(:options)
+ options.dig(:options, :browser_name).downcase.to_sym
+ else
+ :chrome
+ end
+ %i[msedge microsoftedge].include?(inferred) ? :edge : inferred
+ end
+
+ def validate_options
+ if @options.key?(:capabilities) && @options.key?(:options)
+ raise(ArgumentError, ':capabilities and :options are not both allowed')
end
+ raise(ArgumentError, ':url and :service are not both allowed') if @options.key?(:service) && @options.key?(:url)
end
end
end