Skip to content

Commit 9e95e54

Browse files
authored
Allow setting the preferred JS package runner (npx, bunx) (#51)
* Support configuring the package runner and the assets directory * Deprecate `cli` option
1 parent 168b818 commit 9e95e54

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ config :phoenix_test,
1414
endpoint: PhoenixTest.Endpoint,
1515
otp_app: :phoenix_test_playwright,
1616
playwright: [
17-
cli: "priv/static/assets/node_modules/playwright/cli.js",
17+
assets_dir: "priv/static/assets",
1818
headless: System.get_env("PW_HEADLESS", "true") in ~w(t true),
1919
screenshot: System.get_env("PW_SCREENSHOT", "false") in ~w(t true),
2020
trace: System.get_env("PW_TRACE", "false") in ~w(t true),

lib/phoenix_test/playwright/case.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ defmodule PhoenixTest.Playwright.Case do
101101
Playwright.BrowserContext.stop_tracing(browser_context_id, path)
102102

103103
if config[:trace] == :open do
104-
cli_path = Path.join(File.cwd!(), Playwright.Port.cli_path())
105-
System.cmd(cli_path, ["show-trace", path])
104+
System.cmd(
105+
Playwright.Config.global(:runner),
106+
["playwright", "show-trace", path],
107+
cd: Playwright.Config.global(:assets_dir)
108+
)
106109
end
107110
end)
108111
end

lib/phoenix_test/playwright/config.ex

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@ schema =
1212
type: {:in, browsers},
1313
type_doc: "`#{Enum.map_join(browsers, " | ", &":#{&1}")}`"
1414
],
15+
runner: [
16+
default: "npx",
17+
type_spec: quote(do: binary()),
18+
type_doc: "`t:binary/0`",
19+
type: {:custom, PhoenixTest.Playwright.Config, :__validate_runner__, []},
20+
doc:
21+
"The JS package runner to use to run the Playwright CLI. Accepts either a binary executable exposed in PATH or the absolute path to it."
22+
],
23+
assets_dir: [
24+
default: "./assets",
25+
type: :string,
26+
doc: "The directory where the JS assets are located and the Playwright CLI is installed."
27+
],
1528
cli: [
16-
default: "assets/node_modules/playwright/cli.js",
17-
type: :string
29+
type: {:custom, PhoenixTest.Playwright.Config, :__validate_cli__, []},
30+
deprecated: "Use `assets_dir` instead."
1831
],
1932
executable_path: [
2033
type: :string,
@@ -65,7 +78,7 @@ schema =
6578
accept_dialogs: [
6679
default: true,
6780
type: :boolean,
68-
doc: "Accept browser dialogs (`alert()`, `confirm()`, `prompt()`"
81+
doc: "Accept browser dialogs (`alert()`, `confirm()`, `prompt()`)"
6982
]
7083
)
7184

@@ -119,4 +132,26 @@ defmodule PhoenixTest.Playwright.Config do
119132

120133
defp normalize(:screenshot, true), do: NimbleOptions.validate!([], @screenshot_opts_schema)
121134
defp normalize(_key, value), do: value
135+
136+
def __validate_runner__(runner) do
137+
if executable = System.find_executable(runner) do
138+
{:ok, executable}
139+
else
140+
message = """
141+
could not find runner executable at `#{runner}`.
142+
143+
To resolve this please
144+
1. Install a JS package runner like `npx` or `bunx`
145+
2. Configure the preferred runner in `config/test.exs`, e.g.: `config :phoenix_test, playwright: [runner: "npx"]`
146+
3. Ensure either the runner is in your PATH or the `runner` value is a absolute path to the executable (e.g. `Path.absname("_build/bun")`)
147+
"""
148+
149+
{:error, message}
150+
end
151+
end
152+
153+
def __validate_cli__(_cli) do
154+
{:error,
155+
"it is deprecated. Use `assets_dir` instead if you want to customize the Playwright installation directory path and remove the `cli` option."}
156+
end
122157
end

lib/phoenix_test/playwright/port.ex

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,14 @@ defmodule PhoenixTest.Playwright.Port do
1515
:buffer
1616
]
1717

18-
def cli_path do
19-
path = Config.global(:cli)
20-
21-
if !File.exists?(path) do
22-
msg = """
23-
Could not find playwright CLI at #{path}.
24-
25-
To resolve this please
26-
1. Install playwright, e.g. `npm i playwright`
27-
2. Configure the path correctly, e.g. in `config/test.exs`: `config :phoenix_test, playwright: [cli: "assets/node_modules/playwright/cli.js"]`
28-
"""
29-
30-
raise ArgumentError, msg
31-
end
32-
33-
path
34-
end
35-
3618
def open do
37-
port = Port.open({:spawn, "#{cli_path()} run-driver"}, [:binary])
19+
port =
20+
Port.open({:spawn_executable, Config.global(:runner)}, [
21+
:binary,
22+
args: ["playwright", "run-driver"],
23+
cd: Config.global(:assets_dir)
24+
])
25+
3826
%__MODULE__{port: port, remaining: 0, buffer: ""}
3927
end
4028

0 commit comments

Comments
 (0)