Browser plugin: keep the Playwright server's output when it crashes

Open Beginner friendly
#2,040 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
testing

Research direction

Start by reading src/Playwright/Servers/PlaywrightNpmServer.php in pestphp/pest-plugin-browser, especially how the server process starts and how it detects the listening line. Check the existing tests for PlaywrightNpmServer and add coverage for logging to the configured file and startup detection; done means the log captures output when PEST_BROWSER_SERVER_LOG is set while the unset behavior remains unchanged.

Written by the indexing model from the issue text.

Description

Problem

PlaywrightNpmServer reads the Playwright server's output through a pipe held by the test process. When the server crashes mid-suite, whatever it printed last is lost with the pipe: the suite reports hundreds of Client unexpectedly closed failures and nothing says why the server went down.

Proposal

An opt-in environment variable, PEST_BROWSER_SERVER_LOG. When it names a file, the server command appends its stdout and stderr to that file as it comes, and startup detection reads the "listening" line from the file instead of the pipe. When the variable is unset, nothing changes.

Why a file

A file survives the process that wrote it. After a crash the last lines are still there, and a CI job can keep them as an artifact.

Patch

About 35 lines in src/Playwright/Servers/PlaywrightNpmServer.php of pestphp/pest-plugin-browser (written against 5.0.1). We have been running it since early November on a large browser suite across 6 parallel processes. Happy to open a pull request against 5.x if the approach suits you.

Diff
diff --git a/src/Playwright/Servers/PlaywrightNpmServer.php b/src/Playwright/Servers/PlaywrightNpmServer.php
--- a/src/Playwright/Servers/PlaywrightNpmServer.php
+++ b/src/Playwright/Servers/PlaywrightNpmServer.php
@@ -24,6 +24,11 @@
     private const string PLAYWRIGHT_VERSION = '1.62.1';
 
     /**
+     * The environment variable naming the file the server's output is appended to.
+     */
+    public const string LOG = 'PEST_BROWSER_SERVER_LOG';
+
+    /**
      * The underlying process instance, if any.
      */
     private ?SystemProcess $systemProcess = null;
@@ -60,11 +65,27 @@
             return;
         }
 
-        $this->systemProcess = SystemProcess::fromShellCommandline(sprintf(
+        $command = sprintf(
             $this->command,
             $this->host,
             $this->port,
-        ), $this->baseDirectory, [
+        );
+
+        // The file the server's output is appended to as it comes, when one is
+        // named: the output a pipe holds is lost with the process that reads it,
+        // and a server that crashes writes nothing once it is gone.
+        $log = getenv(self::LOG);
+        $log = is_string($log) && $log !== '' ? $log : null;
+        $offset = 0;
+
+        if ($log !== null) {
+            $command .= ' >> '.escapeshellarg($log).' 2>&1';
+
+            clearstatcache(true, $log);
+            $offset = is_file($log) ? (int) filesize($log) : 0;
+        }
+
+        $this->systemProcess = SystemProcess::fromShellCommandline($command, $this->baseDirectory, [
             'APP_URL' => sprintf('http://%s:%d', $this->host, $this->port),
         ]);
 
@@ -72,10 +93,22 @@
 
         $this->systemProcess->start();
 
-        $this->systemProcess->waitUntil(
-            fn (string $type, string $output): bool => str_contains($output, $this->until)
-        );
+        if ($log === null) {
+            $this->systemProcess->waitUntil(
+                fn (string $type, string $output): bool => str_contains($output, $this->until)
+            );
+        } else {
+            while ($this->isRunning()) {
+                $written = @file_get_contents($log, false, null, $offset);
 
+                if (is_string($written) && str_contains($written, $this->until)) {
+                    break;
+                }
+
+                usleep(10_000);
+            }
+        }
+
         if ($this->isRunning() === false) {
             self::ensurePlaywrightIsInstalledAndVersionIsSupported();
 
Dominant language
PHP
Stars
11.8k
Forks
559
Avg merge
5d 23h
Merged PRs (30d)
24

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pestphp/pest

All issues in pestphp/pest

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.