Class: Ferrum::Browser

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

Defined Under Namespace

Classes: Chrome, Client, Command, Firefox, Process, Subscriber, WebSocket

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.



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

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 = @options.values_at(:logger, :timeout)
  @js_errors = @options.fetch(:js_errors, false)
  @slowmo = @options[:slowmo].to_i

  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.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def base_url
  @base_url
end

#clientObject (readonly)

Returns the value of attribute client.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def client
  @client
end

#contextsObject (readonly)

Returns the value of attribute contexts.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def contexts
  @contexts
end

#js_errorsObject (readonly)

Returns the value of attribute js_errors.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def js_errors
  @js_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def process
  @process
end

#slowmoObject (readonly)

Returns the value of attribute slowmo.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def slowmo
  @slowmo
end

#timeoutObject



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

def timeout
  @timeout || DEFAULT_TIMEOUT
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



30
31
32
# File 'lib/ferrum/browser.rb', line 30

def window_size
  @window_size
end

Instance Method Details

#command(*args) ⇒ Object



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

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

#crashObject



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

def crash
  command("Browser.crash")
end

#extensionsObject



70
71
72
73
74
# File 'lib/ferrum/browser.rb', line 70

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

#quitObject



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

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

#resetObject



87
88
89
90
# File 'lib/ferrum/browser.rb', line 87

def reset
  @window_size = @original_window_size
  contexts.reset
end

#resize(**options) ⇒ Object



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

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

#restartObject



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

def restart
  quit
  start
end