class Ferrum::Browser

Constants

BASE_URL_SCHEMA
DEFAULT_TIMEOUT
WINDOW_SIZE

Attributes

base_url[R]
client[R]
contexts[R]
js_errors[R]
logger[R]
options[R]
pending_connection_errors[R]
process[R]
slowmo[R]
timeout[W]
window_size[R]
ws_max_receive_size[R]

Public Class Methods

new(options = nil) click to toggle source
# File lib/ferrum/browser.rb, line 37
def initialize(options = nil)
  options ||= {}

  @client = nil
  @window_size = options.fetch(:window_size, WINDOW_SIZE)
  @original_window_size = @window_size

  @options = Hash(options.merge(window_size: @window_size))
  @logger, @timeout, @ws_max_receive_size =
    @options.values_at(:logger, :timeout, :ws_max_receive_size)
  @js_errors = @options.fetch(:js_errors, false)
  @pending_connection_errors = @options.fetch(:pending_connection_errors, true)
  @slowmo = @options[:slowmo].to_f

  if @options.key?(:base_url)
    self.base_url = @options[:base_url]
  end

  if ENV["FERRUM_DEBUG"] && !@logger
    STDOUT.sync = true
    @logger = STDOUT
    @options[:logger] = @logger
  end

  @options.freeze

  start
end

Public Instance Methods

base_url=(value) click to toggle source
# File lib/ferrum/browser.rb, line 66
def base_url=(value)
  parsed = Addressable::URI.parse(value)
  unless BASE_URL_SCHEMA.include?(parsed.normalized_scheme)
    raise "Set `base_url` should be absolute and include schema: #{BASE_URL_SCHEMA}"
  end

  @base_url = parsed
end
command(*args) click to toggle source
# File lib/ferrum/browser.rb, line 89
def command(*args)
  @client.command(*args)
rescue DeadBrowserError
  restart
  raise
end
crash() click to toggle source
# File lib/ferrum/browser.rb, line 117
def crash
  command("Browser.crash")
end
evaluate_on_new_document(expression) click to toggle source
# File lib/ferrum/browser.rb, line 81
def evaluate_on_new_document(expression)
  extensions << expression
end
extensions() click to toggle source
# File lib/ferrum/browser.rb, line 75
def extensions
  @extensions ||= Array(@options[:extensions]).map do |ext|
    (ext.is_a?(Hash) && ext[:source]) || File.read(ext)
  end
end
quit() click to toggle source
# File lib/ferrum/browser.rb, line 106
def quit
  @client.close
  @process.stop
  @client = @process = @contexts = nil
end
reset() click to toggle source
# File lib/ferrum/browser.rb, line 96
def reset
  @window_size = @original_window_size
  contexts.reset
end
resize(**options) click to toggle source
# File lib/ferrum/browser.rb, line 112
def resize(**options)
  @window_size = [options[:width], options[:height]]
  page.resize(**options)
end
restart() click to toggle source
# File lib/ferrum/browser.rb, line 101
def restart
  quit
  start
end
timeout() click to toggle source
# File lib/ferrum/browser.rb, line 85
def timeout
  @timeout || DEFAULT_TIMEOUT
end

Private Instance Methods

start() click to toggle source
# File lib/ferrum/browser.rb, line 123
def start
  Ferrum.started
  @process = Process.start(@options)
  @client = Client.new(self, @process.ws_url)
  @contexts = Contexts.new(self)
end