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
@@ -75,6 +75,8 @@ module Ferrum
75
75
  # {DEFAULT_RENDER_TIMEOUT} since a full-page capture is a known slow
76
76
  # outlier among CDP commands.
77
77
  #
78
+ # @raise [TimeoutError]
79
+ #
78
80
  # @example
79
81
  # page.go_to("https://google.com/")
80
82
  #
@@ -130,6 +132,8 @@ module Ferrum
130
132
  # @option opts [Float] :paper_height
131
133
  # Sets the paper's height.
132
134
  #
135
+ # @raise [TimeoutError]
136
+ #
133
137
  # @note
134
138
  # See other [native options](https://chromedevtools.github.io/devtools-protocol/tot/Page#method-printToPDF) you
135
139
  # can pass.
@@ -142,7 +146,7 @@ module Ferrum
142
146
  # @example
143
147
  # page.go_to("https://google.com/")
144
148
  # # Save to disk as a PDF
145
- # page.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => true
149
+ # page.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => 4911
146
150
  #
147
151
  def pdf(timeout: DEFAULT_RENDER_TIMEOUT, **opts)
148
152
  path, encoding = common_options(**opts)
@@ -157,6 +161,8 @@ module Ferrum
157
161
  # @param [String, nil] path
158
162
  # The path to save a file on the disk.
159
163
  #
164
+ # @raise [TimeoutError]
165
+ #
160
166
  # @example
161
167
  # page.go_to("https://google.com/")
162
168
  # page.mhtml(path: "google.mhtml") # => 87742
@@ -174,6 +180,8 @@ module Ferrum
174
180
  # @return [(Integer, Integer)]
175
181
  # The width, height of the viewport.
176
182
  #
183
+ # @raise [TimeoutError]
184
+ #
177
185
  # @example
178
186
  # page.viewport_size # => [1024, 768]
179
187
  #
@@ -189,6 +197,8 @@ module Ferrum
189
197
  #
190
198
  # @return [Float]
191
199
  #
200
+ # @raise [TimeoutError]
201
+ #
192
202
  # @example
193
203
  # page.device_pixel_ratio # => 1.0
194
204
  #
@@ -205,6 +215,8 @@ module Ferrum
205
215
  # @return [(Integer, Integer)]
206
216
  # The scroll width, scroll height of the document.
207
217
  #
218
+ # @raise [TimeoutError]
219
+ #
208
220
  # @example
209
221
  # page.document_size # => [1024, 4000]
210
222
  #
@@ -24,9 +24,11 @@ module Ferrum
24
24
  # @param [String] handle
25
25
  # The CDP `IO` stream handle to read from.
26
26
  #
27
- # @return [Boolean, String]
28
- # `true` when saved to disk, otherwise the stream's contents.
27
+ # @return [Integer, String]
28
+ # The number of bytes written when saved to disk, otherwise the
29
+ # stream's contents.
29
30
  #
31
+ # @api private
30
32
  def stream_to(path:, encoding:, handle:)
31
33
  if path.nil?
32
34
  stream_to_memory(encoding: encoding, handle: handle)
@@ -44,11 +46,14 @@ module Ferrum
44
46
  # @param [String] handle
45
47
  # The CDP `IO` stream handle to read from.
46
48
  #
47
- # @return [Boolean]
49
+ # @return [Integer] the number of bytes written.
48
50
  #
51
+ # @api private
49
52
  def stream_to_file(path:, handle:)
50
- File.open(path, "wb") { |f| stream(output: f, handle: handle) }
51
- true
53
+ File.open(path, "wb") do |f|
54
+ stream(output: f, handle: handle)
55
+ f.pos
56
+ end
52
57
  end
53
58
 
54
59
  #
@@ -62,6 +67,7 @@ module Ferrum
62
67
  #
63
68
  # @return [String]
64
69
  #
70
+ # @api private
65
71
  def stream_to_memory(encoding:, handle:)
66
72
  data = String.new # Mutable string has << and compatible to File
67
73
  stream(output: data, handle: handle)
@@ -81,6 +87,7 @@ module Ferrum
81
87
  #
82
88
  # @return [void]
83
89
  #
90
+ # @api private
84
91
  def stream(output:, handle:)
85
92
  loop do
86
93
  result = command("IO.read", handle: handle, size: STREAM_CHUNK)
@@ -101,6 +108,7 @@ module Ferrum
101
108
  #
102
109
  # @return [void]
103
110
  #
111
+ # @api private
104
112
  def close_stream(handle:)
105
113
  command("IO.close", handle: handle)
106
114
  end
@@ -34,7 +34,7 @@ module Ferrum
34
34
  # @param [:binary, :base64] encoding
35
35
  # Encode output as Base64 or plain text.
36
36
  #
37
- # @param [Float, nil] timeout
37
+ # @param [Numeric, Deadline, nil] timeout
38
38
  # Wait until file streaming finishes in the specified time or raise
39
39
  # error.
40
40
  #
@@ -51,10 +51,13 @@ module Ferrum
51
51
  # When `path` is specified returns `true` and stores trace data into
52
52
  # file.
53
53
  #
54
+ # @raise [TimeoutError]
55
+ #
54
56
  def record(path: nil, encoding: :binary, timeout: nil, trace_config: nil, screenshots: false)
55
57
  @path = path
56
58
  @encoding = encoding
57
- @pending = Concurrent::IVar.new
59
+ @pending = Concurrency::Cell.new
60
+ @failure = nil
58
61
  trace_config ||= DEFAULT_TRACE_CONFIG.dup
59
62
 
60
63
  if screenshots
@@ -68,7 +71,10 @@ module Ferrum
68
71
  yield
69
72
  stop
70
73
 
71
- @pending.value!(timeout || @page.timeout)
74
+ @pending.wait(timeout || @page.timeout)
75
+ raise @failure if @failure
76
+
77
+ @pending.value
72
78
  end
73
79
 
74
80
  private
@@ -84,12 +90,11 @@ module Ferrum
84
90
  def subscribe_tracing_complete
85
91
  return if @subscribed_tracing_complete
86
92
 
87
- @page.on("Tracing.tracingComplete") do |event, index|
88
- next if index.to_i != 0
89
-
93
+ @page.on("Tracing.tracingComplete") do |event|
90
94
  @pending.set(stream_handle(event["stream"]))
91
95
  rescue StandardError => e
92
- @pending.fail(e)
96
+ @failure = e
97
+ @pending.close
93
98
  end
94
99
 
95
100
  @subscribed_tracing_complete = true