From ec9b47b3b2cd836ede9f3b7c2f4346141ba4171a Mon Sep 17 00:00:00 2001 From: Fredrik Teschke Date: Mon, 23 Nov 2026 17:44:23 +0100 Subject: [PATCH 1/2] Bump playwright_ex to 1.65.0-rc.4 --- .github/workflows/ci.yml | 2 +- guides/features/testing.md | 13 +- lib/volt/test/browser.ex | 71 ++++----- lib/volt/test/browser/supervisor.ex | 18 +-- lib/volt/test/browser_runner.ex | 34 ++-- mix.exs | 2 +- mix.lock | 2 +- package-lock.json | 50 +----- package.json | 3 +- test/volt/integration/hmr_test.exs | 234 +++++++++++----------------- 10 files changed, 153 insertions(+), 276 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 786f701..10e25b3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/guides/features/testing.md b/guides/features/testing.md index 41fd6b0..d00923f 100644 --- a/guides/features/testing.md +++ b/guides/features/testing.md @@ -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. @@ -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. diff --git a/lib/volt/test/browser.ex b/lib/volt/test/browser.ex index eed2a6d..944d8a4 100644 --- a/lib/volt/test/browser.ex +++ b/lib/volt/test/browser.ex @@ -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 @@ -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 @@ -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 """ @@ -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 -> @@ -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 diff --git a/lib/volt/test/browser/supervisor.ex b/lib/volt/test/browser/supervisor.ex index 0a8a56f..6e39a0e 100644 --- a/lib/volt/test/browser/supervisor.ex +++ b/lib/volt/test/browser/supervisor.ex @@ -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 @@ -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 diff --git a/lib/volt/test/browser_runner.ex b/lib/volt/test/browser_runner.ex index ebaf8fb..4e69628 100644 --- a/lib/volt/test/browser_runner.ex +++ b/lib/volt/test/browser_runner.ex @@ -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 @@ -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 @@ -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 diff --git a/mix.exs b/mix.exs index 6ef2c18..547432c 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/mix.lock b/mix.lock index fb88ccb..c2861d3 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, diff --git a/package-lock.json b/package-lock.json index e882159..cdc3212 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,8 +6,7 @@ "": { "hasInstallScript": true, "devDependencies": { - "oxlint-tsgolint": "0.22.1", - "playwright": "1.61.1" + "oxlint-tsgolint": "0.22.1" } }, "node_modules/@oxlint-tsgolint/darwin-arm64": { @@ -94,21 +93,6 @@ "win32" ] }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "hasInstallScript": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/oxlint-tsgolint": { "version": "0.22.1", "resolved": "https://registry.npmjs.org/oxlint-tsgolint/-/oxlint-tsgolint-0.22.1.tgz", @@ -126,38 +110,6 @@ "@oxlint-tsgolint/win32-arm64": "0.22.1", "@oxlint-tsgolint/win32-x64": "0.22.1" } - }, - "node_modules/playwright": { - "version": "1.61.1", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.61.1.tgz", - "integrity": "sha512-DWnY5o3YbLWK4GovuAVwpqL+1VwGNdUGrRr++8j8PtQQzvAVZUIMjKQ90fY689sEJZJBbZVw1rXaOKSTitkzPQ==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "playwright-core": "1.61.1" - }, - "bin": { - "playwright": "cli.js" - }, - "engines": { - "node": ">=18" - }, - "optionalDependencies": { - "fsevents": "2.3.2" - } - }, - "node_modules/playwright-core": { - "version": "1.61.1", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.61.1.tgz", - "integrity": "sha512-h7Qlt6m4REp25qvIdvbDtVmD4LqVXfpRxhORv9L0jzETM05p4fuPJ3dKyuSXQxDSbXnmS79HAgi9589lGSpLkg==", - "dev": true, - "license": "Apache-2.0", - "bin": { - "playwright-core": "cli.js" - }, - "engines": { - "node": ">=18" - } } } } diff --git a/package.json b/package.json index 27a24b0..e3fa5dd 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/volt/integration/hmr_test.exs b/test/volt/integration/hmr_test.exs index 41fbb4a..ed5457b 100644 --- a/test/volt/integration/hmr_test.exs +++ b/test/volt/integration/hmr_test.exs @@ -117,53 +117,18 @@ defmodule Volt.Integration.HMRTest do @moduletag :integration - alias PlaywrightEx.{Browser, BrowserContext, Frame} + alias Playwright.{Browser, BrowserContext, BrowserType, Page} @fixture_dir Path.join(System.tmp_dir!(), "volt-integration-hmr-test") @port 44_831 setup_all do - ensure_playwright_started!() - {:ok, browser} = launch_browser!() - on_exit(fn -> :ok end) - %{browser: browser} - end - - defp ensure_playwright_started! do - case PlaywrightEx.Supervisor.start_link(timeout: 10_000, executable: playwright_executable()) do - {:ok, _pid} -> :ok - {:error, {:already_started, _pid}} -> :ok - end - end - - defp launch_browser! do - case PlaywrightEx.launch_browser(:chromium, timeout: 10_000) do - {:ok, browser} -> - {:ok, browser} - - {:error, _reason} -> - restart_playwright!() - PlaywrightEx.launch_browser(:chromium, timeout: 10_000) - end - catch - :exit, _reason -> - restart_playwright!() - PlaywrightEx.launch_browser(:chromium, timeout: 10_000) - end + {:ok, _pid} = Playwright.Supervisor.start_link() - defp restart_playwright! do - case Process.whereis(PlaywrightEx.Supervisor) do - nil -> :ok - pid -> Process.exit(pid, :kill) - end + browser = + Playwright.instance() |> Playwright.chromium() |> BrowserType.launch!(timeout: 10_000) - Process.sleep(100) - ensure_playwright_started!() - end - - defp playwright_executable do - local = Path.expand("node_modules/playwright/cli.js") - if File.exists?(local), do: local, else: "playwright" + %{browser: browser} end setup %{browser: browser} do @@ -219,31 +184,28 @@ defmodule Volt.Integration.HMRTest do on_exit(fn -> Process.exit(server, :normal) end) - {:ok, context} = Browser.new_context(browser.guid, timeout: 5000) - {:ok, %{main_frame: frame}} = BrowserContext.new_page(context.guid, timeout: 5000) + {:ok, context} = Browser.new_context(browser) + {:ok, page} = BrowserContext.new_page(context) on_exit(fn -> - BrowserContext.close(context.guid, timeout: 5000) + BrowserContext.close(context) File.rm_rf!(@fixture_dir) end) - %{frame: frame, context: context} + %{page: page, context: context} end describe "dev server module serving" do - test "serves compiled TypeScript and executes in browser", %{frame: frame} do - {:ok, _} = Frame.goto(frame.guid, url: base_url(), timeout: 10_000) + test "serves compiled TypeScript and executes in browser", %{page: page} do + {:ok, _} = Page.goto(page, base_url(), timeout: 10_000) - {:ok, text} = - Frame.wait_for_function(frame.guid, - expression: "document.getElementById('volt-test')?.textContent", + {:ok, _handle} = + Page.wait_for_function(page, "document.getElementById('volt-test')?.textContent", nil, timeout: 5000 ) - - assert text end - test "injects import.meta.hot into served modules", %{frame: frame} do + test "injects import.meta.hot into served modules", %{page: page} do write_fixture("hot_check.ts", """ window.__voltHotAvailable = typeof import.meta.hot?.accept === 'function' """) @@ -255,13 +217,13 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/hot_page.html"), timeout: 10_000) + {:ok, _} = Page.goto(page, base_url("/hot_page.html"), timeout: 10_000) - {:ok, result} = eval_poll(frame, "window.__voltHotAvailable") + {:ok, result} = eval_poll(page, "window.__voltHotAvailable") assert result == true end - test "rewrites relative imports between modules", %{frame: frame} do + test "rewrites relative imports between modules", %{page: page} do write_fixture("utils.ts", """ export const greeting = 'hello from utils' """) @@ -278,13 +240,13 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/import_page.html"), timeout: 10_000) + {:ok, _} = Page.goto(page, base_url("/import_page.html"), timeout: 10_000) - {:ok, result} = eval_poll(frame, "window.__voltImportResult") + {:ok, result} = eval_poll(page, "window.__voltImportResult") assert result == "hello from utils" end - test "imports CSS from JavaScript and injects a style tag", %{frame: frame} do + test "imports CSS from JavaScript and injects a style tag", %{page: page} do write_fixture("imported.css", """ .volt-css-import-target { color: rgb(10, 20, 30); } """) @@ -306,14 +268,14 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/css_import_page.html"), timeout: 10_000) + {:ok, _} = Page.goto(page, base_url("/css_import_page.html"), timeout: 10_000) - {:ok, loaded} = eval_poll(frame, "window.__voltCssImportLoaded") + {:ok, loaded} = eval_poll(page, "window.__voltCssImportLoaded") assert loaded == true {:ok, style_count} = eval_poll( - frame, + page, "document.querySelectorAll('style[data-volt-id=\"/assets/imported.css\"]').length" ) @@ -321,14 +283,14 @@ defmodule Volt.Integration.HMRTest do {:ok, color} = eval_poll( - frame, + page, "getComputedStyle(document.querySelector('.volt-css-import-target')).color" ) assert color == "rgb(10, 20, 30)" end - test "self-accepting modules update without reloading and preserve hot data", %{frame: frame} do + test "self-accepting modules update without reloading and preserve hot data", %{page: page} do write_fixture("self_accept.ts", """ const store = (window.__voltSelfAccept ??= { events: [] }) export const value = 'one' @@ -360,8 +322,8 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/self_accept_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(frame, "document.getElementById('self-accept')?.textContent") + {:ok, _} = Page.goto(page, base_url("/self_accept_page.html"), timeout: 10_000) + {:ok, initial} = eval_poll(page, "document.getElementById('self-accept')?.textContent") assert initial == "one:none" {:ok, watcher} = start_watcher() @@ -369,11 +331,11 @@ defmodule Volt.Integration.HMRTest do update_fixture("self_accept.ts", &String.replace(&1, "'one'", "'two'")) assert {:ok, "two:one"} = - eval_until(frame, "document.getElementById('self-accept')?.textContent", "two:one") + eval_until(page, "document.getElementById('self-accept')?.textContent", "two:one") assert {:ok, true} = eval_until( - frame, + page, "window.__voltSelfAccept.events.includes('dispose:one') && window.__voltSelfAccept.events.includes('accept:two')", true ) @@ -381,7 +343,7 @@ defmodule Volt.Integration.HMRTest do GenServer.stop(watcher) end - test "parent modules accept dependency updates without reloading", %{frame: frame} do + test "parent modules accept dependency updates without reloading", %{page: page} do write_fixture("accepted_dep.ts", """ export const value = 'child-one' """) @@ -413,8 +375,8 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/accept_dep_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(frame, "document.getElementById('accepted-dep')?.textContent") + {:ok, _} = Page.goto(page, base_url("/accept_dep_page.html"), timeout: 10_000) + {:ok, initial} = eval_poll(page, "document.getElementById('accepted-dep')?.textContent") assert initial == "child-one" {:ok, watcher} = start_watcher() @@ -423,25 +385,25 @@ defmodule Volt.Integration.HMRTest do assert {:ok, "child-two"} = eval_until( - frame, + page, "document.getElementById('accepted-dep')?.textContent", "child-two" ) assert {:ok, true} = eval_until( - frame, + page, "window.__voltAcceptedDep.events.includes('accepted:child-two')", true ) - {:ok, reloads} = eval_poll(frame, "window.__voltAcceptedDep.reloads") + {:ok, reloads} = eval_poll(page, "window.__voltAcceptedDep.reloads") assert reloads == 1 GenServer.stop(watcher) end - test "parent modules accept multiple dependency updates", %{frame: frame} do + test "parent modules accept multiple dependency updates", %{page: page} do write_fixture("multi_dep_a.ts", "export const value = 'a-one'") write_fixture("multi_dep_b.ts", "export const value = 'b-one'") @@ -474,10 +436,10 @@ defmodule Volt.Integration.HMRTest do """) {:ok, _} = - Frame.goto(frame.guid, url: base_url("/multi_accept_dep_page.html"), timeout: 10_000) + Page.goto(page, base_url("/multi_accept_dep_page.html"), timeout: 10_000) {:ok, initial} = - eval_poll(frame, "document.getElementById('multi-accepted-dep')?.textContent") + eval_poll(page, "document.getElementById('multi-accepted-dep')?.textContent") assert initial == "a-one:b-one" @@ -487,25 +449,25 @@ defmodule Volt.Integration.HMRTest do assert {:ok, "a-two:b-one"} = eval_until( - frame, + page, "document.getElementById('multi-accepted-dep')?.textContent", "a-two:b-one" ) assert {:ok, true} = eval_until( - frame, + page, "window.__voltMultiDep.events.includes('accepted:a-two:b-one')", true ) - {:ok, reloads} = eval_poll(frame, "window.__voltMultiDep.reloads") + {:ok, reloads} = eval_poll(page, "window.__voltMultiDep.reloads") assert reloads == 1 GenServer.stop(watcher) end - test "content changes reload only the pages whose HTML changed", %{frame: frame} do + test "content changes reload only the pages whose HTML changed", %{page: page} do # Content lives beside the asset root, as a site's pages and posts do. content_dir = @fixture_dir <> "-content" File.rm_rf!(content_dir) @@ -514,8 +476,8 @@ defmodule Volt.Integration.HMRTest do File.write!(Path.join(content_dir, "first.md"), "first page") File.write!(Path.join(content_dir, "second.md"), "second page") - {:ok, _} = Frame.goto(frame.guid, url: base_url("/doc/first"), timeout: 10_000) - assert {:ok, "1"} = eval_until(frame, "document.body.dataset.loads", "1") + {:ok, _} = Page.goto(page, base_url("/doc/first"), timeout: 10_000) + assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") {:ok, watcher} = Volt.Watcher.start_link( @@ -529,37 +491,37 @@ defmodule Volt.Integration.HMRTest do # Another page's content: this page revalidates and stays. File.write!(Path.join(content_dir, "second.md"), "second page, edited") Process.sleep(1000) - assert {:ok, "1"} = eval_poll(frame, "document.body.dataset.loads") + assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") assert {:ok, "first page"} = - eval_poll(frame, "document.getElementById('content').textContent") + eval_poll(page, "document.getElementById('content').textContent") File.write!(Path.join(content_dir, "first.md"), "first page, edited") assert {:ok, "first page, edited"} = eval_until( - frame, + page, "document.getElementById('content')?.textContent", "first page, edited" ) - assert {:ok, "2"} = eval_poll(frame, "document.body.dataset.loads") + assert {:ok, "2"} = eval_poll(page, "document.body.dataset.loads") GenServer.stop(watcher) end - test "pages that opt in are patched in place instead of reloaded", %{frame: frame} do + test "pages that opt in are patched in place instead of reloaded", %{page: page} do content_dir = @fixture_dir <> "-content" File.rm_rf!(content_dir) File.mkdir_p!(content_dir) on_exit(fn -> File.rm_rf!(content_dir) end) File.write!(Path.join(content_dir, "first.md"), "first page") - {:ok, _} = Frame.goto(frame.guid, url: base_url("/morph/first"), timeout: 10_000) - assert {:ok, "1"} = eval_until(frame, "document.body.dataset.loads", "1") + {:ok, _} = Page.goto(page, base_url("/morph/first"), timeout: 10_000) + assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") # State a reload would lose. - {:ok, _} = eval_poll(frame, "(window.voltKept = 'kept')") + {:ok, _} = eval_poll(page, "(window.voltKept = 'kept')") {:ok, watcher} = Volt.Watcher.start_link( @@ -573,44 +535,44 @@ defmodule Volt.Integration.HMRTest do assert {:ok, "first page, edited"} = eval_until( - frame, + page, "document.getElementById('content')?.textContent", "first page, edited" ) - assert {:ok, "kept"} = eval_poll(frame, "window.voltKept") - assert {:ok, "1"} = eval_poll(frame, "document.body.dataset.loads") + assert {:ok, "kept"} = eval_poll(page, "window.voltKept") + assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") # The patched page revalidates against its new HTML. File.write!(Path.join(content_dir, "first.md"), "first page, edited twice") assert {:ok, "first page, edited twice"} = eval_until( - frame, + page, "document.getElementById('content')?.textContent", "first page, edited twice" ) - assert {:ok, "kept"} = eval_poll(frame, "window.voltKept") + assert {:ok, "kept"} = eval_poll(page, "window.voltKept") GenServer.stop(watcher) end - test "the server pushes what it changed to a page it keeps a render of", %{frame: frame} do + test "the server pushes what it changed to a page it keeps a render of", %{page: page} do content_dir = @fixture_dir <> "-content" File.rm_rf!(content_dir) File.mkdir_p!(content_dir) on_exit(fn -> File.rm_rf!(content_dir) end) File.write!(Path.join(content_dir, "page.md"), "first|1|en|old description") - {:ok, _} = Frame.goto(frame.guid, url: base_url("/push/page"), timeout: 10_000) - assert {:ok, "1"} = eval_until(frame, "document.body.dataset.loads", "1") - {:ok, _} = eval_poll(frame, "(window.voltKept = 'kept')") + {:ok, _} = Page.goto(page, base_url("/push/page"), timeout: 10_000) + assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") + {:ok, _} = eval_poll(page, "(window.voltKept = 'kept')") # The page requests nothing more: what changed arrives over the websocket. {:ok, _} = eval_poll( - frame, + page, "(window.voltFetches = 0, window.fetch = ((f) => (...a) => (window.voltFetches++, f(...a)))(window.fetch), 0)" ) @@ -625,30 +587,30 @@ defmodule Volt.Integration.HMRTest do File.write!(Path.join(content_dir, "page.md"), "second|2|ru|new description") assert {:ok, "second"} = - eval_until(frame, "document.getElementById('content')?.textContent", "second") + eval_until(page, "document.getElementById('content')?.textContent", "second") # The owner of the element re-rendered it from the props the server changed. assert {:ok, "props:2"} = - eval_until(frame, "document.getElementById('owned')?.textContent", "props:2") + eval_until(page, "document.getElementById('owned')?.textContent", "props:2") # Attributes the server changed are applied; one a script set stays. - assert {:ok, "ru"} = eval_poll(frame, "document.documentElement.lang") - assert {:ok, "dark"} = eval_poll(frame, "document.documentElement.dataset.theme") + assert {:ok, "ru"} = eval_poll(page, "document.documentElement.lang") + assert {:ok, "dark"} = eval_poll(page, "document.documentElement.dataset.theme") assert {:ok, "new description"} = - eval_poll(frame, "document.querySelector('meta[name=description]').content") + eval_poll(page, "document.querySelector('meta[name=description]').content") assert {:ok, 1} = - eval_poll(frame, "document.querySelectorAll('meta[name=description]').length") + eval_poll(page, "document.querySelectorAll('meta[name=description]').length") - assert {:ok, "kept"} = eval_poll(frame, "window.voltKept") - assert {:ok, "1"} = eval_poll(frame, "document.body.dataset.loads") - assert {:ok, 0} = eval_poll(frame, "window.voltFetches") + assert {:ok, "kept"} = eval_poll(page, "window.voltKept") + assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") + assert {:ok, 0} = eval_poll(page, "window.voltFetches") GenServer.stop(watcher) end - test "non-accepted module updates trigger a full reload", %{frame: frame} do + test "non-accepted module updates trigger a full reload", %{page: page} do write_fixture("full_reload.ts", """ const count = Number(sessionStorage.getItem('voltReloadCount') ?? '0') + 1 sessionStorage.setItem('voltReloadCount', String(count)) @@ -665,8 +627,8 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/full_reload_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(frame, "document.getElementById('full-reload')?.textContent") + {:ok, _} = Page.goto(page, base_url("/full_reload_page.html"), timeout: 10_000) + {:ok, initial} = eval_poll(page, "document.getElementById('full-reload')?.textContent") assert initial == "before:1" {:ok, watcher} = start_watcher() @@ -674,12 +636,12 @@ defmodule Volt.Integration.HMRTest do update_fixture("full_reload.ts", &String.replace(&1, "before", "after")) assert {:ok, "after:2"} = - eval_until(frame, "document.getElementById('full-reload')?.textContent", "after:2") + eval_until(page, "document.getElementById('full-reload')?.textContent", "after:2") GenServer.stop(watcher) end - test "CSS import HMR updates injected styles", %{frame: frame} do + test "CSS import HMR updates injected styles", %{page: page} do write_fixture("hmr-style.css", ".hmr-style { color: rgb(1, 2, 3); }") write_fixture("css_hmr.ts", """ @@ -700,13 +662,13 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/css_hmr_page.html"), timeout: 10_000) + {:ok, _} = Page.goto(page, base_url("/css_hmr_page.html"), timeout: 10_000) {:ok, initial_color} = - eval_poll(frame, "getComputedStyle(document.querySelector('.hmr-style')).color") + eval_poll(page, "getComputedStyle(document.querySelector('.hmr-style')).color") assert initial_color == "rgb(1, 2, 3)" - {:ok, reload_count} = eval_poll(frame, "sessionStorage.getItem('voltCssReloadCount')") + {:ok, reload_count} = eval_poll(page, "sessionStorage.getItem('voltCssReloadCount')") assert reload_count == "1" {:ok, watcher} = start_watcher() @@ -715,7 +677,7 @@ defmodule Volt.Integration.HMRTest do assert {:ok, "rgb(4, 5, 6)"} = eval_until( - frame, + page, "getComputedStyle(document.querySelector('.hmr-style')).color", "rgb(4, 5, 6)" ) @@ -723,7 +685,7 @@ defmodule Volt.Integration.HMRTest do GenServer.stop(watcher) end - test "imports CSS modules from JavaScript with exports and injected styles", %{frame: frame} do + test "imports CSS modules from JavaScript with exports and injected styles", %{page: page} do write_fixture("button.module.css", """ .btn { color: rgb(40, 50, 60); } """) @@ -745,55 +707,45 @@ defmodule Volt.Integration.HMRTest do """) - {:ok, _} = Frame.goto(frame.guid, url: base_url("/css_module_page.html"), timeout: 10_000) + {:ok, _} = Page.goto(page, base_url("/css_module_page.html"), timeout: 10_000) - {:ok, class_name} = eval_poll(frame, "window.__voltCssModuleClass") + {:ok, class_name} = eval_poll(page, "window.__voltCssModuleClass") assert is_binary(class_name) assert class_name =~ "btn" {:ok, style_count} = eval_poll( - frame, + page, "document.querySelectorAll('style[data-volt-id=\"/assets/button.module.css\"]').length" ) assert style_count == 1 - {:ok, color} = eval_poll(frame, "getComputedStyle(document.querySelector('div')).color") + {:ok, color} = eval_poll(page, "getComputedStyle(document.querySelector('div')).color") assert color == "rgb(40, 50, 60)" end end - defp eval_until(frame, expression, expected, attempts \\ 30) do - case Frame.evaluate(frame.guid, - expression: expression, - is_function: false, - arg: nil, - timeout: 5000 - ) do + defp eval_until(page, expression, expected, attempts \\ 30) do + case Page.evaluate(page, expression) do {:ok, ^expected} -> {:ok, expected} {:ok, _other} when attempts > 0 -> - Process.sleep(100) && eval_until(frame, expression, expected, attempts - 1) + Process.sleep(100) && eval_until(page, expression, expected, attempts - 1) _ -> {:error, :timeout} end end - defp eval_poll(frame, expression, attempts \\ 20) do - case Frame.evaluate(frame.guid, - expression: expression, - is_function: false, - arg: nil, - timeout: 5000 - ) do - {:ok, result} when result != nil -> + defp eval_poll(page, expression, attempts \\ 20) do + case Page.evaluate(page, expression) do + {:ok, result} when result not in [nil, :null] -> {:ok, result} - {:ok, nil} when attempts > 0 -> - Process.sleep(100) && eval_poll(frame, expression, attempts - 1) + {:ok, _missing} when attempts > 0 -> + Process.sleep(100) && eval_poll(page, expression, attempts - 1) _ -> {:error, :timeout} From ddd0125aa02fbaa5678903288fe0f98bfd541e7c Mon Sep 17 00:00:00 2001 From: Fredrik Teschke Date: Tue, 24 Nov 2026 11:31:23 +0100 Subject: [PATCH 2/2] Run the browser test on Playwright.Case --- .gitignore | 3 + config/test.exs | 2 + test/volt/integration/hmr_test.exs | 263 +++++++++-------------------- 3 files changed, 83 insertions(+), 185 deletions(-) diff --git a/.gitignore b/.gitignore index 21b1a10..018a769 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/config/test.exs b/config/test.exs index ba65a9a..6392bd1 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/test/volt/integration/hmr_test.exs b/test/volt/integration/hmr_test.exs index ed5457b..6ae9ca0 100644 --- a/test/volt/integration/hmr_test.exs +++ b/test/volt/integration/hmr_test.exs @@ -113,25 +113,14 @@ defmodule Volt.Integration.TestPlug do end defmodule Volt.Integration.HMRTest do - use ExUnit.Case, async: false + use Playwright.Case, async: false @moduletag :integration - alias Playwright.{Browser, BrowserContext, BrowserType, Page} - @fixture_dir Path.join(System.tmp_dir!(), "volt-integration-hmr-test") @port 44_831 - setup_all do - {:ok, _pid} = Playwright.Supervisor.start_link() - - browser = - Playwright.instance() |> Playwright.chromium() |> BrowserType.launch!(timeout: 10_000) - - %{browser: browser} - end - - setup %{browser: browser} do + setup do File.rm_rf!(@fixture_dir) File.mkdir_p!(@fixture_dir) Volt.Cache.clear() @@ -182,17 +171,10 @@ defmodule Volt.Integration.HMRTest do startup_log: false ) - on_exit(fn -> Process.exit(server, :normal) end) - - {:ok, context} = Browser.new_context(browser) - {:ok, page} = BrowserContext.new_page(context) - on_exit(fn -> - BrowserContext.close(context) + Process.exit(server, :normal) File.rm_rf!(@fixture_dir) end) - - %{page: page, context: context} end describe "dev server module serving" do @@ -219,8 +201,7 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/hot_page.html"), timeout: 10_000) - {:ok, result} = eval_poll(page, "window.__voltHotAvailable") - assert result == true + expect_eval(page, "window.__voltHotAvailable", true) end test "rewrites relative imports between modules", %{page: page} do @@ -242,8 +223,7 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/import_page.html"), timeout: 10_000) - {:ok, result} = eval_poll(page, "window.__voltImportResult") - assert result == "hello from utils" + expect_eval(page, "window.__voltImportResult", "hello from utils") end test "imports CSS from JavaScript and injects a style tag", %{page: page} do @@ -270,24 +250,15 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/css_import_page.html"), timeout: 10_000) - {:ok, loaded} = eval_poll(page, "window.__voltCssImportLoaded") - assert loaded == true + expect_eval(page, "window.__voltCssImportLoaded", true) - {:ok, style_count} = - eval_poll( - page, - "document.querySelectorAll('style[data-volt-id=\"/assets/imported.css\"]').length" - ) + Expect.to_have_count!(Page.locator(page, "style[data-volt-id=\"/assets/imported.css\"]"), 1) - assert style_count == 1 - - {:ok, color} = - eval_poll( - page, - "getComputedStyle(document.querySelector('.volt-css-import-target')).color" - ) - - assert color == "rgb(10, 20, 30)" + Expect.to_have_css!( + Page.locator(page, ".volt-css-import-target"), + "color", + "rgb(10, 20, 30)" + ) end test "self-accepting modules update without reloading and preserve hot data", %{page: page} do @@ -323,22 +294,19 @@ defmodule Volt.Integration.HMRTest do """) {:ok, _} = Page.goto(page, base_url("/self_accept_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(page, "document.getElementById('self-accept')?.textContent") - assert initial == "one:none" + Expect.to_have_text!(Page.locator(page, "#self-accept"), "one:none") {:ok, watcher} = start_watcher() update_fixture("self_accept.ts", &String.replace(&1, "'one'", "'two'")) - assert {:ok, "two:one"} = - eval_until(page, "document.getElementById('self-accept')?.textContent", "two:one") + Expect.to_have_text!(Page.locator(page, "#self-accept"), "two:one") - assert {:ok, true} = - eval_until( - page, - "window.__voltSelfAccept.events.includes('dispose:one') && window.__voltSelfAccept.events.includes('accept:two')", - true - ) + expect_eval( + page, + "window.__voltSelfAccept.events.includes('dispose:one') && window.__voltSelfAccept.events.includes('accept:two')", + true + ) GenServer.stop(watcher) end @@ -376,29 +344,17 @@ defmodule Volt.Integration.HMRTest do """) {:ok, _} = Page.goto(page, base_url("/accept_dep_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(page, "document.getElementById('accepted-dep')?.textContent") - assert initial == "child-one" + Expect.to_have_text!(Page.locator(page, "#accepted-dep"), "child-one") {:ok, watcher} = start_watcher() update_fixture("accepted_dep.ts", &String.replace(&1, "child-one", "child-two")) - assert {:ok, "child-two"} = - eval_until( - page, - "document.getElementById('accepted-dep')?.textContent", - "child-two" - ) + Expect.to_have_text!(Page.locator(page, "#accepted-dep"), "child-two") - assert {:ok, true} = - eval_until( - page, - "window.__voltAcceptedDep.events.includes('accepted:child-two')", - true - ) + expect_eval(page, "window.__voltAcceptedDep.events.includes('accepted:child-two')", true) - {:ok, reloads} = eval_poll(page, "window.__voltAcceptedDep.reloads") - assert reloads == 1 + expect_eval(page, "window.__voltAcceptedDep.reloads", 1) GenServer.stop(watcher) end @@ -438,31 +394,17 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/multi_accept_dep_page.html"), timeout: 10_000) - {:ok, initial} = - eval_poll(page, "document.getElementById('multi-accepted-dep')?.textContent") - - assert initial == "a-one:b-one" + Expect.to_have_text!(Page.locator(page, "#multi-accepted-dep"), "a-one:b-one") {:ok, watcher} = start_watcher() update_fixture("multi_dep_a.ts", &String.replace(&1, "a-one", "a-two")) - assert {:ok, "a-two:b-one"} = - eval_until( - page, - "document.getElementById('multi-accepted-dep')?.textContent", - "a-two:b-one" - ) + Expect.to_have_text!(Page.locator(page, "#multi-accepted-dep"), "a-two:b-one") - assert {:ok, true} = - eval_until( - page, - "window.__voltMultiDep.events.includes('accepted:a-two:b-one')", - true - ) + expect_eval(page, "window.__voltMultiDep.events.includes('accepted:a-two:b-one')", true) - {:ok, reloads} = eval_poll(page, "window.__voltMultiDep.reloads") - assert reloads == 1 + expect_eval(page, "window.__voltMultiDep.reloads", 1) GenServer.stop(watcher) end @@ -477,7 +419,7 @@ defmodule Volt.Integration.HMRTest do File.write!(Path.join(content_dir, "second.md"), "second page") {:ok, _} = Page.goto(page, base_url("/doc/first"), timeout: 10_000) - assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") + Expect.to_have_attribute!(Page.locator(page, "body"), "data-loads", "1") {:ok, watcher} = Volt.Watcher.start_link( @@ -491,21 +433,15 @@ defmodule Volt.Integration.HMRTest do # Another page's content: this page revalidates and stays. File.write!(Path.join(content_dir, "second.md"), "second page, edited") Process.sleep(1000) - assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") + Expect.to_have_attribute!(Page.locator(page, "body"), "data-loads", "1") - assert {:ok, "first page"} = - eval_poll(page, "document.getElementById('content').textContent") + Expect.to_have_text!(Page.locator(page, "#content"), "first page") File.write!(Path.join(content_dir, "first.md"), "first page, edited") - assert {:ok, "first page, edited"} = - eval_until( - page, - "document.getElementById('content')?.textContent", - "first page, edited" - ) + Expect.to_have_text!(Page.locator(page, "#content"), "first page, edited") - assert {:ok, "2"} = eval_poll(page, "document.body.dataset.loads") + Expect.to_have_attribute!(Page.locator(page, "body"), "data-loads", "2") GenServer.stop(watcher) end @@ -518,10 +454,10 @@ defmodule Volt.Integration.HMRTest do File.write!(Path.join(content_dir, "first.md"), "first page") {:ok, _} = Page.goto(page, base_url("/morph/first"), timeout: 10_000) - assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") + expect_eval(page, "document.body.dataset.loads", "1") # State a reload would lose. - {:ok, _} = eval_poll(page, "(window.voltKept = 'kept')") + Page.evaluate!(page, "window.voltKept = 'kept'") {:ok, watcher} = Volt.Watcher.start_link( @@ -533,27 +469,17 @@ defmodule Volt.Integration.HMRTest do Process.sleep(150) File.write!(Path.join(content_dir, "first.md"), "first page, edited") - assert {:ok, "first page, edited"} = - eval_until( - page, - "document.getElementById('content')?.textContent", - "first page, edited" - ) + Expect.to_have_text!(Page.locator(page, "#content"), "first page, edited") - assert {:ok, "kept"} = eval_poll(page, "window.voltKept") - assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") + expect_eval(page, "window.voltKept", "kept") + expect_eval(page, "document.body.dataset.loads", "1") # The patched page revalidates against its new HTML. File.write!(Path.join(content_dir, "first.md"), "first page, edited twice") - assert {:ok, "first page, edited twice"} = - eval_until( - page, - "document.getElementById('content')?.textContent", - "first page, edited twice" - ) + Expect.to_have_text!(Page.locator(page, "#content"), "first page, edited twice") - assert {:ok, "kept"} = eval_poll(page, "window.voltKept") + expect_eval(page, "window.voltKept", "kept") GenServer.stop(watcher) end @@ -566,15 +492,14 @@ defmodule Volt.Integration.HMRTest do File.write!(Path.join(content_dir, "page.md"), "first|1|en|old description") {:ok, _} = Page.goto(page, base_url("/push/page"), timeout: 10_000) - assert {:ok, "1"} = eval_until(page, "document.body.dataset.loads", "1") - {:ok, _} = eval_poll(page, "(window.voltKept = 'kept')") + expect_eval(page, "document.body.dataset.loads", "1") + Page.evaluate!(page, "window.voltKept = 'kept'") # The page requests nothing more: what changed arrives over the websocket. - {:ok, _} = - eval_poll( - page, - "(window.voltFetches = 0, window.fetch = ((f) => (...a) => (window.voltFetches++, f(...a)))(window.fetch), 0)" - ) + Page.evaluate!( + page, + "(window.voltFetches = 0, window.fetch = ((f) => (...a) => (window.voltFetches++, f(...a)))(window.fetch), 0)" + ) {:ok, watcher} = Volt.Watcher.start_link( @@ -586,26 +511,26 @@ defmodule Volt.Integration.HMRTest do Process.sleep(150) File.write!(Path.join(content_dir, "page.md"), "second|2|ru|new description") - assert {:ok, "second"} = - eval_until(page, "document.getElementById('content')?.textContent", "second") + Expect.to_have_text!(Page.locator(page, "#content"), "second") # The owner of the element re-rendered it from the props the server changed. - assert {:ok, "props:2"} = - eval_until(page, "document.getElementById('owned')?.textContent", "props:2") + Expect.to_have_text!(Page.locator(page, "#owned"), "props:2") # Attributes the server changed are applied; one a script set stays. - assert {:ok, "ru"} = eval_poll(page, "document.documentElement.lang") - assert {:ok, "dark"} = eval_poll(page, "document.documentElement.dataset.theme") + Expect.to_have_attribute!(Page.locator(page, "html"), "lang", "ru") + Expect.to_have_attribute!(Page.locator(page, "html"), "data-theme", "dark") - assert {:ok, "new description"} = - eval_poll(page, "document.querySelector('meta[name=description]').content") + Expect.to_have_attribute!( + Page.locator(page, "meta[name=description]"), + "content", + "new description" + ) - assert {:ok, 1} = - eval_poll(page, "document.querySelectorAll('meta[name=description]').length") + Expect.to_have_count!(Page.locator(page, "meta[name=description]"), 1) - assert {:ok, "kept"} = eval_poll(page, "window.voltKept") - assert {:ok, "1"} = eval_poll(page, "document.body.dataset.loads") - assert {:ok, 0} = eval_poll(page, "window.voltFetches") + expect_eval(page, "window.voltKept", "kept") + expect_eval(page, "document.body.dataset.loads", "1") + expect_eval(page, "window.voltFetches", 0) GenServer.stop(watcher) end @@ -628,15 +553,13 @@ defmodule Volt.Integration.HMRTest do """) {:ok, _} = Page.goto(page, base_url("/full_reload_page.html"), timeout: 10_000) - {:ok, initial} = eval_poll(page, "document.getElementById('full-reload')?.textContent") - assert initial == "before:1" + Expect.to_have_text!(Page.locator(page, "#full-reload"), "before:1") {:ok, watcher} = start_watcher() update_fixture("full_reload.ts", &String.replace(&1, "before", "after")) - assert {:ok, "after:2"} = - eval_until(page, "document.getElementById('full-reload')?.textContent", "after:2") + Expect.to_have_text!(Page.locator(page, "#full-reload"), "after:2") GenServer.stop(watcher) end @@ -664,23 +587,14 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/css_hmr_page.html"), timeout: 10_000) - {:ok, initial_color} = - eval_poll(page, "getComputedStyle(document.querySelector('.hmr-style')).color") - - assert initial_color == "rgb(1, 2, 3)" - {:ok, reload_count} = eval_poll(page, "sessionStorage.getItem('voltCssReloadCount')") - assert reload_count == "1" + Expect.to_have_css!(Page.locator(page, ".hmr-style"), "color", "rgb(1, 2, 3)") + expect_eval(page, "sessionStorage.getItem('voltCssReloadCount')", "1") {:ok, watcher} = start_watcher() update_fixture("hmr-style.css", &String.replace(&1, "rgb(1, 2, 3)", "rgb(4, 5, 6)")) - assert {:ok, "rgb(4, 5, 6)"} = - eval_until( - page, - "getComputedStyle(document.querySelector('.hmr-style')).color", - "rgb(4, 5, 6)" - ) + Expect.to_have_css!(Page.locator(page, ".hmr-style"), "color", "rgb(4, 5, 6)") GenServer.stop(watcher) end @@ -709,47 +623,26 @@ defmodule Volt.Integration.HMRTest do {:ok, _} = Page.goto(page, base_url("/css_module_page.html"), timeout: 10_000) - {:ok, class_name} = eval_poll(page, "window.__voltCssModuleClass") - assert is_binary(class_name) - assert class_name =~ "btn" - - {:ok, style_count} = - eval_poll( - page, - "document.querySelectorAll('style[data-volt-id=\"/assets/button.module.css\"]').length" - ) - - assert style_count == 1 - - {:ok, color} = eval_poll(page, "getComputedStyle(document.querySelector('div')).color") - assert color == "rgb(40, 50, 60)" - end - end - - defp eval_until(page, expression, expected, attempts \\ 30) do - case Page.evaluate(page, expression) do - {:ok, ^expected} -> - {:ok, expected} + Expect.poll!( + fn -> Page.evaluate!(page, "window.__voltCssModuleClass") end, + &assert(is_binary(&1) and &1 =~ "btn") + ) - {:ok, _other} when attempts > 0 -> - Process.sleep(100) && eval_until(page, expression, expected, attempts - 1) + Expect.to_have_count!( + Page.locator(page, "style[data-volt-id=\"/assets/button.module.css\"]"), + 1 + ) - _ -> - {:error, :timeout} + Expect.to_have_css!( + Page.locator(page, "div") |> Locator.first(), + "color", + "rgb(40, 50, 60)" + ) end end - defp eval_poll(page, expression, attempts \\ 20) do - case Page.evaluate(page, expression) do - {:ok, result} when result not in [nil, :null] -> - {:ok, result} - - {:ok, _missing} when attempts > 0 -> - Process.sleep(100) && eval_poll(page, expression, attempts - 1) - - _ -> - {:error, :timeout} - end + defp expect_eval(page, expression, expected) do + Expect.poll!(fn -> Page.evaluate!(page, expression) end, &assert(&1 == expected)) end defp start_watcher do