Tungsten

Documentation | Hex.pm

Tungsten is a library for browser automation using the Chrome DevTools Protocol for use in Elixir projects. It offers a low-level API to communicate with the Chrome DevTools Protocol. It can be used for end-to-end testing, for testing external dependencies, to automate tasks on websites without API, for scraping and for PDF generation.

In Active Development

This package is currently under heavy development and shouldn't be considered stable.

Installation

To install Tungsten, add tungsten to your list of dependencies in mix.exs:

def deps do
  [
    {:tungsten, "~> 0.1.0"}
  ]
end

Basic Usage

Assuming you have a browser running with --remote-debugging-port=9222, you can fetch the webSocketDebuggerUrl from http://localhost:9222/json/version.

alias Tungsten.{CDP.Target, CDP.Runtime, Connection, Transport.Websocket}

# Start Connection
transport = {Websocket, url: websocket_debugger_url}
{:ok, conn} = Connection.start_link(transport: transport)

# Create CDP session and attach
{:ok, %{target_id: target_id}} = Target.create_target(conn, %{url: "https://google.com"})
{:ok, %{session_id: session_id}} = Target.attach_to_target(conn, %{target_id: target_id, flatten: true})

# Execute commands on a CDP session
{:ok, %{result: %{type: :string, value: "Mozilla/5.0 ..."}}} =
  Runtime.evaluate(conn, %{expression: "navigator.userAgent"}, session_id: session_id)