Running Playwright .NET in Azure Function (Windows)

Viewed 1501

I am not able to run azure function which calls the playwright test after I publish it to the Azure cloud.

The exception is following

    Playwright Test or Playwright was just installed or updated. 
║ ║ Please run the following command to download new browsers: ║ ║ 
║ ║ playwright install ║ ║ 
║ ║ <3 Playwright Team ║

The issue is that I don't know how to run playwright install.

There is some remote build possibility, but the documentation does not contains details.

So I tried following

  1. Copy chrome browser %USERPROFILE%\AppData\Local\ms-playwright\chrome-win into the project folder.
  2. Attach chrome-win folder into the project.
  3. Update .csproj so the chrome browser will be in output directory and output zip
<ItemGroup>
  <Content Include="chrome-win\**">
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>
  1. Update executable path in playwright
await using var browser = await playwright.Chromium.LaunchAsync(new() { ExecutablePath = "chrome-win/chrome.exe"});

Locally, it is working fine. Zip Deploy to Azure is also successful. But when the Azure function is triggered, it throws some unknown exception

Result: Failure Exception: System.AggregateException: One or more errors occurred. (spawn UNKNOWN =========================== logs =========================== <launching> chrome-win/chrome.exe --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --user-data-dir=C:\local\Temp\playwright_chromiumdev_profile-rw44Ph --remote-debugging-pipe --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --no-startup-window ============================================================) ---> Microsoft.Playwright.PlaywrightException: spawn UNKNOWN 

I just don't want to believe that I have to rewrite everything into JavaScript because of one line command in the terminal

Is it possible to switch to Linux OS with functions written in .NET and use browser installed via npm install playwright? The .NET is cross platform now, so it might work?

Has anyone managed to get this up and running?

1 Answers

To run Playwright in a Windows-hosted Azure Function you have to make sure the browser is executed from the HOME_EXPANDED path instead of the default HOME environment variable path.
See Claire Novotny's blog for the details on the cause of this issue.

The HOME_EXPANDED environment variable points to a path like D:\DWASFiles\Sites\<FUNCTION_APP_NAME>\VirtualDirectory0\ which you can check in your Kudu environment which is hosted at https://<FUNCTION_APP_NAME>.scm.azurewebsites.net/Env.cshtml.

You will also need to make sure the .playwright directory is copied to the bin/ directory as described in Maurice Peters blog.

Manual browser download

You can either use the value of HOME_EXPANDED as a hardcoded path or retrieve the environment variable at runtime and pass it to the ExecutablePath property:

Environment.GetEnvironmentVariable("HOME_EXPANDED")

Automatic browser download

Instead you can also let Playwright download the browser binaries automatically for you.

Unfortunately there is no property which can be used but requires the PLAYWRIGHT_BROWSERS_PATH environment variable to be set to your preferred directory in the HOME_EXPANDED path location.

To download the binaries call the install command with any documented CLI arguments:

Microsoft.Playwright.Program.Main(new[] { "install", "chromium" });

This will only download the binaries once so you can just call it before you create the Playwright browser instance.

Related