Class: Puppeteer::DOMWorld

Inherits:
Object
  • Object
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

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)


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

#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(<<-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

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

#focus(selector) ⇒ Object

Parameters:

  • selector (String)


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

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


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

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

#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)


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

#titleString

Returns:

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

Parameters:

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


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

Parameters:

  • page_function (String)
  • args (Array) (defaults to: [])
  • polling (Integer|String) (defaults to: nil)
  • timeout (Integer) (defaults to: nil)

Returns:



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

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)


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

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)


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