Driver8 (driver8 v0.1.3) View Source

This module minimizes the amount of work one needs to do to create a WebDriver extension. Typical usecase would look something like this:

defmodule Driver8.Http do
  @moduledoc false
  use GenServer

  require Logger
  require Driver8.Session

  import Driver8.WireProtocol

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, [], name: MyAwesomeExtension)
  end

  @impl true
  def init(_opts) do
    :ok = Driver8.Session.extend("my-awesome-extension", :get)
    {:ok, []}
  end

  @impl true
  def handle_call({:post, ["my-awesome-extension", "get"], session_id, parameters}, _from, state) do
    response = ... # prepare you response here

    {:reply, {:ok, response}, state}
  end

  @impl true
  def handle_call(request, _from, state) do
    {:reply, GenServer.call(Driver8, request), state}
  end
end

Important steps to create your extension are:

Create a GenServer

Register it as driver level extension (Driver8.extend) or session level extension (Driver8.Session.extend)

Define implementation of handle_call for your endpoints

Define catch all method and pass the message to Driver8

Last step is optional (presumably you are in control of the client code that calls your extension), but it will give you a nice error message on the client end instead of killing your GenServer.

Link to this section Summary

Functions

In case you decide to handle routes yourself this method allows you to dispatch a call to a driver level extension. Method is expected be a Plug.Conn method (so "GET", "POST", etc.)

Returns a specification to start this module under a supervisor.

Registers the calling process as an driver level extension, responsible for handling requests on a given path. Methods can be an atom like :get or a list of atoms. Defaults to [:get, :post, :put, :delete, :head, :patch]. You can pass a different name to be used to find Driver8, defaults to Driver8.

Returns the status of the driver to be used in the /status endpoint.

Starts a Driver8 process linked to the current process (to be used as a part of supervision tree).

A Utility method that will return you a supervisor that is used to register sessions and registry where all the sessions are registered

Returns current version of Driver8

Link to this section Functions

Link to this function

call_extension(method, path, params, driver_name \\ Driver8)

View Source

In case you decide to handle routes yourself this method allows you to dispatch a call to a driver level extension. Method is expected be a Plug.Conn method (so "GET", "POST", etc.)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

extend(path, methods \\ [:get, :post, :put, :delete, :head, :patch], driver_name \\ Driver8)

View Source

Registers the calling process as an driver level extension, responsible for handling requests on a given path. Methods can be an atom like :get or a list of atoms. Defaults to [:get, :post, :put, :delete, :head, :patch]. You can pass a different name to be used to find Driver8, defaults to Driver8.

Examples

:ok = Driver8.extend("my-extension")

:ok = Driver8.extend("my-extension", [:get, :post])

:ok = Driver8.extend("my-extension", [:get, :post], Driver8.IKnowWhatIamDoing)

After extension is registered with any of the examples call http://localhost:8085/my-extension will be translated to the message to the GenServer you have registered as extension. You are expected to have a method similar to

@impl true
def handle_call({:get, ["my-extension"], _parameters}, _from, state) do
  {:reply, {:ok, {200, %{"result" => "Everything went fine."}}}, state}
end

or

@impl true
def handle_call({:get, ["my-extension"], _parameters}, _from, state) do
  {:reply, {:ok, {401, "Go away!"}}, state}
end

First case will result in json response, second in text response.

Link to this function

is_ready(driver_name \\ Driver8)

View Source

Returns the status of the driver to be used in the /status endpoint.

Starts a Driver8 process linked to the current process (to be used as a part of supervision tree).

Once process is started it will register itself with default name Driver8. This name is used by Driver8.Plug to call for request handling.

Options

  • :name - value used for name registration, if not passed defaults to Driver8
Link to this function

supervisor_and_registry(driver_name \\ Driver8)

View Source

A Utility method that will return you a supervisor that is used to register sessions and registry where all the sessions are registered

Returns current version of Driver8