Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Allow setting of PageDimensions and PageMargins in PrintOptions directly #14593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions dotnet/src/webdriver/PrintOptions.cs
shbenzer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,27 @@ public bool ShrinkToFit
}

/// <summary>
/// Gets the dimensions for each page in the printed document.
/// Gets or sets the dimensions for each page in the printed document.
/// </summary>
public PageSize PageDimensions
{
get { return pageSize; }
set
{
pageSize = value ?? throw new ArgumentNullException(nameof(value));
}
}

/// <summary>
/// Gets the margins for each page in the doucment.
/// Gets or sets the margins for each page in the doucment.
/// </summary>
public Margins PageMargins
{
get { return margins; }
set
{
margins = value ?? throw new ArgumentNullException(nameof(value));
}
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/test/common/DriverTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public abstract class DriverTestFixture
public string scrollFrameOutOfViewport = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
public string scrollFrameInViewport = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/frame_with_nested_scrolling_frame.html");

public string printPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("printPage.html");

protected IWebDriver driver;

public IWebDriver DriverInstance
Expand Down
74 changes: 74 additions & 0 deletions dotnet/test/common/PrintTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using NUnit.Framework;
using System;

namespace OpenQA.Selenium
{
[TestFixture]
public class PrintTest : DriverTestFixture
{
private const string MagicString = "JVBER";
private ISupportsPrint printer;

[SetUp]
public void LocalSetUp()
{
Assert.That(driver, Is.InstanceOf<ISupportsPrint>(), $"Driver does not support {nameof(ISupportsPrint)}.");

printer = driver as ISupportsPrint;

driver.Navigate().GoToUrl(this.printPage);
}

[Test]
public void CanPrintPage()
{
var pdf = printer.Print(new PrintOptions());

Assert.That(pdf.AsBase64EncodedString, Does.Contain(MagicString));
}

[Test]
public void CanPrintTwoPages()
{
var options = new PrintOptions();

options.AddPageRangeToPrint("1-2");

var pdf = printer.Print(options);

Assert.That(pdf.AsBase64EncodedString, Does.Contain(MagicString));
}

[Test]
public void CanPrintWithMostParams()
{
var options = new PrintOptions()
{
Orientation = PrintOrientation.Landscape,
ScaleFactor = 0.5,
PageDimensions = new PrintOptions.PageSize { Width = 200, Height = 100 },
PageMargins = new PrintOptions.Margins { Top = 1, Bottom = 1, Left = 2, Right = 2 },
OutputBackgroundImages = true,
ShrinkToFit = false
};

options.AddPageRangeToPrint("1-3");

var pdf = printer.Print(options);

Assert.That(pdf.AsBase64EncodedString, Does.Contain(MagicString));
}

[Test]
public void PageSizeCannotBeNull()
{
Assert.That(() => new PrintOptions { PageDimensions = null }, Throws.InstanceOf<ArgumentNullException>());
}

[Test]
public void MarginsCannotBeNull()
{
Assert.That(() => new PrintOptions { PageMargins = null }, Throws.InstanceOf<ArgumentNullException>());
}
}
}
Loading