Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/playwright_ex/processes/port_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule PlaywrightEx.PortTransport do
Start the PortTransport and link it to the connection process.
"""
def start_link(opts) do
opts = Keyword.validate!(opts, [:executable, :name, :connection_name])
opts = Keyword.validate!(opts, [:executable, :name, :connection_name, :env])
name = Keyword.get(opts, :name, @default_name)
GenServer.start_link(__MODULE__, Map.new(opts), name: name)
end
Expand All @@ -42,12 +42,19 @@ defmodule PlaywrightEx.PortTransport do
end

@impl GenServer
def init(%{executable: executable} = opts) do
def init(%{executable: executable, env: env} = opts) when map_size(env) == 0 do
port = Port.open({:spawn_executable, executable}, [:binary, :stderr_to_stdout, args: ["run-driver"]])
connection_name = Map.get(opts, :connection_name, Connection)
{:ok, %__MODULE__{port: port, connection_name: connection_name}}
end

def init(%{executable: executable, env: env} = opts) when map_size(env) > 0 do
env = Enum.map(env, fn {k, v} -> {maybe_charlist(k), maybe_charlist(v)} end)
port = Port.open({:spawn_executable, executable}, [:binary, :stderr_to_stdout, {:args, ["run-driver"]}, {:env, env}])
connection_name = Map.get(opts, :connection_name, Connection)
{:ok, %__MODULE__{port: port, connection_name: connection_name}}
end

@impl GenServer
def handle_cast({:post, msg}, state) do
frame = to_json(msg)
Expand Down Expand Up @@ -105,4 +112,7 @@ defmodule PlaywrightEx.PortTransport do
|> Serialization.deep_key_underscore()
|> Map.update(:method, nil, &Serialization.underscore/1)
end

defp maybe_charlist(value) when is_list(value), do: value
defp maybe_charlist(value) when is_binary(value), do: String.to_charlist(value)
end
8 changes: 5 additions & 3 deletions lib/playwright_ex/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule PlaywrightEx.Supervisor do
If provided, uses WebSocket transport. Otherwise uses local Port.
If no browser param is provided, `chromium` is used by default.
- `:executable` - Path to playwright CLI (only for Port transport)
- `:env` - A `%{String.t() => String.t()}` map of environment variables to set for the browser instance (only for Port transport).
- `:timeout` - Connection timeout
- `:js_logger` - Module for logging JS console messages
- `:name` - Optional name for this supervisor instance. Defaults to `PlaywrightEx.Supervisor`.
Expand Down Expand Up @@ -43,7 +44,8 @@ defmodule PlaywrightEx.Supervisor do
:fail_on_unknown_opts,
executable: "playwright",
js_logger: nil,
name: __MODULE__
name: __MODULE__,
env: %{}
])

Supervisor.start_link(__MODULE__, Map.new(opts), name: opts[:name])
Expand Down Expand Up @@ -100,12 +102,12 @@ defmodule PlaywrightEx.Supervisor do
{child_spec, {WebSocketTransport, transport_name}}
end

defp transport_child_spec(%{executable: executable, name: name}, connection_name) do
defp transport_child_spec(%{executable: executable, name: name, env: env}, connection_name) do
executable = validate_executable!(executable)

transport_name = Module.concat(name, "PortTransport")

child_spec = {PortTransport, executable: executable, name: transport_name, connection_name: connection_name}
child_spec = {PortTransport, executable: executable, name: transport_name, connection_name: connection_name, env: env}
{child_spec, {PortTransport, transport_name}}
end

Expand Down