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

Returns a new instance of DOMWorld.

Parameters:



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

#frameObject (readonly)

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_tasksObject

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

Parameters:

  • selector (String)
  • delay (Number) (defaults to: nil)
  • button (String) (defaults to: nil)

    “left”|“right”|“middle”

  • click_count (Number) (defaults to: nil)


321
322
323
324
325
# File 'lib/puppeteer/dom_world.rb', line 321

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

#contentString

Returns:

  • (String)


136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppeteer/dom_world.rb', line 136

def content
  evaluate "  () => {\n    let retVal = '';\n    if (document.doctype)\n      retVal = new XMLSerializer().serializeToString(document.doctype);\n    if (document.documentElement)\n      retVal += document.documentElement.outerHTML;\n    return retVal;\n  }\n  JAVASCRIPT\nend\n"

#context=(context) ⇒ Object

Parameters:



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

#detachObject



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<*>

Parameters:

  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:

  • (!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>

Parameters:

  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:



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

#execution_context!Promise<!Puppeteer.ExecutionContext>

Returns:



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

#has_context?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/puppeteer/dom_world.rb', line 43

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.

Parameters:

  • selector (string)

Returns:



81
82
83
# File 'lib/puppeteer/dom_world.rb', line 81

def S(selector)
  document.S(selector)
end

#select(selector, *values) ⇒ Array<String>

Parameters:

  • selector (String)

Returns:

  • (Array<String>)


349
350
351
352
353
354
355
# File 'lib/puppeteer/dom_world.rb', line 349

def select(selector, *values)
  handle = S(selector)
  result = handle.select(*values)
  handle.dispose

  result
end

#set_content(html, timeout: nil, wait_until: nil) ⇒ Object

Parameters:

  • html (String)
  • timeout (Integer) (defaults to: nil)
  • wait_until (String|Array<String>) (defaults to: nil)


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_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 = "  (html) => {\n    document.open();\n    document.write(html);\n    document.close();\n  }\n  JAVASCRIPT\n  evaluate(js, html)\n\n  watcher = Puppeteer::LifecycleWatcher.new(@frame_manager, @frame, option_wait_until, option_timeout)\n  begin\n    await_any(\n      watcher.timeout_or_termination_promise,\n      watcher.lifecycle_promise,\n    )\n  ensure\n    watcher.dispose\n  end\nend\n"

#Seval(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)>

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

Parameters:

  • selector (string)
  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:

  • (!Promise<(!Object|undefined)>)


115
116
117
# File 'lib/puppeteer/dom_world.rb', line 115

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.

Parameters:

  • selector (string)

Returns:



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.

Parameters:

  • selector (string)
  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:

  • (!Promise<(!Object|undefined)>)


124
125
126
# File 'lib/puppeteer/dom_world.rb', line 124

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.

Parameters:

  • expression (string)

Returns:



106
107
108
# File 'lib/puppeteer/dom_world.rb', line 106

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

#tap(selector) ⇒ Object

Parameters:

  • selector (String)


358
359
360
361
362
# File 'lib/puppeteer/dom_world.rb', line 358

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

#titleString

Returns:

  • (String)


403
404
405
# File 'lib/puppeteer/dom_world.rb', line 403

def title
  evaluate('() => document.title')
end

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

Parameters:

  • selector (String)
  • text (String)
  • delay (Number) (defaults to: nil)


367
368
369
370
371
# File 'lib/puppeteer/dom_world.rb', line 367

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

Parameters:

  • selector (String)
  • visible (Boolean) (defaults to: nil)

    Wait for element visible (not ‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • hidden (Boolean) (defaults to: nil)

    Wait for element invisible (‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • timeout (Integer) (defaults to: nil)


377
378
379
# File 'lib/puppeteer/dom_world.rb', line 377

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

Parameters:

  • xpath (String)
  • visible (Boolean) (defaults to: nil)

    Wait for element visible (not ‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • hidden (Boolean) (defaults to: nil)

    Wait for element invisible (‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • timeout (Integer) (defaults to: nil)


385
386
387
# File 'lib/puppeteer/dom_world.rb', line 385

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