Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
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: :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
58 changes: 58 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,58 @@
defmodule PhoenixTest.Playwright.BrowserLaunchOptsTest do
@moduledoc """
Tests that browser_launch_opts are passed through to Playwright.

These tests verify that browser launch flags actually affect browser behavior
by testing getUserMedia with and without fake media device flags.
"""

use PhoenixTest.Playwright.Case,
async: true,
browser_pool: false,
browser_launch_opts: [
args: [
"--use-fake-ui-for-media-stream",
"--use-fake-device-for-media-stream"
]
]

test "getUserMedia succeeds with fake media device flags", %{conn: conn} do
conn
|> visit("/pw/live")
|> assert_has("h1")
|> evaluate(
"""
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => "success")
.catch(e => "error: " + e.name)
""",
&assert(&1 == "success")
)
end
end

defmodule PhoenixTest.Playwright.BrowserLaunchOptsWithoutFlagsTest do
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@moduledoc """
Tests that getUserMedia fails WITHOUT the fake media device flags.
This proves the flags in BrowserLaunchOptsTest actually have an effect.
"""

use PhoenixTest.Playwright.Case,
async: true,
browser_pool: false

test "getUserMedia fails without fake media device flags", %{conn: conn} do
conn
|> visit("/pw/live")
|> assert_has("h1")
|> evaluate(
"""
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => "success")
.catch(e => "error: " + e.name)
""",
# Without fake device flags, getUserMedia should fail in headless mode
&assert(&1 =~ "error:")
)
end
end
Loading