在 Azure Function 中运行 Playwright .NET (Windows)

Muf*_*lix 4 azure-functions playwright

将其发布到 Azure 云后,我无法运行调用剧作家测试的 azure 函数。

\n

异常如下

\n
    Playwright Test or Playwright was just installed or updated. \n\xe2\x95\x91 \xe2\x95\x91 Please run the following command to download new browsers: \xe2\x95\x91 \xe2\x95\x91 \n\xe2\x95\x91 \xe2\x95\x91 playwright install \xe2\x95\x91 \xe2\x95\x91 \n\xe2\x95\x91 \xe2\x95\x91 <3 Playwright Team \xe2\x95\x91\n
Run Code Online (Sandbox Code Playgroud)\n

问题是我不知道如何运行playwright install

\n

有一些远程构建的可能性,但文档不包含详细信息。

\n

所以我尝试遵循

\n
    \n
  1. 将 chrome 浏览器复制%USERPROFILE%\\AppData\\Local\\ms-playwright\\chrome-win到项目文件夹中。
  2. \n
  3. 将 chrome-win 文件夹附加到项目中。
  4. \n
  5. 更新.csproj后 chrome 浏览器将位于输出目录并输出 zip
  6. \n
\n
<ItemGroup>\n  <Content Include="chrome-win\\**">\n     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\n  </Content>\n</ItemGroup>\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 更新剧作家中的可执行路径
  2. \n
\n
await using var browser = await playwright.Chromium.LaunchAsync(new() { ExecutablePath = "chrome-win/chrome.exe"});\n
Run Code Online (Sandbox Code Playgroud)\n

在本地,它运行良好。Zip 部署到 Azure 也成功。但是当Azure函数被触发时,它会抛出一些未知的异常

\n
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 \n
Run Code Online (Sandbox Code Playgroud)\n

我只是不想相信因为终端中的一行命令我必须将所有内容重写为 JavaScript

\n

是否可以切换到具有用.NET编写的功能的Linux操作系统并使用通过安装的浏览器npm install playwright?.NET 现在是跨平台的,所以它可以工作吗?

\n

有人设法让它启动并运行吗?

\n

Kap*_*apé 7

要在 Windows 托管的 Azure Function 中运行 Playwright,您必须确保从该HOME_EXPANDED路径而不是默认HOME环境变量路径执行浏览器。有关此问题原因的详细信息,
请参阅Claire Novotny 的博客。

环境HOME_EXPANDED变量指向一个路径D:\DWASFiles\Sites\<FUNCTION_APP_NAME>\VirtualDirectory0\,您可以在托管于 https://<FUNCTION_APP_NAME>.scm.azurewebsites.net/Env.cshtml 的 Kudu 环境中检查该路径。

您还需要确保将该.playwright目录复制到Maurice Peters 博客bin/中描述的目录中。

手动浏览器下载

您可以使用 的值HOME_EXPANDED作为硬编码路径,也可以在运行时检索环境变量并将其传递给属性ExecutablePath

环境.GetEnvironmentVariable(“HOME_EXPANDED”)

浏览器自动下载

相反,您也可以让 Playwright 自动为您下载浏览器二进制文件。

不幸的是,没有可以使用的属性,但需要将PLAYWRIGHT_BROWSERS_PATH环境变量设置为HOME_EXPANDED路径位置中的首选目录。

要下载二进制文件,请使用任何记录的 CLI 参数调用安装命令:

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

这只会下载一次二进制文件,因此您可以在创建 Playwright 浏览器实例之前调用它。