1

我有以下 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 测试所需的那些依赖项?测试在本地环境中运行,没有任何问题,但是当我尝试在容器内启动浏览器实例时,会抛出错误。

4

1 回答 1

0

最后,我能够找到一种使用自定义映像在 Docker 容器内运行测试的方法。这是我所做的:

FROM mcr.microsoft.com/dotnet/sdk:5.0.404-focal AS build
WORKDIR /src
COPY ["PlaywrightSharp/PlaywrightSharp.csproj", "PlaywrightSharp/"]
RUN dotnet restore "PlaywrightSharp/PlaywrightSharp.csproj"

COPY . .
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 apt-get install -y wget xvfb unzip

# Set up the Chrome PPA
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list

# Update the package list and install chrome
RUN apt-get update -y
RUN apt-get install -y google-chrome-stable
RUN apt-get install -y firefox

#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


FROM build AS testrunner
WORKDIR "/src/PlaywrightSharp"
CMD ["dotnet", "test", "--no-restore", "--settings:Firefox.runsettings", "--logger:trx"]

看起来npx playwright install-deps运行测试还不够,我还必须安装 Chrome 和 Firefox。

于 2022-02-06T20:38:39.780 回答