Class: Puppeteer::DOMWorld
- Inherits:
-
Object
- Object
- Puppeteer::DOMWorld
show all
- Defined in:
- lib/puppeteer/dom_world.rb
Overview
Defined Under Namespace
Classes: DetachedError, DocumentEvaluationError
Constant Summary
collapse
- PREDICATE =
"/**\n * @param {string} selectorOrXPath\n * @param {boolean} isXPath\n * @param {boolean} waitForVisible\n * @param {boolean} waitForHidden\n * @return {?Node|boolean}\n */\nfunction _(selectorOrXPath, isXPath, waitForVisible, waitForHidden) {\n const node = isXPath\n ? document.evaluate(selectorOrXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue\n : document.querySelector(selectorOrXPath);\n if (!node)\n return waitForHidden;\n if (!waitForVisible && !waitForHidden)\n return node;\n const element = /** @type {Element} */ (node.nodeType === Node.TEXT_NODE ? node.parentElement : node);\n const style = window.getComputedStyle(element);\n const isVisible = style && style.visibility !== 'hidden' && hasVisibleBoundingBox();\n const success = (waitForVisible === isVisible || waitForHidden === !isVisible);\n return success ? node : null;\n /**\n * @return {boolean}\n */\n function hasVisibleBoundingBox() {\n const rect = element.getBoundingClientRect();\n return !!(rect.top || rect.bottom || rect.width || rect.height);\n }\n}\n"
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
-
#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>
-
#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>
-
#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
-
#type_text(selector, text, delay: nil) ⇒ Object
-
#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
18
|
# 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
@pending_destroy = []
@wait_tasks = Set.new
@detached = false
end
|
Instance Attribute Details
#frame ⇒ Object
Returns the value of attribute frame.
20
21
22
|
# File 'lib/puppeteer/dom_world.rb', line 20
def frame
@frame
end
|
Instance Method Details
#_wait_tasks ⇒ Object
only used in Puppeteer::WaitTask#initialize
23
24
25
|
# File 'lib/puppeteer/dom_world.rb', line 23
def _wait_tasks
@wait_tasks
end
|
#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object
332
333
334
335
336
|
# File 'lib/puppeteer/dom_world.rb', line 332
def click(selector, delay: nil, button: nil, click_count: nil)
handle = S(selector)
handle.click(delay: delay, button: button, click_count: click_count)
handle.dispose
end
|
#context=(context) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/puppeteer/dom_world.rb', line 28
def context=(context)
if context
if @context_promise.fulfilled?
@pending_destroy << context._context_id
@document = nil
@context_promise = resolvable_future
end
@context_promise.fulfill(context)
@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
49
50
51
52
53
54
55
56
|
# File 'lib/puppeteer/dom_world.rb', line 49
def delete_context(execution_context_id)
if @pending_destroy.include?(execution_context_id)
@pending_destroy.delete(execution_context_id)
else
@document = nil
@context_promise = resolvable_future
end
end
|
#detach ⇒ Object
62
63
64
65
66
67
|
# File 'lib/puppeteer/dom_world.rb', line 62
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<*>
89
90
91
|
# File 'lib/puppeteer/dom_world.rb', line 89
def evaluate(page_function, *args)
execution_context.evaluate(page_function, *args)
end
|
#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>
82
83
84
|
# File 'lib/puppeteer/dom_world.rb', line 82
def evaluate_handle(page_function, *args)
execution_context.evaluate_handle(page_function, *args)
end
|
72
73
74
75
76
77
|
# File 'lib/puppeteer/dom_world.rb', line 72
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
|
#has_context? ⇒ Boolean
58
59
60
|
# File 'lib/puppeteer/dom_world.rb', line 58
def has_context?
@context_promise.resolved?
end
|
‘$()` in JavaScript. $ is not allowed to use as a method name in Ruby.
96
97
98
|
# File 'lib/puppeteer/dom_world.rb', line 96
def S(selector)
document.S(selector)
end
|
#select(selector, *values) ⇒ Array<String>
360
361
362
363
364
365
366
|
# File 'lib/puppeteer/dom_world.rb', line 360
def select(selector, *values)
handle = S(selector)
result = handle.select(*values)
handle.dispose
result
end
|
#Seval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>
‘$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby.
129
130
131
|
# File 'lib/puppeteer/dom_world.rb', line 129
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.
145
146
147
|
# File 'lib/puppeteer/dom_world.rb', line 145
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.
138
139
140
|
# File 'lib/puppeteer/dom_world.rb', line 138
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.
120
121
122
|
# File 'lib/puppeteer/dom_world.rb', line 120
def Sx(expression)
document.Sx(expression)
end
|
#tap(selector) ⇒ Object
369
370
371
372
373
|
# File 'lib/puppeteer/dom_world.rb', line 369
def tap(selector)
handle = S(selector)
handle.tap
handle.dispose
end
|
#type_text(selector, text, delay: nil) ⇒ Object
378
379
380
381
382
|
# File 'lib/puppeteer/dom_world.rb', line 378
def type_text(selector, text, delay: nil)
handle = S(selector)
handle.type_text(text, delay: delay)
handle.dispose
end
|
#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object
388
389
390
|
# File 'lib/puppeteer/dom_world.rb', line 388
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
396
397
398
|
# File 'lib/puppeteer/dom_world.rb', line 396
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
|