Skip to content

Commit

Permalink
Add - ConvertHtmlStringToPngData: take a screenshot without saving files
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee authored Jul 30, 2023
2 parents 39ba1bd + c29f290 commit 77d18b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- uses: actions/setup-node@v3.6.0
- uses: actions/setup-node@v3.7.0
with:
node-version: 18
- name: Restore dependencies
Expand Down
2 changes: 1 addition & 1 deletion Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="PuppeteerSharp" Version="10.1.0" />
<PackageReference Include="PuppeteerSharp" Version="10.1.2" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
23 changes: 23 additions & 0 deletions Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
await page.ScreenshotAsync(destinationPngFilePath, screenshotOptions).ConfigureAwait(false);
}

/// <summary>
/// Converts a HTML string to a PNG buffer
/// </summary>
/// <param name="sourceHtmlData"></param>
public Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData)
{
return ConvertHtmlStringToPngData(sourceHtmlData, new ScreenshotOptions { FullPage = true });
}

/// <summary>
/// Converts a HTML string to a PNG buffer
/// </summary>
/// <param name="sourceHtmlData"></param>
/// <param name="screenshotOptions"></param>
public async Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData, ScreenshotOptions screenshotOptions)
{
await using var page = await Browser.NewPageAsync().ConfigureAwait(false);
await page.SetContentAsync(sourceHtmlData).ConfigureAwait(false);
// Wait for fonts to be loaded. Omitting this might result in no text the screenshot.
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
return await page.ScreenshotDataAsync(screenshotOptions).ConfigureAwait(false);
}

private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (LastProgressValue != e.ProgressPercentage)
Expand Down
32 changes: 32 additions & 0 deletions Codeuctivity.HtmlRendererTests/RendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, b
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

[Theory]
[InlineData("BasicTextFormatedInlineBackground.html", false, 15000)]
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
public async Task ShouldConvertHtmlToPngBufferOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
{
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.{omitBackground}.png");
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.{omitBackground}.png";

if (File.Exists(actualFilePath))
{
File.Delete(actualFilePath);
}

await using (var chromiumRenderer = await Renderer.CreateAsync())
{
ScreenshotOptions screenshotOptions = new ScreenshotOptions
{
OmitBackground = omitBackground
};

var fileContent = await File.ReadAllTextAsync(sourceHtmlFilePath);
var pngData = await chromiumRenderer.ConvertHtmlStringToPngData(fileContent, screenshotOptions);
// File.Copy(actualFilePath, expectReferenceFilePath, true);

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / deployRelease

Remove this commented out code.

Check warning on line 192 in Codeuctivity.HtmlRendererTests/RendererTests.cs

View workflow job for this annotation

GitHub Actions / deployRelease

Remove this commented out code.
await File.WriteAllBytesAsync(actualFilePath, pngData);
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
}

File.Delete(actualFilePath);
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

[Fact]
public async Task ShouldDisposeGracefull()
{
Expand Down

0 comments on commit 77d18b1

Please sign in to comment.