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
@@ -13,6 +13,8 @@ module Ferrum
13
13
  #
14
14
  # @return [Number]
15
15
  #
16
+ # @raise [TimeoutError]
17
+ #
16
18
  def playback_rate
17
19
  command("Animation.getPlaybackRate")["playbackRate"]
18
20
  end
@@ -22,6 +24,8 @@ module Ferrum
22
24
  #
23
25
  # @param [Number] value
24
26
  #
27
+ # @raise [TimeoutError]
28
+ #
25
29
  # @example
26
30
  # browser = Ferrum::Browser.new
27
31
  # browser.playback_rate = 2000
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ferrum
4
+ class Page
5
+ #
6
+ # The cookies one page sees: the ones sent to its URL and the URLs of its
7
+ # frames, via the CDP `Network` domain. The page shares them with every
8
+ # other page in its {Context}, `context.cookies` has all of them whatever
9
+ # their domain.
10
+ #
11
+ class Cookies
12
+ include Ferrum::Cookies::Collection
13
+
14
+ def initialize(page)
15
+ @page = page
16
+ end
17
+
18
+ #
19
+ # Enumerates over the page's cookies.
20
+ #
21
+ # @param [Array<String>, nil] urls
22
+ # The URLs to get the cookies for instead of the page's and its frames'.
23
+ #
24
+ # @yield [cookie]
25
+ # The given block will be passed each cookie.
26
+ #
27
+ # @yieldparam [Ferrum::Cookies::Cookie] cookie
28
+ #
29
+ # @return [Enumerator]
30
+ # If no block is given, an Enumerator object will be returned.
31
+ #
32
+ # @raise [TimeoutError]
33
+ #
34
+ # @example
35
+ # page.cookies.each { |cookie| puts cookie.name }
36
+ # page.cookies.each(urls: ["https://accounts.google.com"]).map(&:name) # => ["SID"]
37
+ #
38
+ def each(urls: nil)
39
+ return enum_for(__method__, urls: urls) unless block_given?
40
+
41
+ params = { urls: urls } if urls
42
+ cookies = @page.command("Network.getCookies", **Hash(params))["cookies"]
43
+
44
+ cookies.each do |c|
45
+ yield Ferrum::Cookies::Cookie.new(c)
46
+ end
47
+ end
48
+
49
+ #
50
+ # Sets a cookie, see {Ferrum::Cookies#set} for the options. Without
51
+ # `:domain` or `:url` it's set for the page's host, or the `:base_url`
52
+ # host before the page has loaded anything.
53
+ #
54
+ # @param [Hash{Symbol => Object}, Ferrum::Cookies::Cookie] options
55
+ #
56
+ # @return [nil]
57
+ #
58
+ # @raise [TimeoutError]
59
+ #
60
+ # @example
61
+ # page.cookies.set(name: "stealth", value: "omg") # => true
62
+ #
63
+ def set(options)
64
+ cookie = cookie_params(options)
65
+ cookie[:domain] ||= default_domain unless cookie[:url]
66
+
67
+ @page.command("Network.setCookie", **cookie)
68
+ nil
69
+ end
70
+
71
+ #
72
+ # Removes the cookies with the given name, on the page's host unless
73
+ # `:domain` or `:url` is given.
74
+ #
75
+ # @param [String] name
76
+ #
77
+ # @param [Hash{Symbol => Object}] options
78
+ #
79
+ # @option options [String] :domain
80
+ # The exact domain of the cookie.
81
+ #
82
+ # @option options [String] :url
83
+ # The URL the cookie is sent to.
84
+ #
85
+ # @option options [String] :path
86
+ # The exact path of the cookie.
87
+ #
88
+ # @return [nil]
89
+ #
90
+ # @raise [TimeoutError]
91
+ #
92
+ # @example
93
+ # page.cookies.remove(name: "stealth") # => true
94
+ #
95
+ def remove(name:, **options)
96
+ raise "Specify :domain or :url option" if !options[:domain] && !options[:url] && !default_domain
97
+
98
+ options = options.merge(name: name)
99
+ options[:domain] ||= default_domain unless options[:url]
100
+
101
+ @page.command("Network.deleteCookies", **options)
102
+
103
+ nil
104
+ end
105
+
106
+ #
107
+ # Removes the page's cookies, the ones {#each} yields. Cookies on other
108
+ # domains stay, `context.cookies.clear` removes them too.
109
+ #
110
+ # @return [nil]
111
+ #
112
+ # @raise [TimeoutError]
113
+ #
114
+ # @example
115
+ # page.cookies.clear # => true
116
+ #
117
+ def clear
118
+ each do |cookie|
119
+ @page.command("Network.deleteCookies", name: cookie.name, domain: cookie.domain, path: cookie.path)
120
+ end
121
+
122
+ nil
123
+ end
124
+
125
+ private
126
+
127
+ def default_domain
128
+ uri = URI.parse(@page.url.to_s)
129
+ %w[http https].include?(uri.scheme) ? uri.host : base_domain(@page.base_url)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -104,7 +104,7 @@ module Ferrum
104
104
  frame.execution_id = nil
105
105
  else
106
106
  @frames.delete(params["frameId"])
107
- @event.set if @main_frame.idle? && frame&.idle? == false && frames.size == 1
107
+ @idle.set(true) if @main_frame.idle? && frame&.idle? == false && frames.size == 1
108
108
  end
109
109
  end
110
110
  end
@@ -125,7 +125,7 @@ module Ferrum
125
125
  on("Page.frameStartedLoading") do |params|
126
126
  frame = @frames[params["frameId"]]
127
127
  frame.state = :started_loading if frame
128
- @event.reset
128
+ @idle.clear
129
129
  end
130
130
  end
131
131
 
@@ -148,14 +148,14 @@ module Ferrum
148
148
  # tree, and we save it in a variable because if we call that again root
149
149
  # node will change the id and all subsequent nodes have to change id too.
150
150
  if @main_frame.id == params["frameId"]
151
- @event.set if idling?
151
+ @idle.set(true) if idling?
152
152
  document_node_id(async: true)
153
153
  end
154
154
 
155
155
  frame = @frames[params["frameId"]]
156
156
  frame&.state = :stopped_loading
157
157
 
158
- @event.set if idling?
158
+ @idle.set(true) if idling?
159
159
  end
160
160
  end
161
161
 
@@ -181,14 +181,14 @@ module Ferrum
181
181
  # `init` and `networkIdle`. Without it `go_to` would wait for such iframe until it raises timeout.
182
182
  if event["name"] == "networkIdle" && !frame.main?
183
183
  frame.state = :stopped_loading
184
- @event.set if idling?
184
+ @idle.set(true) if idling?
185
185
  end
186
186
  end
187
187
  end
188
188
 
189
189
  def subscribe_navigated_within_document
190
190
  on("Page.navigatedWithinDocument") do
191
- @event.set if idling?
191
+ @idle.set(true) if idling?
192
192
  end
193
193
  end
194
194
 
@@ -198,7 +198,7 @@ module Ferrum
198
198
  # Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR,
199
199
  # Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping,
200
200
  # CSPViolationReport, Other
201
- @event.reset if params["frameId"] == @main_frame.id && params["type"] == "Document"
201
+ @idle.clear if params["frameId"] == @main_frame.id && params["type"] == "Document"
202
202
  end
203
203
  end
204
204
 
@@ -0,0 +1,430 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ferrum
4
+ class Page
5
+ #
6
+ # Navigation of the page's main frame: {#go_to}, {#refresh}, the history
7
+ # and {#await_navigation} for a navigation that other code starts.
8
+ #
9
+ module Navigation
10
+ #
11
+ # Navigates the page to a URL and waits for it to load.
12
+ #
13
+ # @param [String, nil] url
14
+ # The URL to navigate to. The url should include scheme unless you set
15
+ # `{Browser#base_url = url}` when configuring.
16
+ #
17
+ # @param [Numeric, Deadline, nil] timeout
18
+ # How long the whole navigation may take, from sending the request to the
19
+ # page loading. Defaults to the page's `timeout`.
20
+ #
21
+ # @param [String, nil] referrer
22
+ # The referrer of this navigation only, what the document request sends
23
+ # as `Referer` and `document.referrer` returns. A `Referer` header set
24
+ # with {Headers#add} or {Headers#set} is sent with every request and
25
+ # takes its place.
26
+ #
27
+ # @return [String, nil]
28
+ # The navigated frame's id, or `nil` when the page doesn't load in time.
29
+ #
30
+ # @raise [StatusError]
31
+ # If the request fails.
32
+ #
33
+ # @raise [NavigationTimeoutError]
34
+ # Once the budget of an enclosing {Deadline.within} is used up.
35
+ #
36
+ # @raise [PendingConnectionsError]
37
+ # If the deprecated `:pending_connection_errors` option is set and the page doesn't load in time.
38
+ #
39
+ # @example
40
+ # page.go_to("https://github.com/")
41
+ # page.go_to("/slow", timeout: 10)
42
+ # page.go_to("/landing", referrer: "https://google.com")
43
+ #
44
+ def go_to(url = nil, timeout: nil, referrer: nil)
45
+ go_to!(url, timeout: timeout, referrer: referrer)
46
+ rescue NavigationTimeoutError => e
47
+ raise PendingConnectionsError.new(e.url, e.pendings) if @options.pending_connection_errors
48
+ raise if Deadline.current&.expired?
49
+
50
+ nil
51
+ end
52
+
53
+ #
54
+ # @deprecated Will be removed in Ferrum 0.22. Use {#go_to}.
55
+ #
56
+ # @param [String, nil] url
57
+ #
58
+ # @param [Hash{Symbol => Object}] options
59
+ # Passed to {#go_to}.
60
+ #
61
+ # @return [String, nil]
62
+ #
63
+ def goto(url = nil, **options)
64
+ Utils::Deprecate.warn("goto will be removed in Ferrum 0.22. Use go_to instead.", user_code: true)
65
+ go_to(url, **options)
66
+ end
67
+
68
+ #
69
+ # @deprecated Will be removed in Ferrum 0.22. Use {#go_to}.
70
+ #
71
+ # @param [String, nil] url
72
+ #
73
+ # @param [Hash{Symbol => Object}] options
74
+ # Passed to {#go_to}.
75
+ #
76
+ # @return [String, nil]
77
+ #
78
+ def go(url = nil, **options)
79
+ Utils::Deprecate.warn("go will be removed in Ferrum 0.22. Use go_to instead.", user_code: true)
80
+ go_to(url, **options)
81
+ end
82
+
83
+ #
84
+ # Same as {#go_to}, but raises if the page doesn't load in time.
85
+ #
86
+ # @param [String, nil] url
87
+ #
88
+ # @param [Numeric, Deadline, nil] timeout
89
+ #
90
+ # @param [String, nil] referrer
91
+ #
92
+ # @return [String]
93
+ # The navigated frame's id.
94
+ #
95
+ # @raise [StatusError]
96
+ # If the request fails.
97
+ #
98
+ # @raise [NavigationTimeoutError]
99
+ # If the page doesn't load in time, naming the requests still pending.
100
+ #
101
+ # @example
102
+ # page.go_to!("/slow", timeout: 10)
103
+ #
104
+ def go_to!(url = nil, timeout: nil, referrer: nil)
105
+ options = { url: combine_url!(url) }
106
+ referrer ||= headers.get["Referer"]
107
+ options.merge!(referrer: referrer) if referrer
108
+ deadline = Deadline.wrap(timeout || self.timeout)
109
+ response = nil
110
+ navigate!(options[:url], deadline) do
111
+ response = command("Page.navigate", timeout: deadline, **options)
112
+ end
113
+ error_text = response["errorText"] # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
114
+ if error_text && error_text != "net::ERR_ABORTED" # Request aborted due to user action or download
115
+ raise StatusError.new(options[:url], "Request to #{options[:url]} failed (#{error_text})")
116
+ end
117
+
118
+ response["frameId"]
119
+ end
120
+
121
+ #
122
+ # Reloads the current page and waits for it to load.
123
+ #
124
+ # @param [Numeric, Deadline, nil] timeout
125
+ # How long the whole reload may take. Defaults to the page's `timeout`.
126
+ #
127
+ # @return [true, nil]
128
+ # `nil` when the page doesn't load in time.
129
+ #
130
+ # @example
131
+ # page.go_to("https://github.com/")
132
+ # page.refresh # => true
133
+ #
134
+ def refresh(timeout: nil)
135
+ nil_on_timeout { refresh!(timeout: timeout) }
136
+ end
137
+
138
+ #
139
+ # @deprecated Will be removed in Ferrum 0.22. Use {#refresh}.
140
+ #
141
+ # @param [Numeric, Deadline, nil] timeout
142
+ #
143
+ # @return [true, nil]
144
+ #
145
+ def reload(timeout: nil)
146
+ Utils::Deprecate.warn("reload will be removed in Ferrum 0.22. Use refresh instead.", user_code: true)
147
+ refresh(timeout: timeout)
148
+ end
149
+
150
+ #
151
+ # Same as {#refresh}, but raises if the page doesn't load in time.
152
+ #
153
+ # @param [Numeric, Deadline, nil] timeout
154
+ #
155
+ # @return [true]
156
+ #
157
+ # @raise [NavigationTimeoutError]
158
+ #
159
+ def refresh!(timeout: nil)
160
+ deadline = Deadline.wrap(timeout || self.timeout)
161
+ navigate!(nil, deadline) { command("Page.reload", timeout: deadline, slowmoable: true) }
162
+ true
163
+ end
164
+
165
+ #
166
+ # @deprecated Will be removed in Ferrum 0.22. Use {#refresh!}.
167
+ #
168
+ # @param [Numeric, Deadline, nil] timeout
169
+ #
170
+ # @return [true]
171
+ #
172
+ # @raise [NavigationTimeoutError]
173
+ #
174
+ def reload!(timeout: nil)
175
+ Utils::Deprecate.warn("reload! will be removed in Ferrum 0.22. Use refresh! instead.", user_code: true)
176
+ refresh!(timeout: timeout)
177
+ end
178
+
179
+ #
180
+ # Stop all navigations and loading pending resources on the page.
181
+ #
182
+ # @return [nil]
183
+ #
184
+ # @raise [TimeoutError]
185
+ #
186
+ # @example
187
+ # page.go_to("https://github.com/")
188
+ # page.stop
189
+ #
190
+ def stop
191
+ command("Page.stopLoading", slowmoable: true)
192
+ nil
193
+ end
194
+
195
+ #
196
+ # Navigates to the previous URL in the history and waits for it to load.
197
+ #
198
+ # @param [Numeric, Deadline, nil] timeout
199
+ # How long the history lookup and the page load may take together, the page timeout by default.
200
+ #
201
+ # @return [true, nil]
202
+ # `nil` when there's no previous URL or the page doesn't load in time.
203
+ #
204
+ # @example
205
+ # page.go_to("https://github.com/")
206
+ # page.at_xpath("//a").click
207
+ # page.back
208
+ #
209
+ def back(timeout: nil)
210
+ nil_on_timeout { back!(timeout: timeout) }
211
+ end
212
+
213
+ #
214
+ # Same as {#back}, but raises if the page doesn't load in time.
215
+ #
216
+ # @param [Numeric, Deadline, nil] timeout
217
+ #
218
+ # @return [true, nil]
219
+ # `nil` when there's no previous URL.
220
+ #
221
+ # @raise [NavigationTimeoutError]
222
+ #
223
+ def back!(timeout: nil)
224
+ history_navigate(delta: -1, timeout: timeout)
225
+ end
226
+
227
+ #
228
+ # Navigates to the next URL in the history and waits for it to load.
229
+ #
230
+ # @param [Numeric, Deadline, nil] timeout
231
+ # How long the history lookup and the page load may take together, the page timeout by default.
232
+ #
233
+ # @return [true, nil]
234
+ # `nil` when there's no next URL or the page doesn't load in time.
235
+ #
236
+ # @example
237
+ # page.go_to("https://github.com/")
238
+ # page.at_xpath("//a").click
239
+ # page.back
240
+ # page.forward
241
+ #
242
+ def forward(timeout: nil)
243
+ nil_on_timeout { forward!(timeout: timeout) }
244
+ end
245
+
246
+ #
247
+ # Same as {#forward}, but raises if the page doesn't load in time.
248
+ #
249
+ # @param [Numeric, Deadline, nil] timeout
250
+ #
251
+ # @return [true, nil]
252
+ # `nil` when there's no next URL.
253
+ #
254
+ # @raise [NavigationTimeoutError]
255
+ #
256
+ def forward!(timeout: nil)
257
+ history_navigate(delta: 1, timeout: timeout)
258
+ end
259
+
260
+ #
261
+ # Runs the block and waits for the navigation of the main frame it starts
262
+ # to finish, e.g. after a click on a link made from JavaScript.
263
+ #
264
+ # @param [Numeric, nil] watch
265
+ # Seconds to watch for a navigation after the block returns, for code
266
+ # that may or may not navigate. When none begins in that time it
267
+ # returns `true` right away. By default a navigation must happen within
268
+ # `timeout`.
269
+ #
270
+ # @param [Numeric, Deadline, nil] timeout
271
+ # How long the block and the navigation it starts may take together.
272
+ # Defaults to the page's `timeout`.
273
+ #
274
+ # @yield The code that starts the navigation.
275
+ #
276
+ # @return [true, nil]
277
+ # `nil` when the page doesn't load in time.
278
+ #
279
+ # @raise [ArgumentError]
280
+ # If no block is given.
281
+ #
282
+ # @example
283
+ # page.await_navigation { page.at_css("a").evaluate("this.click()") } # => true
284
+ # page.await_navigation(watch: 0.1) { page.at_css("button").evaluate("this.click()") } # => true
285
+ #
286
+ def await_navigation(watch: nil, timeout: nil)
287
+ raise ArgumentError, "await_navigation needs a block that starts the navigation" unless block_given?
288
+
289
+ deadline = Deadline.wrap(timeout || self.timeout)
290
+ was_idle = @idle.set?
291
+ generation = @idle.clear
292
+ yield
293
+ if watch
294
+ @idle.wait([watch, deadline.remaining].min)
295
+ if generation == @idle.generation
296
+ @idle.set(true) if was_idle
297
+ return true
298
+ end
299
+ end
300
+ loaded = @idle.wait(deadline)
301
+ @idle.set(true) if !loaded && was_idle && generation == @idle.generation
302
+ loaded || nil
303
+ end
304
+
305
+ #
306
+ # Same as {#await_navigation}, but raises if the page doesn't load in time.
307
+ #
308
+ # @param [Numeric, nil] watch
309
+ #
310
+ # @param [Numeric, Deadline, nil] timeout
311
+ #
312
+ # @yield The code that starts the navigation.
313
+ #
314
+ # @return [true]
315
+ #
316
+ # @raise [TimeoutError]
317
+ # If the page doesn't load in time.
318
+ #
319
+ # @raise [ArgumentError]
320
+ # If no block is given.
321
+ #
322
+ def await_navigation!(watch: nil, timeout: nil, &block)
323
+ await_navigation(watch: watch, timeout: timeout, &block) || raise(TimeoutError)
324
+ end
325
+
326
+ #
327
+ # Blocks until the page reloads or the timeout is reached.
328
+ #
329
+ # @deprecated Will be removed in Ferrum 0.22. Use {#await_navigation} around the code that reloads the page.
330
+ #
331
+ # @param [Numeric, Deadline] timeout
332
+ # Maximum time in seconds to wait for a reload event.
333
+ #
334
+ # @return [void]
335
+ #
336
+ # @example
337
+ # page.wait_for_reload
338
+ #
339
+ def wait_for_reload(timeout = 1)
340
+ Utils::Deprecate.warn(WAIT_FOR_RELOAD_DEPRECATED, user_code: true)
341
+ deadline = Deadline.wrap(timeout)
342
+ generation = @idle.clear if @idle.set?
343
+ @idle.wait(deadline)
344
+ @idle.set(true) if generation == @idle.generation
345
+ end
346
+
347
+ #
348
+ # Runs the block and, if it starts a navigation within `navigation_wait`
349
+ # seconds, waits for that navigation to finish.
350
+ #
351
+ # @deprecated Will be removed in Ferrum 0.22. Use {#await_navigation} with `watch:`.
352
+ #
353
+ # @param [Numeric] navigation_wait
354
+ #
355
+ # @param [Numeric, Deadline, nil] timeout
356
+ #
357
+ # @yield The code that may start a navigation.
358
+ #
359
+ # @return [Boolean]
360
+ #
361
+ def wait_for_navigation(navigation_wait = NAVIGATION_WAIT, timeout: nil, &)
362
+ Utils::Deprecate.warn(WAIT_FOR_NAVIGATION_DEPRECATED, user_code: true)
363
+ await_navigation(watch: navigation_wait, timeout: timeout, &) || false
364
+ end
365
+
366
+ #
367
+ # Same as {#wait_for_navigation}, but raises if the page doesn't load in time.
368
+ #
369
+ # @deprecated Will be removed in Ferrum 0.22. Use {#await_navigation!} with `watch:`.
370
+ #
371
+ # @param [Numeric] navigation_wait
372
+ #
373
+ # @param [Numeric, Deadline, nil] timeout
374
+ #
375
+ # @yield The code that may start a navigation.
376
+ #
377
+ # @return [void]
378
+ #
379
+ # @raise [TimeoutError]
380
+ #
381
+ def wait_for_navigation!(navigation_wait = NAVIGATION_WAIT, timeout: nil, &)
382
+ Utils::Deprecate.warn(WAIT_FOR_NAVIGATION_DEPRECATED, user_code: true)
383
+ await_navigation!(watch: navigation_wait, timeout: timeout, &)
384
+ nil
385
+ end
386
+
387
+ private
388
+
389
+ def history_navigate(delta:, timeout: nil)
390
+ deadline = Deadline.wrap(timeout || self.timeout)
391
+ history = command("Page.getNavigationHistory", timeout: deadline)
392
+ index, entries = history.values_at("currentIndex", "entries")
393
+ return if (index + delta).negative?
394
+
395
+ entry = entries[index + delta]
396
+ return unless entry
397
+
398
+ navigate!(entry["url"], deadline) do
399
+ command("Page.navigateToHistoryEntry", timeout: deadline, slowmoable: true, entryId: entry["id"])
400
+ end
401
+ true
402
+ end
403
+
404
+ def navigate!(url, deadline, &)
405
+ await_navigation!(watch: NAVIGATION_WAIT, timeout: deadline, &)
406
+ rescue TimeoutError
407
+ raise NavigationTimeoutError.new(url, network.traffic.select(&:pending?).map(&:url).compact)
408
+ end
409
+
410
+ def nil_on_timeout
411
+ yield
412
+ rescue TimeoutError
413
+ raise if Deadline.current&.expired?
414
+
415
+ nil
416
+ end
417
+
418
+ def combine_url!(url_or_path)
419
+ url = Addressable::URI.parse(url_or_path)
420
+ nil_or_relative = url.nil? || url.relative?
421
+
422
+ if nil_or_relative && !@options.base_url
423
+ raise "Set :base_url browser's option or use absolute url in `go_to`, you passed: #{url_or_path}"
424
+ end
425
+
426
+ (nil_or_relative ? @options.base_url.join(url.to_s) : url).to_s
427
+ end
428
+ end
429
+ end
430
+ end
@@ -61,6 +61,8 @@ module Ferrum
61
61
  # @yieldparam session_id [Integer]
62
62
  # Frame number.
63
63
  #
64
+ # @raise [TimeoutError]
65
+ #
64
66
  # @example
65
67
  # require "base64"
66
68
  #
@@ -93,6 +95,8 @@ module Ferrum
93
95
  end
94
96
 
95
97
  # Stops sending frames.
98
+ #
99
+ # @raise [TimeoutError]
96
100
  def stop_screencast
97
101
  command("Page.stopScreencast")
98
102
  end