<commit_before>module Watir
  class Capabilities
    attr_reader :options

    def initialize(browser, options = {})
      @options = options.dup
      Watir.logger.info "Creating Browser instance of #{browser} with user provided options: #{@options.inspect}"

      deprecate_desired_capabilities
      deprecate_url_service if @options.key?(:service) && @options.key?(:url)

      @browser = if browser == :remote && @options.key?(:browser)
                   @options.delete(:browser)
                 elsif browser == :remote && @options.key?(:capabilities)
                   @options[:capabilities].browser_name.to_sym
                 else
                   browser.to_sym
                 end
      @selenium_browser = browser == :remote || options[:url] ? :remote : browser

      @selenium_opts = {}
    end

    def to_args
      [@selenium_browser, process_arguments]
    end

    private

    def process_arguments
      @selenium_opts[:listener] = @options.delete(:listener) if @options.key?(:listener)

      if @options.key?(:url)
        @selenium_opts[:url] = @options.delete(:url)
      else
        process_service(@options.delete(:service))
      end

      create_http_client
      process_browser_options
      process_capabilities
      Watir.logger.info "Creating Browser instance with Watir processed options: #{@selenium_opts.inspect}"

      @selenium_opts
    end

    def create_http_client
      client_timeout = @options.delete(:client_timeout)
      open_timeout = @options.delete(:open_timeout)
      read_timeout = @options.delete(:read_timeout)

      http_client = @options.delete(:http_client)

      %i[open_timeout read_timeout client_timeout].each do |t|
        next if http_client.nil? || !respond_to?(t)

        msg = "You can pass #{t} value directly into Watir::Browser opt without needing to use :http_client"
        Watir.logger.warn msg, ids: %i[http_client use_capabilities]
      end

      unless http_client
        Watir.logger.warn 'Check out the new Watir::HttpClient and let us know if there are missing features you need',
                          ids: [:watir_client]

        http_client = Watir::HttpClient.new
      end

      http_client.timeout = client_timeout if client_timeout
      http_client.open_timeout = open_timeout if open_timeout
      http_client.read_timeout = read_timeout if read_timeout
      @selenium_opts[:http_client] = http_client
    end

    def process_browser_options
      browser_options = @options.delete(:options) || {}

      case @selenium_browser
      when :chrome
        process_chrome_options(browser_options)
      when :firefox
        process_firefox_options(browser_options)
      when :safari
        process_safari_options(browser_options)
      when :remote
        process_remote_options
      when :ie
        process_ie_options(browser_options)
      end
    end

    def process_capabilities
      caps = @options.delete(:capabilities)

      if caps
        @selenium_opts.merge!(@options)
      else
        caps = Selenium::WebDriver::Remote::Capabilities.send @browser, @options
      end

      @selenium_opts[:desired_capabilities] = caps
    end

    def deprecate_desired_capabilities
      return unless @options.key?(:desired_capabilities)

      Watir.logger.deprecate(':desired_capabilities to initialize Browser',
                             ':capabilities or preferably :options',
                             ids: [:desired_capabilities],
                             reference: 'http://watir.com/guides/capabilities.html')
      @options[:capabilities] = @options.delete(:desired_capabilities)
    end

    def deprecate_url_service
      Watir.logger.deprecate('allowing Browser initialization with both :url & :service',
                             'just :service',
                             ids: [:url_service],
                             reference: 'http://watir.com/guides/capabilities.html')
    end

    def process_chrome_options(browser_options)
      if @options.key?(:args) || @options.key?(:switches)
        browser_options ||= {}
        browser_options[:args] = (@options.delete(:args) || @options.delete(:switches)).dup
      end
      if @options.delete(:headless)
        browser_options ||= {}
        browser_options[:args] ||= []
        browser_options[:args] += ['--headless', '--disable-gpu']
      end
      @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::Chrome::Options
      @selenium_opts[:options] ||= Selenium::WebDriver::Chrome::Options.new(**browser_options)
    end

    def process_firefox_options(browser_options)
      if browser_options.is_a? Selenium::WebDriver::Firefox::Options
        @selenium_opts[:options] = browser_options
      end

      if @options.delete(:headless)
        browser_options ||= {}
        browser_options[:args] ||= []
        browser_options[:args] += ['--headless']
      end<commit_msg>support building http client and add timeout deprecations<commit_after>module Watir
  class Capabilities
    attr_reader :options

    def initialize(browser, options = {})
      @options = options.dup
      Watir.logger.info "Creating Browser instance of #{browser} with user provided options: #{@options.inspect}"

      deprecate_desired_capabilities
      deprecate_url_service if @options.key?(:service) && @options.key?(:url)

      @browser = if browser == :remote && @options.key?(:browser)
                   @options.delete(:browser)
                 elsif browser == :remote && @options.key?(:capabilities)
                   @options[:capabilities].browser_name.to_sym
                 else
                   browser.to_sym
                 end
      @selenium_browser = browser == :remote || options[:url] ? :remote : browser

      @selenium_opts = {}
    end

    def to_args
      [@selenium_browser, process_arguments]
    end

    private

    def process_arguments
      @selenium_opts[:listener] = @options.delete(:listener) if @options.key?(:listener)

      if @options.key?(:url)
        @selenium_opts[:url] = @options.delete(:url)
      else
        process_service(@options.delete(:service))
      end

      process_http_client
      process_browser_options
      process_capabilities
      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
      if http_client.is_a?(Hash)
        http_client = Watir::HttpClient.new(http_client)
      elsif !http_client.is_a?(Selenium::WebDriver::Remote::Http::Common)
        raise TypeError, ':http_client must be a Hash or a Selenium HTTP Client instance'
      end

      unless http_client.is_a?(Watir::HttpClient)
        Watir.logger.warn 'Check out the new Watir::HttpClient and let us know if there are missing features you need',
                          ids: [:watir_client]
      end

      process_http_client_timeouts(http_client)
      @selenium_opts[:http_client] = http_client
    end

    def process_browser_options
      browser_options = @options.delete(:options) || {}

      case @selenium_browser
      when :chrome
        process_chrome_options(browser_options)
      when :firefox
        process_firefox_options(browser_options)
      when :safari
        process_safari_options(browser_options)
      when :remote
        process_remote_options
      when :ie
        process_ie_options(browser_options)
      end
    end

    def process_capabilities
      caps = @options.delete(:capabilities)

      if caps
        @selenium_opts.merge!(@options)
      else
        caps = Selenium::WebDriver::Remote::Capabilities.send @browser, @options
      end

      @selenium_opts[:desired_capabilities] = caps
    end

    def deprecate_desired_capabilities
      return unless @options.key?(:desired_capabilities)

      Watir.logger.deprecate(':desired_capabilities to initialize Browser',
                             ':capabilities or preferably :options',
                             ids: [:desired_capabilities],
                             reference: 'http://watir.com/guides/capabilities.html')
      @options[:capabilities] = @options.delete(:desired_capabilities)
    end

    def deprecate_url_service
      Watir.logger.deprecate('allowing Browser initialization with both :url & :service',
                             'just :service',
                             ids: [:url_service],
                             reference: 'http://watir.com/guides/capabilities.html')
    end

    def process_http_client_timeouts(http_client)
      deprecate_client_timeout(http_client) if @options.key? :client_timeout
      deprecate_open_timeout(http_client) if @options.key? :open_timeout
      deprecate_read_timeout(http_client) if @options.key? :read_timeout
    end

    def deprecate_client_timeout(http_client)
      Watir.logger.deprecate(':client_timeout to initialize Browser',
                             ':open_timeout and/or :read_timeout in a Hash with :http_client key',
                             ids: [:http_client_timeout],
                             reference: 'http://watir.com/guides/capabilities.html')
      timeout = @options.delete(:client_timeout)
      http_client.open_timeout = timeout
      http_client.read_timeout = timeout
    end

    def deprecate_open_timeout(http_client)
      Watir.logger.deprecate(':open_timeout to initialize Browser',
                             ':open_timeout in a Hash with :http_client key',
                             ids: %i[http_open_timeout capabilities],
                             reference: 'http://watir.com/guides/capabilities.html')
      http_client.open_timeout = @options.delete(:open_timeout)
    end

    def deprecate_read_timeout(http_client)
      Watir.logger.deprecate(':read_timeout to initialize Browser',
                             ':read_timeout in a Hash with :http_client key',
                             ids: %i[http_read_timeout capabilities],
                             reference: 'http://watir.com/guides/capabilities.html')
      http_client.read_timeout = @options.delete(:read_timeout)
    end

    def process_chrome_options(browser_options)
      if @options.key?(:args) || @options.key?(:switches)
        browser_options ||= {}
        browser_options[:args] = (@options.delete(:args) || @options.delete(:switches)).dup
      end
      if @options.delete(:headless)
        browser_options ||= {}
        browser_options[:args] ||= []
        browser_options[:args] += ['--headless', '--disable-gpu']
      end
      @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::Chrome::Options
      @selenium_opts[:options] ||= Selenium::WebDriver::Chrome::Options.new(**browser_options)
    end

    def process_firefox_options(browser_options)
      if browser_options.is_a? Selenium::WebDriver::Firefox::Options
        @selenium_opts[:options] = browser_options
      end

      if @options.delete(:headless)
        browser_options ||= {}
        browser_options[:args] ||= []
        browser_options[:args] += ['--headless']
      end
{'fraction_non_alphanumeric': 0.10017857142857142, 'fraction_numerical': 0.0, 'mean_word_length': 2.6120606256046437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188658', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3073, 'n_words': 615}