Skip to content

Commit 368efa0

Browse files
committed
Workarround chrome 129 blank screen headless issue
hardkoded/puppeteer-sharp#2782
1 parent 5876456 commit 368efa0

14 files changed

+66
-46
lines changed

Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0</TargetFrameworks>
@@ -15,11 +15,10 @@
1515
<PackageReleaseNotes>$(LAST_COMMIT_MESSAGE)</PackageReleaseNotes>
1616
<PackageIcon>NugetIcon.png</PackageIcon>
1717
<PackageProjectUrl>https://github.com/Codeuctivity/PuppeteerSharp.Renderer</PackageProjectUrl>
18-
<Description>Renders HTML to PNG or PDF, supports windows and linux</Description>
18+
<Description>Renders HTML to PNG or PDF, supports windows, linux and macos</Description>
1919
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2121
<SignAssembly>True</SignAssembly>
22-
<Optimize Condition="'$(GITHUB_ACTIONS)' == 'true'">true</Optimize>
2322
<IncludeSymbols>true</IncludeSymbols>
2423
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2524
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -38,9 +37,8 @@
3837
</PropertyGroup>
3938

4039
<ItemGroup>
41-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
4240
<PackageReference Include="PuppeteerSharp" Version="20.0.3" />
43-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
41+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
4442
<PrivateAssets>all</PrivateAssets>
4543
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4644
</PackageReference>

Codeuctivity.HtmlRenderer/Renderer.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using PuppeteerSharp;
22
using System;
3+
using System.Collections.Generic;
34
using System.Diagnostics.CodeAnalysis;
45
using System.IO;
56
using System.Runtime.InteropServices;
@@ -95,6 +96,9 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
9596
BrowserFetcher = browserFetcher;
9697
var revisionInfo = await BrowserFetcher.DownloadAsync(PuppeteerSharp.BrowserData.Chrome.DefaultBuildId).ConfigureAwait(false);
9798
LaunchOptions.ExecutablePath = revisionInfo.GetExecutablePath();
99+
100+
// Temporary work around https://github.com/hardkoded/puppeteer-sharp/issues/2782 https://stackoverflow.com/questions/78996364/chrome-129-headless-shows-blank-window
101+
LaunchOptions.Args = new List<string>(LaunchOptions.Args ?? Array.Empty<string>()) { "--headless=old" }.ToArray();
98102
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
99103

100104
return this;
@@ -171,7 +175,7 @@ private async Task<IPage> GetPage()
171175
{
172176
if (Browser == null)
173177
{
174-
throw new RendererException("Call CreateAsync first");
178+
throw new RendererException($"Call {nameof(CreateAsync)} first");
175179
}
176180

177181
return await Browser.NewPageAsync().ConfigureAwait(false);

Codeuctivity.HtmlRendererCliTests/Codeuctivity.HtmlRendererCliTests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
11-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
11+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
<PrivateAssets>all</PrivateAssets>
1414
</PackageReference>
15-
<PackageReference Include="xunit" Version="2.8.1" />
16-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
15+
<PackageReference Include="xunit" Version="2.9.2" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
<PrivateAssets>all</PrivateAssets>
1919
</PackageReference>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.cs]
2+
3+
# CA1416: Validate platform compatibility
4+
dotnet_diagnostic.CA1416.severity = none
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>net8.0</TargetFrameworks>
5-
<IsPackable>false</IsPackable>
6-
<Nullable>enable</Nullable>
7-
<IsPackable>false</IsPackable>
8-
<EnableNETAnalyzers>true</EnableNETAnalyzers>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
9+
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<Compile Remove="SourceTestFiles\**" />
13-
<EmbeddedResource Remove="SourceTestFiles\**" />
14-
<None Remove="SourceTestFiles\**" />
15-
</ItemGroup>
11+
<ItemGroup>
12+
<Compile Remove="SourceTestFiles\**" />
13+
<EmbeddedResource Remove="SourceTestFiles\**" />
14+
<None Remove="SourceTestFiles\**" />
15+
16+
<SupportedPlatform Include="Linux" />
17+
<SupportedPlatform Include="macOS" />
18+
<SupportedPlatform Include="Windows" />
19+
</ItemGroup>
1620

17-
<ItemGroup>
18-
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="4.0.258" />
19-
<PackageReference Include="PDFtoImage" Version="4.0.2" />
20-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.26.0.92422">
21-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22-
<PrivateAssets>all</PrivateAssets>
23-
</PackageReference>
24-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
25-
<PackageReference Include="xunit" Version="2.8.1" />
26-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
27-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
28-
<PrivateAssets>all</PrivateAssets>
29-
</PackageReference>
30-
</ItemGroup>
21+
<ItemGroup>
22+
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="4.0.273" />
23+
<PackageReference Include="PDFtoImage" Version="4.1.1" />
24+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
<PrivateAssets>all</PrivateAssets>
27+
</PackageReference>
28+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
29+
<PackageReference Include="xunit" Version="2.9.2" />
30+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
31+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
32+
<PrivateAssets>all</PrivateAssets>
33+
</PackageReference>
34+
</ItemGroup>
3135

32-
<ItemGroup>
33-
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
34-
</ItemGroup>
36+
<ItemGroup>
37+
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
38+
</ItemGroup>
39+
40+
<ItemGroup>
41+
<EditorConfigFiles Remove="C:\Users\Stefan\Documents\source\repo\PuppeteerSharp.Renderer\Codeuctivity.HtmlRendererTests\.editorconfig" />
42+
</ItemGroup>
43+
44+
<ItemGroup>
45+
<None Include="C:\Users\Stefan\Documents\source\repo\PuppeteerSharp.Renderer\Codeuctivity.HtmlRendererTests\.editorconfig" />
46+
</ItemGroup>
3547
</Project>

Codeuctivity.HtmlRendererTests/Infrastructure/DocumentAsserter.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,28 @@ internal static void AssertImageIsEqual(string actualImagePath, string expectIma
2929
osSpecificDiffFileSuffix = "osx";
3030
}
3131

32-
var allowedDiffImage = $"{expectFullPath}.diff.{osSpecificDiffFileSuffix}.png";
32+
var osSpecificAllowedDiffImage = $"{expectFullPath}.diff.{osSpecificDiffFileSuffix}.png";
3333
var newDiffImage = $"{actualFullPath}.diff.png";
3434
using (var fileStreamDifferenceMask = File.Create(newDiffImage))
3535
using (var maskImage = ImageSharpCompare.ImageSharpCompare.CalcDiffMaskImage(actualFullPath, expectFullPath))
3636
{
3737
SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
3838
}
3939

40-
if (File.Exists(allowedDiffImage))
40+
if (File.Exists(osSpecificAllowedDiffImage))
4141
{
42-
var resultWithAllowedDiff = ImageSharpCompare.ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, allowedDiffImage);
42+
var resultWithOsSpecificAllowedDiff = ImageSharpCompare.ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, osSpecificAllowedDiffImage);
4343

44-
if (allowedPixelErrorCount < resultWithAllowedDiff.PixelErrorCount)
44+
if (allowedPixelErrorCount < resultWithOsSpecificAllowedDiff.PixelErrorCount)
4545
{
4646
CopyToTestOutput(actualImagePath);
4747
CopyToTestOutput(expectImageFilePath);
4848
CopyToTestOutput(newDiffImage);
4949
}
5050

51-
Assert.True(resultWithAllowedDiff.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {resultWithAllowedDiff.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}");
51+
// File.Copy(newDiffImage, osSpecificAllowedDiffImage, true);
52+
53+
Assert.True(resultWithOsSpecificAllowedDiff.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {resultWithOsSpecificAllowedDiff.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n Diff is {newDiffImage}");
5254
return;
5355
}
5456

@@ -67,7 +69,7 @@ internal static void AssertImageIsEqual(string actualImagePath, string expectIma
6769
CopyToTestOutput(actualImagePath);
6870
}
6971

70-
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}.");
72+
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}.");
7173
}
7274

7375
private static void CopyToTestOutput(string testOutputFile)

Codeuctivity.HtmlRendererTests/RendererTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
3636

3737
if (!IsRunningOnAzureOrMacos())
3838
{
39-
PDFtoImage.Conversion.SavePng(actualImagePathDirectory, await File.ReadAllBytesAsync(actualFilePath));
39+
PDFtoImage.Conversion.SavePng(actualImagePathDirectory, await File.ReadAllBytesAsync(actualFilePath), 0);
4040
DocumentAsserter.AssertImageIsEqual(actualImagePathDirectory, expectReferenceFilePath, 8080);
4141
}
4242
File.Delete(actualFilePath);

0 commit comments

Comments
 (0)