When you have tried to run MS Playwright using C# in the context of an Azure Function, you probably have run into this message:
The driver it is referring to resides in the .playwright folder that is copied to the build output folder. Now, the output folder structure of an Azure Function project is different from most projects in the sense that there is an extra nested bin folder where the drivers should actually be copied.
The build target that the Playwright team uses, at the time of writing (version 1.15.4), always copies the folder containing the driver to the root of the output folder. So the fix here is to add an extra build target to your project file, the corrects for the extra nested folder:
<Target Name="FixPlaywrightCopyAfterBuild" AfterTargets="Build"> <ItemGroup>
<_BuildCopyItems Include="$(OutDir).playwright\**" />
</ItemGroup>
<Message Text="[Fix] Copying files to the nested bin folder of the azure function... $(OutDir)bin" Importance="high"/>
<Copy SourceFiles="@(_BuildCopyItems)" DestinationFiles="@(_BuildCopyItems->'$(OutDir)bin\.playwright\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
This will enable you to run the function. But this only works for the build scenario. When publishing the function to Azure, for instance, the output folder is different again. So we have to correct for that to:
<Target Name="FixPlaywrightCopyAfterPublish" AfterTargets="Publish"> <ItemGroup>
<_BuildCopyItems Include="$(PublishDir).playwright\**" />
</ItemGroup>
<Message Text="[Fix] Copying files to the nested bin folder of the azure function for publishing... $(PublishDir)bin" Importance="high"/>
<Copy SourceFiles="@(_BuildCopyItems)" DestinationFiles="@(_BuildCopyItems->'$(PublishDir)bin\.playwright\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
Hope it will help people since I noticed more people encountering this issue.
Comments
Post a Comment