class Capybara::HeadlessChrome::Driver

Public Class Methods

new(app, options={}) click to toggle source
Calls superclass method
# File lib/capybara/headless_chrome/driver.rb, line 14
def initialize app, options={}
  super app, browser: :chrome, desired_capabilities: chrome_capabilities(options)
  configure_downloads
  fix_whitespace
end

Public Instance Methods

downloads() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 20
def downloads
  @downloads ||= Downloads.new
end
javascript_errors() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 24
def javascript_errors
  console_entries level: "SEVERE"
end
javascript_warnings() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 28
def javascript_warnings
  console_entries level: "WARNING"
end

Private Instance Methods

chrome_capabilities(options) click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 42
def chrome_capabilities options
  ::Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      w3c: false,
      args: options_to_arguments(default_chrome_arguments.merge(options)),
      prefs: chrome_preferences,
    }
  )
end
chrome_preferences() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 78
def chrome_preferences
  {
    "download.default_directory": downloads.dir,
    "download.directory_upgrade": "true",
    "download.prompt_for_download": "false",
    "browser.set_download_behavior": "{ 'behavior': 'allow' }",
  }
end
configure_downloads() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 87
def configure_downloads
  bridge = browser.send(:bridge)
  path = '/session/:session_id/chromium/send_command'
  path[':session_id'] = bridge.session_id

  bridge.http.call(:post,
    path,
    cmd: 'Page.setDownloadBehavior',
    params: {
      behavior: 'allow',
      downloadPath: downloads.dir,
    }
  )
end
console_entries(level: nil) click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 34
def console_entries level: nil
  browser.manage.logs.get(:browser)
    .select {|e| level ? e.level == level : true }
    .map(&:message)
    .select(&:present?)
    .to_a
end
default_chrome_arguments() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 67
def default_chrome_arguments
  {
    no_sandbox: true,
    headless: true,
    disable_infobars: true,
    disable_popup_blocking: true,
    disable_gpu: true,
    window_size: [1280,1024],
  }
end
fix_whitespace() click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 102
def fix_whitespace
  Capybara::Selenium::Node.prepend Module.new {
    def visible_text
      super
        .gsub(/[\u200b\u200e\u200f]/, '')
        .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
        .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
        .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
        .tr("\u00a0", ' ')
    end
  }
end
options_to_arguments(options) click to toggle source
# File lib/capybara/headless_chrome/driver.rb, line 52
def options_to_arguments options
  options.map do |key, value|
    arg = key.to_s.gsub(/_/,'-')
    if [true, false].include?(value)
      arg if value
    elsif value.is_a?(String)
      "#{arg}=#{value}"
    elsif value.is_a?(Array)
      "#{arg}=#{value.join(",")}"
    else
      raise NotImplementedError.new(message: [key, value])
    end
  end.compact
end
visible_text() click to toggle source
Calls superclass method
# File lib/capybara/headless_chrome/driver.rb, line 104
def visible_text
  super
    .gsub(/[\u200b\u200e\u200f]/, '')
    .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
    .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
    .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
    .tr("\u00a0", ' ')
end