speed_test v0.1.2 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 section Functions
attribute(server, node_id, attribute, options \\ [])
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"}
attributes(server, node_id, options \\ [])
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"}}
clear(server, node, options \\ [])
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, ""}
click(server, node_id, params \\ %{}, options \\ [])
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
Closes a page process. Note this does NOT stop the underlying chroxy chrome process
Examples
iex> page = SpeedTest.launch()
iex> SpeedTest.close(page)
true
Returns the current URL
Examples
iex> page = SpeedTest.launch()
iex> :ok = page |> SpeedTest.goto("/")
iex> page |> SpeedTest.current_url()
"http://localhost:8081/"
dimensions(server, params, options \\ [])
Changes the dimensions of a given page
Examples
iex> page = SpeedTest.launch() iex> page |> SpeedTest.dimensions(%{width: 1920, height: 1080}) :ok
focus(server, node_id, options \\ [])
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
get(server, selector, options \\ [])
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
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"}
goto(server, url, options \\ [])
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
intercept_request(server, method, url, options \\ [])
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> {:ok, %{"request" => %{"url" => "http://localhost:8081/app.js"}, "response" => %{"status" => 200}}} = page |> SpeedTest.intercept_request("GET", "/app.js")
Launch creates a new browser session inside headless chrome.
Examples
iex> page = SpeedTest.launch()
iex> is_pid(page)
true
pdf(server, params \\ %{}, options \\ [])
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
property(server, node_id, property, options \\ [])
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"}
screenshot(server, params \\ %{}, options \\ [])
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
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
text(server, node_id, options \\ [])
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"}
type(server, node_id, text, options \\ [])
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"}
value(server, node_id, options \\ [])
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, ""}
wait_for_url(server, url, options \\ [])
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