我有以下 Dockerfile:
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["PlaywrightSharp.csproj", "PlaywrightSharp/"]
RUN dotnet restore "PlaywrightSharp/PlaywrightSharp.csproj"
COPY . "/src/PlaywrightSharp"
WORKDIR "/src/PlaywrightSharp"
RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -yq nodejs build-essential
RUN npm install -g npm
RUN dotnet add package Microsoft.Playwright
RUN dotnet build "PlaywrightSharp.csproj" -c Release -o /app/build
RUN npx playwright install-deps
RUN npx playwright install
RUN dotnet test --no-build
我使用以下命令构建图像:
docker build -t sarbo/playwrightsharp .
一切都很好,但是当开始执行测试时,我得到:
#19 1.227 Starting test execution, please wait...
#19 1.263 A total of 1 test files matched the specified pattern.
#19 3.223 Failed Main [711 ms]
#19 3.223 Error Message:
#19 3.223 Microsoft.Playwright.PlaywrightException :
#19 3.223 ╔════════════════════════════════════════════════════════════╗
#19 3.223 ║ Host system is missing a few dependencies to run browsers. ║
#19 3.223 ║ Please install them with the following command: ║
#19 3.223 ║ ║
#19 3.223 ║ playwright install-deps ║
#19 3.223 ║ ║
#19 3.223 ║ <3 Playwright Team ║
#19 3.223 ╚════════════════════════════════════════════════════════════╝
#19 3.223 Stack Trace:
#19 3.223 at Microsoft.Playwright.Transport.Connection.SendMessageToServerAsync[T](String guid, String method, Object args)
#19 3.223 at Microsoft.Playwright.Core.BrowserType.LaunchAsync(BrowserTypeLaunchOptions options)
#19 3.223 at PlaywrightSharp.Tests.Main() in /Users/sarbo/Documents/Projects/PlaywrightDemo/PlaywrightSharp/UnitTest1.cs:line 96
#19 3.223 at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
#19 3.223 at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
#19 3.223 at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
#19 3.223 at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
#19 3.223 at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
#19 3.223 at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
#19 3.223 at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
#19 3.224 Failed Test1 [362 ms]
来自 UnitTest1.cs 的第 96 行:
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = true,
});
是否有另一种方法来安装在 Docker 中运行 Playwright 测试所需的那些依赖项?测试在本地环境中运行,没有任何问题,但是当我尝试在容器内启动浏览器实例时,会抛出错误。