---
name: playwright-browser-automation-script
description: "A Playwright script for a browser task you need to repeat, with stable selectors, proper waits and screenshots or traces kept as proof. Use when a browser chore such as filling a form, exporting a report or checking a page has to run the same way every time. Do not use for end-to-end test suites for a whole application or one-off scraping of many sites. Related topics: coding, productivity."
---

# Playwright Browser Automation Script

A Playwright script for a browser task you need to repeat, with stable selectors, proper waits and screenshots or traces kept as proof.

## When to use

Use this skill when a browser chore such as filling a form, exporting a report or checking a page has to run the same way every time. Do not use it for end-to-end test suites for a whole application or one-off scraping of many sites.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Split the task into named steps and write the success condition of each one before writing any code.
3. Choose a locator for each element, using role or label first and a test id only when neither exists.
4. Write the script with waits on observable state and one assertion after each page-changing step, so no step relies on timing.
5. Wire in the chosen evidence capture and read any login values from named environment variables.
6. Return the script with the run command, the variables to set and the steps most likely to break.

## Inputs

- **Task in plain words** (longer free text)
- **Start URL** (free text, e.g. "Address of the first page")
- **Language** (choose one: TypeScript, JavaScript, Python)
- **Login needed** (choose one: Yes, No)
- **Page structure notes** (longer free text)
- **Evidence to keep** (choose any: Screenshots, Trace, Video, Text log, Downloaded file)

## Output

One complete script in the chosen language, then the run command, the list of environment variables, and a short list of fragile steps.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Playwright Browser Automation Script

Write a Playwright script for a repeatable browser task.

- Task in plain words: {{Task in plain words}}
- Start URL: {{Start URL}}
- Language: {{Language}}
- Login needed: {{Login needed}}
- Page structure notes: {{Page structure notes}}
- Evidence to keep: {{Evidence to keep}}

Prefer role and label locators over CSS paths, since they survive redesigns. Never use fixed sleeps: wait on a visible element, a URL change or a network response that proves the step finished. Read credentials from environment variables, and say which names to set, without ever hard-coding a value. Break the task into named steps so a failure shows which step broke. After each step that changes the page, assert one thing that must now be true. Save the chosen evidence into an output folder named by date. Retry only the steps that are safe to repeat and make the script exit with a non-zero code on any failed assertion. Return the full script, the command to run it, the environment variables it needs, and a short list of the places most likely to break.

## Pitfalls

- Using waitForTimeout to pass a flaky step, which works on a fast laptop and fails on a loaded server.
- Selecting by generated class names that change on every build, so the script dies after the next deploy.
- Putting a password or token inside the script file, where it ends up in version control.
