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 more...
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
-
#reset ⇒ Object
Closes browser tabs opened by the ‘Browser` instance.
- #restart ⇒ Object
-
#version ⇒ VersionInfo
Gets the version information from the browser.
Constructor Details
permalink #initialize(options = nil) ⇒ Browser
Initializes the browser.
132 133 134 135 136 137 |
# File 'lib/ferrum/browser.rb', line 132 def initialize( = nil) @options = Options.new() @client = @process = @contexts = nil start end |
Instance Attribute Details
permalink #client ⇒ Object (readonly)
Returns the value of attribute client.
34 35 36 |
# File 'lib/ferrum/browser.rb', line 34 def client @client end |
permalink #contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
34 35 36 |
# File 'lib/ferrum/browser.rb', line 34 def contexts @contexts end |
permalink #options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/ferrum/browser.rb', line 34 def @options end |
permalink #process ⇒ Object (readonly)
Returns the value of attribute process.
34 35 36 |
# File 'lib/ferrum/browser.rb', line 34 def process @process end |
Instance Method Details
permalink #close ⇒ Object
Close browser gracefully.
You should clean up resources/connections in ruby world manually, it’s only a CDP command.
237 238 239 |
# File 'lib/ferrum/browser.rb', line 237 def close command("Browser.close") end |
permalink #crash ⇒ Object
Crashes browser.
228 229 230 |
# File 'lib/ferrum/browser.rb', line 228 def crash command("Browser.crash") end |
permalink #create_page(new_context: false, proxy: nil) ⇒ Ferrum::Page
Creates a new page.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ferrum/browser.rb', line 151 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 |
permalink #debug(bind = nil) ⇒ void
This method returns an undefined value.
Opens headless session in the browser devtools frontend.
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/ferrum/browser.rb', line 259 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 |
permalink #evaluate_on_new_document(expression) ⇒ Object
Evaluate JavaScript to modify things before a page load.
188 189 190 |
# File 'lib/ferrum/browser.rb', line 188 def evaluate_on_new_document(expression) extensions << expression end |
permalink #quit ⇒ Object
[View source]
215 216 217 218 219 220 221 222 223 |
# File 'lib/ferrum/browser.rb', line 215 def quit return unless @client contexts.close_connections @client.close @process.stop @client = @process = @contexts = nil end |
permalink #reset ⇒ Object
Closes browser tabs opened by the ‘Browser` instance.
206 207 208 |
# File 'lib/ferrum/browser.rb', line 206 def reset contexts.reset end |
permalink #restart ⇒ Object
[View source]
210 211 212 213 |
# File 'lib/ferrum/browser.rb', line 210 def restart quit start end |
permalink #version ⇒ VersionInfo
Gets the version information from the browser.
248 249 250 |
# File 'lib/ferrum/browser.rb', line 248 def version VersionInfo.new(command("Browser.getVersion")) end |