Class: Puppeteer::DOMWorld

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/dom_world.rb

Overview

Defined Under Namespace

Classes: DetachedError

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

Constructor Details

#initialize(frame_manager, frame, timeout_settings) ⇒ 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

#frameObject (readonly)

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_tasksObject

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



333
334
335
336
337
# File 'lib/puppeteer/dom_world.rb', line 333

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)
  # 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"}
  # 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"}
  # D, [2020-04-12T22:45:03.938960 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextDestroyed", "params"=>{"executionContextId"=>1}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
  # 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"}
  # D, [2020-04-12T22:45:03.939793 #46154] DEBUG -- : RECV << {"method"=>"Runtime.executionContextDestroyed", "params"=>{"executionContextId"=>2}, "sessionId"=>"636CEF0C4FEAFC4FE815E9E7B5F7BA68"}
  # executionContextDestroyed is often notified after executionContextCreated.

  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

#detachObject



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

#execution_context!Promise<!Puppeteer.ExecutionContext>



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

#S(selector) ⇒ !Promise<?Puppeteer.ElementHandle>

‘$()` 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>



361
362
363
364
365
366
367
# File 'lib/puppeteer/dom_world.rb', line 361

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.



130
131
132
# File 'lib/puppeteer/dom_world.rb', line 130

def Seval(selector, page_function, *args)
  document.Seval(selector, page_function, *args)
end

#SS(selector) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>

‘$$()` in JavaScript. $ is not allowed to use as a method name in Ruby.



146
147
148
# File 'lib/puppeteer/dom_world.rb', line 146

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.



139
140
141
# File 'lib/puppeteer/dom_world.rb', line 139

def SSeval(selector, page_function, *args)
  document.SSeval(selector, page_function, *args)
end

#Sx(expression) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>

‘$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.



121
122
123
# File 'lib/puppeteer/dom_world.rb', line 121

def Sx(expression)
  document.Sx(expression)
end

#tap(selector) ⇒ Object



370
371
372
373
374
# File 'lib/puppeteer/dom_world.rb', line 370

def tap(selector)
  handle = S(selector)
  handle.tap
  handle.dispose
end

#type_text(selector, text, delay: nil) ⇒ Object



379
380
381
382
383
# File 'lib/puppeteer/dom_world.rb', line 379

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



389
390
391
# File 'lib/puppeteer/dom_world.rb', line 389

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



397
398
399
# File 'lib/puppeteer/dom_world.rb', line 397

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