View Source PhoenixTest.Playwright (PhoenixTestPlaywright v0.1.2)

Warning

This driver is experimental. If you don't need browser based tests, see PhoenixTest on regular usage.

Execute PhoenixTest cases in an actual browser via Playwright.

Setup

  1. Add to mix.exs deps: {:phoenix_test_playwright, "~> 0.1", only: :test, runtime: false}
  2. Install Playwright: npm --prefix assets i -D playwright
  3. Install browsers: npm --prefix assets exec playwright install --with-deps
  4. Add to config/test.exs: config :phoenix_test, otp_app: :your_app, playwright: [cli: "assets/node_modules/playwright/cli.js"]
  5. Add to test/test_helpers.exs: Application.put_env(:phoenix_test, :base_url, YourAppWeb.Endpoint.url())

Usage

defmodule MyFeatureTest do
  use PhoenixTest.Case, async: true
  @moduletag :playwright

  test "heading", %{conn: conn} do
    conn
    |> visit("/")
    |> assert_has("h1", text: "Heading")
  end
end

As shown above, you can use ExUnit.Case parameterized tests to run tests concurrently in different browsers.

Known limitations and inconsistencies

  • PhoenixTest.select/4 option exact_option is not supported
  • Playwright driver is less strict than Live and Static drivers. It does not raise errors
    • when visiting a page that returns a 404 status
    • when interactive elements such as forms and buttons are missing essential attributes (phx-click, phx-submit, action)
  • A few small bugs

See tests tagged with @tag playwright: false for details.

Configuration

In config/test.exs:

config :phoenix_test,
  playwright: [
    cli: "assets/node_modules/playwright/cli.js",
    browser: [browser: :chromium, headless: System.get_env("PLAYWRIGHT_HEADLESS", "t") in ~w(t true)],
    trace: System.get_env("PLAYWRIGHT_TRACE", "false") in ~w(t true),
    trace_dir: "tmp"
  ],
  timeout_ms: 2000

Ecto SQL.Sandbox

PhoenixTest.Case automatically takes care of this. It passes a user agent referencing your Ecto repos. This allows for concurrent browser tests.

defmodule MyTest do
  use PhoenixTest.Case, async: true

Summary

Functions

assert_has(session, selector)

assert_has(session, selector, opts)

build(page_id, frame_id)

check(session, input_selector, label, opts)

choose(session, input_selector, label, opts)

click(session, selector)

click(session, selector, text, opts \\ [])

click_button(session, orig_selector, text, opts \\ [])

click_link(session, orig_selector, text, opts \\ [])

current_path(session)

fill_in(session, input_selector, label, opts)

open_browser(session, open_fun \\ &OpenBrowser.open_with_system_cmd/1)

refute_has(session, selector)

refute_has(session, selector, opts)

render_html(session)

render_page_title(session)

retry(fun, backoff_ms \\ [100, 250, 500, timeout()])

select(session, input_selector, option_labels, opts)

submit(session)

uncheck(session, input_selector, label, opts)

unwrap(session, fun)

upload(session, input_selector, label, paths, opts)

visit(session, path)

within(session, selector, fun)