|
4 | 4 |
|
5 | 5 | namespace Pest\Browser; |
6 | 6 |
|
| 7 | +use Pest\Browser\Playwright\Client; |
7 | 8 | use Pest\Browser\Support\Screenshot; |
8 | 9 | use Pest\Contracts\Plugins\Bootable; |
9 | 10 | use Pest\Contracts\Plugins\Terminable; |
|
12 | 13 | /** |
13 | 14 | * @internal |
14 | 15 | */ |
15 | | -final readonly class Plugin implements Bootable, Terminable // @pest-arch-ignore-line |
| 16 | +final class Plugin implements Bootable, Terminable // @pest-arch-ignore-line |
16 | 17 | { |
| 18 | + /** |
| 19 | + * Indicates whether the plugin is used in the current test suite. |
| 20 | + */ |
| 21 | + private bool $usesPlugin = false; |
| 22 | + |
17 | 23 | /** |
18 | 24 | * Boots the plugin. |
19 | 25 | */ |
20 | 26 | public function boot(): void |
21 | 27 | { |
| 28 | + if (($this->usesPlugin = $this->usesPlugin()) === false) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
22 | 32 | if (Parallel::isWorker() === false) { |
23 | 33 | ServerManager::instance()->playwright()->start(); |
24 | 34 |
|
25 | 35 | Screenshot::cleanup(); |
26 | 36 | } |
| 37 | + |
| 38 | + Client::instance()->connectTo( |
| 39 | + ServerManager::instance()->playwright()->url().'?browser=chromium', |
| 40 | + ); |
| 41 | + |
| 42 | + if (Parallel::isEnabled() === false || Parallel::isWorker()) { |
| 43 | + $http = ServerManager::instance()->http(); |
| 44 | + |
| 45 | + $http->start(); |
| 46 | + |
| 47 | + $_ENV['APP_URL'] = "http://{$http->url()}"; |
| 48 | + } |
27 | 49 | } |
28 | 50 |
|
29 | 51 | /** |
30 | 52 | * Terminates the plugin. |
31 | 53 | */ |
32 | 54 | public function terminate(): void |
33 | 55 | { |
| 56 | + if ($this->usesPlugin === false) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
34 | 60 | if (Parallel::isWorker() === false) { |
35 | 61 | ServerManager::instance()->playwright()->stop(); |
36 | 62 | } |
37 | 63 |
|
38 | 64 | ServerManager::instance()->http()->stop(); |
39 | 65 | } |
| 66 | + |
| 67 | + /** |
| 68 | + * Checks if the plugin is used in the current test suite. |
| 69 | + */ |
| 70 | + private function usesPlugin(): bool |
| 71 | + { |
| 72 | + // check if any of the tests uses the page() function... |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
40 | 76 | } |
0 commit comments