# Launches a headless browser with the CDP port open and ties its life to this SSH session: # stdin closing (the caller went away, however violently) kills the whole process tree. Without # that leash the browser survives a dropped connection - measured: 8 orphaned chrome.exe. param([int]$Port = 9333, [string]$Size = "1280,800") $ErrorActionPreference = 'Stop' $candidates = @( "C:\Program Files\Google\Chrome\Application\chrome.exe", "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" ) $exe = $candidates | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $exe) { [Console]::Error.WriteLine("no chrome or edge found"); exit 2 } $udd = Join-Path $env:TEMP "gm-e2e-$Port" Remove-Item -Recurse -Force $udd -ErrorAction SilentlyContinue # No --disable-gpu here: forcing software WebGL on a desktop with a real card rasterises every # frame on the CPU and made the host unusable. Let the machine use the card it has. $flags = @( '--headless', '--ignore-gpu-blocklist', '--renderer-process-limit=1', "--remote-debugging-port=$Port", "--user-data-dir=`"$udd`"", "--window-size=$Size", '--hide-scrollbars', '--disable-background-networking', '--disable-sync', '--disable-extensions', '--no-first-run', '--mute-audio', '--no-default-browser-check', 'about:blank' ) $proc = Start-Process -FilePath $exe -ArgumentList $flags -PassThru -NoNewWindow [Console]::Error.WriteLine("gm-browser: $exe pid=$($proc.Id) port=$Port") # Either the browser dies on its own, or our caller does. Both must end this script. while (-not $proc.HasExited) { if ([Console]::In.ReadLine() -eq $null) { break } } if ($proc.HasExited) { [Console]::Error.WriteLine("gm-browser: browser exited on its own with code $($proc.ExitCode)") } else { # taskkill and tasklist filters are denied to this account; Stop-Process is not. Killing by name # is safe here because the account exists only to run these tests. Get-Process chrome, msedge -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue [Console]::Error.WriteLine("gm-browser: stdin closed, killed the browser") }