ferrum 0.20.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -4
  3. data/lib/ferrum/accessibility.rb +40 -6
  4. data/lib/ferrum/browser/command.rb +2 -11
  5. data/lib/ferrum/browser/options/chrome.rb +58 -1
  6. data/lib/ferrum/browser/options.rb +63 -9
  7. data/lib/ferrum/browser/process.rb +12 -4
  8. data/lib/ferrum/browser/xvfb.rb +22 -2
  9. data/lib/ferrum/browser.rb +74 -35
  10. data/lib/ferrum/client/web_socket.rb +13 -6
  11. data/lib/ferrum/client.rb +119 -81
  12. data/lib/ferrum/concurrency/awaitable_map.rb +171 -0
  13. data/lib/ferrum/concurrency/cell.rb +128 -0
  14. data/lib/ferrum/concurrency/dispatcher.rb +146 -0
  15. data/lib/ferrum/console.rb +3 -2
  16. data/lib/ferrum/context.rb +162 -27
  17. data/lib/ferrum/contexts.rb +45 -22
  18. data/lib/ferrum/cookies/collection.rb +93 -0
  19. data/lib/ferrum/cookies/cookie.rb +0 -11
  20. data/lib/ferrum/cookies.rb +79 -86
  21. data/lib/ferrum/deadline.rb +140 -0
  22. data/lib/ferrum/dialog.rb +22 -0
  23. data/lib/ferrum/downloads.rb +83 -10
  24. data/lib/ferrum/errors.rb +96 -6
  25. data/lib/ferrum/frame/dom.rb +147 -38
  26. data/lib/ferrum/frame/runtime.rb +40 -32
  27. data/lib/ferrum/frame/script.rb +7 -38
  28. data/lib/ferrum/frame.rb +57 -20
  29. data/lib/ferrum/headers.rb +32 -27
  30. data/lib/ferrum/interceptable/chain.rb +99 -0
  31. data/lib/ferrum/interceptable.rb +44 -27
  32. data/lib/ferrum/isolated_world.rb +9 -10
  33. data/lib/ferrum/keyboard.rb +27 -6
  34. data/lib/ferrum/mouse.rb +139 -52
  35. data/lib/ferrum/network/auth_request.rb +42 -7
  36. data/lib/ferrum/network/error.rb +34 -4
  37. data/lib/ferrum/network/exchange.rb +35 -7
  38. data/lib/ferrum/network/intercepted_request.rb +42 -9
  39. data/lib/ferrum/network/request.rb +33 -9
  40. data/lib/ferrum/network/request_params.rb +11 -2
  41. data/lib/ferrum/network/response.rb +23 -2
  42. data/lib/ferrum/network.rb +267 -64
  43. data/lib/ferrum/node.rb +430 -62
  44. data/lib/ferrum/page/animation.rb +4 -0
  45. data/lib/ferrum/page/cookies.rb +133 -0
  46. data/lib/ferrum/page/frames.rb +7 -7
  47. data/lib/ferrum/page/navigation.rb +430 -0
  48. data/lib/ferrum/page/screencast.rb +4 -0
  49. data/lib/ferrum/page/screenshot.rb +13 -1
  50. data/lib/ferrum/page/stream.rb +13 -5
  51. data/lib/ferrum/page/tracing.rb +12 -7
  52. data/lib/ferrum/page.rb +168 -190
  53. data/lib/ferrum/remote_object.rb +4 -0
  54. data/lib/ferrum/target.rb +49 -11
  55. data/lib/ferrum/utils/deprecate.rb +73 -2
  56. data/lib/ferrum/utils/poll.rb +57 -0
  57. data/lib/ferrum/version.rb +1 -1
  58. data/lib/ferrum/worker.rb +34 -19
  59. data/lib/ferrum.rb +5 -1
  60. metadata +11 -6
  61. data/lib/ferrum/browser/options/base.rb +0 -112
  62. data/lib/ferrum/client/subscriber.rb +0 -145
  63. data/lib/ferrum/frame/runtime/legacy.rb +0 -336
  64. data/lib/ferrum/utils/event.rb +0 -33
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b5e5118b9e124967f4611bea864c0b5fc961c532a6932848b999538f6a11cef
4
- data.tar.gz: 57d599e04a3abc0baee4a4e9b16381afeaa81cb6e50624344d0a369f2158930a
3
+ metadata.gz: f454530ae66dd36e3ad3344feecd2d8d51eb9d94beca33e012ab54c55a5d529d
4
+ data.tar.gz: fe51c17a1d1ce1210ccd4f26f965d1cedb5e27c3c8622eb81599993fdb5d36f2
5
5
  SHA512:
6
- metadata.gz: 2a407a964d278a1ea0a3f9c0ad1c61d1ad4e6208bbcdc287afbd3d4d969419bf1eea5ad4794c50eb88ef04cf2ccb931f07db1d8ac6358b45565bd2188ca3eadb
7
- data.tar.gz: b0a81bc680dc869d927475ae2d16f3e2ad96e57f28f743903019c0a52d2a78ae641e6b6bf5d1d2835d11472ad044282f830937c6784563b371bf0f450de2c3fa
6
+ metadata.gz: 249f48fd99373a208a08c5a06b8ed3b4b5354e3fbefbd48186fa85c218c914ab9063604464e31cea5a47e1fd4c9c8affa70fe249e25322baca217c8eff3e372e
7
+ data.tar.gz: 05a489b34d85e48a7d61a3d9a87a9f2541e32d564aa015ac1e2337850a5b971efefb9cf8cac38a96b21488bb15071555f1c52eaf915f300f2050e72e53108907
data/README.md CHANGED
@@ -121,7 +121,7 @@ want to keep a value in the browser and pass it back in later, and
121
121
  `Ferrum::Node#evaluate` to run a script with `this` bound to an element:
122
122
 
123
123
  ```ruby
124
- page.execute("window.scrollBy(0, 100)") # => true
124
+ page.execute("window.scrollBy(0, 100)")
125
125
 
126
126
  list = page.evaluate_handle("document.querySelectorAll('li')")
127
127
  page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
@@ -129,9 +129,7 @@ page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
129
129
  page.at_css("input").evaluate("this.value")
130
130
  ```
131
131
 
132
- Scripts written for the old API keep working, and Ferrum warns, with a suggested
133
- rewrite, about any whose result will change. The next release, 0.21, removes the
134
- old behavior, so run your suite with `FERRUM_JS_API=new` to check it's ready. See
132
+ Some scripts written for Ferrum before 0.21 behave differently now, see
135
133
  [upgrading to 0.21](docs/13-javascript.md#upgrading-to-021).
136
134
 
137
135
  Do any mouse movements you like:
@@ -36,6 +36,8 @@ module Ferrum
36
36
  # @param [Boolean] fetch_relatives
37
37
  # @return [Array<AXNode>]
38
38
  #
39
+ # @raise [TimeoutError]
40
+ #
39
41
  def partial_tree(node:, fetch_relatives: false)
40
42
  nodes = node.page.command("Accessibility.getPartialAXTree",
41
43
  nodeId: node.node_id,
@@ -50,6 +52,8 @@ module Ferrum
50
52
  # @param [String, nil] frame_id
51
53
  # @return [Array<AXNode>]
52
54
  #
55
+ # @raise [TimeoutError]
56
+ #
53
57
  def snapshot(depth: nil, frame_id: nil)
54
58
  params = { depth: depth, frameId: frame_id }.compact
55
59
  build(@page.command("Accessibility.getFullAXTree", **params)["nodes"])
@@ -63,6 +67,8 @@ module Ferrum
63
67
  # @param [Ferrum::Node, nil] node Scope the query to this node's subtree.
64
68
  # @return [Array<AXNode>]
65
69
  #
70
+ # @raise [TimeoutError]
71
+ #
66
72
  def query(name: nil, role: nil, node: nil)
67
73
  page = node ? node.page : @page
68
74
  params = { accessibleName: name, role: role }.compact
@@ -76,25 +82,53 @@ module Ferrum
76
82
  # @param [String, nil] frame_id
77
83
  # @return [AXNode, nil]
78
84
  #
85
+ # @raise [TimeoutError]
86
+ #
79
87
  def root(frame_id: nil)
80
88
  params = { depth: 1, frameId: frame_id }.compact
81
89
  build(@page.command("Accessibility.getFullAXTree", **params)["nodes"]).first
82
90
  end
83
91
 
84
- # Enables the Accessibility domain, activating live AX tree change events.
92
+ #
93
+ # Turns the Accessibility domain on or off. Only needed for its live AX
94
+ # events, the query methods work without it.
95
+ #
96
+ # @param [Boolean] enabled
97
+ #
98
+ # @raise [TimeoutError]
99
+ #
100
+ # @example
101
+ # page.accessibility.enabled = true
102
+ # page.on("Accessibility.nodesUpdated") { |params| puts params["nodes"].size }
103
+ #
104
+ def enabled=(enabled)
105
+ @page.command(enabled ? "Accessibility.enable" : "Accessibility.disable")
106
+ end
107
+
108
+ #
109
+ # @deprecated Will be removed in Ferrum 0.22. Use `enabled = true`.
85
110
  #
86
111
  # @return [self]
112
+ #
113
+ # @raise [TimeoutError]
114
+ #
87
115
  def enable
88
- @page.command("Accessibility.enable")
89
- self
116
+ Utils::Deprecate.warn("accessibility.enable will be removed in Ferrum 0.22. Use enabled = true instead.",
117
+ user_code: true)
118
+ tap { self.enabled = true }
90
119
  end
91
120
 
92
- # Disables the Accessibility domain.
121
+ #
122
+ # @deprecated Will be removed in Ferrum 0.22. Use `enabled = false`.
93
123
  #
94
124
  # @return [self]
125
+ #
126
+ # @raise [TimeoutError]
127
+ #
95
128
  def disable
96
- @page.command("Accessibility.disable")
97
- self
129
+ Utils::Deprecate.warn("accessibility.disable will be removed in Ferrum 0.22. Use enabled = false instead.",
130
+ user_code: true)
131
+ tap { self.enabled = false }
98
132
  end
99
133
 
100
134
  private
@@ -5,7 +5,7 @@ module Ferrum
5
5
  #
6
6
  # Builds the OS-level command used to spawn the browser process: resolves
7
7
  # the executable path and merges the browser-specific required/default
8
- # flags (see {Options::Base}) with any user-supplied ones into a single
8
+ # flags (see {Options::Chrome}) with any user-supplied ones into a single
9
9
  # argument list.
10
10
  #
11
11
  class Command
@@ -13,17 +13,8 @@ module Ferrum
13
13
  "it available on the PATH or set environment variable for " \
14
14
  "example BROWSER_PATH=\"/usr/bin/chrome\""
15
15
 
16
- # Currently only these browsers support CDP:
17
- # https://github.com/cyrus-and/chrome-remote-interface#implementations
18
16
  def self.build(options, user_data_dir)
19
- defaults = case options.browser_name
20
- when :chrome, :opera, :edge, nil
21
- Options::Chrome.options
22
- else
23
- raise NotImplementedError, "not supported browser"
24
- end
25
-
26
- new(defaults, options, user_data_dir)
17
+ new(Options::Chrome.options, options, user_data_dir)
27
18
  end
28
19
 
29
20
  attr_reader :defaults, :path, :options
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "singleton"
4
+ require "open3"
5
+
3
6
  module Ferrum
4
7
  class Browser
5
8
  class Options
@@ -9,7 +12,9 @@ module Ferrum
9
12
  # hardened defaults (disabling background networking, extensions,
10
13
  # infobars, etc.) applied unless the user opts out.
11
14
  #
12
- class Chrome < Base
15
+ class Chrome
16
+ include Singleton
17
+
13
18
  DEFAULT_OPTIONS = {
14
19
  "allow-pre-commit-input" => nil,
15
20
  "disable-background-networking" => nil,
@@ -85,6 +90,58 @@ module Ferrum
85
90
  linux: LINUX_BIN_PATH
86
91
  }.freeze
87
92
 
93
+ #
94
+ # Singleton instance holding the browser's default options.
95
+ #
96
+ # @return [Chrome]
97
+ #
98
+ def self.options
99
+ instance
100
+ end
101
+
102
+ # The installed browser's version string, obtained by running its
103
+ # binary with `--version`.
104
+ #
105
+ # @return [String, nil]
106
+ # The version output, or `nil` if the binary can't be found/run.
107
+ def self.version
108
+ out, = Open3.capture2(instance.detect_path, "--version")
109
+ out.strip
110
+ rescue Errno::ENOENT
111
+ nil
112
+ end
113
+
114
+ #
115
+ # Default CLI flags for the browser.
116
+ #
117
+ # @return [Hash{String => Object}]
118
+ #
119
+ def to_h
120
+ DEFAULT_OPTIONS
121
+ end
122
+
123
+ #
124
+ # Default CLI flags excluding the given keys.
125
+ #
126
+ # @param [Array<String>] keys
127
+ # Flag names to exclude.
128
+ #
129
+ # @return [Hash{String => Object}]
130
+ #
131
+ def except(*keys)
132
+ to_h.except(*keys)
133
+ end
134
+
135
+ #
136
+ # Locates the browser executable on the system.
137
+ #
138
+ # @return [String, nil]
139
+ # Absolute path to the browser binary, or `nil` if not found.
140
+ #
141
+ def detect_path
142
+ Binary.find(PLATFORM_PATH[Utils::Platform.platform_name])
143
+ end
144
+
88
145
  #
89
146
  # Merges CLI flags required for Chrome to work with CDP.
90
147
  #
@@ -15,18 +15,25 @@ module Ferrum
15
15
  BROWSER_HOST = "127.0.0.1"
16
16
  WINDOW_SIZE = [1024, 768].freeze
17
17
  BASE_URL_SCHEMA = %w[http https].freeze
18
- DEFAULT_TIMEOUT = ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_i
19
- DEFAULT_PROTOCOL_TIMEOUT = ENV.fetch("FERRUM_PROTOCOL_TIMEOUT", DEFAULT_TIMEOUT).to_i
18
+ DEFAULT_TIMEOUT = ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_f
20
19
  PROCESS_TIMEOUT = ENV.fetch("FERRUM_PROCESS_TIMEOUT", 10).to_i
21
20
 
22
- warn "Ferrum: FERRUM_DEBUG is deprecated, use FERRUM_LOG=* instead" if ENV.key?("FERRUM_DEBUG")
21
+ PROTOCOL_TIMEOUT_REMOVED = "is ignored and will be removed in Ferrum 0.22. Internal commands have a fixed " \
22
+ "timeout, :timeout bounds everything else."
23
+
24
+ if ENV.key?("FERRUM_DEBUG")
25
+ Utils::Deprecate.warn("FERRUM_DEBUG will be removed in Ferrum 0.22. Use FERRUM_LOG=* instead.")
26
+ end
27
+ if ENV.key?("FERRUM_PROTOCOL_TIMEOUT")
28
+ Utils::Deprecate.warn("FERRUM_PROTOCOL_TIMEOUT #{PROTOCOL_TIMEOUT_REMOVED}")
29
+ end
23
30
 
24
31
  attr_reader :window_size, :logger, :log, :ws_max_receive_size,
25
32
  :js_errors, :base_url, :slowmo, :pending_connection_errors,
26
- :url, :ws_url, :env, :process_timeout, :browser_name, :browser_path,
33
+ :url, :ws_url, :env, :process_timeout, :browser_path,
27
34
  :save_path, :proxy, :port, :host, :headless, :incognito, :dockerize, :browser_options,
28
35
  :ignore_default_browser_options, :xvfb, :flatten, :extensions
29
- attr_accessor :timeout, :protocol_timeout, :default_user_agent
36
+ attr_accessor :timeout, :default_user_agent
30
37
 
31
38
  def initialize(options = nil)
32
39
  @options = Hash(options&.dup)
@@ -34,21 +41,27 @@ module Ferrum
34
41
  @port = @options.fetch(:port, BROWSER_PORT)
35
42
  @host = @options.fetch(:host, BROWSER_HOST)
36
43
  @timeout = @options.fetch(:timeout, DEFAULT_TIMEOUT)
37
- @protocol_timeout = @options.fetch(:protocol_timeout, DEFAULT_PROTOCOL_TIMEOUT)
44
+ warn_protocol_timeout if @options.key?(:protocol_timeout)
38
45
  @window_size = @options.fetch(:window_size, WINDOW_SIZE)
39
- @js_errors = @options.fetch(:js_errors, false)
46
+ @js_errors = fetch_deprecated(
47
+ :js_errors,
48
+ "Uncaught exceptions are in the console, use page.console.select { |m| m.source == :exception } " \
49
+ "or page.on(:console)."
50
+ )
40
51
  @headless = @options.fetch(:headless, true)
41
52
  @incognito = @options.fetch(:incognito, true)
42
53
  @dockerize = @options.fetch(:dockerize, false)
43
54
  @flatten = @options.fetch(:flatten, true)
44
- @pending_connection_errors = @options.fetch(:pending_connection_errors, false)
55
+ warn_flatten unless @flatten
56
+ @pending_connection_errors = fetch_deprecated(
57
+ :pending_connection_errors, "Use page.go_to!(url) to raise when the page doesn't load in time."
58
+ )
45
59
  @process_timeout = @options.fetch(:process_timeout, PROCESS_TIMEOUT)
46
60
  @slowmo = @options[:slowmo].to_f
47
61
 
48
62
  @env = @options[:env]
49
63
  @xvfb = @options[:xvfb]
50
64
  @save_path = @options[:save_path]
51
- @browser_name = @options[:browser_name]
52
65
  @browser_path = @options[:browser_path]
53
66
  @ws_max_receive_size = @options[:ws_max_receive_size]
54
67
  @ignore_default_browser_options = @options[:ignore_default_browser_options]
@@ -64,6 +77,29 @@ module Ferrum
64
77
  @browser_options = @options.fetch(:browser_options, {}).freeze
65
78
  end
66
79
 
80
+ #
81
+ # Deprecated, will be removed in Ferrum 0.22. Internal commands have a fixed timeout.
82
+ #
83
+ # @return [Integer]
84
+ # {Client::PROTOCOL_TIMEOUT}.
85
+ #
86
+ def protocol_timeout
87
+ warn_protocol_timeout
88
+ Client::PROTOCOL_TIMEOUT
89
+ end
90
+
91
+ #
92
+ # Deprecated, will be removed in Ferrum 0.22. The value is ignored.
93
+ #
94
+ # @param [Numeric] value
95
+ #
96
+ # @return [Numeric]
97
+ #
98
+ def protocol_timeout=(value)
99
+ warn_protocol_timeout
100
+ value
101
+ end
102
+
67
103
  #
68
104
  # Sets the base URL relative navigations are resolved against.
69
105
  #
@@ -132,6 +168,24 @@ module Ferrum
132
168
 
133
169
  private
134
170
 
171
+ def warn_protocol_timeout
172
+ Utils::Deprecate.warn(":protocol_timeout #{PROTOCOL_TIMEOUT_REMOVED}", user_code: true)
173
+ end
174
+
175
+ def warn_flatten
176
+ Utils::Deprecate.warn(
177
+ "flatten: false will be removed in Ferrum 0.22. Every page will share the browser's websocket connection.",
178
+ user_code: true
179
+ )
180
+ end
181
+
182
+ def fetch_deprecated(name, instead)
183
+ return false unless @options.key?(name)
184
+
185
+ Utils::Deprecate.warn(":#{name} will be removed in Ferrum 0.22. #{instead}", user_code: true)
186
+ @options[name]
187
+ end
188
+
135
189
  def read_extension(extension)
136
190
  (extension.is_a?(Hash) && extension[:source]) || File.read(extension)
137
191
  end
@@ -5,7 +5,6 @@ require "json"
5
5
  require "addressable"
6
6
  require "tmpdir"
7
7
  require "forwardable"
8
- require "ferrum/browser/options/base"
9
8
  require "ferrum/browser/options/chrome"
10
9
  require "ferrum/browser/process/killer"
11
10
  require "ferrum/browser/process/output"
@@ -59,7 +58,7 @@ module Ferrum
59
58
  @output = Output.new(@log)
60
59
 
61
60
  if options.ws_url || options.url
62
- self.ws_url = options.ws_url || fetch_ws_url(options.url)
61
+ self.ws_url = options.ws_url || fetch_ws_url(options.url, options.process_timeout)
63
62
  return
64
63
  end
65
64
 
@@ -208,8 +207,8 @@ module Ferrum
208
207
  uri.to_s
209
208
  end
210
209
 
211
- def fetch_ws_url(url)
212
- response = ::Net::HTTP.get_response(URI(json_version_url(url).to_s))
210
+ def fetch_ws_url(url, timeout)
211
+ response = request_json_version(URI(json_version_url(url).to_s), timeout)
213
212
  status = "#{response.code} #{response.message}" unless response.is_a?(::Net::HTTPSuccess)
214
213
  ws_url = begin
215
214
  JSON.parse(response.body.to_s)["webSocketDebuggerUrl"]
@@ -220,6 +219,15 @@ module Ferrum
220
219
  reachable_ws_url(ws_url, url) || raise(NoWebSocketUrlError.new(url, status))
221
220
  end
222
221
 
222
+ def request_json_version(uri, timeout)
223
+ ::Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https",
224
+ open_timeout: timeout, read_timeout: timeout) do |http|
225
+ http.request_get(uri)
226
+ end
227
+ rescue ::Net::OpenTimeout, ::Net::ReadTimeout, Errno::ETIMEDOUT
228
+ raise ConnectTimeoutError.new("#{uri.hostname}:#{uri.port}", timeout)
229
+ end
230
+
223
231
  def json_version_url(url)
224
232
  url = Addressable::URI.parse(url).dup
225
233
  url.scheme = HTTP_SCHEMES.fetch(url.scheme, url.scheme)
@@ -32,16 +32,27 @@ module Ferrum
32
32
 
33
33
  @screen_size = "#{options.window_size.join('x')}x24"
34
34
  @display_id = (Time.now.to_f * 1000).to_i % 100_000_000
35
+ @timeout = options.process_timeout
35
36
  end
36
37
 
37
38
  #
38
- # Spawns the Xvfb process on the configured display.
39
+ # Spawns the Xvfb process on the configured display and waits until it
40
+ # accepts connections, so a browser started right after finds the display.
39
41
  #
40
42
  # @return [void]
41
43
  #
44
+ # @raise [Error]
45
+ # When Xvfb exits or isn't ready within `:process_timeout`.
46
+ #
42
47
  def start
43
- @pid = ::Process.spawn("#{@path} :#{display_id} -screen 0 #{screen_size}")
48
+ reader, writer = IO.pipe
49
+ @pid = ::Process.spawn(@path, ":#{display_id}", "-screen", "0", screen_size, "-displayfd", "1", out: writer)
44
50
  ::Process.detach(@pid)
51
+ writer.close
52
+ wait_for_display(reader)
53
+ ensure
54
+ reader&.close
55
+ writer&.close
45
56
  end
46
57
 
47
58
  #
@@ -52,6 +63,15 @@ module Ferrum
52
63
  def to_env
53
64
  { "DISPLAY" => ":#{display_id}" }
54
65
  end
66
+
67
+ private
68
+
69
+ def wait_for_display(io)
70
+ return if io.wait_readable(@timeout) && io.gets
71
+
72
+ Process::Killer.kill(@pid)
73
+ raise Error, "Xvfb exited or wasn't ready on display :#{display_id} within #{@timeout} seconds"
74
+ end
55
75
  end
56
76
  end
57
77
  end
@@ -27,29 +27,30 @@ module Ferrum
27
27
  extend Forwardable
28
28
 
29
29
  delegate %i[default_context] => :contexts
30
- delegate %i[targets create_target page pages windows workers service_workers attach_target] => :default_context
31
- delegate %i[go_to goto go back forward refresh reload stop wait_for_reload
32
- wait_for_navigation wait_for_navigation!
33
- at_css at_xpath css xpath wait_for_selector current_url current_title url title
34
- body doctype content=
35
- headers cookies network console accessibility downloads
30
+ delegate %i[targets create_target page pages windows await_page await_page! workers service_workers
31
+ attach_target cookies] => :default_context
32
+ delegate %i[go_to go_to! goto go back back! forward forward! refresh refresh! reload reload! stop
33
+ await_navigation await_navigation! await_download await_download! wait_until wait_until!
34
+ wait_for_reload wait_for_navigation wait_for_navigation!
35
+ at_css at_xpath css xpath wait_css wait_css! wait_xpath wait_xpath! wait_for_selector
36
+ current_url current_title url title
37
+ html body doctype content=
38
+ headers network console accessibility downloads tracing
36
39
  mouse keyboard
37
- screenshot pdf mhtml viewport_size device_pixel_ratio
40
+ screenshot pdf mhtml viewport_size device_pixel_ratio document_size
38
41
  start_screencast stop_screencast
39
42
  frames frame_by main_frame
40
- evaluate evaluate_handle execute
41
- evaluate_on evaluate_async evaluate_func isolated_world
42
- add_script_tag add_style_tag bypass_csp
43
+ evaluate evaluate_handle execute isolated_world
44
+ add_script_tag add_style_tag bypass_csp bypass_csp=
43
45
  evaluate_on_new_document remove_script_on_new_document
44
- on position position=
46
+ on off position position= window_bounds window_bounds= activate
45
47
  playback_rate playback_rate=
46
- disable_javascript set_viewport resize] => :page
48
+ disable_javascript javascript= javascript? set_viewport resize] => :page
47
49
 
48
50
  attr_reader :client, :process, :contexts, :options
49
51
 
50
- delegate %i[timeout timeout= protocol_timeout protocol_timeout=
51
- base_url base_url= default_user_agent default_user_agent= extensions add_extension] => :options
52
- delegate %i[command] => :client
52
+ delegate %i[timeout timeout= protocol_timeout protocol_timeout= base_url base_url= default_user_agent
53
+ default_user_agent= extensions add_extension] => :options
53
54
 
54
55
  #
55
56
  # Initializes the browser.
@@ -71,6 +72,8 @@ module Ferrum
71
72
  #
72
73
  # @option options [Boolean] :flatten (true)
73
74
  # Use one websocket connection to the browser and all the pages in flatten mode.
75
+ # Passing `false` is deprecated, the option will be removed in Ferrum 0.22
76
+ # and every page will share the browser's connection.
74
77
  #
75
78
  # @option options [(Integer, Integer)] :window_size ([1024, 768])
76
79
  # The dimensions of the browser window in which to test, expressed as a
@@ -89,25 +92,21 @@ module Ferrum
89
92
  # changes.
90
93
  #
91
94
  # @option options [Numeric] :timeout (5)
92
- # The number of seconds we'll wait for a response when communicating
93
- # with the browser: navigations, JS evaluation, DOM queries, dispatching input, etc.
94
- #
95
- # @option options [Numeric] :protocol_timeout (5)
96
- # The number of seconds we'll wait for an individual internal CDP
97
- # bookkeeping call to respond, e.g. `Target.createTarget`,
98
- # `Target.attachToTarget`. These normally resolve in milliseconds.
95
+ # How long a call waits for the browser, in seconds: navigations, JS evaluation, DOM queries, dispatching
96
+ # input, etc. `go_to`, `refresh` and `await_navigation` wait for the page to load within it. A call that
97
+ # only sends commands waits for the browser's answer. A call that runs out raises {TimeoutError}.
98
+ # `FERRUM_DEFAULT_TIMEOUT` sets the default. Pass `timeout:` to `go_to`, `refresh` or `await_navigation` to
99
+ # change it for one call, or wrap calls in {Deadline.within} to bound them together.
99
100
  #
100
101
  # @option options [Boolean] :js_errors
101
- # When true, JavaScript errors get re-raised in Ruby.
102
- #
103
- # @option options [Boolean] :pending_connection_errors (true)
104
- # When main frame is still waiting for slow responses while timeout is
105
- # reached {PendingConnectionsError} is raised. It's better to figure out
106
- # why you have slow responses and fix or block them rather than turn this
107
- # setting off.
102
+ # Deprecated, will be removed in Ferrum 0.22. When true, JavaScript
103
+ # errors get re-raised in Ruby. Uncaught exceptions are in the console,
104
+ # use `page.console.select { |m| m.source == :exception }` or
105
+ # `page.on(:console)` instead.
108
106
  #
109
- # @option options [:chrome, :opera, :edge] :browser_name (:chrome)
110
- # Sets the browser's name.
107
+ # @option options [Boolean] :pending_connection_errors (false)
108
+ # Deprecated, will be removed in Ferrum 0.22. Makes `go_to` raise
109
+ # {PendingConnectionsError} on timeout, use `go_to!` instead.
111
110
  #
112
111
  # @option options [String] :browser_path
113
112
  # Path to Chrome binary, you can also set ENV variable as
@@ -134,7 +133,8 @@ module Ferrum
134
133
  # will not be spawned.
135
134
  #
136
135
  # @option options [Integer] :process_timeout
137
- # How long to wait for the Chrome process to respond on startup.
136
+ # How long to wait for the Chrome process to respond on startup, or to
137
+ # connect to the browser at `:url` or `:ws_url`.
138
138
  #
139
139
  # @option options [Integer] :ws_max_receive_size
140
140
  # How big messages to accept from Chrome over the web socket, in bytes.
@@ -170,6 +170,8 @@ module Ferrum
170
170
  # @return [Ferrum::Page]
171
171
  # Created page.
172
172
  #
173
+ # @raise [TimeoutError]
174
+ #
173
175
  def create_page(new_context: false, proxy: nil)
174
176
  page = if new_context || proxy
175
177
  params = {}
@@ -197,6 +199,10 @@ module Ferrum
197
199
  #
198
200
  # Closes browser tabs opened by the `Browser` instance.
199
201
  #
202
+ # @return [nil]
203
+ #
204
+ # @raise [TimeoutError]
205
+ #
200
206
  # @example
201
207
  # # connect to a long-running Chrome process
202
208
  # browser = Ferrum::Browser.new(url: 'http://localhost:9222')
@@ -210,6 +216,7 @@ module Ferrum
210
216
  #
211
217
  def reset
212
218
  contexts.reset
219
+ nil
213
220
  end
214
221
 
215
222
  #
@@ -254,8 +261,34 @@ module Ferrum
254
261
  #
255
262
  # You should clean up resources/connections in ruby world manually, it's only a CDP command.
256
263
  #
264
+ # @return [nil]
265
+ #
257
266
  def close
258
267
  command("Browser.close")
268
+ nil
269
+ end
270
+
271
+ #
272
+ # Sends a CDP command to the browser itself, not to the default page like
273
+ # most other `Browser` methods, {#on} included. Use `page.command` for page
274
+ # domains such as `Page`, `Runtime` or `DOM`.
275
+ #
276
+ # @param [String] method
277
+ # The CDP method name, e.g. `"Browser.getVersion"`.
278
+ #
279
+ # @param [Numeric, Deadline, nil] timeout
280
+ # How long to wait for the response, the browser's `timeout` by default.
281
+ #
282
+ # @param [Hash] params
283
+ # The command's parameters.
284
+ #
285
+ # @return [Hash]
286
+ # The command's result.
287
+ #
288
+ # @raise [TimeoutError]
289
+ #
290
+ def command(method, timeout: nil, **params)
291
+ client.command(method, timeout: timeout || self.timeout, **params)
259
292
  end
260
293
 
261
294
  #
@@ -263,6 +296,8 @@ module Ferrum
263
296
  #
264
297
  # @return [VersionInfo]
265
298
  #
299
+ # @raise [TimeoutError]
300
+ #
266
301
  # @since 0.13
267
302
  #
268
303
  def version
@@ -292,6 +327,7 @@ module Ferrum
292
327
  def start
293
328
  Utils::ElapsedTime.start
294
329
  @process = Process.new(options)
330
+ started = false
295
331
 
296
332
  begin
297
333
  @process.start
@@ -299,9 +335,12 @@ module Ferrum
299
335
  @client = Client.new(@process.ws_url, options)
300
336
  @options.default_user_agent = version.user_agent
301
337
  @contexts = Contexts.new(@client)
302
- rescue StandardError
303
- @process.stop
304
- raise
338
+ started = true
339
+ ensure
340
+ unless started
341
+ @client&.close
342
+ @process.stop
343
+ end
305
344
  end
306
345
  end
307
346