mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 08:34:55 +00:00
a39686b339
- Add null check - Rename params - Reduce code duplication
60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using System.Threading.Tasks;
|
|
using Xunit.Abstractions;
|
|
using Xunit;
|
|
using System.IO;
|
|
using CefSharp.OffScreen;
|
|
using CefSharp.Example;
|
|
using System;
|
|
|
|
namespace CefSharp.Test.PrintToPdf
|
|
{
|
|
[Collection(CefSharpFixtureCollection.Key)]
|
|
public class PrintToPdfTests : BrowserTests
|
|
{
|
|
private readonly ITestOutputHelper output;
|
|
private readonly CefSharpFixture fixture;
|
|
|
|
public PrintToPdfTests(ITestOutputHelper output, CefSharpFixture fixture)
|
|
{
|
|
this.fixture = fixture;
|
|
this.output = output;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShouldWork()
|
|
{
|
|
AssertInitialLoadComplete();
|
|
|
|
var tempFile = Path.Combine(Path.GetTempPath(), "test.pdf");
|
|
|
|
if(File.Exists(tempFile))
|
|
{
|
|
File.Delete(tempFile);
|
|
}
|
|
|
|
var success = await Browser.PrintToPdfAsync(tempFile);
|
|
|
|
Assert.True(success, $"PDF Generation Failed {tempFile}");
|
|
Assert.True(File.Exists(tempFile), $"PDF File not found {tempFile}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShouldFailIfPageNotLoaded()
|
|
{
|
|
var tempFile = Path.Combine(Path.GetTempPath(), "test.pdf");
|
|
|
|
if (File.Exists(tempFile))
|
|
{
|
|
File.Delete(tempFile);
|
|
}
|
|
|
|
using (var browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl, automaticallyCreateBrowser: false))
|
|
{
|
|
var exception = await Assert.ThrowsAsync<Exception>(async () => await browser.PrintToPdfAsync(tempFile));
|
|
|
|
Assert.Equal(WebBrowserExtensions.BrowserNullExceptionString, exception.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|