hound v1.1.1 Hound.Helpers.ScriptExecution View Source

Functions to execute javascript

Link to this section Summary

Functions

Execute a phantomjs script to configure callbacks. This will only work with phantomjs driver

Execute javascript synchronously

Link to this section Functions

Link to this function

execute_phantom_script(script_function, function_args \\ []) View Source
execute_phantom_script(String.t(), list()) :: any()

Execute a phantomjs script to configure callbacks. This will only work with phantomjs driver.

  • The first argument is the script to execute.

  • The second argument is a list of arguments that is passed. These arguments are accessible in the script via arguments.

    execute_phantom_script("return(arguments[0] + arguments[1]);", [1, 2])

    execute_phantom_script("doSomething(); return(arguments[0] + arguments[1]);")

  • NOTE: "this" in the context of the script function refers to the phantomjs result of require('webpage').create().

    To use it, capture it in a variable at the beginning of the script. Example:

    page = this;

    page.onResourceRequested = function(requestData, request) { // Do something with the request };

Link to this function

execute_script(script_function, function_args \\ []) View Source
execute_script(String.t(), list()) :: any()

Execute javascript synchronously.

  • The first argument is the script to execute.
  • The second argument is a list of arguments that is passed. These arguments are accessible in the script via arguments.

    execute_script("return(arguments[0] + arguments[1]);", [1, 2])

    execute_script("doSomething(); return(arguments[0] + arguments[1]);")

Link to this function

execute_script_async(script_function, function_args \\ []) View Source
execute_script_async(String.t(), list()) :: any()

Execute javascript asynchronously.

  • The first argument is the script to execute.
  • The second argument is a list of arguments that is passed. These arguments are accessible in the script via arguments.

Webdriver passes a callback function as the last argument to the script. When your script has completed execution, it has to call the last argument, which is a callback function, to indicate that the execute is complete.

# Once we perform whatever we want,
# we call the callback function with the arguments that must be returned.
execute_script_async("doSomething(); arguments[arguments.length-1]('hello')", [])

# We have no arguments to pass, so we'll skip the second argument.
execute_script_async("console.log('hello'); doSomething(); arguments[arguments.length-1]()")

Unless you call the callback function, the function is not assumed to be completed. It will error out.