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/frame.rb CHANGED
@@ -25,12 +25,22 @@ module Ferrum
25
25
  # The Frame's unique id.
26
26
  #
27
27
  # @return [String]
28
- attr_accessor :id
28
+ #
29
+ # @note The writer is a private API, Ferrum sets it.
30
+ attr_reader :id
31
+
32
+ # @api private
33
+ attr_writer :id
29
34
 
30
35
  # If frame was given a name it should be here.
31
36
  #
32
37
  # @return [String, nil]
33
- attr_accessor :name
38
+ #
39
+ # @note The writer is a private API, Ferrum sets it.
40
+ attr_reader :name
41
+
42
+ # @api private
43
+ attr_writer :name
34
44
 
35
45
  # The page the frame belongs to.
36
46
  #
@@ -45,16 +55,25 @@ module Ferrum
45
55
  # One of the states frame's in.
46
56
  #
47
57
  # @return [:started_loading, :navigated, :stopped_loading, :canceled, nil]
58
+ #
59
+ # @note The writer is a private API, Ferrum sets it.
48
60
  attr_reader :state
49
61
 
50
62
  # Frame loader id.
51
63
  #
52
64
  # @return [String, nil]
53
- attr_accessor :loader_id
65
+ #
66
+ # @note The writer is a private API, Ferrum sets it.
67
+ attr_reader :loader_id
68
+
69
+ # @api private
70
+ attr_writer :loader_id
54
71
 
55
72
  # Frame's lifecycle events (navigation, load, paint, etc.).
56
73
  #
57
74
  # @return [Array<Hash{String => (String|Float)}>]
75
+ #
76
+ # @api private
58
77
  attr_reader :lifecycle_events
59
78
 
60
79
  def initialize(id, page, parent_id = nil)
@@ -62,7 +81,7 @@ module Ferrum
62
81
  @page = page
63
82
  @parent_id = parent_id
64
83
  @lifecycle_events = []
65
- @execution_id = Concurrent::MVar.new
84
+ @execution_id = Concurrency::Cell.new
66
85
  @isolated_worlds = Concurrent::Map.new
67
86
  end
68
87
 
@@ -75,6 +94,8 @@ module Ferrum
75
94
  # @return [Symbol]
76
95
  #
77
96
  # @raise [ArgumentError]
97
+ #
98
+ # @api private
78
99
  def state=(value)
79
100
  raise ArgumentError unless STATE_VALUES.include?(value)
80
101
 
@@ -86,6 +107,8 @@ module Ferrum
86
107
  #
87
108
  # @return [String]
88
109
  #
110
+ # @raise [TimeoutError]
111
+ #
89
112
  # @example
90
113
  # browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
91
114
  # frame = browser.frames[1]
@@ -100,6 +123,8 @@ module Ferrum
100
123
  #
101
124
  # @return [String]
102
125
  #
126
+ # @raise [TimeoutError]
127
+ #
103
128
  # @example
104
129
  # browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
105
130
  # frame = browser.frames[1]
@@ -159,12 +184,14 @@ module Ferrum
159
184
  #
160
185
  # @param [String] html
161
186
  #
187
+ # @raise [TimeoutError]
188
+ #
162
189
  # @example
163
190
  # browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
164
191
  # frame = browser.frames[1]
165
- # frame.body # <html lang="en"><head><style>body {transition: opacity ease-in 0.2s; }...
192
+ # frame.html # <html lang="en"><head><style>body {transition: opacity ease-in 0.2s; }...
166
193
  # frame.content = "<html><head></head><body><p>lol</p></body></html>"
167
- # frame.body # => <html><head></head><body><p>lol</p></body></html>
194
+ # frame.html # => <html><head></head><body><p>lol</p></body></html>
168
195
  #
169
196
  def content=(html)
170
197
  execute(<<~JS, html: html)
@@ -174,7 +201,18 @@ module Ferrum
174
201
  JS
175
202
  @page.document_node_id
176
203
  end
177
- alias set_content content=
204
+
205
+ #
206
+ # @deprecated Will be removed in Ferrum 0.22. Use {#content=}.
207
+ #
208
+ # @param [String] html
209
+ #
210
+ # @raise [TimeoutError]
211
+ #
212
+ def set_content(html) # rubocop:disable Naming/AccessorMethodName
213
+ Utils::Deprecate.warn("set_content will be removed in Ferrum 0.22. Use content= instead.", user_code: true)
214
+ self.content = html
215
+ end
178
216
 
179
217
  #
180
218
  # Execution context id which is used by JS, each frame has it's own
@@ -182,15 +220,16 @@ module Ferrum
182
220
  # an error if an execution id hasn't been set yet, if id is set
183
221
  # returns immediately.
184
222
  #
223
+ # @param [Numeric, Deadline, nil] timeout
224
+ # How long to wait for the id, defaults to the page's `timeout`.
225
+ #
185
226
  # @return [Integer]
186
227
  #
187
228
  # @raise [NoExecutionContextError]
188
229
  #
189
- def execution_id!
190
- value = @execution_id.borrow(@page.timeout, &:itself)
191
- raise NoExecutionContextError if value.instance_of?(Object)
192
-
193
- value
230
+ def execution_id!(timeout = nil)
231
+ @execution_id.wait(timeout || @page.timeout)
232
+ @execution_id.value || raise(NoExecutionContextError)
194
233
  end
195
234
 
196
235
  #
@@ -200,10 +239,7 @@ module Ferrum
200
239
  # @return [Integer, nil]
201
240
  #
202
241
  def execution_id
203
- value = @execution_id.value
204
- return if value.instance_of?(Object)
205
-
206
- value
242
+ @execution_id.value
207
243
  end
208
244
 
209
245
  #
@@ -215,11 +251,12 @@ module Ferrum
215
251
  #
216
252
  # @return [Integer, nil]
217
253
  #
254
+ # @api private
218
255
  def execution_id=(value)
219
256
  if value.nil?
220
- @execution_id.try_take!
221
- else
222
- @execution_id.try_put!(value)
257
+ @execution_id.clear
258
+ elsif !@execution_id.set?
259
+ @execution_id.set(value)
223
260
  end
224
261
  end
225
262
 
@@ -264,7 +301,7 @@ module Ferrum
264
301
  "@loader_id=#{@loader_id.inspect} " \
265
302
  "@lifecycle_events=#{@lifecycle_events.inspect} " \
266
303
  "@state=#{@state.inspect} " \
267
- "@execution_id=#{@execution_id.inspect}>"
304
+ "@execution_id=#{execution_id.inspect}>"
268
305
  end
269
306
  end
270
307
  end
@@ -4,10 +4,16 @@ module Ferrum
4
4
  #
5
5
  # Manages the extra HTTP headers a page sends with its requests, via the
6
6
  # CDP `Network.setExtraHTTPHeaders` command. Setting a `User-Agent` or
7
- # `Accept-Language` header also updates the corresponding navigator
8
- # override so JavaScript sees consistent values.
7
+ # `Accept-Language` header also changes what `navigator` reports, which is
8
+ # deprecated and will stop in Ferrum 0.22, use {Network#emulate} for that.
9
+ # Headers that don't contain one of them leave the override alone.
9
10
  #
10
11
  class Headers
12
+ OVERRIDES = { user_agent: "User-Agent", accept_language: "Accept-Language" }.freeze
13
+ OVERRIDES_DEPRECATED = "User-Agent and Accept-Language set through headers will only be sent with requests in " \
14
+ "Ferrum 0.22 and no longer change what navigator reports. " \
15
+ "Use network.emulate(user_agent:, accept_language:) to override both."
16
+
11
17
  def initialize(page)
12
18
  @page = page
13
19
  @headers = {}
@@ -28,21 +34,28 @@ module Ferrum
28
34
  # @param [Hash{String => String}] headers
29
35
  # key-value pairs for example `"User-Agent" => "Browser"`.
30
36
  #
31
- # @return [true]
37
+ # @return [nil]
38
+ #
39
+ # @raise [TimeoutError]
32
40
  #
33
41
  def set(headers)
34
- clear
42
+ reset_overrides(keep: headers)
43
+ @headers = {}
35
44
  add(headers)
36
45
  end
37
46
 
38
47
  #
39
- # Clear all headers.
48
+ # Clear all headers, and the user agent and language they set.
40
49
  #
41
- # @return [true]
50
+ # @return [nil]
51
+ #
52
+ # @raise [TimeoutError]
42
53
  #
43
54
  def clear
55
+ reset_overrides
44
56
  @headers = {}
45
- true
57
+ @page.command("Network.setExtraHTTPHeaders", headers: @headers)
58
+ nil
46
59
  end
47
60
 
48
61
  #
@@ -51,34 +64,26 @@ module Ferrum
51
64
  # @param [Hash{String => String}] headers
52
65
  # key-value pairs for example `"Referer" => "http://example.com"`.
53
66
  #
54
- # @param [Boolean] permanent
67
+ # @return [nil]
55
68
  #
56
- # @return [true]
69
+ # @raise [TimeoutError]
57
70
  #
58
- def add(headers, permanent: true)
59
- if headers["Referer"]
60
- @page.referrer = headers["Referer"]
61
- headers.delete("Referer") unless permanent
62
- end
63
-
71
+ def add(headers)
64
72
  @headers.merge!(headers)
65
- user_agent = @headers["User-Agent"]
66
- accept_language = @headers["Accept-Language"]
67
-
68
- set_overrides(user_agent: user_agent, accept_language: accept_language)
73
+ overrides = OVERRIDES.select { |_, name| headers.key?(name) }.transform_values { |name| headers[name] }
74
+ unless overrides.empty?
75
+ Utils::Deprecate.warn(OVERRIDES_DEPRECATED, user_code: true)
76
+ @page.network.emulate(**overrides)
77
+ end
69
78
  @page.command("Network.setExtraHTTPHeaders", headers: @headers)
70
- true
79
+ nil
71
80
  end
72
81
 
73
82
  private
74
83
 
75
- def set_overrides(user_agent: nil, accept_language: nil, platform: nil)
76
- options = {}
77
- options[:userAgent] = user_agent || @page.default_user_agent
78
- options[:acceptLanguage] = accept_language if accept_language
79
- options[:platform] if platform
80
-
81
- @page.command("Network.setUserAgentOverride", **options) unless options.empty?
84
+ def reset_overrides(keep: {})
85
+ dropped = OVERRIDES.select { |_, name| @headers.key?(name) && !keep.key?(name) }
86
+ @page.network.emulate(**dropped.transform_values { nil }) unless dropped.empty?
82
87
  end
83
88
  end
84
89
  end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ferrum
4
+ module Interceptable
5
+ #
6
+ # An ordered chain of handlers for an event that has to be answered
7
+ # exactly once, e.g. a paused request or a dialog. Handlers run in the
8
+ # order they were added and the chain stops at the first one that handles
9
+ # the event object, so a handler that doesn't want the object just
10
+ # returns. An object nobody handled is passed to the default action.
11
+ #
12
+ class Chain
13
+ # @param [#on, #off] client
14
+ # Where the event comes from.
15
+ #
16
+ # @param [String] event
17
+ # The CDP event name.
18
+ #
19
+ # @param [#call] build
20
+ # Turns the event params into the object handlers receive. The object
21
+ # has to respond to `handled?`.
22
+ #
23
+ # @param [#call] default
24
+ # Called with the object when no handler handled it.
25
+ #
26
+ # @param [Boolean] persistent
27
+ # Keep listening to the event while no handlers are left, so the
28
+ # default action still applies.
29
+ def initialize(client, event, build:, default:, persistent: false)
30
+ @client = client
31
+ @event = event
32
+ @build = build
33
+ @default = default
34
+ @handlers = Concurrent::Hash.new
35
+ @sequence = Concurrent::AtomicFixnum.new
36
+ @subscription = nil
37
+ @persistent = persistent
38
+ @mutex = Mutex.new
39
+ @mutex.synchronize { subscribe } if persistent
40
+ end
41
+
42
+ #
43
+ # Adds a handler to the end of the chain.
44
+ #
45
+ # @return [Integer]
46
+ # The id to pass to {#remove}.
47
+ #
48
+ def add(&block)
49
+ id = @sequence.increment
50
+ @mutex.synchronize do
51
+ @handlers[id] = block
52
+ subscribe
53
+ end
54
+ id
55
+ end
56
+
57
+ #
58
+ # Removes a handler, and stops listening to the event once no handlers
59
+ # are left.
60
+ #
61
+ # @param [Integer] id
62
+ # The id returned by {#add}.
63
+ #
64
+ # @return [void]
65
+ #
66
+ def remove(id)
67
+ @mutex.synchronize do
68
+ @handlers.delete(id)
69
+ next if @persistent || @handlers.any? || @subscription.nil?
70
+
71
+ @client.off(@event, @subscription)
72
+ @subscription = nil
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def subscribe
79
+ @subscribe ||= @client.on(@event) { |params| run(params) }
80
+ end
81
+
82
+ def run(params)
83
+ object = @build.call(params)
84
+ @handlers.each_value do |handler|
85
+ break if object.handled?
86
+
87
+ call(handler, object)
88
+ end
89
+ @default.call(object) unless object.handled?
90
+ end
91
+
92
+ def call(handler, object)
93
+ handler.call(object)
94
+ rescue StandardError => e
95
+ warn("Ferrum: #{@event} callback raised #{e.class}: #{e.message}\n #{e.backtrace&.first}")
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ferrum/interceptable/chain"
4
+
3
5
  module Ferrum
4
6
  # Shared by {Page} and {Worker}: subscribes to the CDP events behind the
5
7
  # `:request` (Fetch-domain request interception), `:response` (a request's
@@ -10,48 +12,40 @@ module Ferrum
10
12
  module Interceptable
11
13
  # Subscribes to a CDP event, or to `:request`/`:response`/`:auth`/`:console`.
12
14
  #
15
+ # `:request` and `:auth` callbacks form a chain: they run in the order they
16
+ # were added, and the rest are skipped as soon as one of them handles the
17
+ # object (`continue`, `respond`, `abort`). An object nobody handled is
18
+ # continued, an auth challenge gets Chrome's default response.
19
+ #
13
20
  # @param [Symbol, String] name
14
21
  #
15
22
  # @yieldparam [Network::InterceptedRequest, Network::Exchange, Network::AuthRequest, Console::Message, Hash] arg
16
23
  # For `:request`, the intercepted {Network::InterceptedRequest}. For
17
- # `:response`, the finished {Network::Exchange}. For `:auth`, the
24
+ # `:response`, the finished {Network::Exchange}, not a {Network::Response}:
25
+ # read the response via `exchange.response`. For `:auth`, the
18
26
  # {Network::AuthRequest} challenge. For `:console`, the new
19
27
  # {Console::Message}. For any other (raw CDP) event name, the event's
20
28
  # params `Hash`.
21
29
  #
22
- # @yieldparam [Integer] index
23
- # This callback's position among all callbacks currently registered for
24
- # the event. Not passed for `:console`.
25
- #
26
- # @yieldparam [Integer] total
27
- # Total number of callbacks currently registered for the event. Not
28
- # passed for `:console`.
29
- #
30
30
  # @return [Integer]
31
31
  # The subscription id, used to unsubscribe via {#off}.
32
32
  #
33
33
  # @example
34
34
  # page.on(:console) { |message| warn(message) if message.error? }
35
+ #
36
+ # @example
37
+ # page.on(:request) { |r| r.abort if r.match?(/ads/) }
38
+ # page.on(:request) { |r| r.respond(body: "stub") if r.match?(/api/) }
35
39
  def on(name, &block)
36
40
  case name
37
41
  when :console
38
42
  console.on(&block)
39
- when :request
40
- client.on("Fetch.requestPaused") do |params, index, total|
41
- request = Network::InterceptedRequest.new(client, params)
42
- exchange = network.find_or_build_exchange(request.network_id)
43
- exchange.intercepted_request = request
44
- block.call(request, index, total)
45
- end
43
+ when :request, :auth
44
+ chain(name).add(&block)
46
45
  when :response
47
- client.on("Network.loadingFinished") do |params, index, total|
46
+ client.on("Network.loadingFinished") do |params|
48
47
  exchange = network.select(params["requestId"]).last
49
- block.call(exchange, index, total) if exchange
50
- end
51
- when :auth
52
- client.on("Fetch.authRequired") do |params, index, total|
53
- request = Network::AuthRequest.new(self, params)
54
- block.call(request, index, total)
48
+ block.call(exchange) if exchange
55
49
  end
56
50
  else
57
51
  client.on(name, &block)
@@ -70,12 +64,10 @@ module Ferrum
70
64
  case name
71
65
  when :console
72
66
  console.off(id)
73
- when :request
74
- client.off("Fetch.requestPaused", id)
67
+ when :request, :auth
68
+ chain(name).remove(id)
75
69
  when :response
76
70
  client.off("Network.loadingFinished", id)
77
- when :auth
78
- client.off("Fetch.authRequired", id)
79
71
  else
80
72
  client.off(name, id)
81
73
  end
@@ -89,5 +81,30 @@ module Ferrum
89
81
  def subscribed?(event)
90
82
  client.subscribed?(event)
91
83
  end
84
+
85
+ private
86
+
87
+ def chain(name)
88
+ @chains ||= Concurrent::Hash.new
89
+ @chains[name] ||= build_chain(name)
90
+ end
91
+
92
+ def build_chain(name)
93
+ case name
94
+ when :request
95
+ Chain.new(client, "Fetch.requestPaused",
96
+ build: lambda do |params|
97
+ request = Network::InterceptedRequest.new(client, params)
98
+ exchange = network.find_or_build_exchange(request.network_id)
99
+ exchange.intercepted_request = request
100
+ request
101
+ end,
102
+ default: lambda(&:continue))
103
+ when :auth
104
+ Chain.new(client, "Fetch.authRequired",
105
+ build: ->(params) { Network::AuthRequest.new(self, params) },
106
+ default: ->(request) { request.continue(authChallengeResponse: { response: "Default" }) })
107
+ end
108
+ end
92
109
  end
93
110
  end
@@ -31,7 +31,7 @@ module Ferrum
31
31
  @frame = frame
32
32
  @page = frame.page
33
33
  @name = name
34
- @execution_id = Concurrent::MVar.new
34
+ @execution_id = Concurrency::Cell.new
35
35
  end
36
36
 
37
37
  #
@@ -40,9 +40,10 @@ module Ferrum
40
40
  #
41
41
  # @return [Integer]
42
42
  #
43
- def execution_id!
43
+ def execution_id!(timeout = nil)
44
44
  execution_id || begin
45
- id = @page.command("Page.createIsolatedWorld", frameId: @frame.id, worldName: @name)["executionContextId"]
45
+ response = @page.command("Page.createIsolatedWorld", timeout: timeout, frameId: @frame.id, worldName: @name)
46
+ id = response["executionContextId"]
46
47
  self.execution_id = id
47
48
  execution_id || id
48
49
  end
@@ -54,10 +55,7 @@ module Ferrum
54
55
  # @return [Integer, nil]
55
56
  #
56
57
  def execution_id
57
- value = @execution_id.value
58
- return if value.instance_of?(Object)
59
-
60
- value
58
+ @execution_id.value
61
59
  end
62
60
 
63
61
  #
@@ -68,11 +66,12 @@ module Ferrum
68
66
  #
69
67
  # @return [Integer, nil]
70
68
  #
69
+ # @api private
71
70
  def execution_id=(value)
72
71
  if value.nil?
73
- @execution_id.try_take!
74
- else
75
- @execution_id.try_put!(value)
72
+ @execution_id.clear
73
+ elsif !@execution_id.set?
74
+ @execution_id.set(value)
76
75
  end
77
76
  end
78
77
 
@@ -35,6 +35,8 @@ module Ferrum
35
35
  # Modifier keys currently pressed by {#hold}, in the same format as
36
36
  # {#modifiers} returns. It's 0 outside a {#hold} block.
37
37
  #
38
+ # @api private
39
+ #
38
40
  # @return [Integer]
39
41
  attr_reader :held_modifiers
40
42
 
@@ -53,6 +55,8 @@ module Ferrum
53
55
  #
54
56
  # @raise [ArgumentError] If the key is a modifier, use {#hold} instead.
55
57
  #
58
+ # @raise [TimeoutError]
59
+ #
56
60
  def down(key)
57
61
  reject_modifier(key)
58
62
  key = normalize_keys(Array(key)).first
@@ -71,6 +75,8 @@ module Ferrum
71
75
  #
72
76
  # @raise [ArgumentError] If the key is a modifier, use {#hold} instead.
73
77
  #
78
+ # @raise [TimeoutError]
79
+ #
74
80
  def up(key)
75
81
  reject_modifier(key)
76
82
  key = normalize_keys(Array(key)).first
@@ -85,15 +91,26 @@ module Ferrum
85
91
  # @param [Array<String, Symbol, (Symbol, String)>] keys
86
92
  # The text to type into a focused element, `[:Shift, "s"], "tring"`.
87
93
  #
94
+ # @param [Numeric, Deadline, nil] timeout
95
+ # How long typing all the keys may take together, the page timeout by default. Raise it for a long text.
96
+ #
88
97
  # @return [self]
89
98
  #
90
- def type(*keys)
99
+ # @raise [TimeoutError]
100
+ #
101
+ # @example
102
+ # page.keyboard.type("Hello World")
103
+ # page.keyboard.type(long_text, timeout: 30)
104
+ #
105
+ def type(*keys, timeout: nil)
91
106
  keys = normalize_keys(Array(keys))
92
107
 
93
- keys.each do |key|
94
- type = key[:text] ? "keyDown" : "rawKeyDown"
95
- @page.command("Input.dispatchKeyEvent", type: type, **key)
96
- @page.command("Input.dispatchKeyEvent", slowmoable: true, type: "keyUp", **key)
108
+ Deadline.within(timeout || @page.timeout) do
109
+ keys.each do |key|
110
+ type = key[:text] ? "keyDown" : "rawKeyDown"
111
+ @page.command("Input.dispatchKeyEvent", type: type, **key)
112
+ @page.command("Input.dispatchKeyEvent", slowmoable: true, type: "keyUp", **key)
113
+ end
97
114
  end
98
115
 
99
116
  self
@@ -112,6 +129,8 @@ module Ferrum
112
129
  #
113
130
  # @raise [ArgumentError] If no block is given or a key isn't a modifier.
114
131
  #
132
+ # @raise [TimeoutError]
133
+ #
115
134
  # @example
116
135
  # page.keyboard.hold(:shift) do
117
136
  # page.mouse.move(x: 10, y: 10).down.move(x: 200, y: 200, steps: 10).up
@@ -140,6 +159,8 @@ module Ferrum
140
159
  #
141
160
  # Returns bitfield for a given keys.
142
161
  #
162
+ # @api private
163
+ #
143
164
  # @param [Array<:alt, :ctrl, :command, :shift>] keys
144
165
  #
145
166
  # @return [Integer]
@@ -174,7 +195,7 @@ module Ferrum
174
195
  while (name = pressed.pop)
175
196
  @held_modifiers &= ~MODIFIERS[name]
176
197
  begin
177
- dispatch_modifier("keyUp", name)
198
+ Deadline.suspend { dispatch_modifier("keyUp", name) }
178
199
  rescue Error
179
200
  raise unless quietly
180
201
  end