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
data/lib/ferrum/mouse.rb CHANGED
@@ -8,7 +8,6 @@ module Ferrum
8
8
  # can be composed while reporting consistent state to Chrome.
9
9
  #
10
10
  class Mouse
11
- CLICK_WAIT = ENV.fetch("FERRUM_CLICK_WAIT", 0.1).to_f
12
11
  BUTTON_MASKS = {
13
12
  "none" => 0,
14
13
  "left" => 1,
@@ -25,39 +24,60 @@ module Ferrum
25
24
  end
26
25
 
27
26
  #
28
- # Scroll page by the given amount x, y.
27
+ # Scrolls the page by the given amount. Scrolls instantly, even when the
28
+ # page sets `scroll-behavior: smooth`.
29
29
  #
30
- # @param [Integer] x
31
- # The horizontal pixel value that you want to scroll by.
30
+ # @param [Array(Numeric, Numeric)] coordinates
31
+ # Deprecated, pass `x:` and `y:`.
32
32
  #
33
- # @param [Integer] y
33
+ # @param [Numeric] x
34
+ # The horizontal pixel value that you want to scroll by.
35
+ #
36
+ # @param [Numeric] y
34
37
  # The vertical pixel value that you want to scroll by.
35
38
  #
39
+ # @return [self]
40
+ #
41
+ # @raise [TimeoutError]
42
+ #
36
43
  # @example
37
44
  # browser.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
38
- # browser.mouse.scroll_by(0, 400)
45
+ # browser.mouse.scroll_by(y: 400)
39
46
  #
40
- def scroll_by(x, y)
41
- tap { @page.execute("window.scrollBy(#{x}, #{y})") }
47
+ def scroll_by(*coordinates, x: 0, y: 0)
48
+ x, y = positional_coordinates(:scroll_by, coordinates, x, y)
49
+ tap { @page.execute("window.scrollBy({ left: x, top: y, behavior: 'instant' })", x:, y:) }
42
50
  end
43
51
 
44
52
  #
45
- # Scroll page to a given x, y coordinates.
53
+ # Scrolls the page to the given coordinates. Scrolls instantly, even when
54
+ # the page sets `scroll-behavior: smooth`.
46
55
  #
47
- # @param [Integer] top
48
- # The pixel along the horizontal axis of the document that you want
49
- # displayed in the upper left.
56
+ # @param [Array(Numeric, Numeric)] coordinates
57
+ # Deprecated, pass `x:` and `y:`.
50
58
  #
51
- # @param [Integer] left
59
+ # @param [Numeric, nil] x
60
+ # The pixel along the horizontal axis of the document that you want
61
+ # displayed in the upper left. If omitted, keeps the current one.
62
+ #
63
+ # @param [Numeric, nil] y
52
64
  # The pixel along the vertical axis of the document that you want
53
- # displayed in the upper left.
65
+ # displayed in the upper left. If omitted, keeps the current one.
66
+ #
67
+ # @return [self]
68
+ #
69
+ # @raise [TimeoutError]
54
70
  #
55
71
  # @example
56
72
  # browser.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
57
- # browser.mouse.scroll_to(0, 400)
73
+ # browser.mouse.scroll_to(x: 0, y: 400)
74
+ # browser.mouse.scroll_to(y: 0)
58
75
  #
59
- def scroll_to(top, left)
60
- tap { @page.execute("window.scrollTo(#{top}, #{left})") }
76
+ def scroll_to(*coordinates, x: nil, y: nil)
77
+ x, y = positional_coordinates(:scroll_to, coordinates, x, y)
78
+ tap do
79
+ @page.execute("window.scrollTo({ left: x ?? undefined, top: y ?? undefined, behavior: 'instant' })", x:, y:)
80
+ end
61
81
  end
62
82
 
63
83
  #
@@ -70,29 +90,44 @@ module Ferrum
70
90
  # @param [Float] delay
71
91
  # Delay between mouse down and mouse up events.
72
92
  #
73
- # @param [Float] wait
93
+ # @param [Numeric, false] navigation_wait
94
+ # Seconds to watch for a navigation started by the click, then wait for it
95
+ # to load. `false` returns as soon as the click is dispatched.
96
+ #
97
+ # @param [Numeric, nil] wait
98
+ # Deprecated, renamed to `navigation_wait`.
99
+ #
100
+ # @param [Numeric, Deadline, nil] timeout
101
+ # How long the click and the page load it starts may take together, the page timeout by default.
74
102
  #
75
103
  # @param [Hash{Symbol => Object}] options
76
104
  # Additional keyword arguments.
77
105
  #
78
- # @option options [:left, :right] :button (:left)
106
+ # @option options [:left, :right, :middle, :back, :forward] :button (:left)
79
107
  # The mouse button to click.
80
108
  #
81
109
  # @option options [Integer] :count (1)
82
110
  #
83
- # @option options [Integer] :modifiers
84
- # Bitfield for key modifiers. See `keyboard.modifiers`.
111
+ # @option options [Array<Symbol>, Integer] :modifiers
112
+ # Modifier keys to hold: `:alt`, `:ctrl` (or `:control`), `:meta` (or
113
+ # `:command`) and `:shift`, or CDP's `modifiers` bitfield.
85
114
  # If omitted, uses the keys held by `keyboard.hold`, or none outside of it.
86
115
  #
87
116
  # @return [self]
88
117
  #
89
- def click(x:, y:, delay: 0, wait: CLICK_WAIT, **options)
90
- move(x: x, y: y, modifiers: options[:modifiers])
91
- down(**options)
92
- sleep(delay)
93
- # Potential wait because if some network event is triggered then we have
94
- # to wait until it's over and frame is loaded or failed to load.
95
- up(wait: wait, **options)
118
+ # @raise [TimeoutError]
119
+ #
120
+ def click(x:, y:, delay: 0, navigation_wait: Page::NAVIGATION_WAIT, wait: nil, timeout: nil, **options) # rubocop:disable Metrics/ParameterLists
121
+ navigation_wait = Utils::Deprecate.renamed_keyword(:wait, :navigation_wait, wait, navigation_wait)
122
+ deadline = Deadline.wrap(timeout || @page.timeout)
123
+ Deadline.within(deadline) do
124
+ move(x: x, y: y, modifiers: options[:modifiers])
125
+ down(**options)
126
+ sleep(delay)
127
+ # Potential wait because if some network event is triggered then we have
128
+ # to wait until it's over and frame is loaded or failed to load.
129
+ up(navigation_wait: navigation_wait, timeout: deadline, **options)
130
+ end
96
131
  self
97
132
  end
98
133
 
@@ -102,17 +137,20 @@ module Ferrum
102
137
  # @param [Hash{Symbol => Object}] options
103
138
  # Additional keyword arguments.
104
139
  #
105
- # @option options [:left, :right] :button (:left)
140
+ # @option options [:left, :right, :middle, :back, :forward] :button (:left)
106
141
  # The mouse button to click.
107
142
  #
108
143
  # @option options [Integer] :count (1)
109
144
  #
110
- # @option options [Integer] :modifiers
111
- # Bitfield for key modifiers. See `keyboard.modifiers`.
145
+ # @option options [Array<Symbol>, Integer] :modifiers
146
+ # Modifier keys to hold: `:alt`, `:ctrl` (or `:control`), `:meta` (or
147
+ # `:command`) and `:shift`, or CDP's `modifiers` bitfield.
112
148
  # If omitted, uses the keys held by `keyboard.hold`, or none outside of it.
113
149
  #
114
150
  # @return [self]
115
151
  #
152
+ # @raise [TimeoutError]
153
+ #
116
154
  def down(**options)
117
155
  tap { mouse_event(type: "mousePressed", **options) }
118
156
  end
@@ -123,17 +161,29 @@ module Ferrum
123
161
  # @param [Hash{Symbol => Object}] options
124
162
  # Additional keyword arguments.
125
163
  #
126
- # @option options [:left, :right] :button (:left)
164
+ # @option options [:left, :right, :middle, :back, :forward] :button (:left)
127
165
  # The mouse button to click.
128
166
  #
129
167
  # @option options [Integer] :count (1)
130
168
  #
131
- # @option options [Integer] :modifiers
132
- # Bitfield for key modifiers. See `keyboard.modifiers`.
169
+ # @option options [Array<Symbol>, Integer] :modifiers
170
+ # Modifier keys to hold: `:alt`, `:ctrl` (or `:control`), `:meta` (or
171
+ # `:command`) and `:shift`, or CDP's `modifiers` bitfield.
133
172
  # If omitted, uses the keys held by `keyboard.hold`, or none outside of it.
134
173
  #
174
+ # @option options [Numeric, false] :navigation_wait (false)
175
+ # Seconds to watch for a navigation started by releasing the button, then wait for it to load.
176
+ #
177
+ # @option options [Numeric] :wait
178
+ # Deprecated, renamed to `:navigation_wait`.
179
+ #
180
+ # @option options [Numeric, Deadline] :timeout
181
+ # How long the event and the page load it starts may take together, the page timeout by default.
182
+ #
135
183
  # @return [self]
136
184
  #
185
+ # @raise [TimeoutError]
186
+ #
137
187
  def up(**options)
138
188
  tap { mouse_event(type: "mouseReleased", **options) }
139
189
  end
@@ -148,31 +198,40 @@ module Ferrum
148
198
  # @param [Integer] steps
149
199
  # Sends intermediate mousemove events.
150
200
  #
151
- # @param [Integer, nil] modifiers
152
- # Bitfield for key modifiers. See `keyboard.modifiers`.
201
+ # @param [Array<Symbol>, Integer, nil] modifiers
202
+ # Modifier keys to hold: `:alt`, `:ctrl` (or `:control`), `:meta` (or
203
+ # `:command`) and `:shift`, or CDP's `modifiers` bitfield.
153
204
  # If omitted, uses the keys held by `keyboard.hold`, or none outside of it.
154
205
  #
206
+ # @param [Numeric, Deadline, nil] timeout
207
+ # How long all the steps may take together, the page timeout by default.
208
+ #
155
209
  # @return [self]
156
210
  #
157
- def move(x:, y:, steps: 1, modifiers: nil)
211
+ # @raise [TimeoutError]
212
+ #
213
+ def move(x:, y:, steps: 1, modifiers: nil, timeout: nil)
158
214
  from_x = @x
159
215
  from_y = @y
160
216
  @x = x
161
217
  @y = y
162
218
  button = pressed_button
219
+ modifiers = modifiers_mask(modifiers)
220
+
221
+ Deadline.within(timeout || @page.timeout) do
222
+ steps.times do |i|
223
+ new_x = from_x + ((@x - from_x) * ((i + 1) / steps.to_f))
224
+ new_y = from_y + ((@y - from_y) * ((i + 1) / steps.to_f))
163
225
 
164
- steps.times do |i|
165
- new_x = from_x + ((@x - from_x) * ((i + 1) / steps.to_f))
166
- new_y = from_y + ((@y - from_y) * ((i + 1) / steps.to_f))
167
-
168
- @page.command("Input.dispatchMouseEvent",
169
- slowmoable: true,
170
- type: "mouseMoved",
171
- x: new_x,
172
- y: new_y,
173
- button: button,
174
- buttons: @buttons,
175
- modifiers: modifiers || @page.keyboard.held_modifiers)
226
+ @page.command("Input.dispatchMouseEvent",
227
+ slowmoable: true,
228
+ type: "mouseMoved",
229
+ x: new_x,
230
+ y: new_y,
231
+ button: button,
232
+ buttons: @buttons,
233
+ modifiers: modifiers)
234
+ end
176
235
  end
177
236
 
178
237
  self
@@ -180,12 +239,40 @@ module Ferrum
180
239
 
181
240
  private
182
241
 
183
- def mouse_event(type:, button: :left, count: 1, modifiers: nil, wait: 0)
242
+ def mouse_event(type:, button: :left, count: 1, modifiers: nil, navigation_wait: false, wait: nil, # rubocop:disable Metrics/ParameterLists
243
+ timeout: nil)
244
+ navigation_wait = Utils::Deprecate.renamed_keyword(:wait, :navigation_wait, wait, navigation_wait)
184
245
  button = validate_button(button)
185
246
  register_event_button(type, button)
186
247
  options = { x: @x, y: @y, type: type, button: button, buttons: @buttons, clickCount: count,
187
- modifiers: modifiers || @page.keyboard.held_modifiers }
188
- @page.command("Input.dispatchMouseEvent", wait: wait, slowmoable: true, **options)
248
+ modifiers: modifiers_mask(modifiers) }
249
+ if !navigation_wait || navigation_wait.zero?
250
+ return @page.command("Input.dispatchMouseEvent", slowmoable: true, **options)
251
+ end
252
+
253
+ deadline = Deadline.wrap(timeout || @page.timeout)
254
+ result = nil
255
+ @page.await_navigation!(watch: navigation_wait, timeout: deadline) do
256
+ result = @page.command("Input.dispatchMouseEvent", slowmoable: true, timeout: deadline, **options)
257
+ end
258
+ result
259
+ end
260
+
261
+ def positional_coordinates(name, coordinates, x, y)
262
+ return [x, y] if coordinates.empty?
263
+ raise ArgumentError, "#{name} takes x and y" unless coordinates.size == 2
264
+
265
+ Utils::Deprecate.warn("Passing x and y to #{name} positionally will be removed in Ferrum 0.22, " \
266
+ "pass x: and y: instead.", user_code: true)
267
+ coordinates
268
+ end
269
+
270
+ def modifiers_mask(modifiers)
271
+ case modifiers
272
+ when nil then @page.keyboard.held_modifiers
273
+ when Integer then modifiers
274
+ else @page.keyboard.modifiers(modifiers)
275
+ end
189
276
  end
190
277
 
191
278
  def validate_button(button)
@@ -8,15 +8,48 @@ module Ferrum
8
8
  # answered with credentials or canceled.
9
9
  #
10
10
  class AuthRequest
11
- attr_accessor :request_id, :frame_id, :resource_type
11
+ REQUEST_ID_DEPRECATED = "request_id will be removed in Ferrum 0.22. Use id instead."
12
+
13
+ # The id of the paused request, the `requestId` that `Fetch` commands take.
14
+ #
15
+ # @return [String]
16
+ #
17
+ # @note The writer is a private API, Ferrum sets it.
18
+ attr_reader :id
19
+
20
+ # @note The writer is a private API, Ferrum sets it.
21
+ attr_reader :frame_id, :resource_type
22
+
23
+ # @api private
24
+ attr_writer :id, :frame_id, :resource_type
12
25
 
13
26
  def initialize(page, params)
14
27
  @page = page
15
28
  @params = params
16
- @request_id = params["requestId"]
29
+ @id = params["requestId"]
17
30
  @frame_id = params["frameId"]
18
31
  @resource_type = params["resourceType"]
19
32
  @request = params["request"]
33
+ @handled = false
34
+ end
35
+
36
+ #
37
+ # @deprecated Will be removed in Ferrum 0.22. Use {#id}.
38
+ #
39
+ # @return [String]
40
+ #
41
+ def request_id
42
+ Utils::Deprecate.warn(REQUEST_ID_DEPRECATED, user_code: true)
43
+ id
44
+ end
45
+
46
+ #
47
+ # Whether the challenge was answered or canceled.
48
+ #
49
+ # @return [Boolean]
50
+ #
51
+ def handled?
52
+ @handled
20
53
  end
21
54
 
22
55
  #
@@ -26,7 +59,7 @@ module Ferrum
26
59
  # @return [Boolean]
27
60
  #
28
61
  def navigation_request?
29
- @params["isNavigationRequest"]
62
+ resource_type == "Document"
30
63
  end
31
64
 
32
65
  #
@@ -72,15 +105,17 @@ module Ferrum
72
105
  # @option options [String] :password
73
106
  #
74
107
  def continue(**options)
75
- options = options.merge(requestId: request_id)
108
+ options = options.merge(requestId: id)
109
+ @handled = true
76
110
  @page.command("Fetch.continueWithAuth", **options)
77
111
  end
78
112
 
79
113
  #
80
- # Cancels the authentication challenge and the request.
114
+ # Cancels the authentication challenge, so the request completes with
115
+ # the unauthorized response.
81
116
  #
82
117
  def abort
83
- @page.command("Fetch.failRequest", requestId: request_id, errorReason: "BlockedByClient")
118
+ continue(authChallengeResponse: { response: "CancelAuth" })
84
119
  end
85
120
 
86
121
  #
@@ -136,7 +171,7 @@ module Ferrum
136
171
  #
137
172
  def inspect
138
173
  "#<#{self.class} " \
139
- "@request_id=#{@request_id.inspect} " \
174
+ "@id=#{@id.inspect} " \
140
175
  "@frame_id=#{@frame_id.inspect} " \
141
176
  "@resource_type=#{@resource_type.inspect} " \
142
177
  "@request=#{@request.inspect}>"
@@ -8,9 +8,14 @@ module Ferrum
8
8
  # defined in `ferrum/errors.rb`.
9
9
  #
10
10
  class Error
11
- attr_writer :canceled
12
- attr_reader :time, :timestamp
13
- attr_accessor :id, :url, :type, :error_text, :monotonic_time, :description
11
+ TIMESTAMP_DEPRECATED = "timestamp will be removed in Ferrum 0.22. Use time instead."
12
+ MONOTONIC_TIME_DEPRECATED = "monotonic_time will be removed in Ferrum 0.22. Use time instead."
13
+
14
+ # @note The writer is a private API, Ferrum sets it.
15
+ attr_reader :id, :url, :type, :error_text, :description
16
+
17
+ # @api private
18
+ attr_writer :canceled, :id, :url, :type, :error_text, :monotonic_time, :description, :time
14
19
 
15
20
  # Whether the request was canceled.
16
21
  #
@@ -19,6 +24,29 @@ module Ferrum
19
24
  @canceled
20
25
  end
21
26
 
27
+ # When the request failed.
28
+ #
29
+ # @return [Time, nil]
30
+ #
31
+ # @note The writer is a private API, Ferrum sets it.
32
+ attr_reader :time
33
+
34
+ # @deprecated Will be removed in Ferrum 0.22. Use {#time}.
35
+ #
36
+ # @return [Float, nil]
37
+ def timestamp
38
+ Utils::Deprecate.warn(TIMESTAMP_DEPRECATED, user_code: true)
39
+ @timestamp
40
+ end
41
+
42
+ # @deprecated Will be removed in Ferrum 0.22. Use {#time}.
43
+ #
44
+ # @return [Float, nil]
45
+ def monotonic_time
46
+ Utils::Deprecate.warn(MONOTONIC_TIME_DEPRECATED, user_code: true)
47
+ @monotonic_time
48
+ end
49
+
22
50
  # Sets the error's timestamp, deriving {#time} from it.
23
51
  #
24
52
  # @param [Float] value
@@ -26,9 +54,11 @@ module Ferrum
26
54
  # `Log.entryAdded`.
27
55
  #
28
56
  # @return [Float]
57
+ #
58
+ # @api private
29
59
  def timestamp=(value)
30
60
  @timestamp = value
31
- @time = Time.strptime((value / 1000).to_s, "%s")
61
+ @time = Time.at(value / 1000.0)
32
62
  end
33
63
  end
34
64
  end
@@ -16,28 +16,53 @@ module Ferrum
16
16
  # The intercepted request.
17
17
  #
18
18
  # @return [InterceptedRequest, nil]
19
- attr_accessor :intercepted_request
19
+ #
20
+ # @note The writer is a private API, Ferrum sets it.
21
+ attr_reader :intercepted_request
22
+
23
+ # @api private
24
+ attr_writer :intercepted_request
20
25
 
21
26
  # The request object.
22
27
  #
23
28
  # @return [Request, nil]
24
- attr_accessor :request
29
+ #
30
+ # @note The writer is a private API, Ferrum sets it.
31
+ attr_reader :request
32
+
33
+ # @api private
34
+ attr_writer :request
25
35
 
26
36
  # The response object.
27
37
  #
28
38
  # @return [Response, nil]
29
- attr_accessor :response
39
+ #
40
+ # @note The writer is a private API, Ferrum sets it.
41
+ attr_reader :response
42
+
43
+ # @api private
44
+ attr_writer :response
30
45
 
31
46
  # The error object.
32
47
  #
33
48
  # @return [Error, nil]
34
- attr_accessor :error
49
+ #
50
+ # @note The writer is a private API, Ferrum sets it.
51
+ attr_reader :error
52
+
53
+ # @api private
54
+ attr_writer :error
35
55
 
36
56
  # Determines if the network exchange is unknown due to
37
57
  # a lost of its context
38
58
  #
39
59
  # @return Boolean
40
- attr_accessor :unknown
60
+ #
61
+ # @note The writer is a private API, Ferrum sets it.
62
+ attr_reader :unknown
63
+
64
+ # @api private
65
+ attr_writer :unknown
41
66
 
42
67
  # The raw `Network.requestWillBeSentExtraInfo` params for the request,
43
68
  # if it arrived before `request` was set.
@@ -45,7 +70,10 @@ module Ferrum
45
70
  # @return [Hash, nil]
46
71
  #
47
72
  # @api private
48
- attr_accessor :request_extra_info
73
+ attr_reader :request_extra_info
74
+
75
+ # @api private
76
+ attr_writer :request_extra_info
49
77
 
50
78
  #
51
79
  # Initializes the network exchange.
@@ -72,7 +100,7 @@ module Ferrum
72
100
  # @return [Boolean]
73
101
  #
74
102
  def navigation_request?(frame_id)
75
- request&.type?(:document) && request&.frame_id == frame_id
103
+ request&.resource_type?(:document) && request&.frame_id == frame_id
76
104
  end
77
105
 
78
106
  #
@@ -13,19 +13,43 @@ module Ferrum
13
13
  class InterceptedRequest
14
14
  include RequestParams
15
15
 
16
- attr_accessor :request_id, :frame_id, :resource_type, :network_id, :status
16
+ REQUEST_ID_DEPRECATED = "request_id will be removed in Ferrum 0.22. Use id instead."
17
+
18
+ # The interception id, the `requestId` that `Fetch` commands take. {#network_id} is the {Request#id} of the
19
+ # same request.
20
+ #
21
+ # @return [String]
22
+ #
23
+ # @note The writer is a private API, Ferrum sets it.
24
+ attr_reader :id
25
+
26
+ # @note The writer is a private API, Ferrum sets it.
27
+ attr_reader :frame_id, :resource_type, :network_id, :status
28
+
29
+ # @api private
30
+ attr_writer :id, :frame_id, :resource_type, :network_id, :status
17
31
 
18
32
  def initialize(client, params)
19
33
  @status = nil
20
34
  @client = client
21
35
  @params = params
22
- @request_id = params["requestId"]
36
+ @id = params["requestId"]
23
37
  @frame_id = params["frameId"]
24
38
  @resource_type = params["resourceType"]
25
39
  @request = params["request"]
26
40
  @network_id = params["networkId"]
27
41
  end
28
42
 
43
+ #
44
+ # @deprecated Will be removed in Ferrum 0.22. Use {#id}.
45
+ #
46
+ # @return [String]
47
+ #
48
+ def request_id
49
+ Utils::Deprecate.warn(REQUEST_ID_DEPRECATED, user_code: true)
50
+ id
51
+ end
52
+
29
53
  #
30
54
  # Whether the request's current status matches the given value.
31
55
  #
@@ -38,6 +62,15 @@ module Ferrum
38
62
  @status == value.to_sym
39
63
  end
40
64
 
65
+ #
66
+ # Whether the request was continued, responded to or aborted.
67
+ #
68
+ # @return [Boolean]
69
+ #
70
+ def handled?
71
+ !@status.nil?
72
+ end
73
+
41
74
  #
42
75
  # Whether this request is for the navigation of a frame, as opposed
43
76
  # to a subresource (script, image, XHR, etc).
@@ -45,7 +78,7 @@ module Ferrum
45
78
  # @return [Boolean]
46
79
  #
47
80
  def navigation_request?
48
- @params["isNavigationRequest"]
81
+ resource_type == "Document"
49
82
  end
50
83
 
51
84
  #
@@ -88,11 +121,11 @@ module Ferrum
88
121
  headers = headers.merge(options.fetch(:responseHeaders, {}))
89
122
 
90
123
  options = { responseCode: 200 }.merge(options)
91
- options = options.merge(requestId: request_id, responseHeaders: header_array(headers))
124
+ options = options.merge(requestId: id, responseHeaders: header_array(headers))
92
125
  options = options.merge(body: Base64.strict_encode64(options.fetch(:body, ""))) if has_body
93
126
 
94
127
  @status = :responded
95
- @client.command("Fetch.fulfillRequest", async: true, **options)
128
+ @client.notify("Fetch.fulfillRequest", **options)
96
129
  end
97
130
 
98
131
  #
@@ -117,9 +150,9 @@ module Ferrum
117
150
  # request.continue
118
151
  #
119
152
  def continue(**options)
120
- options = options.merge(requestId: request_id)
153
+ options = options.merge(requestId: id)
121
154
  @status = :continued
122
- @client.command("Fetch.continueRequest", async: true, **options)
155
+ @client.notify("Fetch.continueRequest", **options)
123
156
  end
124
157
 
125
158
  #
@@ -130,7 +163,7 @@ module Ferrum
130
163
  #
131
164
  def abort
132
165
  @status = :aborted
133
- @client.command("Fetch.failRequest", async: true, requestId: request_id, errorReason: "BlockedByClient")
166
+ @client.notify("Fetch.failRequest", requestId: id, errorReason: "BlockedByClient")
134
167
  end
135
168
 
136
169
  #
@@ -159,7 +192,7 @@ module Ferrum
159
192
  #
160
193
  def inspect
161
194
  "#<#{self.class} " \
162
- "@request_id=#{@request_id.inspect} " \
195
+ "@id=#{@id.inspect} " \
163
196
  "@frame_id=#{@frame_id.inspect} " \
164
197
  "@resource_type=#{@resource_type.inspect} " \
165
198
  "@request=#{@request.inspect}>"