# `mix playwright.codegen`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.7/lib/mix/tasks/playwright.codegen.ex#L1)

Usage: `mix playwright.codegen [--target test|script] [-o FILE] [CODEGEN_OPTIONS...] [URL]`

Opens a browser and Playwright's Inspector, as `npx playwright codegen` does, and writes what you
do in the browser as Elixir: a `Playwright.Case` test module (`--target test`, the default) or a
script that starts a driver and launches the browser (`--target script`, for `mix run`). Close
the browser to finish.

`-o FILE` (`--output`) writes the code to `FILE` as you record, and again at the end; without
it, the code is printed when the browser closes. A test module is named after `FILE`
(`test/checkout_test.exs` gives `CheckoutTest`), else `RecordedTest`.

Other options go to the driver's `playwright codegen` as they are: `--browser firefox`,
`--device "iPhone 13"`, `--viewport-size 1280,720`, `--color-scheme dark`, `--lang`,
`--timezone`, `--geolocation`, `--load-storage FILE`, `--save-storage FILE`, `--save-har FILE`,
`--test-id-attribute`, `--user-data-dir`, `--channel`, ... (`mix playwright.cli codegen --help`
lists them). The context options among them become the test module's `use Playwright.Case`
options, or the script's `Browser.new_context!/2` options.

    mix playwright.codegen http://localhost:4000
    mix playwright.codegen -o test/e2e/sign_up_test.exs --device "Pixel 7" http://localhost:4000
    mix playwright.codegen --target script -o record.exs playwright.dev

The driver records to a temporary `jsonl` file, which this task turns into Elixir with a port
of upstream's JavaScript generator. So the Inspector shows JSONL (or JavaScript, from its
language menu), not Elixir: read `FILE`. Its assertion tools, locator picking and pause work as
in `npx playwright codegen`. The `jsonl` format drops an aria snapshot assertion's snapshot: a
test gets `Expect.to_match_aria_snapshot!/1` without it, which writes the snapshot on its first
run (and fails that run); a script gets the assertion as a comment.

Installs the driver first if needed, as `mix playwright.cli` does.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
