Class: Puppeteer::ExecutionContext::JavaScriptExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/execution_context.rb,
sig/puppeteer/execution_context.rbs

Instance Method Summary collapse

Constructor Details

#initialize(execution_context, expression, return_by_value) ⇒ JavaScriptExpression

Returns a new instance of JavaScriptExpression.

Parameters:

  • execution_context (Object)
  • expression (Object)
  • return_by_value (Object)


51
52
53
54
55
# File 'lib/puppeteer/execution_context.rb', line 51

def initialize(execution_context, expression, return_by_value)
  @execution_context = execution_context
  @expression = expression
  @return_by_value = return_by_value
end

Instance Method Details

#evaluate_with(client:, context_id:) ⇒ Object|JSHandle

Parameters:

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppeteer/execution_context.rb', line 60

def evaluate_with(client:, context_id:)
  result =
    begin
      client.send_message('Runtime.evaluate',
        expression: expression_with_source_url,
        contextId: context_id,
        returnByValue: @return_by_value,
        awaitPromise: true,
        userGesture: true,
      )
    rescue Puppeteer::Connection::ProtocolError => err
      raise @execution_context.send(:rewrite_evaluation_error, err)
    end
  # }).catch(rewriteError);

  exception_details = result['exceptionDetails']
  if exception_details
    raise EvaluationError.new("Evaluation failed: #{exception_details}")
  end

  remote_object = Puppeteer::RemoteObject.new(result['result'])
  if @return_by_value
    remote_object.value
  else
    Puppeteer::JSHandle.create(
      context: @execution_context,
      remote_object: remote_object,
    )
  end
end