microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

[BUG] Driver not found: /app/bin/.playwright/node/linux-x64/playwright.sh #2619

Closed rasupit closed 1 year ago

rasupit commented 1 year ago

Context:

Code Snippet c# code:

        public static async Task BrowseAsync()
        {
            using var playwright = await Playwright.CreateAsync();
            await using var browser = await playwright.Chromium.LaunchAsync();
            var page = await browser.NewPageAsync();
            await page.GotoAsync("https://playwright.dev/dotnet");
            await page.ScreenshotAsync(new()
            {
                Path = "screenshot.png"
            });

        }

dockerfile:

FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["PlaywrightNet/PlaywrightNet.csproj", "PlaywrightNet/"]
RUN dotnet restore "PlaywrightNet/PlaywrightNet.csproj"
COPY . .
WORKDIR "/src/PlaywrightNet"
RUN dotnet build "PlaywrightNet.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PlaywrightNet.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PlaywrightNet.dll"]

Code Snippet: test code is available: https://github.com/rasupit/PlaywrightNet

Describe the bug It appear that the folder linux-x64 is not exists. this foleder is needed because of the program is running inside linux docker

Error Screenshot: image

mxschmitt commented 1 year ago

Looks like you need https://playwright.dev/dotnet/docs/library#bundle-drivers-for-different-platforms

zpertee commented 1 year ago

@rasupit were you able to get your issues resolved? I was having the same issue, so I followed the advice from @mxschmitt and added: <PlaywrightPlatform>all</PlaywrightPlatform>, however that only got me to the next line: await using var browser = await playwright.Chromium.LaunchAsync(); at which point I get the error:

Microsoft.Playwright.PlaywrightException: 'Executable doesn't exist at /root/.cache/ms-playwright/chromium-1067/chrome-linux/chrome

mxschmitt commented 1 year ago

Since you now have the driver you can install now the browsers, see here: https://playwright.dev/dotnet/docs/browsers#install-browsers

zpertee commented 1 year ago

Thanks @mxschmitt. I'm up and running now!! :) 👍

rasupit commented 1 year ago

Looks like you need https://playwright.dev/dotnet/docs/library#bundle-drivers-for-different-platforms

this gets me past the driver not found. I ran into the same error message as @zpertee did but could not figure out how to run the playwright.sh. could this be added inside dockerfile?

mxschmitt commented 1 year ago

There is no need to run the playwright.sh manually. Once you have the PlaywrightPlatform correctly configured, you should be able to just install the browsers and it should all work.

If you want to skip installing the browsers, you can use our recommended Docker image: https://playwright.dev/dotnet/docs/docker

zpertee commented 1 year ago

@rasupit FYI, I ended up creating a function like this that I can access when when I pass in a -i argument into my console app:

static void PerformInitialPlaywrightInstall()
{
    var exitCode = Microsoft.Playwright.Program.Main(new[] { "install", "--with-deps", "chromium" });
    if (exitCode != 0)
    {
        throw new Exception($"Playwright exited with code {exitCode}");
    }
}

And then added this line to my dockerfile: RUN dotnet MyPlaywrightApp.dll -i. At that point I'm able to do a 1 time install during my docker build process and it seems like that everything works perfectly after that.

zpertee commented 1 year ago

@mxschmitt Does the recommended docker image already contain the .net runtime (if so which one(s)) or would I need to install that separately during the build process?

rasupit commented 1 year ago

Thanks @zpertee. With the help from @zpertee, I was able to figure out how to install the browser and its dependencies.

I wish there is some good step by step documentation about this process and not just some general documentation describing how to install this through powershell.

For those who struggles on how to setup playwright using dotnet with docker container, I'll leave the sample working project here: https://github.com/rasupit/PlaywrightNet