Skip to content

Commit b549378

Browse files
committed
feat: add BROWSER_ALWAYS_START_WEBSERVER env var
1 parent 49881ed commit b549378

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

README.md

+25-16
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ $browser
407407
Make assertions about json responses using [JMESPath expressions](https://jmespath.org/)
408408
See the [JMESPath Tutorials](https://jmespath.org/tutorial.html) to learn more.
409409

410-
> **Note**
410+
> [!NOTE]
411411
> `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`.
412412
413413
```php
@@ -453,14 +453,21 @@ $json = $browser
453453
;
454454
```
455455

456-
> **Note**
456+
> [!NOTE]
457457
> See the [full `zenstruck/assert` expectation API documentation](https://github.com/zenstruck/assert#expectation-api)
458458
> to see all the methods available on `Zenstruck\Browser\Json`.
459459
460460
### PantherBrowser
461461

462462
*The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.*
463463

464+
> [!TIP]
465+
> By default, Panther will not start a web server if it detects one already running
466+
> with the Symfony CLI. This is likely running in your `dev` environment and will cause
467+
> unexpected test failures. Set the env variable `BROWSER_ALWAYS_START_WEBSERVER=1`
468+
> to always start a webserver configured for your current test env when running
469+
> Panther tests.
470+
464471
This browser has the following extra methods:
465472

466473
```php
@@ -543,17 +550,18 @@ class MyTest extends PantherTestCase
543550

544551
There are several environment variables available to configure:
545552

546-
| Variable | Description | Default |
547-
|----------------------------|--------------------------------------------------------------------------------------------|------------------------------------|
548-
| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `./var/browser/source` |
549-
| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to (only applies to `PantherBrowser`). | `./var/browser/screenshots` |
550-
| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to (only applies to `PantherBrowser`). | `./var/browser/console-logs` |
551-
| `BROWSER_FOLLOW_REDIRECTS` | Whether to follow redirects by default (only applies to `KernelBrowser`). | `1` _(true)_ |
552-
| `BROWSER_CATCH_EXCEPTIONS` | Whether to catch exceptions by default (only applies to `KernelBrowser`). | `1` _(true)_ |
553-
| `BROWSER_SOURCE_DEBUG` | Whether to add request metadata to written source files (only applies to `KernelBrowser`). | `0` _(false)_ |
554-
| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` |
555-
| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` |
556-
| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` _(false)_ |
553+
| Variable | Description | Default |
554+
|----------------------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------------|
555+
| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `./var/browser/source` |
556+
| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to (only applies to `PantherBrowser`). | `./var/browser/screenshots` |
557+
| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to (only applies to `PantherBrowser`). | `./var/browser/console-logs` |
558+
| `BROWSER_FOLLOW_REDIRECTS` | Whether to follow redirects by default (only applies to `KernelBrowser`). | `1` _(true)_ |
559+
| `BROWSER_CATCH_EXCEPTIONS` | Whether to catch exceptions by default (only applies to `KernelBrowser`). | `1` _(true)_ |
560+
| `BROWSER_SOURCE_DEBUG` | Whether to add request metadata to written source files (only applies to `KernelBrowser`). | `0` _(false)_ |
561+
| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` |
562+
| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` |
563+
| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` _(false)_ |
564+
| `BROWSER_ALWAYS_START_WEBSERVER` | Always start a webserver configured for your current test env before running tests (only applies to `PantherBrowser`). | `0` _(false)_ |
557565

558566
## Extending
559567

@@ -818,9 +826,10 @@ Then, depending on the implementation you extended from, set the appropriate env
818826

819827
For the example above, you would set `KERNEL_BROWSER_CLASS=App\Tests\AppBrowser`.
820828

821-
**TIP**: Create a base functional test case so all your tests can use your
822-
custom browser and use the `@method` annotation to ensure your tests can
823-
autocomplete your custom methods:
829+
> [!TIP]
830+
> Create a base functional test case so all your tests can use your
831+
> custom browser and use the `@method` annotation to ensure your tests can
832+
> autocomplete your custom methods:
824833
825834
```php
826835
namespace App\Tests;

src/Browser/Test/HasBrowser.php

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ protected function pantherBrowser(array $options = [], array $kernelOptions = []
6464
'console_log_dir' => $_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs',
6565
];
6666

67+
if ($_SERVER['BROWSER_ALWAYS_START_WEBSERVER'] ?? null) {
68+
$_SERVER['PANTHER_APP_ENV'] = $_SERVER['APP_ENV'] ?? 'test'; // use current environment
69+
$_SERVER['SYMFONY_PROJECT_DEFAULT_ROUTE_URL'] = ''; // ignore existing server running with Symfony CLI
70+
}
71+
6772
if (self::$primaryPantherClient) {
6873
$browser = new $class(static::createAdditionalPantherClient(), $browserOptions);
6974
} else {

0 commit comments

Comments
 (0)