Skip to content

Commit

Permalink
Workarround chrome 129 blank screen headless issue
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee committed Oct 30, 2024
1 parent 5876456 commit 368efa0
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 46 deletions.
8 changes: 3 additions & 5 deletions Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
Expand All @@ -15,11 +15,10 @@
<PackageReleaseNotes>$(LAST_COMMIT_MESSAGE)</PackageReleaseNotes>
<PackageIcon>NugetIcon.png</PackageIcon>
<PackageProjectUrl>https://github.com/Codeuctivity/PuppeteerSharp.Renderer</PackageProjectUrl>
<Description>Renders HTML to PNG or PDF, supports windows and linux</Description>
<Description>Renders HTML to PNG or PDF, supports windows, linux and macos</Description>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<SignAssembly>True</SignAssembly>
<Optimize Condition="'$(GITHUB_ACTIONS)' == 'true'">true</Optimize>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -38,9 +37,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="PuppeteerSharp" Version="20.0.3" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 5 additions & 1 deletion Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PuppeteerSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -95,6 +96,9 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
BrowserFetcher = browserFetcher;
var revisionInfo = await BrowserFetcher.DownloadAsync(PuppeteerSharp.BrowserData.Chrome.DefaultBuildId).ConfigureAwait(false);
LaunchOptions.ExecutablePath = revisionInfo.GetExecutablePath();

// Temporary work around https://github.com/hardkoded/puppeteer-sharp/issues/2782 https://stackoverflow.com/questions/78996364/chrome-129-headless-shows-blank-window
LaunchOptions.Args = new List<string>(LaunchOptions.Args ?? Array.Empty<string>()) { "--headless=old" }.ToArray();
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);

return this;
Expand Down Expand Up @@ -171,7 +175,7 @@ private async Task<IPage> GetPage()
{
if (Browser == null)
{
throw new RendererException("Call CreateAsync first");
throw new RendererException($"Call {nameof(CreateAsync)} first");
}

return await Browser.NewPageAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 4 additions & 0 deletions Codeuctivity.HtmlRendererTests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CA1416: Validate platform compatibility
dotnet_diagnostic.CA1416.severity = none
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

<ItemGroup>
<Compile Remove="SourceTestFiles\**" />
<EmbeddedResource Remove="SourceTestFiles\**" />
<None Remove="SourceTestFiles\**" />
</ItemGroup>
<ItemGroup>
<Compile Remove="SourceTestFiles\**" />
<EmbeddedResource Remove="SourceTestFiles\**" />
<None Remove="SourceTestFiles\**" />

<SupportedPlatform Include="Linux" />
<SupportedPlatform Include="macOS" />
<SupportedPlatform Include="Windows" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="4.0.258" />
<PackageReference Include="PDFtoImage" Version="4.0.2" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="4.0.273" />
<PackageReference Include="PDFtoImage" Version="4.1.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
</ItemGroup>

<ItemGroup>
<EditorConfigFiles Remove="C:\Users\Stefan\Documents\source\repo\PuppeteerSharp.Renderer\Codeuctivity.HtmlRendererTests\.editorconfig" />
</ItemGroup>

<ItemGroup>
<None Include="C:\Users\Stefan\Documents\source\repo\PuppeteerSharp.Renderer\Codeuctivity.HtmlRendererTests\.editorconfig" />
</ItemGroup>
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,28 @@ internal static void AssertImageIsEqual(string actualImagePath, string expectIma
osSpecificDiffFileSuffix = "osx";
}

var allowedDiffImage = $"{expectFullPath}.diff.{osSpecificDiffFileSuffix}.png";
var osSpecificAllowedDiffImage = $"{expectFullPath}.diff.{osSpecificDiffFileSuffix}.png";
var newDiffImage = $"{actualFullPath}.diff.png";
using (var fileStreamDifferenceMask = File.Create(newDiffImage))
using (var maskImage = ImageSharpCompare.ImageSharpCompare.CalcDiffMaskImage(actualFullPath, expectFullPath))
{
SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
}

if (File.Exists(allowedDiffImage))
if (File.Exists(osSpecificAllowedDiffImage))
{
var resultWithAllowedDiff = ImageSharpCompare.ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, allowedDiffImage);
var resultWithOsSpecificAllowedDiff = ImageSharpCompare.ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, osSpecificAllowedDiffImage);

if (allowedPixelErrorCount < resultWithAllowedDiff.PixelErrorCount)
if (allowedPixelErrorCount < resultWithOsSpecificAllowedDiff.PixelErrorCount)
{
CopyToTestOutput(actualImagePath);
CopyToTestOutput(expectImageFilePath);
CopyToTestOutput(newDiffImage);
}

Assert.True(resultWithAllowedDiff.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {resultWithAllowedDiff.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}");
// File.Copy(newDiffImage, osSpecificAllowedDiffImage, true);

Assert.True(resultWithOsSpecificAllowedDiff.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {resultWithOsSpecificAllowedDiff.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}");
return;
}

Expand All @@ -67,7 +69,7 @@ internal static void AssertImageIsEqual(string actualImagePath, string expectIma
CopyToTestOutput(actualImagePath);
}

Assert.True(result.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {result.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}\nReplace {actualFullPath} with the new value or store the diff as {allowedDiffImage}.");
Assert.True(result.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {result.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}\nReplace {actualFullPath} with the new value or store the diff as {osSpecificAllowedDiffImage}.");
}

private static void CopyToTestOutput(string testOutputFile)
Expand Down
2 changes: 1 addition & 1 deletion Codeuctivity.HtmlRendererTests/RendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)

if (!IsRunningOnAzureOrMacos())
{
PDFtoImage.Conversion.SavePng(actualImagePathDirectory, await File.ReadAllBytesAsync(actualFilePath));
PDFtoImage.Conversion.SavePng(actualImagePathDirectory, await File.ReadAllBytesAsync(actualFilePath), 0);
DocumentAsserter.AssertImageIsEqual(actualImagePathDirectory, expectReferenceFilePath, 8080);
}
File.Delete(actualFilePath);
Expand Down

0 comments on commit 368efa0

Please sign in to comment.