Sha256: 2607e4e958389993cdb0fec96dc40b8df20b6250ebf1477addba4191dd4df2d1

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'facets/hash/except'

module Webdriver
  module UserAgent
    class BrowserOptions

      def initialize(opts, user_agent_string)
        @options = opts
        options[:browser] ||= :firefox
        options[:agent] ||= :iphone
        options[:orientation] ||= :portrait
        
        options[:viewport_width], options[:viewport_height] = parse_viewport_sizes(options[:viewport_width], options[:viewport_height])
        
        initialize_for_browser(user_agent_string)
      end

      def method_missing(*args, &block)
        m = args.first
        value = options[m]
        super unless value
        value.downcase
      end

      def browser_options
        options.except(:browser, :agent, :orientation, :user_agent_string, :viewport_width, :viewport_height)
      end

      private

      def options
        @options ||= {}
      end

      def initialize_for_browser(user_agent_string)
        case options[:browser]
        when :firefox
          options[:profile] ||= Selenium::WebDriver::Firefox::Profile.new
          options[:profile]['general.useragent.override'] = user_agent_string
        when :chrome
          options[:switches] ||= []
          options[:switches] << "--user-agent=#{user_agent_string}"
        else
          raise "WebDriver UserAgent currently only supports :firefox and :chrome."
        end
        
      end
      
      def parse_viewport_sizes(width, height)
        return ["0","0"] unless "#{width}".to_i > 0 && "#{height}".to_i > 0
        
        ["#{width}", "#{height}"]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webdriver-user-agent-7.4 lib/webdriver-user-agent/browser_options.rb
webdriver-user-agent-7.3 lib/webdriver-user-agent/browser_options.rb