Module: Capybara::Cuprite::Browser::Runtime

Included in:
Page
Defined in:
lib/capybara/cuprite/browser/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 TimedOutPromise), %s);
         %s
       } catch(error) {
         __reject(error);
       }
     });
    }
  )
}.freeze

Instance Method Summary collapse

Instance Method Details

#evaluate(expr, *args) ⇒ Object



31
32
33
34
# File 'lib/capybara/cuprite/browser/runtime.rb', line 31

def evaluate(expr, *args)
  response = call(expr, nil, nil, *args)
  handle(response)
end

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



55
56
57
58
# File 'lib/capybara/cuprite/browser/runtime.rb', line 55

def evaluate_async(expr, wait_time, *args)
  response = call(expr, wait_time * 1000, EVALUATE_ASYNC_OPTIONS, *args)
  handle(response)
end

#evaluate_in(context_id, expr) ⇒ Object



36
37
38
39
# File 'lib/capybara/cuprite/browser/runtime.rb', line 36

def evaluate_in(context_id, expr)
  response = call(expr, nil, { executionContextId: context_id })
  handle(response)
end

#evaluate_on(node:, expr:, by_value: true, wait: 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capybara/cuprite/browser/runtime.rb', line 41

def evaluate_on(node:, expr:, by_value: true, wait: 0)
  object_id = command("DOM.resolveNode", nodeId: node["nodeId"]).dig("object", "objectId")
  options = DEFAULT_OPTIONS.merge(objectId: object_id)
  options[:functionDeclaration] = options[:functionDeclaration] % expr
  options.merge!(returnByValue: by_value)

  @wait = wait if wait > 0

  response = command("Runtime.callFunctionOn", **options)
    .dig("result").tap { |r| handle_error(r) }

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

#execute(expr, *args) ⇒ Object



60
61
62
63
# File 'lib/capybara/cuprite/browser/runtime.rb', line 60

def execute(expr, *args)
  call(expr, nil, EXECUTE_OPTIONS, *args)
  true
end