Class: Ferrum::Browser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ferrum/browser.rb,
lib/ferrum/browser/xvfb.rb,
lib/ferrum/browser/client.rb,
lib/ferrum/browser/command.rb,
lib/ferrum/browser/process.rb,
lib/ferrum/browser/subscriber.rb,
lib/ferrum/browser/web_socket.rb,
lib/ferrum/browser/options/base.rb,
lib/ferrum/browser/options/chrome.rb,
lib/ferrum/browser/options/firefox.rb

Defined Under Namespace

Modules: Options Classes: Client, Command, Process, Subscriber, WebSocket, Xvfb

Constant Summary collapse

DEFAULT_TIMEOUT =
ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_i
WINDOW_SIZE =
[1024, 768].freeze
BASE_URL_SCHEMA =
%w[http https].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Browser

Returns a new instance of Browser.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ferrum/browser.rb', line 36

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

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def base_url
  @base_url
end

#clientObject (readonly)

Returns the value of attribute client.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def client
  @client
end

#contextsObject (readonly)

Returns the value of attribute contexts.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def contexts
  @contexts
end

#js_errorsObject (readonly)

Returns the value of attribute js_errors.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def js_errors
  @js_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def options
  @options
end

#pending_connection_errorsObject (readonly)

Returns the value of attribute pending_connection_errors.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def pending_connection_errors
  @pending_connection_errors
end

#processObject (readonly)

Returns the value of attribute process.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def process
  @process
end

#slowmoObject (readonly)

Returns the value of attribute slowmo.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def slowmo
  @slowmo
end

#timeoutObject



80
81
82
# File 'lib/ferrum/browser.rb', line 80

def timeout
  @timeout || DEFAULT_TIMEOUT
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def window_size
  @window_size
end

#ws_max_receive_sizeObject (readonly)

Returns the value of attribute ws_max_receive_size.



32
33
34
# File 'lib/ferrum/browser.rb', line 32

def ws_max_receive_size
  @ws_max_receive_size
end

Instance Method Details

#command(*args) ⇒ Object



84
85
86
87
88
89
# File 'lib/ferrum/browser.rb', line 84

def command(*args)
  @client.command(*args)
rescue DeadBrowserError
  restart
  raise
end

#crashObject



112
113
114
# File 'lib/ferrum/browser.rb', line 112

def crash
  command("Browser.crash")
end

#extensionsObject



74
75
76
77
78
# File 'lib/ferrum/browser.rb', line 74

def extensions
  @extensions ||= Array(@options[:extensions]).map do |ext|
    (ext.is_a?(Hash) && ext[:source]) || File.read(ext)
  end
end

#quitObject



101
102
103
104
105
# File 'lib/ferrum/browser.rb', line 101

def quit
  @client.close
  @process.stop
  @client = @process = @contexts = nil
end

#resetObject



91
92
93
94
# File 'lib/ferrum/browser.rb', line 91

def reset
  @window_size = @original_window_size
  contexts.reset
end

#resize(**options) ⇒ Object



107
108
109
110
# File 'lib/ferrum/browser.rb', line 107

def resize(**options)
  @window_size = [options[:width], options[:height]]
  page.resize(**options)
end

#restartObject



96
97
98
99
# File 'lib/ferrum/browser.rb', line 96

def restart
  quit
  start
end