Class: Ferrum::RemoteObject

Inherits:
Object
  • Object
show all
Includes:
Evaluatable
Defined in:
lib/ferrum/remote_object.rb,
sig/ferrum/remote_object.rbs

Overview

An opaque reference to a JavaScript value living in the browser, returned by Frame::Runtime#evaluate_handle. Unlike Frame::Runtime#evaluate, which serializes the result into Ruby, a handle keeps the value in the page so it can be passed straight back into a later evaluation without a round trip through JSON.

Examples:

list = page.evaluate_handle("document.querySelectorAll('li')")
page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
list.evaluate("this.length") # => 3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Evaluatable

#evaluate, #evaluate_handle, #execute

Constructor Details

#initialize(runtime, page, result) ⇒ RemoteObject

Returns a new instance of RemoteObject.

Parameters:

  • runtime (Frame, Worker) —

    The frame or worker that evaluated the value.

  • page (Page, Worker) —

    The target the value belongs to.

  • result (Hash) —

    A CDP Runtime.RemoteObject.



43
44
45
46
47
48
49
50
# File 'lib/ferrum/remote_object.rb', line 43

def initialize(runtime, page, result)
  @runtime = runtime
  @page = page
  @remote_id = result["objectId"]
  @type = result["type"]
  @subtype = result["subtype"]
  @description = result["description"]
end

Instance Attribute Details

#description ⇒ String? (readonly)

Returns the browser's own description of the value.

Returns:

  • (String, nil) —

    the browser's own description of the value.



31
32
33
# File 'lib/ferrum/remote_object.rb', line 31

def description
  @description
end

#remote_id ⇒ String (readonly)

Returns the CDP remote object id.

Returns:

  • (String) —

    the CDP remote object id.



22
23
24
# File 'lib/ferrum/remote_object.rb', line 22

def remote_id
  @remote_id
end

#subtype ⇒ String? (readonly)

Returns the JavaScript subtype, e.g. "array" or "map".

Returns:

  • (String, nil) —

    the JavaScript subtype, e.g. "array" or "map".



28
29
30
# File 'lib/ferrum/remote_object.rb', line 28

def subtype
  @subtype
end

#type ⇒ String (readonly)

Returns the JavaScript type, e.g. "object" or "function".

Returns:

  • (String) —

    the JavaScript type, e.g. "object" or "function".



25
26
27
# File 'lib/ferrum/remote_object.rb', line 25

def type
  @type
end

Instance Method Details

#inspect ⇒ String

Returns:

  • (String)


79
80
81
82
# File 'lib/ferrum/remote_object.rb', line 79

def inspect
  %(#<#{self.class} @remote_id=#{@remote_id.inspect} @type=#{@type.inspect} ) +
    %(@subtype=#{@subtype.inspect} @description=#{@description.inspect}>)
end

#release ⇒ void

This method returns an undefined value.

Releases the browser-side reference. The handle is unusable afterwards.

Raises:



71
72
73
74
75
76
# File 'lib/ferrum/remote_object.rb', line 71

def release
  @page.command("Runtime.releaseObject", objectId: @remote_id)
  nil
rescue Ferrum::BrowserError
  nil
end

#value ⇒ Object

Serializes the handle into a plain Ruby value, resolving nodes into Node objects the same way Frame::Runtime#evaluate does.

Returns:

  • (Object)

Raises:



60
61
62
# File 'lib/ferrum/remote_object.rb', line 60

def value
  evaluate("this")
end