Snippets alias PlaywrightEx.{Browser, BrowserType, Playwright, ...}.
Installation
Add the dependency to mix.exs as shown in the Overview. If you only
use it in tests, prefer only: [:dev, :test] to only: :test: its mix tasks, and
mix usage_rules.sync for its usage rules, then work in :dev,
where it starts no driver. Fetch it and install the driver and the browsers:
mix deps.get
mix playwright_ex.install
The driver is Node.js plus the playwright-core package, both pinned and checksum-verified, so
no Node.js or npm is needed beforehand. It goes to the user cache directory
(PLAYWRIGHT_EX_CACHE_DIR overrides it) and installs itself on first start if missing. Browsers
go to Playwright's browser cache.
Arguments after -- go to playwright install:
mix playwright_ex.install -- chromium # one browser
mix playwright_ex.install -- chromium --with-deps # and its system packages (Linux CI)
To run a browser you already have, such as a system or Nix Chrome, install only the driver and
launch the browser with :executable_path or :channel:
mix playwright_ex.install --no-browsers
playwright = PlaywrightEx.playwright()
BrowserType.launch!(Playwright.chromium(playwright), executable_path: "/path/to/chrome")PlaywrightEx needs Elixir 1.18 or later. The browsers run where Playwright's do: Windows 11+, macOS 14+, Debian 12 and 13, Ubuntu 22.04 and 24.04 (system requirements).
Starting the driver
PlaywrightEx.Supervisor runs one driver and its connection. Add it to your supervision tree:
children = [
{PlaywrightEx.Supervisor, name: MyApp.Playwright}
]and get the root PlaywrightEx.Playwright object by that name:
playwright = PlaywrightEx.playwright(MyApp.Playwright)Without :name, the supervisor registers as PlaywrightEx, which PlaywrightEx.playwright/0
uses. If only your tests need a browser, start it in test/test_helper.exs:
{:ok, _} = PlaywrightEx.Supervisor.start_link()
ExUnit.start()To use an npm-managed Playwright instead of the pinned driver, pass
executable: "assets/node_modules/.bin/playwright"; PlaywrightEx.Driver lists the other
options.
First script
Navigate to a page, print its title and take a screenshot:
alias PlaywrightEx.{Browser, BrowserType, Page, Playwright}
{:ok, _} = PlaywrightEx.Supervisor.start_link()
browser = PlaywrightEx.playwright() |> Playwright.chromium() |> BrowserType.launch!()
page = Browser.new_page!(browser)
Page.goto!(page, "https://playwright.dev/")
IO.puts(Page.title!(page))
Page.screenshot!(page, path: "example.png")
Browser.close!(browser)Save it as example.exs and run mix run example.exs. If your application already starts the
supervisor, drop the start_link/1 line and pass its name to PlaywrightEx.playwright/1.
Browser.new_page!/1 opens a page in a context of its own. A
context is an isolated browser session (cookies,
storage, options such as base_url) and is cheap to create, so use one per test or per user:
context = Browser.new_context!(browser, base_url: "http://localhost:4000")
page = BrowserContext.new_page!(context)
Page.goto!(page, "/")
BrowserContext.close!(context)Playwright.firefox/1 and
Playwright.webkit/1 launch the other browsers. Browsers run
headless by default; Watching it run shows how to launch
one headed.
Finding elements
Find elements the way a user does, with get_by_role, get_by_label, get_by_text and
get_by_test_id. Use Page.locator/3 with CSS when nothing
user-facing identifies the element.
Page.get_by_role(page, :button, name: "Save") |> Locator.click!()
Page.get_by_role(page, :listitem)
|> Locator.filter(has_text: "Apple")
|> Locator.get_by_role(:button, name: "Buy")
|> Locator.click!()Locators are lazy and strict: nothing is looked up until an action, and an action on a locator that
matches several elements fails. Narrow it with
Locator.filter/2,
Locator.first/1,
Locator.last/1 or
Locator.nth/2. Act directly, as actions wait for their element, so
don't check is_visible first. Avoid
Page.wait_for_timeout/2, which is for debugging: wait
for what the user sees. More: locators.
Timeouts
Actions and navigations time out after 30 seconds. Set defaults per context (or page) instead
of a timeout: per call:
BrowserContext.set_default_timeout(context, 5_000)
BrowserContext.set_default_navigation_timeout(context, 10_000)timeout: 0 means no timeout
As in Playwright JS, timeout: 0 waits forever instead of failing at once. :infinity is
the same and reads better in Elixir.
A call's timeout: option wins. Assertions (PlaywrightEx.Expect) retry for 5 seconds by
default:
# config/test.exs
config :playwright_ex, :expect, timeout: 10_000Logging
Each API call logs at :debug, as DEBUG=pw:api does in JS. To silence it:
Logger.put_application_level(:playwright_ex, :info)Upgrading
To upgrade, bump the dependency and run mix playwright_ex.install again for the new driver and
browsers. Read the changelog and Playwright's release
notes. From 0.16, follow the
migration guide.
Next
From playwright.dev to Elixir shows how a JS call reads in Elixir, and the Cheatsheet lists frequent calls.