Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ defmodule DebuggingFeatureTest do
end
```

### Browser Launch Options

Pass additional arguments to the browser via `browser_launch_opts`. This is useful for
testing features that require specific browser flags, like fake media streams for
audio/video capture tests:

```elixir
# config/test.exs
config :phoenix_test,
playwright: [
browser_launch_opts: [
args: [
"--use-fake-ui-for-media-stream",
"--use-fake-device-for-media-stream"
]
]
]
```

See [Playwright's browserType.launch() docs](https://playwright.dev/docs/api/class-browsertype#browser-type-launch)
for all available options.


## Remote Playwright Server

Expand Down
13 changes: 11 additions & 2 deletions lib/phoenix_test/playwright/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ browser_opts = [
type: {:in, browsers},
type_doc: "`#{Enum.map_join(browsers, " | ", &":#{&1}")}`"
],
browser_launch_opts: [
default: [],
type: {:or, [:map, :keyword_list]},
doc: """
Additional arguments passed to Playwright [browserType.launch](https://playwright.dev/docs/api/class-browsertype#browser-type-launch).
E.g. `[args: ["--use-fake-ui-for-media-stream", "--use-fake-device-for-media-stream"]]`.
"""
],
browser_launch_timeout: [
default: to_timeout(second: 4),
type: :non_neg_integer
Expand Down Expand Up @@ -60,6 +68,7 @@ schema_opts = [
"""
],
browser: browser_opts[:browser],
browser_launch_opts: browser_opts[:browser_launch_opts],
browser_context_opts: [
default: [],
type: {:or, [:map, :keyword_list]},
Expand Down Expand Up @@ -172,9 +181,9 @@ schema_opts = [

schema = NimbleOptions.new!(schema_opts)

setup_all_keys = ~w(browser_pool browser browser_launch_timeout executable_path headless slow_mo)a
setup_all_keys = ~w(browser_pool browser browser_launch_opts browser_launch_timeout executable_path headless slow_mo)a
setup_keys = ~w(accept_dialogs ecto_sandbox_stop_owner_delay screenshot trace browser_context_opts browser_page_opts)a
merge_global_into_browser_pool_keys = ~w(browser browser_launch_timeout headless slow_mo)a
merge_global_into_browser_pool_keys = ~w(browser browser_launch_opts browser_launch_timeout headless slow_mo)a

defmodule PhoenixTest.Playwright.Config do
@moduledoc """
Expand Down
2 changes: 2 additions & 0 deletions lib/phoenix_test/playwright/internal/browser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule PhoenixTest.Playwright.Browser do
def launch_browser!(config) do
{launch_timeout, opts} = Keyword.pop!(config, :browser_launch_timeout)
{browser, opts} = Keyword.pop!(opts, :browser)
{launch_opts, opts} = Keyword.pop(opts, :browser_launch_opts, [])
opts = opts |> Keyword.put(:timeout, launch_timeout) |> Keyword.delete(:browser_pool)
opts = Keyword.merge(opts, launch_opts)

case PlaywrightEx.launch_browser(browser, opts) do
{:ok, browser} ->
Expand Down
18 changes: 18 additions & 0 deletions test/phoenix_test/playwright/browser_launch_opts_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule PhoenixTest.Playwright.BrowserLaunchOptsTest do
@moduledoc """
Tests that browser_launch_opts are passed through to Playwright.
"""

use PhoenixTest.Playwright.Case,
async: true,
browser_pool: false,
browser_launch_opts: [args: ["--disable-background-networking"]]

test "launches browser with custom launch opts", %{conn: conn} do
# If browser_launch_opts weren't handled correctly, the browser launch would fail.
# This test verifies the option is properly passed through the launch pipeline.
conn
|> visit("/pw/live")
|> assert_has("h1")
end
end
26 changes: 26 additions & 0 deletions test/phoenix_test/playwright/config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule PhoenixTest.Playwright.ConfigTest do
use ExUnit.Case, async: true

alias PhoenixTest.Playwright.Config

describe "browser_launch_opts" do
test "accepts keyword list" do
config = Config.validate!(browser_launch_opts: [args: ["--disable-gpu"]])
assert config[:browser_launch_opts] == [args: ["--disable-gpu"]]
end

test "accepts map" do
config = Config.validate!(browser_launch_opts: %{args: ["--disable-gpu"]})
assert config[:browser_launch_opts] == %{args: ["--disable-gpu"]}
end

test "defaults to empty list" do
config = Config.validate!([])
assert config[:browser_launch_opts] == []
end

test "is included in setup_all_keys" do
assert :browser_launch_opts in Config.setup_all_keys()
end
end
end