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]: .NET Framework 4.7.2 $Driver not found: ***\node\win32_x64\playwright.cmd #1797

Closed JohnnyZhang0628 closed 2 years ago

JohnnyZhang0628 commented 3 years ago

Playwright version

1.12

Operating system

Windows

What browsers are you seeing the problem on?

Chromium

Other information

No response

What happened? / Describe the bug

Then i run playwright install,i got an exception "Microsoft.Playwright assembly was found, but is missing required assets. Please ensure to build your project before running Playwright tool." The same code in .NET 5 app,it's OK.

Code snippet to reproduce your bug

using var playwright = await Playwright.CreateAsync();
                await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
                {
                    Headless = _headless
                });

                // 设置默认窗口大小
                var context = await browser.NewContextAsync(new BrowserNewContextOptions
                {
                    ViewportSize = new ViewportSize() { Width = 1920, Height = 1080 }
                });

Relevant log output

No response

JoelEinbinder commented 2 years ago

Sorry for the late response. Can you try a more recent version of playwright to see if this persists?

JohnnyZhang0628 commented 2 years ago

image I try to update the package of "Microsoft.Playwright" 1.16.1 version. It still not work.

vandre commented 2 years ago

I can replicate this issue as well on a Console Application with the latest Nuget Package (Version 1.17.0) and a .Net Framework Console App targeting .NetFramework 4.8. It seems like the install tool is not smart enough to recognize that this is not a .Net Core/5/6 project.

As a workaround what I did is copy the the .playwright folder to the bin/Debug folder from a .Net Core Project.

vandre commented 2 years ago

I figured out what the issue is. It has to do with how packages are handled in Full .Net Framework Projects (4.7, 4.8, etc..)

By default PackageReference is used for .Net Core/5/6 projects. .NET Framework projects support PackageReference, but currently default to packages.config.

When the Nuget package for Playwright is installed in a Full .Net Project it creates a packages.config file and the following reference in the .CSPROJ file:

    <Reference Include="Microsoft.Playwright, Version=1.17.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23, processorArchitecture=MSIL">
      <HintPath>packages\Microsoft.Playwright.1.17.0\lib\netstandard2.0\Microsoft.Playwright.dll</HintPath>
    </Reference>

Compare this to .Net Core/5/6 projects, which default to PackageReference:

  <ItemGroup>
    <PackageReference Include="Microsoft.Playwright" Version="1.17.0" />
  </ItemGroup>

This causes two separate issues with Playwright on Full .Net Projects:

Issue 1: Playwright install fails with the error described above Issue 2: dotnet build does not generate the .playwright folder

The workaround is to migrate your full framework project to package reference

Once I migrated my project to PackageReference the CLI tools (dotnet build, playwright install) worked as expected.

EDIT: After converting to using PackageReference I found out that Building from within the Visual Studio IDE works correctly, but dotnet build will fail if the .CSPROJ file is using the old project format. This may not be an issue for others, but if you want to use dotnet build, then your .CSPROJ file needs to be using the newer SDK project format. I used this tool to convert my .CSPROJ file

mxschmitt commented 2 years ago

Awesome, closing since it seems solved!

DeminChou commented 2 years ago

我把我的框架升级到了5.0,就没问题了。当然如果非得要在4.7.2下面使用,简直直接把.playwrite文件夹放到你的目标目录。