Class: Puppeteer::DOMWorld
- Inherits:
-
Object
- Object
- Puppeteer::DOMWorld
show all
- Defined in:
- lib/puppeteer/dom_world.rb
Overview
Defined Under Namespace
Classes: DetachedError, ElementNotFoundError
Constant Summary
collapse
- 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
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#_wait_tasks ⇒ Object
only used in Puppeteer::WaitTask#initialize.
-
#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object
-
#content ⇒ String
-
#context=(context) ⇒ Object
-
#delete_context(execution_context_id) ⇒ Object
-
#detach ⇒ Object
-
#evaluate(page_function, *args) ⇒ !Promise<*>
-
#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>
-
#execution_context ⇒ !Promise<!Puppeteer.ExecutionContext>
-
#focus(selector) ⇒ Object
-
#has_context? ⇒ Boolean
-
#initialize(frame_manager, frame, timeout_settings) ⇒ DOMWorld
constructor
A new instance of DOMWorld.
-
#S(selector) ⇒ !Promise<?Puppeteer.ElementHandle>
-
#select(selector, *values) ⇒ Array<String>
-
#set_content(html, timeout: nil, wait_until: nil) ⇒ Object
-
#Seval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>
-
#SS(selector) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>
-
#SSeval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>
‘$$eval()` in JavaScript.
-
#Sx(expression) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>
-
#tap(selector) ⇒ Object
-
#title ⇒ String
-
#type_text(selector, text, delay: nil) ⇒ Object
-
#wait_for_function(page_function, args: [], polling: nil, timeout: nil) ⇒ Puppeteer::JSHandle
-
#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object
-
#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Object
Constructor Details
#initialize(frame_manager, frame, timeout_settings) ⇒ DOMWorld
Returns a new instance of DOMWorld.
10
11
12
13
14
15
16
17
|
# File 'lib/puppeteer/dom_world.rb', line 10
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
|
Instance Attribute Details
#frame ⇒ Object
Returns the value of attribute frame.
19
20
21
|
# File 'lib/puppeteer/dom_world.rb', line 19
def frame
@frame
end
|
Instance Method Details
#_wait_tasks ⇒ Object
only used in Puppeteer::WaitTask#initialize
22
23
24
|
# File 'lib/puppeteer/dom_world.rb', line 22
def _wait_tasks
@wait_tasks
end
|
#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object
327
328
329
330
331
|
# File 'lib/puppeteer/dom_world.rb', line 327
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
|
#content ⇒ String
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/puppeteer/dom_world.rb', line 136
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
|
#context=(context) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/puppeteer/dom_world.rb', line 27
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
|
#delete_context(execution_context_id) ⇒ Object
38
39
40
41
|
# File 'lib/puppeteer/dom_world.rb', line 38
def delete_context(execution_context_id)
@document = nil
@context_promise = resolvable_future
end
|
#detach ⇒ Object
47
48
49
50
51
52
|
# File 'lib/puppeteer/dom_world.rb', line 47
def detach
@detached = true
@wait_tasks.each do |wait_task|
wait_task.terminate(Puppeteer::WaitTask::TerminatedError.new('waitForFunction failed: frame got detached.'))
end
end
|
#evaluate(page_function, *args) ⇒ !Promise<*>
74
75
76
|
# File 'lib/puppeteer/dom_world.rb', line 74
def evaluate(page_function, *args)
execution_context.evaluate(page_function, *args)
end
|
#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>
67
68
69
|
# File 'lib/puppeteer/dom_world.rb', line 67
def evaluate_handle(page_function, *args)
execution_context.evaluate_handle(page_function, *args)
end
|
57
58
59
60
61
62
|
# File 'lib/puppeteer/dom_world.rb', line 57
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
|
#focus(selector) ⇒ Object
334
335
336
337
338
|
# File 'lib/puppeteer/dom_world.rb', line 334
def focus(selector)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.focus
handle.dispose
end
|
#has_context? ⇒ Boolean
43
44
45
|
# File 'lib/puppeteer/dom_world.rb', line 43
def has_context?
@context_promise.resolved?
end
|
‘$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
81
82
83
|
# File 'lib/puppeteer/dom_world.rb', line 81
def S(selector)
document.S(selector)
end
|
#select(selector, *values) ⇒ Array<String>
352
353
354
355
356
357
358
|
# File 'lib/puppeteer/dom_world.rb', line 352
def select(selector, *values)
handle = S(selector) or raise ElementNotFoundError.new(selector)
result = handle.select(*values)
handle.dispose
result
end
|
#set_content(html, timeout: nil, wait_until: nil) ⇒ Object
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
|
# File 'lib/puppeteer/dom_world.rb', line 152
def set_content(html, timeout: nil, wait_until: nil)
option_wait_until = [wait_until || 'load'].flatten
option_timeout = timeout || @timeout_settings.navigation_timeout
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
|
#Seval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>
‘$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
115
116
117
|
# File 'lib/puppeteer/dom_world.rb', line 115
def Seval(selector, page_function, *args)
document.Seval(selector, page_function, *args)
end
|
‘$$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
131
132
133
|
# File 'lib/puppeteer/dom_world.rb', line 131
def SS(selector)
document.SS(selector)
end
|
#SSeval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>
‘$$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
124
125
126
|
# File 'lib/puppeteer/dom_world.rb', line 124
def SSeval(selector, page_function, *args)
document.SSeval(selector, page_function, *args)
end
|
‘$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.
106
107
108
|
# File 'lib/puppeteer/dom_world.rb', line 106
def Sx(expression)
document.Sx(expression)
end
|
#tap(selector) ⇒ Object
361
362
363
364
365
|
# File 'lib/puppeteer/dom_world.rb', line 361
def tap(selector)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.tap
handle.dispose
end
|
#title ⇒ String
426
427
428
|
# File 'lib/puppeteer/dom_world.rb', line 426
def title
evaluate('() => document.title')
end
|
#type_text(selector, text, delay: nil) ⇒ Object
370
371
372
373
374
|
# File 'lib/puppeteer/dom_world.rb', line 370
def type_text(selector, text, delay: nil)
handle = S(selector) or raise ElementNotFoundError.new(selector)
handle.type_text(text, delay: delay)
handle.dispose
end
|
#wait_for_function(page_function, args: [], polling: nil, timeout: nil) ⇒ Puppeteer::JSHandle
410
411
412
413
414
415
416
417
418
419
420
421
422
|
# File 'lib/puppeteer/dom_world.rb', line 410
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
|
#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object
380
381
382
|
# File 'lib/puppeteer/dom_world.rb', line 380
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
|
#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Object
388
389
390
|
# File 'lib/puppeteer/dom_world.rb', line 388
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
|