-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
dom_world.rb
494 lines (440 loc) · 15.3 KB
/
dom_world.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
require 'thread'
# https://github.com/puppeteer/puppeteer/blob/master/src/DOMWorld.js
class Puppeteer::DOMWorld
using Puppeteer::DefineAsyncMethod
# @param {!Puppeteer.FrameManager} frameManager
# @param {!Puppeteer.Frame} frame
# @param {!Puppeteer.TimeoutSettings} timeoutSettings
def initialize(frame_manager, frame, timeout_settings)
@frame_manager = frame_manager
@frame = frame
@timeout_settings = timeout_settings
@context_promise = resolvable_future
@wait_tasks = Set.new
@detached = false
end
attr_reader :frame
# only used in Puppeteer::WaitTask#initialize
def _wait_tasks
@wait_tasks
end
# @param context [Puppeteer::ExecutionContext]
def context=(context)
if context
unless @context_promise.resolved?
@context_promise.fulfill(context)
end
@wait_tasks.each(&:async_rerun)
else
raise ArgumentError.new("context should now be nil. Use #delete_context for clearing document.")
end
end
def delete_context(execution_context_id)
@document = nil
@context_promise = resolvable_future
end
def has_context?
@context_promise.resolved?
end
def detach
@detached = true
@wait_tasks.each do |wait_task|
wait_task.terminate(Puppeteer::WaitTask::TerminatedError.new('waitForFunction failed: frame got detached.'))
end
end
class DetachedError < StandardError; end
# @return {!Promise<!Puppeteer.ExecutionContext>}
def execution_context
if @detached
raise DetachedError.new("Execution Context is not available in detached frame \"#{@frame.url}\" (are you trying to evaluate?)")
end
@context_promise.value!
end
# @param {Function|string} pageFunction
# @param {!Array<*>} args
# @return {!Promise<!Puppeteer.JSHandle>}
def evaluate_handle(page_function, *args)
execution_context.evaluate_handle(page_function, *args)
end
# @param {Function|string} pageFunction
# @param {!Array<*>} args
# @return {!Promise<*>}
def evaluate(page_function, *args)
execution_context.evaluate(page_function, *args)
end
# `$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
# @param {string} selector
# @return {!Promise<?Puppeteer.ElementHandle>}
def S(selector)
document.S(selector)
end
private def evaluate_document
# sometimes execution_context.evaluate_handle('document') returns null object.
# D, [2020-04-24T02:17:51.023631 #220] DEBUG -- : RECV << {"id"=>20, "result"=>{"result"=>{"type"=>"object", "subtype"=>"null", "value"=>nil}}, "sessionId"=>"78E9CF1E14D81294E320E7C20E5CDE06"}
# retry if so.
Timeout.timeout(3) do
loop do
handle = execution_context.evaluate_handle('document')
return handle if handle.is_a?(Puppeteer::ElementHandle)
end
end
rescue Timeout::Error
raise 'Bug of puppeteer-ruby...'
end
private def document
@document ||= evaluate_document.as_element
end
# `$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.
# @param {string} expression
# @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
def Sx(expression)
document.Sx(expression)
end
# `$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
# @param {string} selector
# @param {Function|string} pageFunction
# @param {!Array<*>} args
# @return {!Promise<(!Object|undefined)>}
def Seval(selector, page_function, *args)
document.Seval(selector, page_function, *args)
end
# `$$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
# @param {string} selector
# @param {Function|string} pageFunction
# @param {!Array<*>} args
# @return {!Promise<(!Object|undefined)>}
def SSeval(selector, page_function, *args)
document.SSeval(selector, page_function, *args)
end
# `$$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
# @param {string} selector
# @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
def SS(selector)
document.SS(selector)
end
# @return [String]
def content
evaluate(<<-JAVASCRIPT)
() => {
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
}
JAVASCRIPT
end
# @param html [String]
# @param timeout [Integer]
# @param wait_until [String|Array<String>]
def set_content(html, timeout: nil, wait_until: nil)
option_wait_until = [wait_until || 'load'].flatten
option_timeout = @timeout_settings.navigation_timeout
# We rely upon the fact that document.open() will reset frame lifecycle with "init"
# lifecycle event. @see https://crrev.com/608658
js = <<-JAVASCRIPT
(html) => {
document.open();
document.write(html);
document.close();
}
JAVASCRIPT
evaluate(js, html)
watcher = Puppeteer::LifecycleWatcher.new(@frame_manager, @frame, option_wait_until, option_timeout)
begin
await_any(
watcher.timeout_or_termination_promise,
watcher.lifecycle_promise,
)
ensure
watcher.dispose
end
end
# /**
# * @param {!{url?: string, path?: string, content?: string, type?: string}} options
# * @return {!Promise<!Puppeteer.ElementHandle>}
# */
# async addScriptTag(options) {
# const {
# url = null,
# path = null,
# content = null,
# type = ''
# } = options;
# if (url !== null) {
# try {
# const context = await this.executionContext();
# return (await context.evaluateHandle(addScriptUrl, url, type)).asElement();
# } catch (error) {
# throw new Error(`Loading script from ${url} failed`);
# }
# }
# if (path !== null) {
# let contents = await readFileAsync(path, 'utf8');
# contents += '//# sourceURL=' + path.replace(/\n/g, '');
# const context = await this.executionContext();
# return (await context.evaluateHandle(addScriptContent, contents, type)).asElement();
# }
# if (content !== null) {
# const context = await this.executionContext();
# return (await context.evaluateHandle(addScriptContent, content, type)).asElement();
# }
# throw new Error('Provide an object with a `url`, `path` or `content` property');
# /**
# * @param {string} url
# * @param {string} type
# * @return {!Promise<!HTMLElement>}
# */
# async function addScriptUrl(url, type) {
# const script = document.createElement('script');
# script.src = url;
# if (type)
# script.type = type;
# const promise = new Promise((res, rej) => {
# script.onload = res;
# script.onerror = rej;
# });
# document.head.appendChild(script);
# await promise;
# return script;
# }
# /**
# * @param {string} content
# * @param {string} type
# * @return {!HTMLElement}
# */
# function addScriptContent(content, type = 'text/javascript') {
# const script = document.createElement('script');
# script.type = type;
# script.text = content;
# let error = null;
# script.onerror = e => error = e;
# document.head.appendChild(script);
# if (error)
# throw error;
# return script;
# }
# }
# /**
# * @param {!{url?: string, path?: string, content?: string}} options
# * @return {!Promise<!Puppeteer.ElementHandle>}
# */
# async addStyleTag(options) {
# const {
# url = null,
# path = null,
# content = null
# } = options;
# if (url !== null) {
# try {
# const context = await this.executionContext();
# return (await context.evaluateHandle(addStyleUrl, url)).asElement();
# } catch (error) {
# throw new Error(`Loading style from ${url} failed`);
# }
# }
# if (path !== null) {
# let contents = await readFileAsync(path, 'utf8');
# contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
# const context = await this.executionContext();
# return (await context.evaluateHandle(addStyleContent, contents)).asElement();
# }
# if (content !== null) {
# const context = await this.executionContext();
# return (await context.evaluateHandle(addStyleContent, content)).asElement();
# }
# throw new Error('Provide an object with a `url`, `path` or `content` property');
# /**
# * @param {string} url
# * @return {!Promise<!HTMLElement>}
# */
# async function addStyleUrl(url) {
# const link = document.createElement('link');
# link.rel = 'stylesheet';
# link.href = url;
# const promise = new Promise((res, rej) => {
# link.onload = res;
# link.onerror = rej;
# });
# document.head.appendChild(link);
# await promise;
# return link;
# }
# /**
# * @param {string} content
# * @return {!Promise<!HTMLElement>}
# */
# async function addStyleContent(content) {
# const style = document.createElement('style');
# style.type = 'text/css';
# style.appendChild(document.createTextNode(content));
# const promise = new Promise((res, rej) => {
# style.onload = res;
# style.onerror = rej;
# });
# document.head.appendChild(style);
# await promise;
# return style;
# }
# }
class ElementNotFoundError < StandardError
def initialize(selector)
super("No node found for selector: #{selector}")
end
end
# @param selector [String]
# @param delay [Number]
# @param button [String] "left"|"right"|"middle"
# @param click_count [Number]
def click(selector, delay: nil, button: nil, click_count: nil)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.click(delay: delay, button: button, click_count: click_count)
handle.dispose
end
# @param selector [String]
def focus(selector)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.focus
handle.dispose
end
# /**
# * @param {string} selector
# */
# async hover(selector) {
# const handle = await this.$(selector);
# assert(handle, 'No node found for selector: ' + selector);
# await handle.hover();
# await handle.dispose();
# }
# @param selector [String]
# @return [Array<String>]
def select(selector, *values)
handle = S(selector) or raise ElementNotFoundError.new(selector)
result = handle.select(*values)
handle.dispose
result
end
# @param selector [String]
def tap(selector)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.tap
handle.dispose
end
# @param selector [String]
# @param text [String]
# @param delay [Number]
def type_text(selector, text, delay: nil)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.type_text(text, delay: delay)
handle.dispose
end
# @param selector [String]
# @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false.
# @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false.
# @param timeout [Integer]
def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil)
wait_for_selector_or_xpath(selector, false, visible: visible, hidden: hidden, timeout: timeout)
end
# @param xpath [String]
# @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false.
# @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false.
# @param timeout [Integer]
def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil)
wait_for_selector_or_xpath(xpath, true, visible: visible, hidden: hidden, timeout: timeout)
end
# /**
# * @param {Function|string} pageFunction
# * @param {!{polling?: string|number, timeout?: number}=} options
# * @return {!Promise<!Puppeteer.JSHandle>}
# */
# waitForFunction(pageFunction, options = {}, ...args) {
# const {
# polling = 'raf',
# timeout = this._timeoutSettings.timeout(),
# } = options;
# return new WaitTask(this, pageFunction, 'function', polling, timeout, ...args).promise;
# }
# @param page_function [String]
# @param args [Array]
# @param polling [Integer|String]
# @param timeout [Integer]
# @return [Puppeteer::JSHandle]
def wait_for_function(page_function, args: [], polling: nil, timeout: nil)
option_polling = polling || 'raf'
option_timeout = timeout || @timeout_settings.timeout
Puppeteer::WaitTask.new(
dom_world: self,
predicate_body: page_function,
title: 'function',
polling: option_polling,
timeout: option_timeout,
args: args,
).await_promise
end
# @return [String]
def title
evaluate('() => document.title')
end
# @param selector_or_xpath [String]
# @param is_xpath [Boolean]
# @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false.
# @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false.
# @param timeout [Integer]
private def wait_for_selector_or_xpath(selector_or_xpath, is_xpath, visible: nil, hidden: nil, timeout: nil)
option_wait_for_visible = visible || false
option_wait_for_hidden = hidden || false
option_timeout = timeout || @timeout_settings.timeout
polling =
if option_wait_for_visible || option_wait_for_hidden
'raf'
else
'mutation'
end
title = "#{is_xpath ? :XPath : :selector} #{selector_or_xpath}#{option_wait_for_hidden ? 'to be hidden' : ''}"
wait_task = Puppeteer::WaitTask.new(
dom_world: self,
predicate_body: PREDICATE,
title: title,
polling: polling,
timeout: option_timeout,
args: [selector_or_xpath, is_xpath, option_wait_for_visible, option_wait_for_hidden],
)
handle = wait_task.await_promise
unless handle.as_element
handle.dispose
return nil
end
handle.as_element
end
PREDICATE = <<~JAVASCRIPT
/**
* @param {string} selectorOrXPath
* @param {boolean} isXPath
* @param {boolean} waitForVisible
* @param {boolean} waitForHidden
* @return {?Node|boolean}
*/
function _(selectorOrXPath, isXPath, waitForVisible, waitForHidden) {
const node = isXPath
? document.evaluate(selectorOrXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
: document.querySelector(selectorOrXPath);
if (!node)
return waitForHidden;
if (!waitForVisible && !waitForHidden)
return node;
const element = /** @type {Element} */ (node.nodeType === Node.TEXT_NODE ? node.parentElement : node);
const style = window.getComputedStyle(element);
const isVisible = style && style.visibility !== 'hidden' && hasVisibleBoundingBox();
const success = (waitForVisible === isVisible || waitForHidden === !isVisible);
return success ? node : null;
/**
* @return {boolean}
*/
function hasVisibleBoundingBox() {
const rect = element.getBoundingClientRect();
return !!(rect.top || rect.bottom || rect.width || rect.height);
}
}
JAVASCRIPT
end