Exploring the page with codegen
Playwright can write test code for you. Codegen opens a browser and records what you do in it. Every click turns into a line of Python, and it picks a locator for each element you touch.
Before we write any tests, let’s use codegen to see what Playwright makes of our page.
1. Start the development server
Codegen needs a running app to look at. In one terminal, start the development server:
uv run python manage.py runserverLeave it running.
2. Start codegen
In a second terminal, start codegen:
uv run playwright codegen --target python-pytest http://127.0.0.1:8000/codegenopens a browser, and a second window called the Playwright Inspector.--target python-pytestwrites the code as a pytest test.- The URL is the page to start on.
Put the two windows side by side, so you can see the browser and the Inspector at the same time.
3. Click around and watch the code
In the browser, tick one of the unticked todo items, then click the arrow next to another item to open its description. Watch the Inspector. Every click turns into a line of Python.
Now try other things, and watch what each one turns into:
- Add a new todo item with the form. Notice how codegen finds the text boxes, and what it does when you type.
- Tick an item, then untick it.
- Open a description, then close it again.
4. Find out how to target an element
Hover over elements in the browser before you click. Codegen highlights each one and shows the locator it would use. Try hovering over:
- a todo item’s title
- a checkbox
- an arrow
- the Add button
Some locators use the element’s role and name, like get_by_role("button", name="Add"). Others use an id, like #todo-item-2. Notice which elements codegen can describe the way a user would, and which ones it can’t.
You can also click Pick locator in the Inspector toolbar, then click an element. Codegen shows its locator without recording a click.
5. Record some checks
So far, codegen has only recorded clicks. A test also needs to check what happened. The Inspector toolbar has buttons for that:
- Assert visibility checks that an element is on the screen.
- Assert text checks the text inside an element.
- Assert value checks what’s typed into a text box.
Click one of these buttons, then click an element in the browser. Codegen adds an expect line. Try these:
- Open a description, then use Assert visibility on it.
- Use Assert text on a todo item’s title.
- Type something into the form, then use Assert value on that text box before you click Add.
Keep experimenting. Each time you’re not sure how to point at something, or how to check it, try it here and see what codegen writes.
6. Why we can’t use this code as it is
Codegen saved us some typing, but its code is a starting point, not a finished test:
- The URL is our development server. Tests should use
live_server.url, so they run against the test server. - The ids come from our development database. The tests get their own empty database, so
#todo-item-2won’t exist. We need to create our own todo items, and find them by their text. - It only records what you did. It doesn’t know what should happen. We decide what to check.
Close the codegen windows, and stop the development server with Ctrl+C. Next, we’ll use what we learned to write the real tests.
Recap
- We use codegen to record clicks in a browser and turn them into Python.
- We hover over elements, or use Pick locator, to find out how Playwright would target them.
- We use the assert buttons to see how to check that an element is visible, or has the text or value we expect.
- We treat codegen’s code as a first draft, and tidy it into a real test.