Class: Billy::Browsers::Capybara

Inherits:
Object
  • Object
show all
Defined in:
lib/billy/browsers/capybara.rb

Constant Summary collapse

DRIVERS =
{
  poltergeist: 'capybara/poltergeist',
  webkit: 'capybara/webkit',
  selenium: 'selenium/webdriver',
  apparition: 'capybara/apparition',
  cuprite: 'capybara/cuprite'
}

Class Method Summary collapse

Class Method Details

.build_selenium_options_for_firefoxObject



119
120
121
122
123
124
125
126
127
# File 'lib/billy/browsers/capybara.rb', line 119

def self.build_selenium_options_for_firefox
  profile = Selenium::WebDriver::Firefox::Profile.new.tap do |prof|
    prof.proxy = Selenium::WebDriver::Proxy.new(
      http: "#{Billy.proxy.host}:#{Billy.proxy.port}",
      ssl: "#{Billy.proxy.host}:#{Billy.proxy.port}")
  end

  Selenium::WebDriver::Firefox::Options.new(profile: profile)
end

.register_apparition_driverObject



97
98
99
100
101
102
103
# File 'lib/billy/browsers/capybara.rb', line 97

def self.register_apparition_driver
  ::Capybara.register_driver :apparition_billy do |app|
    ::Capybara::Apparition::Driver.new(app, ignore_https_errors: true).tap do |driver|
      driver.set_proxy(Billy.proxy.host, Billy.proxy.port)
    end
  end
end

.register_cuprite_driverObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/billy/browsers/capybara.rb', line 105

def self.register_cuprite_driver
  driver_options = {
    browser_options: {
      'ignore-certificate-errors' => nil
    }
  }.deep_merge(Billy.config.cuprite_options)

  ::Capybara.register_driver :cuprite_billy do |app|
    ::Capybara::Cuprite::Driver.new(app, **driver_options).tap do |driver|
      driver.set_proxy(Billy.proxy.host, Billy.proxy.port)
    end
  end
end

.register_driversObject



15
16
17
18
19
20
21
22
23
# File 'lib/billy/browsers/capybara.rb', line 15

def self.register_drivers
  DRIVERS.each do |name, driver|
    begin
      require driver
      send("register_#{name}_driver")
    rescue LoadError
    end
  end
end

.register_poltergeist_driverObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/billy/browsers/capybara.rb', line 27

def self.register_poltergeist_driver
  ::Capybara.register_driver :poltergeist_billy do |app|
    options = {
      phantomjs_options: [
        '--ignore-ssl-errors=yes',
        "--proxy=#{Billy.proxy.host}:#{Billy.proxy.port}"
      ]
    }
    ::Capybara::Poltergeist::Driver.new(app, options)
  end
end

.register_selenium_driverObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/billy/browsers/capybara.rb', line 49

def self.register_selenium_driver
  ::Capybara.register_driver :selenium_billy do |app|
    options = build_selenium_options_for_firefox

    ::Capybara::Selenium::Driver.new(app, options: options)
  end

  ::Capybara.register_driver :selenium_headless_billy do |app|
    options = build_selenium_options_for_firefox.tap do |opts|
      opts.add_argument '-headless'
    end

    ::Capybara::Selenium::Driver.new(app, options: options)
  end

  ::Capybara.register_driver :selenium_chrome_billy do |app|
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--ignore-certificate-errors')
    options.add_argument("--proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")

    ::Capybara::Selenium::Driver.new(
      app,
      browser: :chrome,
      options: options,
      clear_local_storage: true,
      clear_session_storage: true
    )
  end

  ::Capybara.register_driver :selenium_chrome_headless_billy do |app|
      options = Selenium::WebDriver::Chrome::Options.new
      options.add_argument('--headless=new')
      options.add_argument('--enable-features=NetworkService,NetworkServiceInProcess')
      options.add_argument('--ignore-certificate-errors')
      options.add_argument("--proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")
      options.add_argument('--disable-gpu') if Gem.win_platform?
      options.add_argument('--no-sandbox') if ENV['CI']

    ::Capybara::Selenium::Driver.new(
      app,
      browser: :chrome,
      options: options,
      clear_local_storage: true,
      clear_session_storage: true
    )
  end
end

.register_webkit_driverObject



39
40
41
42
43
44
45
46
47
# File 'lib/billy/browsers/capybara.rb', line 39

def self.register_webkit_driver
  ::Capybara.register_driver :webkit_billy do |app|
    options = {
      ignore_ssl_errors: true,
      proxy: {host: Billy.proxy.host, port: Billy.proxy.port}
    }
    ::Capybara::Webkit::Driver.new(app, ::Capybara::Webkit::Configuration.to_hash.merge(options))
  end
end