Module: Ferrum::Page::Runtime

Included in:
Ferrum::Page
Defined in:
lib/ferrum/page/runtime.rb

Constant Summary collapse

EXECUTE_OPTIONS =
{
  returnByValue: true,
  functionDeclaration: %Q(function() { %s })
}.freeze
DEFAULT_OPTIONS =
{
  functionDeclaration: %Q(function() { return %s })
}.freeze
EVALUATE_ASYNC_OPTIONS =
{
  awaitPromise: true,
  functionDeclaration: %Q(
    function() {
     return new Promise((__resolve, __reject) => {
       try {
         arguments[arguments.length] = r => __resolve(r);
         arguments.length = arguments.length + 1;
         setTimeout(() => __reject(new Error("timed out promise")), %s);
         %s
       } catch(error) {
         __reject(error);
       }
     });
    }
  )
}.freeze

Instance Method Summary collapse

Instance Method Details

#evaluate(expression, *args) ⇒ Object



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

def evaluate(expression, *args)
  call(*args, expression: expression)
end

#evaluate_async(expression, wait_time, *args) ⇒ Object



35
36
37
# File 'lib/ferrum/page/runtime.rb', line 35

def evaluate_async(expression, wait_time, *args)
  call(*args, expression: expression, wait_time: wait_time * 1000, **EVALUATE_ASYNC_OPTIONS)
end

#evaluate_on(node:, expression:, by_value: true, timeout: 0) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ferrum/page/runtime.rb', line 44

def evaluate_on(node:, expression:, by_value: true, timeout: 0)
  rescue_intermittent_error do
    response = command("DOM.resolveNode", nodeId: node.node_id)
    object_id = response.dig("object", "objectId")
    options = DEFAULT_OPTIONS.merge(objectId: object_id)
    options[:functionDeclaration] = options[:functionDeclaration] % expression
    options.merge!(returnByValue: by_value)

    response = command("Runtime.callFunctionOn",
                       timeout: timeout,
                       **options)["result"].tap { |r| handle_error(r) }

    by_value ? response.dig("value") : handle_response(response)
  end
end

#execute(expression, *args) ⇒ Object



39
40
41
42
# File 'lib/ferrum/page/runtime.rb', line 39

def execute(expression, *args)
  call(*args, expression: expression, handle: false, **EXECUTE_OPTIONS)
  true
end