Class: Ferrum::Browser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
API
Defined in:
lib/ferrum/browser.rb,
lib/ferrum/browser/api.rb,
lib/ferrum/browser/client.rb,
lib/ferrum/browser/process.rb,
lib/ferrum/browser/api/cookie.rb,
lib/ferrum/browser/api/header.rb,
lib/ferrum/browser/subscriber.rb,
lib/ferrum/browser/web_socket.rb,
lib/ferrum/browser/api/intercept.rb,
lib/ferrum/browser/api/screenshot.rb

Defined Under Namespace

Modules: API Classes: Client, Process, Subscriber, WebSocket

Constant Summary collapse

TIMEOUT =
5
WINDOW_SIZE =
[1024, 768].freeze
BASE_URL_SCHEMA =
%w[http https].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Cookie

#clear_cookies, #cookies, #remove_cookie, #set_cookie

Methods included from API::Header

#add_header, #add_headers, #headers=

Methods included from API::Screenshot

#paper_size=, #screenshot, #zoom_factor=

Methods included from API::Intercept

#url_blacklist=, #url_whitelist=

Constructor Details

#initialize(options = nil) ⇒ Browser

Returns a new instance of Browser.



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
64
65
66
67
# File 'lib/ferrum/browser.rb', line 39

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

  self.url_blacklist = @options[:url_blacklist]
  self.url_whitelist = @options[:url_whitelist]

  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.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



20
21
22
# File 'lib/ferrum/browser.rb', line 20

def headers
  @headers
end

#js_errorsObject (readonly)

Returns the value of attribute js_errors.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def js_errors
  @js_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def process
  @process
end

#slowmoObject (readonly)

Returns the value of attribute slowmo.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def slowmo
  @slowmo
end

#timeoutObject



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

def timeout
  @timeout || TIMEOUT
end

#url_blacklistObject (readonly)

Returns the value of attribute url_blacklist.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def url_blacklist
  @url_blacklist
end

#url_whitelistObject (readonly)

Returns the value of attribute url_whitelist.



35
36
37
# File 'lib/ferrum/browser.rb', line 35

def url_whitelist
  @url_whitelist
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



20
21
22
# File 'lib/ferrum/browser.rb', line 20

def window_size
  @window_size
end

Instance Method Details

#clear_memory_cacheObject



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

def clear_memory_cache
  page.command("Network.clearBrowserCache")
end

#command(*args) ⇒ Object



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

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

#crashObject



133
134
135
# File 'lib/ferrum/browser.rb', line 133

def crash
  command("Browser.crash")
end

#extensionsObject



78
79
80
# File 'lib/ferrum/browser.rb', line 78

def extensions
  @extensions ||= Array(@options[:extensions]).map { |p| File.read(p) }
end

#quitObject



118
119
120
121
122
# File 'lib/ferrum/browser.rb', line 118

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

#resetObject



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

def reset
  @headers = {}
  @zoom_factor = nil
  @window_size = @original_window_size
  targets.reset
end

#resize(**options) ⇒ Object



128
129
130
131
# File 'lib/ferrum/browser.rb', line 128

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

#restartObject



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

def restart
  quit
  start
end

#set_overrides(user_agent: nil, accept_language: nil, platform: nil) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/ferrum/browser.rb', line 93

def set_overrides(user_agent: nil, accept_language: nil, platform: nil)
  options = Hash.new
  options[:userAgent] = user_agent if user_agent
  options[:acceptLanguage] = accept_language if accept_language
  options[:platform] if platform

  page.command("Network.setUserAgentOverride", **options) if !options.empty?
end

#targetsObject



124
125
126
# File 'lib/ferrum/browser.rb', line 124

def targets
  @targets ||= Targets.new(self)
end