Functions/Install-PWBrowsers.ps1


function Install-PWBrowsers
{
    <#
    .SYNOPSIS
    Installs Playwright browsers.
 
    .DESCRIPTION
    The `Install-PWBrowsers` function installs Playwright browsers. It uses the .NET CLI to create a new console
    application, add the Microsoft.Playwright package, and build the application. It then installs the Playwright
    browsers using the generated console application.
 
    Once the browsers are installed, the console application is removed.
 
    .EXAMPLE
    Install-PWBrowsers
 
    Demonstrates installing Playwright browsers.
    #>

    [CmdletBinding()]
    param()

    if (-not (Get-Command 'dotnet' -ErrorAction SilentlyContinue))
    {
        throw 'The .NET CLI is required to install Playwright browsers.'
    }

    Push-Location -Path $env:TEMP
    $consoleLocation = Join-Path -Path $env:TEMP -ChildPath 'PlaywrightAssets'
    dotnet new console -n 'PlaywrightAssets'
    Set-Location -Path $consoleLocation
    dotnet add package Microsoft.Playwright
    dotnet build

    $netVersionLocations = Get-ChildItem -Path (Join-Path -Path $consoleLocation -ChildPath 'bin\Debug') -Directory
    & (Join-Path -Path $netVersionLocations -ChildPath 'playwright.ps1' -Resolve) install
    Start-Sleep -Seconds 5
    Pop-Location
}