Class: Ferrum::Browser
- Inherits:
-
Object
- Object
- Ferrum::Browser
- Extended by:
- Forwardable
- Defined in:
- lib/ferrum/browser.rb,
lib/ferrum/browser/xvfb.rb,
lib/ferrum/browser/binary.rb,
lib/ferrum/browser/command.rb,
lib/ferrum/browser/options.rb,
lib/ferrum/browser/process.rb,
lib/ferrum/browser/options/base.rb,
lib/ferrum/browser/version_info.rb,
lib/ferrum/browser/options/chrome.rb,
lib/ferrum/browser/options/firefox.rb
Overview
A Browser can host multiple Contexts (like incognito profiles)
and multiple Pages within each of them, reachable through #contexts
and #create_page.
The main entry point of the library. Instantiating it spawns (or connects
to) a browser process and opens a CDP connection to it, exposing a single
default Page that most of the top-level methods (go_to, at_css,
screenshot, etc.) are delegated to for convenience.
Defined Under Namespace
Modules: Binary Classes: Command, Options, Process, VersionInfo, Xvfb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
Instance Method Summary collapse
-
#close ⇒ Object
Close browser gracefully.
-
#crash ⇒ Object
Crashes browser.
-
#create_page(new_context: false, proxy: nil) ⇒ Ferrum::Page
Creates a new page.
-
#debug(bind = nil) ⇒ void
Opens headless session in the browser devtools frontend.
-
#evaluate_on_new_document(expression) ⇒ Object
Evaluate JavaScript to modify things before a page load.
-
#initialize(options = nil) ⇒ Browser
constructor
Initializes the browser.
-
#quit ⇒ Object
Terminates the browser process and closes the client connection.
-
#reset ⇒ Object
Closes browser tabs opened by the
Browserinstance. -
#restart ⇒ Object
Restarts the browser process, keeping the same options.
-
#version ⇒ VersionInfo
Gets the version information from the browser.
Constructor Details
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
45 46 47 |
# File 'lib/ferrum/browser.rb', line 45 def client @client end |
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
45 46 47 |
# File 'lib/ferrum/browser.rb', line 45 def contexts @contexts end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/ferrum/browser.rb', line 45 def @options end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
45 46 47 |
# File 'lib/ferrum/browser.rb', line 45 def process @process end |
Instance Method Details
#close ⇒ Object
Close browser gracefully.
You should clean up resources/connections in ruby world manually, it's only a CDP command.
263 264 265 |
# File 'lib/ferrum/browser.rb', line 263 def close command("Browser.close") end |
#crash ⇒ Object
Crashes browser.
254 255 256 |
# File 'lib/ferrum/browser.rb', line 254 def crash command("Browser.crash") end |
#create_page(new_context: false, proxy: nil) ⇒ Ferrum::Page
Creates a new page.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ferrum/browser.rb', line 171 def create_page(new_context: false, proxy: nil) page = if new_context || proxy params = {} if proxy .validate_proxy(proxy) params.merge!(proxyServer: "#{proxy[:host]}:#{proxy[:port]}") params.merge!(proxyBypassList: proxy[:bypass]) if proxy[:bypass] end context = contexts.create(**params) context.create_page(proxy: proxy) else default_context.create_page end block_given? ? yield(page) : page ensure if block_given? page&.close context&.dispose if new_context end end |
#debug(bind = nil) ⇒ void
This method returns an undefined value.
Opens headless session in the browser devtools frontend.
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/ferrum/browser.rb', line 285 def debug(bind = nil) ::Process.spawn(process.path, debug_url) bind ||= binding if bind.respond_to?(:pry) Pry.start(bind) else bind.irb end end |
#evaluate_on_new_document(expression) ⇒ Object
Evaluate JavaScript to modify things before a page load.
208 209 210 |
# File 'lib/ferrum/browser.rb', line 208 def evaluate_on_new_document(expression) extensions << expression end |
#quit ⇒ Object
Terminates the browser process and closes the client connection.
241 242 243 244 245 246 247 248 249 |
# File 'lib/ferrum/browser.rb', line 241 def quit return unless @client contexts.close_connections @client.close @process.stop @client = @process = @contexts = nil end |
#reset ⇒ Object
Closes browser tabs opened by the Browser instance.
226 227 228 |
# File 'lib/ferrum/browser.rb', line 226 def reset contexts.reset end |
#restart ⇒ Object
Restarts the browser process, keeping the same options.
233 234 235 236 |
# File 'lib/ferrum/browser.rb', line 233 def restart quit start end |
#version ⇒ VersionInfo
Gets the version information from the browser.
274 275 276 |
# File 'lib/ferrum/browser.rb', line 274 def version VersionInfo.new(command("Browser.getVersion")) end |