Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ This repository contains the Pest Plugin for Browser.

> If you want to start testing your application with Pest, visit the main **[Pest Repository](https://github.com/pestphp/pest)**.

### Recording Tests

Generate browser-tests by recording your interactions in the browser:

```bash
vendor/bin/pest --record
```

See **[RECORDING.md](RECORDING.md)** for more information.

- Explore our docs at **[pestphp.com »](https://pestphp.com)**
- Follow the creator Nuno Maduro:
- YouTube: **[youtube.com/@nunomaduro](https://www.youtube.com/@nunomaduro)** — Videos every weekday
Expand Down
79 changes: 79 additions & 0 deletions RECORDING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Recording Guide

## How it works

`vendor/bin/pest --record` automatically starts an HTTP server with `APP_ENV=testing` before the browser opens, then
shuts it down when recording ends — matching the same behavior as running browser tests normally with `vendor/bin/pest`.

The browser opens at full screen resolution by default (auto-detected per OS). Close the browser when done. Pest prompts
for a test description and output file, then writes the generated test to `tests/Browser/`.

## Options

| Option | Default | Description |
|------------------------|-------------------|---------------------------------------------------------------|
| `--url=` | auto | Skip auto-server; connect to this URL instead |
| `--visit=` | `/` | Path to open on start |
| `--test-id-attribute=` | `id` | HTML attribute used for element selectors |
| `--device=` | — | Emulate a device (e.g. `"iPhone 15"`) |
| `--viewport=` | screen resolution | Viewport size in pixels (e.g. `1280,800`) |
| `--auth` / `--user` | — | Start browser pre-authenticated as a factory user (see below) |
| `--auth-script=` | — | Path to a custom auth bootstrap script (non-Laravel apps) |
| `--env=` | `testing` | Environment for the auto-started server |
| `--migrate-fresh` | — | Run `migrate:fresh` before opening the browser |
| `--seed` | — | Seed the database after `--migrate-fresh` |
| `--browser=` | `chrome` | Browser to record with: `chrome`, `firefox`, `safari` |
| `--channel=` | — | Browser channel (e.g. `msedge`, `chrome-canary`) |
| `--lang=` | — | Override browser locale (e.g. `fr`, `nl`) |
| `--timezone=` | — | Override browser timezone (e.g. `Europe/Paris`) |
| `--color-scheme=` | — | Preferred color scheme: `dark`, `light`, `no-preference` |

## Recording in a specific browser

```bash
vendor/bin/pest --record --browser=firefox
vendor/bin/pest --record --browser=chrome --channel=msedge
```

## Recording with locale or timezone

```bash
vendor/bin/pest --record --lang=fr --timezone=Europe/Paris
vendor/bin/pest --record --color-scheme=dark
```

## Recording authenticated flows (`--auth`)

Tests run with `APP_ENV=testing` (fresh, isolated DB). Recording against credentials that only exist in your local DB
means those credentials won't exist when the test runs.

`--auth` solves this without manual login: before the browser opens, Pest bootstraps the Laravel app, creates a factory
user, starts a session authenticated as that user, and injects a valid session cookie into the browser. The browser
starts pre-authenticated.

```bash
vendor/bin/pest --record --auth --visit=/dashboard
```

`--user` is an alias for `--auth`.

The generated test uses `$this->actingAs(\App\Models\User::factory()->create())` — no real credentials, works in any
environment.

## Fresh database before recording

```bash
vendor/bin/pest --record --migrate-fresh --seed --visit=/dashboard
```

Useful when you want to record against a predictable dataset.

## Recording against an existing server

Pass `--url=` to skip the auto-server entirely:

```bash
vendor/bin/pest --record --url=http://localhost:8000 --visit=/dashboard
```

Useful when you have Herd, Valet, or a running `php artisan serve` instance.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"extra": {
"pest": {
"plugins": [
"Pest\\Browser\\Plugin"
"Pest\\Browser\\Plugin",
"Pest\\Browser\\Record"
]
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function boot(): void
/**
* Handles the arguments passed to the plugin.
*
* @param array<int, string> $arguments}
* @param array<int, string> $arguments }
*/
public function handleArguments(array $arguments): array
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public function handleArguments(array $arguments): array
$arguments = $this->popArgument('--light', $arguments);
}

if ($this->hasArgument('--browser', $arguments)) {
if ($this->hasArgument('--browser', $arguments) && ! $this->hasArgument('--record', $arguments)) {
$index = array_search('--browser', $arguments, true);

if ($index === false || ! isset($arguments[$index + 1])) {
Expand All @@ -106,8 +106,8 @@ public function handleArguments(array $arguments): array

if (($browser = BrowserType::tryFrom($browser)) === null) {
throw new BrowserNotSupportedException(
'The specified browser type is not supported. Supported types are: '.
implode(', ', array_map(fn (BrowserType $type): string => mb_strtolower($type->name), BrowserType::cases()))
'The specified browser type is not supported. Supported types are: ' .
implode(', ', array_map(fn(BrowserType $type): string => mb_strtolower($type->name), BrowserType::cases()))
);
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function terminate(): void
*/
private function in(): string
{
return TestSuite::getInstance()->rootPath.DIRECTORY_SEPARATOR.TestSuite::getInstance()->testPath;
return TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . TestSuite::getInstance()->testPath;
}

/**
Expand Down
Loading