Skip to content

Commit a4d1a97

Browse files
committed
Production ready
fixed test
1 parent 92b7c50 commit a4d1a97

File tree

6 files changed

+330
-286
lines changed

6 files changed

+330
-286
lines changed

PuppeteerSharp.RendererTests/DocumentAsserter.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,48 @@ namespace PuppeteerSharp.RendererTests
88
{
99
internal static class DocumentAsserter
1010
{
11-
internal static void AssertImageIsEqual(string actualImagePath, string expectImageFilePath)
11+
internal static void AssertImageIsEqual(string actualImagePath, string expectImageFilePath, int allowedPixelErrorCount)
1212
{
1313
var actualFullPath = Path.GetFullPath(actualImagePath);
1414
var expectFullPath = Path.GetFullPath(expectImageFilePath);
1515

1616
Assert.True(File.Exists(actualFullPath), $"actualImagePath not found {actualFullPath}");
1717
Assert.True(File.Exists(expectFullPath), $"ExpectReferenceImagePath not found \n{expectFullPath}\n copy over \n{actualFullPath}\n if this is a new test case.");
18+
var base64fyedActualImage = Convert.ToBase64String(File.ReadAllBytes(actualFullPath));
1819

1920
if (ImageSharpCompare.ImageAreEqual(actualFullPath, expectFullPath))
2021
{
2122
return;
2223
}
2324

24-
var osSpezificDiffFileSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" : "win";
25+
var osSpecificDiffFileSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" : "win";
2526

26-
var allowedDiffImage = $"{expectFullPath}.diff.{osSpezificDiffFileSuffix}.png";
27+
var allowedDiffImage = $"{expectFullPath}.diff.{osSpecificDiffFileSuffix}.png";
28+
var newDiffImage = $"{actualFullPath}.diff.png";
29+
using (var fileStreamDifferenceMask = File.Create(newDiffImage))
30+
using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(actualFullPath, expectFullPath))
31+
{
32+
SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
33+
}
34+
var base64fyedImageDiff = Convert.ToBase64String(File.ReadAllBytes(newDiffImage));
2735

2836
if (File.Exists(allowedDiffImage))
2937
{
30-
var result = ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, allowedDiffImage);
38+
var resultWithAllowedDiff = ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath, allowedDiffImage);
3139

32-
if (result.AbsoluteError == 0)
33-
{
34-
return;
35-
}
40+
Assert.True(resultWithAllowedDiff.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {resultWithAllowedDiff.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n {base64fyedActualImage}\n \n Diff is {newDiffImage} \n {base64fyedImageDiff}\n");
41+
return;
3642
}
3743

38-
var newDiffImage = $"{actualFullPath}.diff.png";
3944
using (var fileStreamDifferenceMask = File.Create(newDiffImage))
4045
using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(actualFullPath, expectFullPath))
4146
{
4247
SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
4348
}
4449

45-
var base64fyedActualImage = Convert.ToBase64String(File.ReadAllBytes(actualFullPath));
46-
var base64fyedImageDiff = Convert.ToBase64String(File.ReadAllBytes(newDiffImage));
47-
Assert.True(ImageSharpCompare.ImageAreEqual(actualFullPath, expectFullPath), $"Expected {expectFullPath}\ndiffers to actual {actualFullPath}\n {base64fyedActualImage}\n \n Diff is {newDiffImage} \n {base64fyedImageDiff}\n");
50+
var result = ImageSharpCompare.CalcDiff(actualFullPath, expectFullPath);
51+
52+
Assert.True(result.PixelErrorCount <= allowedPixelErrorCount, $"Expected PixelErrorCount beyond {allowedPixelErrorCount} but was {result.PixelErrorCount}\nExpected {expectFullPath}\ndiffers to actual {actualFullPath}\n {base64fyedActualImage}\n \n Diff is {newDiffImage} \n {base64fyedImageDiff}\nReplace {actualFullPath} with the new value or store the diff as {allowedDiffImage}.");
4853
}
4954
}
5055
}
-18 Bytes
Loading
-100 Bytes
Loading

PuppeteerSharp.RendererTests/RendererTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
3131
var actualImages = await rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
3232

3333
Assert.Single(actualImages);
34-
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath);
34+
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 50);
3535
}
3636

3737
[Theory]
@@ -51,7 +51,7 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
5151

5252
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath);
5353

54-
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath);
54+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 50);
5555
}
5656
}
5757
}

0 commit comments

Comments
 (0)