# Run Playwright While You Develop

## Run Playwright while you develop

Develop an external Playwright test against a sandbox you claim. Run
Playwright on your machine through a tunnel (Way 1), or sync your edits and
run the suite inside the sandbox's VM (Way 2).

### Claim a sandbox

This example uses the Acme App, the `nightly` pushed-image tag, and the
`Acme browser tests` suite registered for that app. Replace those names and
the organization and CLI stage with yours. The claim command needs `jq`.

```bash
cofactor_org="Acme"
cofactor_stage="prod"
sandbox_id=$(
  set -o pipefail
  cofactor sandbox claim --application "Acme App" --at nightly --wait --json \
    --org "$cofactor_org" --env "$cofactor_stage" | jq -er '.id'
)
```

Once the claim succeeds, use the ID captured from your own `claim --json` response throughout.

### Way 1. Run Playwright on your machine

Use this for local debugging with your installed Playwright, including
`--debug` or `--ui`. Forward the declared `web` port and keep the command
running. A declared port can be public or private.

```bash
cofactor sandbox forward "$sandbox_id" --port web --local-port 3100 \
  --org "$cofactor_org" --env "$cofactor_stage"
```

In a second terminal, from your local test project, point Playwright at the
forwarded address. This example assumes your config reads `BASE_URL` for
`use.baseURL`.

```bash
BASE_URL=http://127.0.0.1:3100 npm exec -- playwright test --grep "opens the dashboard"
```

Stop the tunnel with Ctrl-C when you finish.

### Way 2. Sync and run in the sandbox VM

Use the suite's pinned toolchain in the VM. A pushed-image suite must ship
its tests in the image. This example maps the local layout to `WORKDIR /app`.

```text
Local checkout                      Pushed image
acme-app/cofactor.sandbox.yaml      /app/cofactor.sandbox.yaml
acme-app/tests/dashboard.spec.ts    /app/tests/dashboard.spec.ts
```

See `cofactor sandbox guide local` for where to run plain sync and which
files it keeps. In the claim terminal, enter `acme-app/` for this example.
Sync the working tree and run one test by title.

```bash
cofactor sandbox sync "$sandbox_id" --org "$cofactor_org" --env "$cofactor_stage" &&
cofactor test-suite run --suite "Acme browser tests" --sandbox "$sandbox_id" \
  --from-sandbox --external-grep "opens the dashboard" \
  --org "$cofactor_org" --env "$cofactor_stage"
```

The command waits and prints a summary and a `Run: .../runs/<uuid>` link.
Use the UUID from that link to read the test report and download its trace,
if the run captured one. You can also open the run link in your browser.

```bash
run_id="<uuid from the Run link>"
cofactor external-run get "$run_id" --item "opens the dashboard" \
  --org "$cofactor_org" --env "$cofactor_stage"
cofactor external-run trace "$run_id" --item "opens the dashboard" \
  --org "$cofactor_org" --env "$cofactor_stage"
```

A test the catalog has yet to discover is shown by its key. To find this
claim's dev runs, use
`cofactor test-suite executions --sandbox "$sandbox_id" --org "$cofactor_org" --env "$cofactor_stage"`.
Dev runs are kept out of suite history, test health, alerts, and PR proof.

Edit `tests/dashboard.spec.ts` locally. Once the previous run finishes,
restore any app data the test changed with your suite's reset or a sandbox
action. Repeat edit, sync, and run on the same claim. Read each result with
its new run UUID.

### Clean up

After either way, destroy the claim you created.

```bash
cofactor sandbox destroy "$sandbox_id" --org "$cofactor_org" --env "$cofactor_stage"
```

For sync or run failures, see section 13 of `cofactor sandbox guide troubleshooting`.
