Class: Crabfarm::DefaultDriverFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/crabfarm/default_driver_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(_config = {}) ⇒ DefaultDriverFactory

Returns a new instance of DefaultDriverFactory.



4
5
6
# File 'lib/crabfarm/default_driver_factory.rb', line 4

def initialize(_config={})
  @config = _config
end

Instance Method Details

#build_driver(_session_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/crabfarm/default_driver_factory.rb', line 8

def build_driver(_session_id)

  raise ConfigurationError.new 'must provide a webdriver type' unless config_present? :name
  driver_name = @config[:name].to_sym

  driver = case driver_name
  when :noop
    require "crabfarm/mocks/noop_driver"
    driver = Crabfarm::Mocks::NoopDriver.new # TODO: improve dummy driver...
  when :remote
    load_remote_driver
  when :firefox
    load_firefox_driver
  when :chrome
    load_chrome_driver
  else
    load_other_driver driver_name
  end

  # apply browser configuration to new driver
  driver.manage.window.resize_to(@config[:window_width], @config[:window_height]) rescue nil

  return driver
end

#config_present?(_key) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/crabfarm/default_driver_factory.rb', line 81

def config_present?(_key)
  not (@config[_key].nil? or @config[_key].empty?)
end

#load_chrome_driverObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/crabfarm/default_driver_factory.rb', line 64

def load_chrome_driver
  switches = []

  if config_present? :proxy
    switches << "--proxy-server=#{@config[:proxy]}"
    switches << "--ignore-certificate-errors"
  end

  Selenium::WebDriver.for :chrome, :switches => switches
end

#load_firefox_driverObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/crabfarm/default_driver_factory.rb', line 51

def load_firefox_driver
  profile = Selenium::WebDriver::Firefox::Profile.new

  if config_present? :proxy
    profile.proxy = Selenium::WebDriver::Proxy.new({
      :http => @config[:proxy],
      :ssl => @config[:proxy]
    })
  end

  Selenium::WebDriver.for :firefox, :profile => profile
end

#load_other_driver(_name) ⇒ Object



75
76
77
78
79
# File 'lib/crabfarm/default_driver_factory.rb', line 75

def load_other_driver(_name)
  raise ConfigurationError.new 'default driver does not support proxy' if config_present? :proxy

  Selenium::WebDriver.for _name.to_sym
end

#load_remote_driverObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crabfarm/default_driver_factory.rb', line 33

def load_remote_driver
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.timeout = @config[:remote_timeout]

  if config_present? :proxy
    client.proxy = Selenium::WebDriver::Proxy.new({
      :http => @config[:proxy],
      :ssl => @config[:proxy]
    })
  end

  Selenium::WebDriver.for(:remote, {
    :url => @config[:remote_host],
    :http_client => client,
    :desired_capabilities => @config[:capabilities]
  })
end