【问题标题】:Running Playwright .NET in Azure Function (Windows)在 Azure Function (Windows) 中运行 Playwright .NET
【发布时间】:2021-12-04 13:55:16
【问题描述】:

在我发布到 Azure 云后,我无法运行调用 playwright 测试的 azure 函数。

异常如下

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

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

有一些remote build possibility,但文档没有包含详细信息。

所以我尝试关注

  1. 将chrome浏览器%USERPROFILE%\AppData\Local\ms-playwright\chrome-win复制到项目文件夹中。
  2. 将 chrome-win 文件夹附加到项目中。
  3. 更新.csproj,这样chrome浏览器就会在输出目录中并输出zip
<ItemGroup>
  <Content Include="chrome-win\**">
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>
  1. 更新 playwright 中的可执行路径
await using var browser = await playwright.Chromium.LaunchAsync(new() { ExecutablePath = "chrome-win/chrome.exe"});

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

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 

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

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

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

【问题讨论】:

标签: azure-functions playwright


【解决方案1】:

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

HOME_EXPANDED 环境变量指向类似D:\DWASFiles\Sites\&lt;FUNCTION_APP_NAME&gt;\VirtualDirectory0\ 的路径,您可以在托管于 https://.scm.azurewebsites.net/Env.cshtml 的 Kudu 环境中检查该路径。

您还需要确保将.playwright 目录复制到bin/ 目录,如Maurice Peters blog 中所述。

手动浏览器下载

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

Environment.GetEnvironmentVariable("HOME_EXPANDED")

自动浏览器下载

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

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

要下载二进制文件,请使用任何 documented CLI arguments 调用 install 命令:

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

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

【讨论】:

    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 2022-08-16
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多