What does the --target flag on Playwright's codegen command change about the script it emits?
answer
- The flag changes output, not recording
- One recording, several language renderings
- Harness variants per binding language
- Pair it with an output file path
- A starting point, never a finished port
basics
~20 sIt picks the output language and test harness. Playwright codegen prints the same recorded steps as Playwright Test JavaScript by default, or as python-pytest, java-junit or csharp-nunit, so a porting team gets a starter script in its own language.
solid answer
~40 s`npx playwright codegen --target=<name>` selects the **language and harness of the generated file**, not what is recorded. The steps are captured once and then printed in the requested shape: `playwright-test` (the default) and `javascript`, `python`, `python-async` and `python-pytest`, `java` and `java-junit`, `csharp` with `csharp-nunit` and `csharp-mstest`. Combine it with `-o` to write straight to a file, as in `npx playwright codegen --target=python-pytest -o tests/test_quote.py https://quotes.example.com`. During a port its value is narrow but real: it shows the idiomatic skeleton in the destination language and the locator Playwright itself would choose for a control. It does not read, translate or restructure your existing suite — the test data, assertions and ordering are still yours to carry across.
code
bash · 1 linenpx playwright codegen --target=python-pytest -o tests/test_quote.py https://quotes.example.com/motorgo deeper
Know the shape of the command: codegen plus --target plus an output file and a start URL. The flag decides the language and harness of the file you get.
Explain the split between recording and rendering. Steps and element choices are captured once; the target only decides how they are printed, which is why switching it changes no locator.
Position it correctly in a migration. It seeds structure and shows locator style, but the coverage in the old suite comes across by hand, and raw generated files should not become the new suite.
Use it to remove an objection rather than to plan. That the recorder emits Java or .NET as easily as JavaScript matters when a team fears a language switch, but it changes nothing about the effort of porting real cases.
`codegen` is the command that opens a browser, follows what you do in it, and prints the equivalent script. The `--target` flag decides what that printed script looks like. ## What the flag selects `--target` changes only the **rendering** of the recorded steps: which language they are written in, and which test harness they are wrapped in. The same click on *Get quote* comes out as `page.get_by_role("button", name="Get quote").click()` under a Python target and as `page.getByRole('button', { name: 'Get quote' }).click()` under a JavaScript one. Nothing about which element is chosen depends on the target. The accepted values group into the language bindings Playwright ships, plus a harness variant for each: - `playwright-test` — the default, a `@playwright/test` file with `test(...)` and the `page` fixture; - `javascript` — a plain script that launches a browser itself; - `python`, `python-async`, `python-pytest`; - `java`, `java-junit`; - `csharp`, `csharp-nunit`, `csharp-mstest`. Pair it with `-o <file>` to write the result to disk instead of the terminal, and pass the start URL as the final argument so the browser opens on the page you want to record. ## Why it matters during a port, and how much A team moving a legacy insurance-quote suite usually has two open questions in the first week: *what does an idiomatic test look like in our language on the new stack*, and *what locator should we be writing for this control*. `--target` answers both cheaply, in the language the team is actually keeping. | It is good for | It is not for | |---|---| | a first working file in the destination language | translating your existing cases | | seeing the locator style for an unfamiliar control | choosing your suite's locator conventions | | checking a flow still works while you port it | producing tests you commit unedited | | showing the harness skeleton to a team new to it | carrying over data setup or assertions | The honest framing in an interview is that codegen output is a **starting point, not a port**. It records the steps you performed, so it captures a happy path with your ad-hoc data baked in, and the assertions it produces are the ones you asked for while recording rather than the ones the original test made. Committing raw generated files is how a migrated suite ends up with a hundred near-identical scripts nobody can maintain. ## A sane way to use it in a migration 1. Record one representative flow in the target language and read the file, to fix the shape of the ported tests. 2. Keep the locators it suggests where they are role- or label-based, and replace the brittle structural ones with the test attributes your application already exposes. 3. Port the *original* test's data, preconditions and assertions in by hand; the recording gives you the navigation, not the intent. 4. Throw the generated file away once its structure has been absorbed, rather than growing the suite by recording every case. ## Version and language notes The target list above is what Playwright 1.63 accepts; treat it as version-sensitive and check `npx playwright codegen --help` on the version the team has pinned rather than trusting a remembered list. One practical consequence for a port is that the destination language does not have to be JavaScript at all: the same recorder can hand a Java or .NET team a file in their own language, which removes the "we must also learn Node" objection from the migration plan without changing anything about how the tests are written.
- Why should generated files rarely be committed as they are during a migration?Because they record a happy path with whatever values you typed, and only the assertions you asked for while recording. The original case's data, preconditions and intent are missing, so committing the raw file grows the suite without carrying the coverage the old test had.
- Does --target change which locators are generated?No. Element choice happens while recording and is independent of the output shape; the target only decides the language and harness the same steps are printed in. A locator you dislike is changed by exposing a better test attribute, not by switching target.
saying these in an interview costs you the question
- Thinking codegen reads and converts the old suite
- Believing --target changes which locators are picked
- Committing generated files unedited as ported tests
- Assuming the output is JavaScript only
- Treating recorded steps as equivalent to the original assertions