Class: Ferrum::Browser
- Inherits:
-
Object
- Object
- Ferrum::Browser
- 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
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#js_errors ⇒ Object
readonly
Returns the value of attribute js_errors.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#slowmo ⇒ Object
readonly
Returns the value of attribute slowmo.
- #timeout ⇒ Object
-
#url_blacklist ⇒ Object
readonly
Returns the value of attribute url_blacklist.
-
#url_whitelist ⇒ Object
readonly
Returns the value of attribute url_whitelist.
-
#window_size ⇒ Object
readonly
Returns the value of attribute window_size.
Instance Method Summary collapse
- #clear_memory_cache ⇒ Object
- #command(*args) ⇒ Object
- #crash ⇒ Object
- #extensions ⇒ Object
-
#initialize(options = nil) ⇒ Browser
constructor
A new instance of Browser.
- #quit ⇒ Object
- #reset ⇒ Object
- #resize(**options) ⇒ Object
- #restart ⇒ Object
- #set_overrides(user_agent: nil, accept_language: nil, platform: nil) ⇒ Object
- #targets ⇒ Object
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( = nil) ||= {} @client = nil @window_size = .fetch(:window_size, WINDOW_SIZE) @original_window_size = @window_size @options = Hash(.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_url ⇒ Object
Returns the value of attribute base_url.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
20 21 22 |
# File 'lib/ferrum/browser.rb', line 20 def headers @headers end |
#js_errors ⇒ Object (readonly)
Returns the value of attribute js_errors.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def js_errors @js_errors end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def @options end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def process @process end |
#slowmo ⇒ Object (readonly)
Returns the value of attribute slowmo.
35 36 37 |
# File 'lib/ferrum/browser.rb', line 35 def slowmo @slowmo end |
#timeout ⇒ Object
82 83 84 |
# File 'lib/ferrum/browser.rb', line 82 def timeout @timeout || TIMEOUT end |
#url_blacklist ⇒ Object (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_whitelist ⇒ Object (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_size ⇒ Object (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_cache ⇒ Object
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 |
#crash ⇒ Object
133 134 135 |
# File 'lib/ferrum/browser.rb', line 133 def crash command("Browser.crash") end |
#extensions ⇒ Object
78 79 80 |
# File 'lib/ferrum/browser.rb', line 78 def extensions @extensions ||= Array(@options[:extensions]).map { |p| File.read(p) } end |
#quit ⇒ Object
118 119 120 121 122 |
# File 'lib/ferrum/browser.rb', line 118 def quit @client.close @process.stop @client = @process = @targets = nil end |
#reset ⇒ Object
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(**) @window_size = [[:width], [:height]] page.resize(**) end |
#restart ⇒ Object
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) = Hash.new [:userAgent] = user_agent if user_agent [:acceptLanguage] = accept_language if accept_language [:platform] if platform page.command("Network.setUserAgentOverride", **) if !.empty? end |