Create a build/playwright.ps1 script that discovers and runs the Playwright CLI. This abstracts away the Playwright CLI location which varies by project structure.
# build/playwright.ps1
# Discovers Microsoft.Playwright.dll and runs the bundled Playwright CLI
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Arguments
)
# Find the Playwright DLL (after dotnet build/restore)
$playwrightDll = Get-ChildItem -Path . -Recurse -Filter "Microsoft.Playwright.dll" -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $playwrightDll) {
Write-Error "Microsoft.Playwright.dll not found. Run 'dotnet build' first."
exit 1
}
$playwrightDir = $playwrightDll.DirectoryName
# Find the playwright CLI (path varies by OS and node version)
$playwrightCmd = Get-ChildItem -Path "$playwrightDir/.playwright/node" -Recurse -Filter "playwright.cmd" -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $playwrightCmd) {
# Try Unix executable
$playwrightCmd = Get-ChildItem -Path "$playwrightDir/.playwright/node" -Recurse -Filter "playwright" -ErrorAction SilentlyContinue |
Where-Object { $_.Name -eq "playwright" } |
Select-Object -First 1
}
if (-not $playwrightCmd) {
Write-Error "Playwright CLI not found in $playwrightDir/.playwright/node"
exit 1
}
Write-Host "Using Playwright CLI: $($playwrightCmd.FullName)"
& $playwrightCmd.FullName @Arguments
Usage:
# Install browsers
./build/playwright.ps1 install --with-deps
# Install specific browser
./build/playwright.ps1 install chromium
# Show installed browsers
./build/playwright.ps1 install --dry-run
Prerequisites
This pattern assumes:
Central Package Management (CPM) with Directory.Packages.props:
Project has been built before running playwright.ps1 (so DLLs exist)
PowerShell available on CI agents (pre-installed on GitHub Actions and Azure DevOps)
Why Version-Based Cache Keys Matter
Using the Playwright version in the cache key ensures:
Automatic invalidation when you upgrade Playwright
No stale browser binaries that don't match the SDK version
No manual cache clearing needed after version bumps
If you hardcode the cache key (e.g., playwright-browsers-v1), you'll need to manually bump it every time you upgrade Playwright, or you'll get cryptic version mismatch errors.
Troubleshooting
Cache not being used
Verify the version extraction step outputs the correct version
Check that the cache path matches your OS
Ensure Directory.Packages.props exists and has the Playwright package
"Browser not found" after cache hit
The cached browsers don't match the Playwright SDK version. This happens when:
The cache key doesn't include the version
The version extraction failed silently
Fix: Ensure the Playwright version is in the cache key.
playwright.ps1 can't find the DLL
Run dotnet build or dotnet restore before running the script. The Playwright DLL only exists after NuGet restore.
References
This pattern is battle-tested in production projects: