From e031b8d6e576d8ee6d15e7d129542fd02991a712 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 13 Sep 2024 20:14:35 +0300 Subject: [PATCH] [dotnet] [bidi] Enable implicit ways to specify page ranges for printing --- .../webdriver/BiDi/Communication/Broker.cs | 1 + .../Converters/PrintPageRangeConverter.cs | 28 +++++++ .../BrowsingContext/BrowsingContext.cs | 6 +- .../Modules/BrowsingContext/PrintCommand.cs | 75 +++++++++++++++---- 4 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 dotnet/src/webdriver/BiDi/Communication/Json/Converters/PrintPageRangeConverter.cs diff --git a/dotnet/src/webdriver/BiDi/Communication/Broker.cs b/dotnet/src/webdriver/BiDi/Communication/Broker.cs index 101177b3a195c..82549d1d5c5b1 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Broker.cs @@ -58,6 +58,7 @@ public Broker(BiDi bidi, ITransport transport) new RealmConverter(_bidi), new RealmTypeConverter(), new DateTimeOffsetConverter(), + new PrintPageRangeConverter(), new JsonStringEnumConverter(JsonNamingPolicy.CamelCase), // https://github.com/dotnet/runtime/issues/72604 diff --git a/dotnet/src/webdriver/BiDi/Communication/Json/Converters/PrintPageRangeConverter.cs b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/PrintPageRangeConverter.cs new file mode 100644 index 0000000000000..afd375d078a59 --- /dev/null +++ b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/PrintPageRangeConverter.cs @@ -0,0 +1,28 @@ +using OpenQA.Selenium.BiDi.Modules.BrowsingContext; +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace OpenQA.Selenium.BiDi.Communication.Json.Converters; + +internal class PrintPageRangeConverter : JsonConverter +{ + public override PrintPageRange Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } + + public override void Write(Utf8JsonWriter writer, PrintPageRange value, JsonSerializerOptions options) + { + // 5, "5-6", "-2", "2-" + + if (value.Start.HasValue && value.End.HasValue && value.Start == value.End) + { + writer.WriteNumberValue(value.Start.Value); + } + else + { + writer.WriteStringValue($"{value.Start}-{value.End}"); + } + } +} diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContext.cs index 4b00551f03520..2e653e8ddfacd 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContext.cs @@ -88,11 +88,9 @@ public Task SetViewportAsync(SetViewportOptions? options = null) return _bidi.BrowsingContextModule.SetViewportAsync(this, options); } - public async Task PrintAsync(PrintOptions? options = null) + public Task PrintAsync(PrintOptions? options = null) { - var result = await _bidi.BrowsingContextModule.PrintAsync(this, options).ConfigureAwait(false); - - return result.Data; + return _bidi.BrowsingContextModule.PrintAsync(this, options); } public Task HandleUserPromptAsync(HandleUserPromptOptions? options = null) diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/PrintCommand.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/PrintCommand.cs index d926e83e4cc27..66c270c12dd97 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/PrintCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/PrintCommand.cs @@ -1,4 +1,5 @@ -using OpenQA.Selenium.BiDi.Communication; +using OpenQA.Selenium.BiDi.Communication; +using System; using System.Collections.Generic; namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext; @@ -9,14 +10,13 @@ internal record PrintCommandParameters(BrowsingContext Context) : CommandParamet { public bool? Background { get; set; } - public Margin? Margin { get; set; } + public PrintMargin? Margin { get; set; } - public Orientation? Orientation { get; set; } + public PrintOrientation? Orientation { get; set; } - public Page? Page { get; set; } + public PrintPage? Page { get; set; } - // TODO: It also supports strings - public IEnumerable? PageRanges { get; set; } + public IEnumerable? PageRanges { get; set; } public double? Scale { get; set; } @@ -27,21 +27,20 @@ public record PrintOptions : CommandOptions { public bool? Background { get; set; } - public Margin? Margin { get; set; } + public PrintMargin? Margin { get; set; } - public Orientation? Orientation { get; set; } + public PrintOrientation? Orientation { get; set; } - public Page? Page { get; set; } + public PrintPage? Page { get; set; } - // TODO: It also supports strings - public IEnumerable? PageRanges { get; set; } + public IEnumerable? PageRanges { get; set; } public double? Scale { get; set; } public bool? ShrinkToFit { get; set; } } -public struct Margin +public struct PrintMargin { public double? Bottom { get; set; } @@ -52,17 +51,63 @@ public struct Margin public double? Top { get; set; } } -public enum Orientation +public enum PrintOrientation { Portrait, Landscape } -public struct Page +public struct PrintPage { public double? Height { get; set; } public double? Width { get; set; } } -public record PrintResult(string Data); +public readonly record struct PrintPageRange(int? Start, int? End) +{ + public static implicit operator PrintPageRange(int index) { return new PrintPageRange(index, index); } + +#if NET8_0_OR_GREATER + public static implicit operator PrintPageRange(Range range) + { + int? start; + int? end; + + if (range.Start.IsFromEnd && range.Start.Value == 0) + { + start = null; + } + else + { + if (range.Start.IsFromEnd) + { + throw new NotSupportedException($"Page index from end ({range.Start}) is not supported in page range for printing."); + } + + start = range.Start.Value; + } + + if (range.End.IsFromEnd && range.End.Value == 0) + { + end = null; + } + else + { + if (range.End.IsFromEnd) + { + throw new NotSupportedException($"Page index from end ({range.End}) is not supported in page range for printing."); + } + + end = range.End.Value; + } + + return new PrintPageRange(start, end); + } +#endif +} + +public record PrintResult(string Data) +{ + public byte[] ToByteArray() => Convert.FromBase64String(Data); +}