Class: Puppeteer::RemoteObject

Inherits:
Object
  • Object
show all
Includes:
DebugPrint
Defined in:
lib/puppeteer/remote_object.rb

Overview

providing #valueFromRemoteObject, #releaseObject

Constant Summary collapse

UNSERIALIZABLE_SENTINEL_KEY =
'__puppeteer_unserializable__'
NUMBER_SENTINEL_KEY =
'__puppeteer_number__'
REGEXP_SENTINEL_KEY =
'__puppeteer_regexp__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(payload) ⇒ RemoteObject

Returns a new instance of RemoteObject.

Parameters:

  • payload (Hash)


15
16
17
18
19
20
21
22
23
# File 'lib/puppeteer/remote_object.rb', line 15

def initialize(payload)
  @object_id = payload['objectId']
  @type = payload['type']
  @sub_type = payload['subtype']
  @class_name = payload['className']
  @unserializable_value = payload['unserializableValue']
  @value = payload['value']
  @description = payload['description']
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#sub_typeObject (readonly)

Returns the value of attribute sub_type.



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

def sub_type
  @sub_type
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#box_model(client) ⇒ Object

used in ElementHandle#_box_model



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppeteer/remote_object.rb', line 76

def box_model(client)
  result = client.send_message('DOM.getBoxModel', objectId: @object_id)

  # Some browsers return zeroed box model data instead of throwing errors.
  model = result['model']
  if model['width'] == 0 && model['height'] == 0 &&
    %w(content padding border margin).all? { |key| model[key].all?(&:nil?) }

    debug_puts('Could not compute box model.')
    return nil
  end
  result
rescue => err
  debug_puts(err)
  nil
end

#content_quads(client) ⇒ Object

used in ElementHandle#clickable_point



71
72
73
# File 'lib/puppeteer/remote_object.rb', line 71

def content_quads(client)
  client.send_message('DOM.getContentQuads', objectId: @object_id)
end

#converted_argObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/puppeteer/remote_object.rb', line 236

def converted_arg
  # ported logic from ExecutionContext#convertArgument
  # https://github.com/puppeteer/puppeteer/blob/master/lib/ExecutionContext.js
  #
  # Original logic:
  # if (objectHandle._remoteObject.unserializableValue)
  #   return { unserializableValue: objectHandle._remoteObject.unserializableValue };
  # if (!objectHandle._remoteObject.objectId)
  #   return { value: objectHandle._remoteObject.value };
  # return { objectId: objectHandle._remoteObject.objectId };

  if @unserializable_value
    { unserializableValue: @unserializable_value }
  elsif @object_id
    { objectId: @object_id }
  else
    { value: value }
  end
end

#node_info(client) ⇒ Object

used in ElementHandle#content_frame, ElementHandle#upload_file



66
67
68
# File 'lib/puppeteer/remote_object.rb', line 66

def node_info(client)
  client.send_message('DOM.describeNode', objectId: @object_id)
end

#object_id?Boolean

Returns:

  • (Boolean)


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

def object_id?
  !@object_id.nil?
end

#object_id_valueObject



33
34
35
# File 'lib/puppeteer/remote_object.rb', line 33

def object_id_value
  @object_id
end

#properties(client) ⇒ Object

used in JSHandle#properties



56
57
58
59
60
61
62
63
# File 'lib/puppeteer/remote_object.rb', line 56

def properties(client)
  # original logic:
  #   const response = await this._client.send('Runtime.getProperties', {
  #     objectId: this._remoteObject.objectId,
  #     ownProperties: true
  #   });
  client.send_message('Runtime.getProperties', objectId: @object_id, ownProperties: true)
end

#query_ax_tree(client, accessible_name: nil, role: nil) ⇒ Object

used in ElementHandle#query_ax_tree



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppeteer/remote_object.rb', line 94

def query_ax_tree(client, accessible_name: nil, role: nil)
  result = client.send_message('Accessibility.queryAXTree', {
    objectId: @object_id,
    accessibleName: accessible_name,
    role: role,
  }.compact)

  result['nodes'].select do |node|
    node['role'] && node['role']['value'] != 'StaticText'
  end
end

#release(client) ⇒ Object

Parameters:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/puppeteer/remote_object.rb', line 218

def release(client)
  return unless @object_id

  begin
    client.send_message('Runtime.releaseObject',
      objectId: @object_id,
    )
  rescue => err
    # Exceptions might happen in case of a page been navigated or closed.
    # Swallow these since they are harmless and we don't leak anything in this case.
    debug_puts(err)
  end

  nil
end

#scroll_into_view_if_needed(client) ⇒ Object



261
262
263
# File 'lib/puppeteer/remote_object.rb', line 261

def scroll_into_view_if_needed(client)
  client.send_message('DOM.scrollIntoViewIfNeeded', objectId: @object_id)
end

#set_file_input_files(client, files, backend_node_id) ⇒ Object

used in ElementHandle#upload_file



257
258
259
# File 'lib/puppeteer/remote_object.rb', line 257

def set_file_input_files(client, files, backend_node_id)
  client.send_message('DOM.setFileInputFiles', objectId: @object_id, files: files, backendNodeId: backend_node_id)
end

#type_strString

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppeteer/remote_object.rb', line 38

def type_str
  # used in JSHandle#to_s
  # original logic:
  #   if (this._remoteObject.objectId) {
  #     const type =  this._remoteObject.subtype || this._remoteObject.type;
  #     return 'JSHandle@' + type;
  #   }
  if @object_id
    return @sub_type if @sub_type
    return 'window' if @type == 'object' && @description == 'Window'

    @type
  else
    nil
  end
end

#valueObject

helper#valueFromRemoteObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppeteer/remote_object.rb', line 107

def value
  if @unserializable_value
    if @type == 'bigint' || @unserializable_value.end_with?('n')
      return Integer(@unserializable_value.delete_suffix('n'))
    end

    case @unserializable_value
    when '-0'
      -0.0
    when 'NaN'
      Float::NAN
    when 'Infinity'
      Float::INFINITY
    when '-Infinity'
      -Float::INFINITY
    else
      raise NotImplementedError.new("Unsupported unserializable value: #{@unserializable_value}")
    end
  end

  if @sub_type == 'date'
    return parse_date_value(@value)
  end

  if @sub_type == 'regexp' && @description
    source, flags = parse_regexp(@description)
    return Regexp.new(source, regexp_options(flags))
  end

  normalize_serialized_value(@value)
end