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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
mix deps.get
mix volt.priv.vendor priv/ts
npm ci
npx playwright install --with-deps chromium
MIX_ENV=test mix playwright.install chromium --with-deps
latest-command: MIX_ENV=dev mix lint && MIX_ENV=test mix test
min-command: mix compile --warnings-as-errors && mix test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ volt-*.tar
# Temporary files, for example, from tests.
/tmp/

# Playwright traces and screenshots of failed browser tests.
/test-results/

# Node tools used by local JS checks.
/node_modules/

Expand Down
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ config :volt,
tsgolint: "node_modules/.bin/tsgolint",
cwd: File.cwd!()
)

config :playwright, :test, trace: :retain_on_failure, screenshot: :only_on_failure
13 changes: 8 additions & 5 deletions guides/features/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,17 @@ test('updates the DOM', () => {
})
```

Volt runs these tests through PlaywrightEx. Install Playwright for local browser execution:
Volt runs these tests through [playwright_ex](https://hexdocs.pm/playwright_ex). Add it to your test dependencies and install the driver and a browser:

```elixir
{:playwright, "~> 1.65.0-rc.4", hex: :playwright_ex, only: :test}
```

```bash
npm install --save-dev playwright
npx playwright install chromium
MIX_ENV=test mix playwright.install chromium
```

By default Volt uses `node_modules/playwright/cli.js` when present, otherwise `playwright` from `PATH`. Pass PlaywrightEx supervisor options with `playwright: [...]` if your executable lives elsewhere.
The driver is configured with `config :playwright, :driver`. Pass `Playwright.Supervisor` options with `playwright: [...]`, such as `executable:` for an npm-managed `playwright` CLI.

A test run shares one browser, launched the first time a test needs it, and every test gets a browser context of its own, so tests do not see each other's storage, cookies or pages. The ExUnit modules Volt generates for browser tests are `async: true`. The browser runs under Volt's application, which the first browser test starts if the project has not, as with a `runtime: false` dependency.

Expand Down Expand Up @@ -356,4 +359,4 @@ Available sigils: `~JS`, `~TS`, `~JSX`, `~TSX`, `~CSS`, and `~HTML`.

## Current scope

The QuickBEAM runner is intended for fast JS/TS unit tests. The browser runner is intended for tests that genuinely need a browser environment. Higher-level Phoenix end-to-end flows can still use PlaywrightEx or Phoenix-oriented browser-test helpers alongside Volt's JS test runner.
The QuickBEAM runner is intended for fast JS/TS unit tests. The browser runner is intended for tests that genuinely need a browser environment. Higher-level Phoenix end-to-end flows can still use playwright_ex or Phoenix-oriented browser-test helpers alongside Volt's JS test runner.
71 changes: 29 additions & 42 deletions lib/volt/test/browser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Volt.Test.Browser do

@table __MODULE__

@type launched :: %{guid: String.t()}
@type launched :: term()

@doc false
def start_link(opts) do
Expand All @@ -33,37 +33,37 @@ defmodule Volt.Test.Browser do

@doc """
Open a context in the browser of `type`, with the test runtime installed
and a page on the blank document, and call `fun` with the page's main frame.
and a page on the blank document, and call `fun` with the page.

The context closes when `fun` returns or raises. A browser that is no longer
reachable, because it crashed since it was launched, is launched again once.
"""
@spec with_page(atom(), timeout(), (map() -> result)) :: result | {:error, term()}
@spec with_page(atom(), timeout(), (term() -> result)) :: result | {:error, term()}
when result: term()
def with_page(type, timeout, fun) do
with {:ok, browser} <- launch(type, timeout),
{:ok, context} <- new_context(browser, type, timeout) do
try do
with {:ok, _} <- browser_context_add_init_script(context.guid, runtime(), timeout),
{:ok, %{main_frame: frame}} <- browser_context_new_page(context.guid, timeout),
{:ok, _} <- frame_goto(frame.guid, page_url(), timeout) do
fun.(frame)
with {:ok, _} <- browser_context_add_init_script(context, runtime()),
{:ok, page} <- browser_context_new_page(context),
{:ok, _} <- page_goto(page, page_url(), timeout) do
fun.(page)
end
after
browser_context_close(context.guid, timeout)
browser_context_close(context)
end
end
end

defp new_context(browser, type, timeout) do
case browser_new_context(browser.guid, timeout) do
case browser_new_context(browser) do
{:ok, context} ->
{:ok, context}

{:error, _reason} ->
with :ok <- forget(type, browser.guid),
with :ok <- forget(type, browser),
{:ok, browser} <- launch(type, timeout) do
browser_new_context(browser.guid, timeout)
browser_new_context(browser)
end
end
end
Expand All @@ -75,9 +75,9 @@ defmodule Volt.Test.Browser do
end

@doc "Drop a browser that is no longer reachable, so the next request launches a new one."
@spec forget(atom(), String.t()) :: :ok
def forget(type, guid) do
GenServer.call(__MODULE__, {:forget, type, guid})
@spec forget(atom(), launched()) :: :ok
def forget(type, browser) do
GenServer.call(__MODULE__, {:forget, type, browser})
end

@doc """
Expand Down Expand Up @@ -178,9 +178,9 @@ defmodule Volt.Test.Browser do
end
end

def handle_call({:forget, type, guid}, _from, state) do
def handle_call({:forget, type, browser}, _from, state) do
case state.browsers do
%{^type => %{guid: ^guid}} ->
%{^type => ^browser} ->
{:reply, :ok, %{state | browsers: Map.delete(state.browsers, type)}}

_other ->
Expand All @@ -191,41 +191,28 @@ defmodule Volt.Test.Browser do
# The playwright_ex dependency is only present in test environments, so its
# functions are called without compile-time references.
defp launch_browser(type, timeout) do
apply(PlaywrightEx, :launch_browser, [type, [connection: Tree.connection(), timeout: timeout]])
playwright = apply(Playwright, :instance, [Tree.playwright()])
browser_type = apply(Playwright, type, [playwright])
apply(Playwright.BrowserType, :launch, [browser_type, [timeout: timeout]])
end

defp browser_new_context(browser_guid, timeout) do
apply(PlaywrightEx.Browser, :new_context, [
browser_guid,
[connection: Tree.connection(), timeout: timeout]
])
defp browser_new_context(browser) do
apply(Playwright.Browser, :new_context, [browser])
end

defp browser_context_add_init_script(context_guid, source, timeout) do
apply(PlaywrightEx.BrowserContext, :add_init_script, [
context_guid,
[source: source, connection: Tree.connection(), timeout: timeout]
])
defp browser_context_add_init_script(context, source) do
apply(Playwright.BrowserContext, :add_init_script, [context, source])
end

defp browser_context_new_page(context_guid, timeout) do
apply(PlaywrightEx.BrowserContext, :new_page, [
context_guid,
[connection: Tree.connection(), timeout: timeout]
])
defp browser_context_new_page(context) do
apply(Playwright.BrowserContext, :new_page, [context])
end

defp browser_context_close(context_guid, timeout) do
apply(PlaywrightEx.BrowserContext, :close, [
context_guid,
[connection: Tree.connection(), timeout: timeout]
])
defp browser_context_close(context) do
apply(Playwright.BrowserContext, :close, [context])
end

defp frame_goto(frame_guid, url, timeout) do
apply(PlaywrightEx.Frame, :goto, [
frame_guid,
[url: url, connection: Tree.connection(), timeout: timeout]
])
defp page_goto(page, url, timeout) do
apply(Playwright.Page, :goto, [page, url, [timeout: timeout]])
end
end
18 changes: 5 additions & 13 deletions lib/volt/test/browser/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Volt.Test.Browser.Supervisor do
children = [
%{
id: @playwright,
start: {PlaywrightEx.Supervisor, :start_link, [playwright_opts(config)]},
start: {Playwright.Supervisor, :start_link, [playwright_opts(config)]},
type: :supervisor
},
Volt.Test.Browser
Expand All @@ -64,19 +64,11 @@ defmodule Volt.Test.Browser.Supervisor do
Supervisor.init(children, strategy: :rest_for_one)
end

@doc "The Playwright connection the run's browsers belong to."
@spec connection() :: GenServer.name()
def connection, do: Module.concat(@playwright, "Connection")
@doc "The name of the Playwright driver the run's browsers belong to."
@spec playwright() :: atom()
def playwright, do: @playwright

defp playwright_opts(%Config{} = config) do
config.playwright
|> Keyword.put_new(:timeout, config.timeout)
|> Keyword.put_new(:executable, playwright_executable())
|> Keyword.put(:name, @playwright)
end

defp playwright_executable do
local = Path.expand("node_modules/playwright/cli.js", Volt.Paths.root())
if File.exists?(local), do: local, else: "playwright"
Keyword.put(config.playwright, :name, @playwright)
end
end
34 changes: 13 additions & 21 deletions lib/volt/test/browser_runner.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Volt.Test.BrowserRunner do
@moduledoc """
Executes Volt JavaScript and TypeScript tests inside a real browser via PlaywrightEx.
Executes Volt JavaScript and TypeScript tests inside a real browser via Playwright.

This runner is intentionally small and mirrors `Volt.Test.Runner`'s result
contract so ExUnit integration can switch between QuickBEAM and browser
Expand Down Expand Up @@ -42,10 +42,10 @@ defmodule Volt.Test.BrowserRunner do
timeout = Keyword.get(opts, :timeout, config.timeout)

with {:ok, _pid} <- Browser.Supervisor.start(config) do
Browser.with_page(browser(config), timeout, fn frame ->
Browser.with_page(browser(config), timeout, fn page ->
with {:ok, module_url} <-
Browser.module(path, Volt.Test.Shared.bundle_opts(path, config, opts)) do
evaluate(frame, module_url, path, mode, test_id, timeout)
evaluate(page, module_url, path, mode, test_id)
end
end)
end
Expand All @@ -54,28 +54,20 @@ defmodule Volt.Test.BrowserRunner do
defp browser(%Config{browsers: [browser | _]}), do: browser
defp browser(%Config{}), do: :chromium

# The page the run opens carries the test runtime, so the frame already has
# The page the run opens carries the test runtime, so it already has
# `__voltExecuteBrowserTest`.
defp evaluate(frame, test_url, file, mode, test_id, timeout) do
frame_evaluate(frame.guid,
expression: "payload => globalThis.__voltExecuteBrowserTest(payload)",
is_function: true,
arg: %{
"testUrl" => test_url,
"file" => file,
"mode" => Atom.to_string(mode),
"testId" => test_id
},
timeout: timeout
)
defp evaluate(page, test_url, file, mode, test_id) do
page_evaluate(page, "payload => globalThis.__voltExecuteBrowserTest(payload)", %{
"testUrl" => test_url,
"file" => file,
"mode" => Atom.to_string(mode),
"testId" => test_id
})
end

# The playwright_ex dependency is only present in test environments, so its
# functions are called without compile-time references.
defp frame_evaluate(frame_guid, opts) do
apply(PlaywrightEx.Frame, :evaluate, [
frame_guid,
[connection: Browser.Supervisor.connection()] ++ opts
])
defp page_evaluate(page, expression, arg) do
apply(Playwright.Page, :evaluate, [page, expression, arg])
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Volt.MixProject do
{:ex_doc, "~> 0.35", only: :dev, runtime: false},
{:makeup_js, "~> 0.1", only: :dev, runtime: false},
{:bandit, ">= 1.12.5 and < 2.0.0", only: :test},
{:playwright_ex, "~> 0.5", only: :test}
{:playwright, "~> 1.65.0-rc.4", hex: :playwright_ex, only: :test}
]
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"oxide_ex": {:hex, :oxide_ex, "0.2.2", "063e0d2899e81632b7efc7849eb1dd89a7a623290a075c446b43bef14cf5795e", [:mix], [{:rustler, "~> 0.36", [hex: :rustler, repo: "hexpm", optional: true]}, {:rustler_precompiled, "~> 0.8", [hex: :rustler_precompiled, repo: "hexpm", optional: false]}], "hexpm", "577a2fbd97c46904a31143bb700cdb374105085cfd1f7605416bb6c012cb7f60"},
"pegasus": {:hex, :pegasus, "0.2.6", "b4af6522326fbb2ffd1bb706e78ec05854fbcb4b03a7fe08f8db2e9de1d0be67", [:mix], [{:nimble_parsec, "~> 1.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0ac159f0ccab7967cf90208327cc8a35788874814c8d78e19d47104d3fc049b9"},
"pi_bridge": {:hex, :pi_bridge, "0.6.21", "f1a63da5f49fef4f6b35e98edd5400cbd64a49fb8bda84be41b7e91f3deb0b89", [:mix], [{:bandit, "~> 1.8", [hex: :bandit, repo: "hexpm", optional: false]}, {:dune, "~> 0.3", [hex: :dune, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.13", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ex_ast, "~> 0.12", [hex: :ex_ast, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:json_codec, "~> 0.1.3", [hex: :json_codec, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:quackdb, "~> 0.5.4", [hex: :quackdb, repo: "hexpm", optional: false]}, {:reach, "~> 2.6", [hex: :reach, repo: "hexpm", optional: false]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:req_llm, "~> 1.6", [hex: :req_llm, repo: "hexpm", optional: true]}], "hexpm", "b226fb60965a8e928186af3c3b96fe8f0fefd8c6cc7be679d6e169ba84105de9"},
"playwright_ex": {:hex, :playwright_ex, "0.7.1", "4a8a3f317734a5d20c4f25b5b425d565d05730166013f86f0c1a799714e61e2d", [:mix], [{:nimble_options, "~> 1.1", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:websockex, "~> 0.4", [hex: :websockex, repo: "hexpm", optional: true]}], "hexpm", "558176309cf322b8ba5c4f85ec6c655c1667cd68531db83d1d3065bd572755c2"},
"playwright": {:hex, :playwright_ex, "1.65.0-rc.4", "be9390181664ad361880c5da24771b0ea524add332c10cad1c9b0da9ed2e33ae", [:mix], [], "hexpm", "7505d4ba8260bc88c096f4a8c538d21639d7d71a7166f8d6e0afade506b569cc"},
"plug": {:hex, :plug, "1.20.3", "56c480c633ec2ce10140e236e15233bf576e1d323887d7c96711bd02ab5160db", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "be266aee1b8536ef6409d58cf39a3121319f0ec47cfa1b24024485aa0e76ad76"},
"plug_crypto": {:hex, :plug_crypto, "2.2.0", "144014737daaf485407f5ed77daeaad74d651b216a28c87543f8cc7043f8efc8", [:mix], [], "hexpm", "83a95744ab1c75876542b6fab135fcc176280e0f301a111c1f757fddcec95d2c"},
"protoss": {:hex, :protoss, "1.1.0", "853533313989751c7da5b0e1ce71501a23f3dc41f128709478af40daccc6e959", [:mix], [], "hexpm", "c2f874383dd047fcfdf467b814dd33a208a4d24a467aef90d86185b41a0752ad"},
Expand Down
50 changes: 1 addition & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"private": true,
"devDependencies": {
"oxlint-tsgolint": "0.22.1",
"playwright": "1.61.1"
"oxlint-tsgolint": "0.22.1"
},
"scripts": {
"postinstall": "node scripts/postinstall.mjs"
Expand Down
Loading