puppeteer-ruby 0.0.9 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +0 -12
- data/.github/workflows/docs.yml +45 -0
- data/.github/workflows/reviewdog.yml +15 -0
- data/README.md +54 -1
- data/lib/puppeteer.rb +37 -13
- data/lib/puppeteer/browser.rb +40 -27
- data/lib/puppeteer/cdp_session.rb +3 -19
- data/lib/puppeteer/concurrent_ruby_utils.rb +12 -2
- data/lib/puppeteer/connection.rb +16 -16
- data/lib/puppeteer/define_async_method.rb +23 -0
- data/lib/puppeteer/dom_world.rb +17 -33
- data/lib/puppeteer/element_handle.rb +80 -151
- data/lib/puppeteer/element_handle/bounding_box.rb +12 -0
- data/lib/puppeteer/element_handle/box_model.rb +19 -0
- data/lib/puppeteer/element_handle/point.rb +26 -0
- data/lib/puppeteer/emulation_manager.rb +2 -6
- data/lib/puppeteer/errors.rb +1 -3
- data/lib/puppeteer/event_callbackable.rb +11 -0
- data/lib/puppeteer/execution_context.rb +1 -6
- data/lib/puppeteer/frame.rb +34 -1
- data/lib/puppeteer/frame_manager.rb +7 -25
- data/lib/puppeteer/js_handle.rb +3 -12
- data/lib/puppeteer/keyboard.rb +6 -27
- data/lib/puppeteer/launcher.rb +6 -6
- data/lib/puppeteer/launcher/chrome.rb +10 -8
- data/lib/puppeteer/mouse.rb +8 -33
- data/lib/puppeteer/page.rb +82 -67
- data/lib/puppeteer/remote_object.rb +11 -5
- data/lib/puppeteer/target.rb +10 -13
- data/lib/puppeteer/touch_screen.rb +2 -7
- data/lib/puppeteer/version.rb +1 -1
- data/lib/puppeteer/viewport.rb +18 -0
- data/lib/puppeteer/wait_task.rb +2 -4
- data/lib/puppeteer/web_socket.rb +3 -1
- data/lib/puppeteer/web_socket_transport.rb +8 -8
- data/puppeteer-ruby.gemspec +1 -1
- data/puppeteer-ruby.png +0 -0
- metadata +11 -102
- data/Dockerfile +0 -6
- data/docker-compose.yml +0 -15
- data/docs/Puppeteer.html +0 -2020
- data/docs/Puppeteer/AsyncAwaitBehavior.html +0 -105
- data/docs/Puppeteer/Browser.html +0 -2148
- data/docs/Puppeteer/BrowserContext.html +0 -809
- data/docs/Puppeteer/BrowserFetcher.html +0 -214
- data/docs/Puppeteer/BrowserRunner.html +0 -914
- data/docs/Puppeteer/BrowserRunner/BrowserProcess.html +0 -477
- data/docs/Puppeteer/CDPSession.html +0 -813
- data/docs/Puppeteer/CDPSession/Error.html +0 -124
- data/docs/Puppeteer/ConcurrentRubyUtils.html +0 -430
- data/docs/Puppeteer/Connection.html +0 -960
- data/docs/Puppeteer/Connection/MessageCallback.html +0 -434
- data/docs/Puppeteer/Connection/ProtocolError.html +0 -216
- data/docs/Puppeteer/Connection/RequestDebugPrinter.html +0 -217
- data/docs/Puppeteer/Connection/ResponseDebugPrinter.html +0 -244
- data/docs/Puppeteer/ConsoleMessage.html +0 -565
- data/docs/Puppeteer/ConsoleMessage/Location.html +0 -433
- data/docs/Puppeteer/DOMWorld.html +0 -2219
- data/docs/Puppeteer/DOMWorld/DetachedError.html +0 -124
- data/docs/Puppeteer/DOMWorld/DocumentEvaluationError.html +0 -124
- data/docs/Puppeteer/DebugPrint.html +0 -233
- data/docs/Puppeteer/Device.html +0 -470
- data/docs/Puppeteer/Devices.html +0 -139
- data/docs/Puppeteer/ElementHandle.html +0 -2224
- data/docs/Puppeteer/ElementHandle/ElementNotFoundError.html +0 -206
- data/docs/Puppeteer/ElementHandle/ElementNotVisibleError.html +0 -206
- data/docs/Puppeteer/ElementHandle/Point.html +0 -481
- data/docs/Puppeteer/ElementHandle/ScrollIntoViewError.html +0 -124
- data/docs/Puppeteer/EmulationManager.html +0 -454
- data/docs/Puppeteer/EventCallbackable.html +0 -433
- data/docs/Puppeteer/EventCallbackable/EventListeners.html +0 -435
- data/docs/Puppeteer/ExecutionContext.html +0 -998
- data/docs/Puppeteer/ExecutionContext/EvaluationError.html +0 -124
- data/docs/Puppeteer/ExecutionContext/JavaScriptExpression.html +0 -357
- data/docs/Puppeteer/ExecutionContext/JavaScriptFunction.html +0 -389
- data/docs/Puppeteer/FileChooser.html +0 -455
- data/docs/Puppeteer/Frame.html +0 -3677
- data/docs/Puppeteer/FrameManager.html +0 -2410
- data/docs/Puppeteer/FrameManager/NavigationError.html +0 -124
- data/docs/Puppeteer/IfPresent.html +0 -222
- data/docs/Puppeteer/JSHandle.html +0 -1352
- data/docs/Puppeteer/Keyboard.html +0 -1557
- data/docs/Puppeteer/Keyboard/KeyDefinition.html +0 -831
- data/docs/Puppeteer/Keyboard/KeyDescription.html +0 -603
- data/docs/Puppeteer/Launcher.html +0 -237
- data/docs/Puppeteer/Launcher/Base.html +0 -385
- data/docs/Puppeteer/Launcher/Base/ExecutablePathNotFound.html +0 -124
- data/docs/Puppeteer/Launcher/BrowserOptions.html +0 -441
- data/docs/Puppeteer/Launcher/Chrome.html +0 -669
- data/docs/Puppeteer/Launcher/Chrome/DefaultArgs.html +0 -382
- data/docs/Puppeteer/Launcher/ChromeArgOptions.html +0 -531
- data/docs/Puppeteer/Launcher/LaunchOptions.html +0 -893
- data/docs/Puppeteer/LifecycleWatcher.html +0 -834
- data/docs/Puppeteer/LifecycleWatcher/ExpectedLifecycle.html +0 -363
- data/docs/Puppeteer/LifecycleWatcher/FrameDetachedError.html +0 -206
- data/docs/Puppeteer/LifecycleWatcher/TerminatedError.html +0 -124
- data/docs/Puppeteer/Mouse.html +0 -1105
- data/docs/Puppeteer/Mouse/Button.html +0 -136
- data/docs/Puppeteer/NetworkManager.html +0 -901
- data/docs/Puppeteer/NetworkManager/Credentials.html +0 -385
- data/docs/Puppeteer/Page.html +0 -5970
- data/docs/Puppeteer/Page/FileChooserTimeoutError.html +0 -206
- data/docs/Puppeteer/Page/ScreenshotOptions.html +0 -845
- data/docs/Puppeteer/Page/ScriptTag.html +0 -555
- data/docs/Puppeteer/Page/StyleTag.html +0 -448
- data/docs/Puppeteer/Page/TargetCrashedError.html +0 -124
- data/docs/Puppeteer/RemoteObject.html +0 -1016
- data/docs/Puppeteer/Target.html +0 -1314
- data/docs/Puppeteer/Target/InitializeFailure.html +0 -124
- data/docs/Puppeteer/Target/TargetInfo.html +0 -729
- data/docs/Puppeteer/TimeoutError.html +0 -135
- data/docs/Puppeteer/TimeoutSettings.html +0 -496
- data/docs/Puppeteer/TouchScreen.html +0 -464
- data/docs/Puppeteer/Viewport.html +0 -757
- data/docs/Puppeteer/WaitTask.html +0 -637
- data/docs/Puppeteer/WaitTask/TerminatedError.html +0 -124
- data/docs/Puppeteer/WaitTask/TimeoutError.html +0 -206
- data/docs/Puppeteer/WebSocket.html +0 -673
- data/docs/Puppeteer/WebSocket/DriverImpl.html +0 -412
- data/docs/Puppeteer/WebSocketTransport.html +0 -600
- data/docs/Puppeteer/WebSocktTransportError.html +0 -124
- data/docs/_index.html +0 -809
- data/docs/class_list.html +0 -51
- data/docs/css/common.css +0 -1
- data/docs/css/full_list.css +0 -58
- data/docs/css/style.css +0 -496
- data/docs/file.README.html +0 -123
- data/docs/file_list.html +0 -56
- data/docs/frames.html +0 -17
- data/docs/index.html +0 -123
- data/docs/js/app.js +0 -314
- data/docs/js/full_list.js +0 -216
- data/docs/js/jquery.js +0 -4
- data/docs/method_list.html +0 -3971
- data/docs/top-level-namespace.html +0 -126
- data/lib/puppeteer/async_await_behavior.rb +0 -38
@@ -1,5 +1,8 @@
|
|
1
1
|
# utility methods for Concurrent::Promises.
|
2
2
|
module Puppeteer::ConcurrentRubyUtils
|
3
|
+
# wait for all promises.
|
4
|
+
# REMARK: This method doesn't assure the order of calling.
|
5
|
+
# for example, await_all(async1, async2) calls calls2 -> calls1 often.
|
3
6
|
def await_all(*args)
|
4
7
|
if args.length == 1 && args[0].is_a?(Enumerable)
|
5
8
|
Concurrent::Promises.zip(*(args[0])).value!
|
@@ -8,6 +11,9 @@ module Puppeteer::ConcurrentRubyUtils
|
|
8
11
|
end
|
9
12
|
end
|
10
13
|
|
14
|
+
# wait for first promises.
|
15
|
+
# REMARK: This method doesn't assure the order of calling.
|
16
|
+
# for example, await_all(async1, async2) calls calls2 -> calls1 often.
|
11
17
|
def await_any(*args)
|
12
18
|
if args.length == 1 && args[0].is_a?(Enumerable)
|
13
19
|
Concurrent::Promises.any(*(args[0])).value!
|
@@ -29,8 +35,12 @@ module Puppeteer::ConcurrentRubyUtils
|
|
29
35
|
Concurrent::Promises.future(&block)
|
30
36
|
end
|
31
37
|
|
32
|
-
def resolvable_future
|
33
|
-
Concurrent::Promises.resolvable_future
|
38
|
+
def resolvable_future(&block)
|
39
|
+
future = Concurrent::Promises.resolvable_future
|
40
|
+
if block
|
41
|
+
block.call(future)
|
42
|
+
end
|
43
|
+
future
|
34
44
|
end
|
35
45
|
end
|
36
46
|
|
data/lib/puppeteer/connection.rb
CHANGED
@@ -3,7 +3,7 @@ require 'json'
|
|
3
3
|
class Puppeteer::Connection
|
4
4
|
include Puppeteer::DebugPrint
|
5
5
|
include Puppeteer::EventCallbackable
|
6
|
-
using Puppeteer::
|
6
|
+
using Puppeteer::DefineAsyncMethod
|
7
7
|
|
8
8
|
class ProtocolError < StandardError
|
9
9
|
def initialize(method:, error_message:, error_data: nil)
|
@@ -44,7 +44,9 @@ class Puppeteer::Connection
|
|
44
44
|
|
45
45
|
@transport = transport
|
46
46
|
@transport.on_message do |data|
|
47
|
-
|
47
|
+
message = JSON.parse(data)
|
48
|
+
sleep_before_handling_message(message)
|
49
|
+
async_handle_message(message)
|
48
50
|
end
|
49
51
|
@transport.on_close do |reason, code|
|
50
52
|
handle_close(reason, code)
|
@@ -54,6 +56,16 @@ class Puppeteer::Connection
|
|
54
56
|
@closed = false
|
55
57
|
end
|
56
58
|
|
59
|
+
private def sleep_before_handling_message(message)
|
60
|
+
# Puppeteer doesn't handle any Network monitoring responses.
|
61
|
+
# So we don't have to sleep.
|
62
|
+
return if message['method']&.start_with?('Network.')
|
63
|
+
|
64
|
+
# For some reasons, sleeping a bit reduces trivial errors...
|
65
|
+
# 4ms is an interval of internal shared timer of WebKit.
|
66
|
+
sleep 0.004
|
67
|
+
end
|
68
|
+
|
57
69
|
def self.from_session(session)
|
58
70
|
session.connection
|
59
71
|
end
|
@@ -198,9 +210,7 @@ class Puppeteer::Connection
|
|
198
210
|
end
|
199
211
|
end
|
200
212
|
|
201
|
-
private
|
202
|
-
handle_message(message)
|
203
|
-
end
|
213
|
+
private define_async_method :async_handle_message
|
204
214
|
|
205
215
|
private def handle_close
|
206
216
|
return if @closed
|
@@ -239,16 +249,6 @@ class Puppeteer::Connection
|
|
239
249
|
def create_session(target_info)
|
240
250
|
result = send_message('Target.attachToTarget', targetId: target_info.target_id, flatten: true)
|
241
251
|
session_id = result['sessionId']
|
242
|
-
|
243
|
-
# Target.attachedToTarget is often notified after the result of Target.attachToTarget.
|
244
|
-
# D, [2020-04-04T23:04:30.736311 #91875] DEBUG -- : RECV << {"id"=>2, "result"=>{"sessionId"=>"DA002F8A95B04710502CB40D8430B95A"}}
|
245
|
-
# D, [2020-04-04T23:04:30.736649 #91875] DEBUG -- : RECV << {"method"=>"Target.attachedToTarget", "params"=>{"sessionId"=>"DA002F8A95B04710502CB40D8430B95A", "targetInfo"=>{"targetId"=>"EBAB949A7DE63F12CB94268AD3A9976B", "type"=>"page", "title"=>"about:blank", "url"=>"about:blank", "attached"=>true, "browserContextId"=>"46D23767E9B79DD9E589101121F6DADD"}, "waitingForDebugger"=>false}}
|
246
|
-
# So we have to wait for "Target.attachedToTarget" a bit.
|
247
|
-
20.times do
|
248
|
-
if @sessions[session_id]
|
249
|
-
return @sessions[session_id]
|
250
|
-
end
|
251
|
-
sleep 0.1
|
252
|
-
end
|
252
|
+
@sessions[session_id]
|
253
253
|
end
|
254
254
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Puppeteer::DefineAsyncMethod
|
2
|
+
refine Class do
|
3
|
+
def define_async_method(async_method_name)
|
4
|
+
unless async_method_name.to_s.start_with?('async_')
|
5
|
+
raise ArgumentError.new('async method name should start with "async_"')
|
6
|
+
end
|
7
|
+
|
8
|
+
if method_defined?(async_method_name) || private_method_defined?(async_method_name)
|
9
|
+
raise ArgumentError.new("#{async_method_name} is already defined")
|
10
|
+
end
|
11
|
+
|
12
|
+
original_method = instance_method(async_method_name[6..-1])
|
13
|
+
define_method(async_method_name) do |*args|
|
14
|
+
Concurrent::Promises.future do
|
15
|
+
original_method.bind(self).call(*args)
|
16
|
+
rescue => err
|
17
|
+
Logger.new(STDERR).warn(err)
|
18
|
+
raise err
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/puppeteer/dom_world.rb
CHANGED
@@ -2,7 +2,7 @@ require 'thread'
|
|
2
2
|
|
3
3
|
# https://github.com/puppeteer/puppeteer/blob/master/src/DOMWorld.js
|
4
4
|
class Puppeteer::DOMWorld
|
5
|
-
using Puppeteer::
|
5
|
+
using Puppeteer::DefineAsyncMethod
|
6
6
|
|
7
7
|
# @param {!Puppeteer.FrameManager} frameManager
|
8
8
|
# @param {!Puppeteer.Frame} frame
|
@@ -12,7 +12,6 @@ class Puppeteer::DOMWorld
|
|
12
12
|
@frame = frame
|
13
13
|
@timeout_settings = timeout_settings
|
14
14
|
@context_promise = resolvable_future
|
15
|
-
@pending_destroy = []
|
16
15
|
@wait_tasks = Set.new
|
17
16
|
@detached = false
|
18
17
|
end
|
@@ -24,22 +23,12 @@ class Puppeteer::DOMWorld
|
|
24
23
|
@wait_tasks
|
25
24
|
end
|
26
25
|
|
27
|
-
# @param
|
26
|
+
# @param context [Puppeteer::ExecutionContext]
|
28
27
|
def context=(context)
|
29
|
-
# D, [2020-04-12T22:45:03.938754 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextCreated", "params"=>{"context"=>{"id"=>3, "origin"=>"https://github.com", "name"=>"", "auxData"=>{"isDefault"=>true, "type"=>"default", "frameId"=>"3AD7F1E82BCBA88BFE31D03BC49FF6CB"}}}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
|
30
|
-
# D, [2020-04-12T22:45:03.938856 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextCreated", "params"=>{"context"=>{"id"=>4, "origin"=>"://", "name"=>"__puppeteer_utility_world__", "auxData"=>{"isDefault"=>false, "type"=>"isolated", "frameId"=>"3AD7F1E82BCBA88BFE31D03BC49FF6CB"}}}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
|
31
|
-
# D, [2020-04-12T22:45:03.938960 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextDestroyed", "params"=>{"executionContextId"=>1}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
|
32
|
-
# D, [2020-04-12T22:45:03.939110 #46154] DEBUG -- : RECV << {"method"=>"Page.frameNavigated", "params"=>{"frame"=>{"id"=>"3AD7F1E82BCBA88BFE31D03BC49FF6CB", "loaderId"=>"301B349884E582986C502CBE020966DF", "url"=>"https://github.com/", "securityOrigin"=>"https://github.com", "mimeType"=>"text/html"}}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
|
33
|
-
# D, [2020-04-12T22:45:03.939793 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextDestroyed", "params"=>{"executionContextId"=>2}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
|
34
|
-
# executionContextDestroyed is often notified after executionContextCreated.
|
35
|
-
|
36
28
|
if context
|
37
|
-
|
38
|
-
@
|
39
|
-
@document = nil
|
40
|
-
@context_promise = resolvable_future
|
29
|
+
unless @context_promise.resolved?
|
30
|
+
@context_promise.fulfill(context)
|
41
31
|
end
|
42
|
-
@context_promise.fulfill(context)
|
43
32
|
@wait_tasks.each(&:async_rerun)
|
44
33
|
else
|
45
34
|
raise ArgumentError.new("context should now be nil. Use #delete_context for clearing document.")
|
@@ -47,12 +36,8 @@ class Puppeteer::DOMWorld
|
|
47
36
|
end
|
48
37
|
|
49
38
|
def delete_context(execution_context_id)
|
50
|
-
|
51
|
-
|
52
|
-
else
|
53
|
-
@document = nil
|
54
|
-
@context_promise = resolvable_future
|
55
|
-
end
|
39
|
+
@document = nil
|
40
|
+
@context_promise = resolvable_future
|
56
41
|
end
|
57
42
|
|
58
43
|
def has_context?
|
@@ -97,17 +82,18 @@ class Puppeteer::DOMWorld
|
|
97
82
|
document.S(selector)
|
98
83
|
end
|
99
84
|
|
100
|
-
class DocumentEvaluationError < StandardError; end
|
101
|
-
|
102
85
|
private def evaluate_document
|
103
86
|
# sometimes execution_context.evaluate_handle('document') returns null object.
|
104
87
|
# D, [2020-04-24T02:17:51.023631 #220] DEBUG -- : RECV << {"id"=>20, "result"=>{"result"=>{"type"=>"object", "subtype"=>"null", "value"=>nil}}, "sessionId"=>"78E9CF1E14D81294E320E7C20E5CDE06"}
|
105
88
|
# retry if so.
|
106
|
-
|
107
|
-
|
108
|
-
|
89
|
+
Timeout.timeout(3) do
|
90
|
+
loop do
|
91
|
+
handle = execution_context.evaluate_handle('document')
|
92
|
+
return handle if handle.is_a?(Puppeteer::ElementHandle)
|
93
|
+
end
|
109
94
|
end
|
110
|
-
|
95
|
+
rescue Timeout::Error
|
96
|
+
raise 'Bug of puppeteer-ruby...'
|
111
97
|
end
|
112
98
|
|
113
99
|
private def document
|
@@ -410,12 +396,10 @@ class Puppeteer::DOMWorld
|
|
410
396
|
# return new WaitTask(this, pageFunction, 'function', polling, timeout, ...args).promise;
|
411
397
|
# }
|
412
398
|
|
413
|
-
#
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
# return this.evaluate(() => document.title);
|
418
|
-
# }
|
399
|
+
# @return [String]
|
400
|
+
def title
|
401
|
+
evaluate('() => document.title')
|
402
|
+
end
|
419
403
|
|
420
404
|
# @param selector_or_xpath [String]
|
421
405
|
# @param is_xpath [Boolean]
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
require_relative './element_handle/bounding_box'
|
2
|
+
require_relative './element_handle/box_model'
|
3
|
+
require_relative './element_handle/point'
|
2
4
|
|
3
5
|
class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
4
6
|
include Puppeteer::IfPresent
|
5
|
-
using Puppeteer::
|
7
|
+
using Puppeteer::DefineAsyncMethod
|
6
8
|
|
7
9
|
# @param context [Puppeteer::ExecutionContext]
|
8
10
|
# @param client [Puppeteer::CDPSession]
|
@@ -53,29 +55,6 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
53
55
|
sleep 0.16
|
54
56
|
end
|
55
57
|
|
56
|
-
class Point
|
57
|
-
def initialize(x:, y:)
|
58
|
-
@x = x
|
59
|
-
@y = y
|
60
|
-
end
|
61
|
-
|
62
|
-
def +(other)
|
63
|
-
Point.new(
|
64
|
-
x: @x + other.x,
|
65
|
-
y: @y + other.y,
|
66
|
-
)
|
67
|
-
end
|
68
|
-
|
69
|
-
def /(num)
|
70
|
-
Point.new(
|
71
|
-
x: @x / num,
|
72
|
-
y: @y / num,
|
73
|
-
)
|
74
|
-
end
|
75
|
-
|
76
|
-
attr_reader :x, :y
|
77
|
-
end
|
78
|
-
|
79
58
|
class ElementNotVisibleError < StandardError
|
80
59
|
def initialize
|
81
60
|
super("Node is either not visible or not an HTMLElement")
|
@@ -105,15 +84,6 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
105
84
|
quads.first.reduce(:+) / 4
|
106
85
|
end
|
107
86
|
|
108
|
-
# /**
|
109
|
-
# * @return {!Promise<void|Protocol.DOM.getBoxModelReturnValue>}
|
110
|
-
# */
|
111
|
-
# _getBoxModel() {
|
112
|
-
# return this._client.send('DOM.getBoxModel', {
|
113
|
-
# objectId: this._remoteObject.objectId
|
114
|
-
# }).catch(error => debugError(error));
|
115
|
-
# }
|
116
|
-
|
117
87
|
# @param quad [Array<number>]
|
118
88
|
# @return [Array<Point>]
|
119
89
|
private def from_protocol_quad(quad)
|
@@ -135,11 +105,11 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
135
105
|
end
|
136
106
|
end
|
137
107
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
108
|
+
def hover
|
109
|
+
scroll_into_view_if_needed
|
110
|
+
point = clickable_point
|
111
|
+
@page.mouse.move(point.x, point.y)
|
112
|
+
end
|
143
113
|
|
144
114
|
# @param delay [Number]
|
145
115
|
# @param button [String] "left"|"right"|"middle"
|
@@ -150,12 +120,7 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
150
120
|
@page.mouse.click(point.x, point.y, delay: delay, button: button, click_count: click_count)
|
151
121
|
end
|
152
122
|
|
153
|
-
|
154
|
-
# @param button [String] "left"|"right"|"middle"
|
155
|
-
# @param click_count [Number]
|
156
|
-
async def async_click(delay: nil, button: nil, click_count: nil)
|
157
|
-
click(delay: delay, button: button, click_count: click_count)
|
158
|
-
end
|
123
|
+
define_async_method :async_click
|
159
124
|
|
160
125
|
# @return [Array<String>]
|
161
126
|
def select(*values)
|
@@ -225,17 +190,13 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
225
190
|
@page.touchscreen.tap(point.x, point.y)
|
226
191
|
end
|
227
192
|
|
228
|
-
|
229
|
-
tap
|
230
|
-
end
|
193
|
+
define_async_method :async_tap
|
231
194
|
|
232
195
|
def focus
|
233
196
|
evaluate('element => element.focus()')
|
234
197
|
end
|
235
198
|
|
236
|
-
|
237
|
-
focus
|
238
|
-
end
|
199
|
+
define_async_method :async_focus
|
239
200
|
|
240
201
|
# @param text [String]
|
241
202
|
# @param delay [number|nil]
|
@@ -244,12 +205,7 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
244
205
|
@page.keyboard.type_text(text, delay: delay)
|
245
206
|
end
|
246
207
|
|
247
|
-
|
248
|
-
# @param delay [number|nil]
|
249
|
-
# @return [Future]
|
250
|
-
async def async_type_text(text, delay: nil)
|
251
|
-
type_text(text, delay: delay)
|
252
|
-
end
|
208
|
+
define_async_method :async_type_text
|
253
209
|
|
254
210
|
# @param key [String]
|
255
211
|
# @param delay [number|nil]
|
@@ -258,96 +214,79 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
258
214
|
@page.keyboard.press(key, delay: delay)
|
259
215
|
end
|
260
216
|
|
261
|
-
|
262
|
-
# @param delay [number|nil]
|
263
|
-
# @return [Future]
|
264
|
-
async def async_press(key, delay: nil)
|
265
|
-
press(key, delay: delay)
|
266
|
-
end
|
267
|
-
|
268
|
-
# /**
|
269
|
-
# * @return {!Promise<?{x: number, y: number, width: number, height: number}>}
|
270
|
-
# */
|
271
|
-
# async boundingBox() {
|
272
|
-
# const result = await this._getBoxModel();
|
273
|
-
|
274
|
-
# if (!result)
|
275
|
-
# return null;
|
276
|
-
|
277
|
-
# const quad = result.model.border;
|
278
|
-
# const x = Math.min(quad[0], quad[2], quad[4], quad[6]);
|
279
|
-
# const y = Math.min(quad[1], quad[3], quad[5], quad[7]);
|
280
|
-
# const width = Math.max(quad[0], quad[2], quad[4], quad[6]) - x;
|
281
|
-
# const height = Math.max(quad[1], quad[3], quad[5], quad[7]) - y;
|
282
|
-
|
283
|
-
# return {x, y, width, height};
|
284
|
-
# }
|
285
|
-
|
286
|
-
# /**
|
287
|
-
# * @return {!Promise<?BoxModel>}
|
288
|
-
# */
|
289
|
-
# async boxModel() {
|
290
|
-
# const result = await this._getBoxModel();
|
291
|
-
|
292
|
-
# if (!result)
|
293
|
-
# return null;
|
294
|
-
|
295
|
-
# const {content, padding, border, margin, width, height} = result.model;
|
296
|
-
# return {
|
297
|
-
# content: this._fromProtocolQuad(content),
|
298
|
-
# padding: this._fromProtocolQuad(padding),
|
299
|
-
# border: this._fromProtocolQuad(border),
|
300
|
-
# margin: this._fromProtocolQuad(margin),
|
301
|
-
# width,
|
302
|
-
# height
|
303
|
-
# };
|
304
|
-
# }
|
217
|
+
define_async_method :async_press
|
305
218
|
|
306
|
-
#
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
# */
|
311
|
-
# async screenshot(options = {}) {
|
312
|
-
# let needsViewportReset = false;
|
313
|
-
|
314
|
-
# let boundingBox = await this.boundingBox();
|
315
|
-
# assert(boundingBox, 'Node is either not visible or not an HTMLElement');
|
219
|
+
# @return [BoundingBox|nil]
|
220
|
+
def bounding_box
|
221
|
+
if_present(box_model) do |result_model|
|
222
|
+
quads = result_model.border
|
316
223
|
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
224
|
+
x = quads.map(&:x).min
|
225
|
+
y = quads.map(&:y).min
|
226
|
+
BoundingBox.new(
|
227
|
+
x: x,
|
228
|
+
y: y,
|
229
|
+
width: quads.map(&:x).max - x,
|
230
|
+
height: quads.map(&:y).max - y,
|
231
|
+
)
|
232
|
+
end
|
233
|
+
end
|
325
234
|
|
326
|
-
#
|
327
|
-
|
235
|
+
# @return [BoxModel|nil]
|
236
|
+
def box_model
|
237
|
+
if_present(@remote_object.box_model(@client)) do |result|
|
238
|
+
BoxModel.new(result['model'])
|
239
|
+
end
|
240
|
+
end
|
328
241
|
|
329
|
-
|
242
|
+
def screenshot(options = {})
|
243
|
+
needs_viewport_reset = false
|
330
244
|
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
245
|
+
box = bounding_box
|
246
|
+
unless box
|
247
|
+
raise ElementNotVisibleError.new
|
248
|
+
end
|
335
249
|
|
336
|
-
|
250
|
+
viewport = @page.viewport
|
251
|
+
if viewport && (box.width > viewport.width || box.height > viewport.height)
|
252
|
+
new_viewport = viewport.merge(
|
253
|
+
width: [viewport.width, box.width.to_i].min,
|
254
|
+
height: [viewport.height, box.height.to_i].min,
|
255
|
+
)
|
256
|
+
@page.viewport = new_viewport
|
337
257
|
|
338
|
-
|
339
|
-
|
340
|
-
|
258
|
+
needs_viewport_reset = true
|
259
|
+
end
|
260
|
+
scroll_into_view_if_needed
|
341
261
|
|
342
|
-
|
343
|
-
|
344
|
-
|
262
|
+
box = bounding_box
|
263
|
+
unless box
|
264
|
+
raise ElementNotVisibleError.new
|
265
|
+
end
|
266
|
+
if box.width == 0
|
267
|
+
raise 'Node has 0 width.'
|
268
|
+
end
|
269
|
+
if box.height == 0
|
270
|
+
raise 'Node has 0 height.'
|
271
|
+
end
|
345
272
|
|
346
|
-
|
347
|
-
|
273
|
+
layout_metrics = @client.send_message('Page.getLayoutMetrics')
|
274
|
+
page_x = layout_metrics["layoutViewport"]["pageX"]
|
275
|
+
page_y = layout_metrics["layoutViewport"]["pageY"]
|
276
|
+
|
277
|
+
clip = {
|
278
|
+
x: page_x + box.x,
|
279
|
+
y: page_y + box.y,
|
280
|
+
width: box.width,
|
281
|
+
height: box.height,
|
282
|
+
}
|
348
283
|
|
349
|
-
|
350
|
-
|
284
|
+
@page.screenshot({ clip: clip }.merge(options))
|
285
|
+
ensure
|
286
|
+
if needs_viewport_reset
|
287
|
+
@page.viewport = viewport
|
288
|
+
end
|
289
|
+
end
|
351
290
|
|
352
291
|
# `$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
|
353
292
|
# @param selector [String]
|
@@ -398,13 +337,7 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
398
337
|
result
|
399
338
|
end
|
400
339
|
|
401
|
-
|
402
|
-
# @param selector [String]
|
403
|
-
# @param page_function [String]
|
404
|
-
# @return [Object]
|
405
|
-
async def async_Seval(selector, page_function, *args)
|
406
|
-
Seval(selector, page_function, *args)
|
407
|
-
end
|
340
|
+
define_async_method :async_Seval
|
408
341
|
|
409
342
|
# `$$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
|
410
343
|
# @param selector [String]
|
@@ -421,13 +354,7 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
421
354
|
result
|
422
355
|
end
|
423
356
|
|
424
|
-
|
425
|
-
# @param selector [String]
|
426
|
-
# @param page_function [String]
|
427
|
-
# @return [Object]
|
428
|
-
async def async_SSeval(selector, page_function, *args)
|
429
|
-
SSeval(selector, page_function, *args)
|
430
|
-
end
|
357
|
+
define_async_method :async_SSeval
|
431
358
|
|
432
359
|
# `$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.
|
433
360
|
# @param expression [String]
|
@@ -450,6 +377,8 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
|
|
450
377
|
properties.values.map(&:as_element).compact
|
451
378
|
end
|
452
379
|
|
380
|
+
define_async_method :async_Sx
|
381
|
+
|
453
382
|
# /**
|
454
383
|
# * @returns {!Promise<boolean>}
|
455
384
|
# */
|