speed_test v0.1.0 SpeedTest

High level wrapper for the chrome debug protocol and managing headless chrome browsers.

Link to this section Summary

Functions

Returns a single attribute for a node

Returns all attributes for a dom node as a map

Clears a text input. Wrapper fro calling click on an input 3 times and typing backspace.

Clicks on a dom node. Can take in a params object with the number of times to click.

Closes a page process. Note this does NOT stop the underlying chroxy chrome process

Returns the current URL

Changes the dimensions of a given page

Focuses on a given node.

Gets a node_id/0 to a given selector on the page.

Gets a cookie from the page

Navigates to a given URL. This URL can be shorted by providing the configuration value :base_url

Waits for a given network request to happen. Can be anything that shows up in the network tab from XHR request to CSS to javascript.

Launch creates a new browser session inside headless chrome.

Creates a PDF from the current page. Optionally can be saved to a provided path.

Gets arbitrary properties from a dom node.

Captures a screenshot of the current window. Optionally saves the output to a given path

Sets a cookie on the page.

Utility function for returning the text of a DOM node ("textContent" property)

Types text in a given node.

Gets the "value" property from a given node.

Waits for the current URL to equal the provided one. Useful if you have a redirect that occurs after some user action.

Link to this section Types

Link to this type

node_id()

node_id() :: integer()
Link to this type

options()

options() :: [timeout: timeout(), retry: SpeedTest.Retry.t()]

Link to this section Functions

Link to this function

attribute(server, node_id, attribute, options \\ [])

attribute(pid(), node_id(), binary(), options()) :: {:ok, binary()} | :notfound

Returns a single attribute for a node

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, input} = page |> SpeedTest.get("input")
iex> page |> SpeedTest.attribute(input, "name")
{:ok, "test"}
Link to this function

attributes(server, node_id, options \\ [])

attributes(pid(), node_id(), options()) :: {:ok, map()} | :notfound

Returns all attributes for a dom node as a map

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, input} = page |> SpeedTest.get("input")
iex> page |> SpeedTest.attributes(input)
{:ok, %{"type" => "email", "data-test" => "login_email", "name" => "test"}}
Link to this function

clear(server, node, options \\ [])

clear(pid(), node_id(), options()) :: :ok | {:error, :timeout}

Clears a text input. Wrapper fro calling click on an input 3 times and typing backspace.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, email_input} = page |> SpeedTest.get("[data-test=login_email]")
iex> :ok = page |> SpeedTest.type(email_input, "testing@test.com")
iex> page |> SpeedTest.value(email_input)
{:ok, "testing@test.com"}
iex> page |> SpeedTest.clear(email_input)
:ok
iex> page |> SpeedTest.value(email_input)
{:ok, ""}
Link to this function

click(server, node_id, params \\ %{}, options \\ [])

click(pid(), node_id(), %{click_count: integer()} | %{}, options()) ::
  :ok | {:error, any()}

Clicks on a dom node. Can take in a params object with the number of times to click.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, button} = page |> SpeedTest.get("button")
iex> page |> SpeedTest.click(button)
:ok
iex> page |> SpeedTest.click(button, %{click_count: 2})
:ok
Link to this function

close(page)

close(pid()) :: true

Closes a page process. Note this does NOT stop the underlying chroxy chrome process

Examples

iex> page = SpeedTest.launch()
iex> SpeedTest.close(page)
true
Link to this function

current_url(server, options \\ [])

current_url(pid(), options()) :: binary()

Returns the current URL

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> page |> SpeedTest.current_url()
"http://localhost:8081/"
Link to this function

dimensions(server, params, options \\ [])

dimensions(pid(), %{width: number(), height: number()}, nil | options()) ::
  :ok | {:error, any()}

Changes the dimensions of a given page

Examples

iex> page = SpeedTest.launch() iex> page |> SpeedTest.dimensions(%{width: 1920, height: 1080}) :ok

Link to this function

focus(server, node_id, options \\ [])

focus(pid(), node_id(), options()) :: :ok | {:error, any()}

Focuses on a given node.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, id} = page |> SpeedTest.get("input")
iex> page |> SpeedTest.focus(id)
:ok
Link to this function

get(server, selector, options \\ [])

get(pid(), node_id(), options()) ::
  {:ok, node_id()} | {:error, :timeout} | {:error, any()}

Gets a node_id/0 to a given selector on the page.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, id} = page |> SpeedTest.get("input")
iex> is_integer(id)
true
iex> {:ok, id} = page |> SpeedTest.get("[data-test=login_email]")
iex> is_integer(id)
true
Link to this function

get_cookie(server, name, options \\ [])

get_cookie(pid(), binary(), options()) ::
  {:ok, SpeedTest.Cookie.t()} | {:error, :notfound}

Gets a cookie from the page

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> page |> SpeedTest.set_cookie(%SpeedTest.Cookie{domain: "localhost", name: "testing", value: "123"})
:ok
iex> page |> SpeedTest.get_cookie("testing")
%SpeedTest.Cookie{domain: "localhost", name: "testing", value: "123"}
Link to this function

goto(server, url, options \\ [])

goto(pid(), binary(), options()) :: :ok | {:error, :timeout} | {:error, any()}

Navigates to a given URL. This URL can be shorted by providing the configuration value :base_url

i.e. config :speed_test, base_url: "http://localhost:4003"

Examples

iex> page = SpeedTest.launch()
iex> page |> SpeedTest.goto("/")
:ok
Link to this function

intercept_request(server, method, url, options \\ [])

intercept_request(pid(), binary(), binary(), options()) ::
  :ok | {:error, :timeout}

Waits for a given network request to happen. Can be anything that shows up in the network tab from XHR request to CSS to javascript.

NOTE you do not want to use this on the home page since the goto() function already waits for page load and will not record that HTTP request.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> page |> SpeedTest.intercept_request("GET", "/app.js")
:ok
Link to this function

launch(page \\ %{id: UUID.uuid4()})

launch(atom() | %{id: any()}) :: pid()

Launch creates a new browser session inside headless chrome.

Examples

iex> page = SpeedTest.launch()
iex> is_pid(page)
true
Link to this function

pdf(server, params \\ %{}, options \\ [])

pdf(pid(), %{path: binary()} | %{}, options()) ::
  :ok | {:ok, binary()} | {:error, any()}

Creates a PDF from the current page. Optionally can be saved to a provided path.

Parameters

  • scale = 1,
  • displayHeaderFooter = false,
  • headerTemplate = '',
  • footerTemplate = '',
  • printBackground = false,
  • landscape = false,
  • pageRanges = '',
  • preferCSSPageSize = false,
  • margin = {},
  • path = null

Examples

iex> page = SpeedTest.launch()
iex> page |> SpeedTest.goto("/")
iex> {:ok, pdf} = page |> SpeedTest.pdf("/")
iex> is_binary(pdf) and String.length(pdf) > 0
true
Link to this function

property(server, node_id, property, options \\ [])

property(pid(), node_id(), binary(), options()) ::
  {:ok, binary()} | {:error, :notfound}

Gets arbitrary properties from a dom node.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, email_input} = page |> SpeedTest.get("[data-test=login_email]")
iex> page |> SpeedTest.property(email_input, "type")
{:ok, "email"}
Link to this function

screenshot(server, params \\ %{}, options \\ [])

screenshot(pid(), %{path: binary()} | %{}, options()) ::
  :ok | {:ok, binary()} | {:error, any()}

Captures a screenshot of the current window. Optionally saves the output to a given path

Examples

iex> page = SpeedTest.launch()
iex> page |> SpeedTest.goto("/")
iex> {:ok, png} = page |> SpeedTest.screenshot("/")
iex> is_binary(png) and String.length(png) > 0
true
Link to this function

set_cookie(server, cookie, options \\ [])

set_cookie(pid(), SpeedTest.Cookie.t(), options()) :: :ok | any()

Sets a cookie on the page.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> page |> SpeedTest.set_cookie(%SpeedTest.Cookie{domain: "localhost", name: "testing", value: "123"})
:ok
Link to this function

text(server, node_id, options \\ [])

text(pid(), node_id(), options()) :: {:ok, binary()} | {:error, :notfound}

Utility function for returning the text of a DOM node ("textContent" property)

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, header} = page |> SpeedTest.get("h1")
iex> page |> SpeedTest.text(header)
{:ok, "I am a website"}
Link to this function

type(server, node_id, text, options \\ [])

type(pid(), node_id(), binary(), options()) :: :ok | {:error, any()}

Types text in a given node.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, email_input} = page |> SpeedTest.get("[data-test=login_email]")
iex> :ok = page |> SpeedTest.type(email_input, "testing@test.com")
iex> page |> SpeedTest.value(email_input)
{:ok, "testing@test.com"}
Link to this function

value(server, node_id, options \\ [])

value(pid(), node_id(), options()) :: binary() | :notfound

Gets the "value" property from a given node.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> {:ok, email_input} = page |> SpeedTest.get("[data-test=login_email]")
iex> page |> SpeedTest.value(email_input)
{:ok, ""}
Link to this function

wait_for_url(server, url, options \\ [])

wait_for_url(pid(), binary(), options()) ::
  {:ok, any()} | {:error, :timeout} | {:error, any()}

Waits for the current URL to equal the provided one. Useful if you have a redirect that occurs after some user action.

Examples

iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> :ok = page |> SpeedTest.wait_for_url("/", retry: %SpeedTest.Retry{timeout: :timer.seconds(2)})
:ok