Module: UserAgent

Defined in:
lib/webdriver-user-agent.rb

Class Method Summary collapse

Class Method Details

.agent_string_for(device) ⇒ Object



39
40
41
42
43
# File 'lib/webdriver-user-agent.rb', line 39

def self.agent_string_for device
  user_agent_string = devices[downcase_sym device][:user_agent]
  raise "Unsupported user agent: '#{options[:agent]}'." unless user_agent_string
  user_agent_string 
end

.devicesObject



30
31
32
# File 'lib/webdriver-user-agent.rb', line 30

def self.devices
  @devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
end

.driver(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webdriver-user-agent.rb', line 7

def self.driver options={} 
  options[:browser] ||= :firefox
  options[:agent] ||= :iphone
  options[:orientation] ||= :portrait
  
  user_agent_string = agent_string_for options[:agent]
  device_resolution = resolution_for options[:agent], options[:orientation]

  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
  driver = Selenium::WebDriver.for options[:browser], options.except(:browser, :agent, :orientation)
  resize_inner_window driver, *device_resolution
  driver
end

.resolution_for(device_name, orientation) ⇒ Object



34
35
36
37
# File 'lib/webdriver-user-agent.rb', line 34

def self.resolution_for device_name, orientation
  device = devices[downcase_sym device_name][downcase_sym orientation]
  [device[:width],device[:height]]
end