Module: Ferrum::Evaluatable

Included in:
Node, RemoteObject
Defined in:
lib/ferrum/evaluatable.rb,
sig/ferrum/evaluatable.rbs

Overview

Shared by Node and RemoteObject: runs JavaScript with this bound to the handle, on the runtime that returned it. Takes the same script shapes and arguments as Frame::Runtime#evaluate. Arrow functions don't get this, so use a function declaration when the script needs it.

Instance Method Summary collapse

Instance Method Details

#evaluate(expression, *positional, **keywords) ⇒ Object

Evaluates JavaScript with this bound to the handle, and returns the result serialized into Ruby.

Examples:

page.at_css("input").evaluate("this.value")
page.at_css("input").evaluate("this.value + suffix", suffix: "!")
page.evaluate_handle("new Map([[1, 'a']])").evaluate("this.get(1)") # => "a"

Parameters:

  • expression (String) —

    A JavaScript expression, or a function declaration.

  • positional (Array) —

    Arguments reachable as arguments[0] and up. Can't be combined with named ones.

  • keywords (Hash) —

    Named arguments, which become the function's parameters, plus:

Options Hash (**keywords):

  • :timeout (Numeric, nil) —

    How long to wait for the script, and any promise it returns, to settle, in seconds. Defaults to the page timeout.

Returns:

  • (Object)

Raises:



27
28
29
# File 'lib/ferrum/evaluatable.rb', line 27

def evaluate(expression, *positional, **keywords)
  @runtime.evaluate_in(self, expression, positional, keywords, mode: :value)
end

#evaluate_handle(expression, *positional, **keywords) ⇒ RemoteObject, ...

Same as #evaluate, but returns a RemoteObject that stays in the browser instead of being serialized.

Examples:

page.at_css("form").evaluate_handle("this.dataset") # => RemoteObject
page.at_css("form").evaluate_handle("this") # => Node

Parameters:

  • expression (String) —

    A JavaScript expression, or a function declaration.

  • positional (Array) —

    Arguments reachable as arguments[0] and up. Can't be combined with named ones.

  • keywords (Hash) —

    Named arguments, which become the function's parameters, plus:

Options Hash (**keywords):

  • :timeout (Numeric, nil) —

    How long to wait for the script, and any promise it returns, to settle, in seconds. Defaults to the page timeout.

Returns:

Raises:



47
48
49
# File 'lib/ferrum/evaluatable.rb', line 47

def evaluate_handle(expression, *positional, **keywords)
  @runtime.evaluate_in(self, expression, positional, keywords, mode: :handle)
end

#execute(expression, *positional, **keywords) ⇒ self

Runs JavaScript with this bound to the handle, for its side effects. The script is used as a function body, so multiple statements need no wrapping.

Examples:

page.at_css("input").execute("this.value = text", text: "hello")

Parameters:

  • expression (String) —

    A JavaScript expression, or a function declaration.

  • positional (Array) —

    Arguments reachable as arguments[0] and up. Can't be combined with named ones.

  • keywords (Hash) —

    Named arguments, which become the function's parameters, plus:

Options Hash (**keywords):

  • :timeout (Numeric, nil) —

    How long to wait for the script, and any promise it returns, to settle, in seconds. Defaults to the page timeout.

Returns:

  • (self)

Raises:



66
67
68
69
# File 'lib/ferrum/evaluatable.rb', line 66

def execute(expression, *positional, **keywords)
  @runtime.evaluate_in(self, expression, positional, keywords, mode: :none)
  self
end