From 2ea02458f1f541724a5a2c51a1018f121334eb04 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 16 Mar 2025 16:18:50 +0300 Subject: [PATCH 1/2] [dotnet] [bidi] Make `LogEntry` as not nested --- .../Json/BiDiJsonSerializerContext.cs | 6 +++--- .../Converters/Polymorphic/LogEntryConverter.cs | 10 +++++----- .../BrowsingContext/BrowsingContextLogModule.cs | 4 ++-- .../BiDi/Modules/Log/{Entry.cs => LogEntry.cs} | 14 +++++++------- dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs | 4 ++-- dotnet/test/common/BiDi/Log/LogTest.cs | 12 ++++++------ .../test/common/BiDi/Script/ScriptCommandsTest.cs | 2 +- 7 files changed, 26 insertions(+), 26 deletions(-) rename dotnet/src/webdriver/BiDi/Modules/Log/{Entry.cs => LogEntry.cs} (68%) diff --git a/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs b/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs index 32c3335a61bb3..3f4fc34018617 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs @@ -66,8 +66,8 @@ namespace OpenQA.Selenium.BiDi.Communication.Json; [JsonSerializable(typeof(Modules.Script.RealmInfo.AudioWorklet))] [JsonSerializable(typeof(Modules.Script.RealmInfo.Worklet))] -[JsonSerializable(typeof(Modules.Log.Entry.Console))] -[JsonSerializable(typeof(Modules.Log.Entry.Javascript))] +[JsonSerializable(typeof(Modules.Log.ConsoleLogEntry))] +[JsonSerializable(typeof(Modules.Log.JavascriptLogEntry))] #endregion [JsonSerializable(typeof(Command))] @@ -157,7 +157,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json; [JsonSerializable(typeof(Modules.Script.RealmDestroyedEventArgs))] [JsonSerializable(typeof(IReadOnlyList))] -[JsonSerializable(typeof(Modules.Log.Entry))] +[JsonSerializable(typeof(Modules.Log.LogEntry))] [JsonSerializable(typeof(Modules.Storage.GetCookiesCommand))] [JsonSerializable(typeof(Modules.Storage.GetCookiesResult))] diff --git a/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/LogEntryConverter.cs b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/LogEntryConverter.cs index e573ea57df0fe..82cf56dd6013b 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/LogEntryConverter.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/LogEntryConverter.cs @@ -25,21 +25,21 @@ namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Polymorphic; // https://github.com/dotnet/runtime/issues/72604 -internal class LogEntryConverter : JsonConverter +internal class LogEntryConverter : JsonConverter { - public override Modules.Log.Entry? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override Modules.Log.LogEntry? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var jsonDocument = JsonDocument.ParseValue(ref reader); return jsonDocument.RootElement.GetProperty("type").ToString() switch { - "console" => jsonDocument.Deserialize(options), - "javascript" => jsonDocument.Deserialize(options), + "console" => jsonDocument.Deserialize(options), + "javascript" => jsonDocument.Deserialize(options), _ => null, }; } - public override void Write(Utf8JsonWriter writer, Modules.Log.Entry value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, Modules.Log.LogEntry value, JsonSerializerOptions options) { throw new NotImplementedException(); } diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextLogModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextLogModule.cs index 026e2dfd496e6..f70473c50dea6 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextLogModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextLogModule.cs @@ -25,7 +25,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext; public class BrowsingContextLogModule(BrowsingContext context, LogModule logModule) { - public Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) { return logModule.OnEntryAddedAsync(async args => { @@ -36,7 +36,7 @@ public Task OnEntryAddedAsync(Func handler, Subscript }, options); } - public Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) { return logModule.OnEntryAddedAsync(args => { diff --git a/dotnet/src/webdriver/BiDi/Modules/Log/Entry.cs b/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs similarity index 68% rename from dotnet/src/webdriver/BiDi/Modules/Log/Entry.cs rename to dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs index 52b57a4db11fd..3f2249ececad9 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Log/Entry.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -26,17 +26,17 @@ namespace OpenQA.Selenium.BiDi.Modules.Log; //[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] //[JsonDerivedType(typeof(Console), "console")] //[JsonDerivedType(typeof(Javascript), "javascript")] -public abstract record Entry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp) +public abstract record LogEntry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp) : EventArgs(BiDi) { public Script.StackTrace? StackTrace { get; set; } +} - public record Console(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp, string Method, IReadOnlyList Args) - : Entry(BiDi, Level, Source, Text, Timestamp); +public record ConsoleLogEntry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp, string Method, IReadOnlyList Args) + : LogEntry(BiDi, Level, Source, Text, Timestamp); - public record Javascript(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp) - : Entry(BiDi, Level, Source, Text, Timestamp); -} +public record JavascriptLogEntry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp) + : LogEntry(BiDi, Level, Source, Text, Timestamp); public enum Level { diff --git a/dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs b/dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs index 5fbdfc9acca07..f1442ee1ee358 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs @@ -25,12 +25,12 @@ namespace OpenQA.Selenium.BiDi.Modules.Log; public sealed class LogModule(Broker broker) : Module(broker) { - public async Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) + public async Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("log.entryAdded", handler, options).ConfigureAwait(false); } - public async Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) + public async Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("log.entryAdded", handler, options).ConfigureAwait(false); } diff --git a/dotnet/test/common/BiDi/Log/LogTest.cs b/dotnet/test/common/BiDi/Log/LogTest.cs index 58e0c6a701f6b..00d9b1cedc203 100644 --- a/dotnet/test/common/BiDi/Log/LogTest.cs +++ b/dotnet/test/common/BiDi/Log/LogTest.cs @@ -29,7 +29,7 @@ class LogTest : BiDiTestFixture [Test] public async Task CanListenToConsoleLog() { - TaskCompletionSource tcs = new(); + TaskCompletionSource tcs = new(); await using var subscription = await context.Log.OnEntryAddedAsync(tcs.SetResult); @@ -44,9 +44,9 @@ public async Task CanListenToConsoleLog() Assert.That(logEntry.Source.Realm, Is.Not.Null); Assert.That(logEntry.Text, Is.EqualTo("Hello, world!")); Assert.That(logEntry.Level, Is.EqualTo(Level.Info)); - Assert.That(logEntry, Is.AssignableFrom()); + Assert.That(logEntry, Is.AssignableFrom()); - var consoleLogEntry = logEntry as Entry.Console; + var consoleLogEntry = logEntry as ConsoleLogEntry; Assert.That(consoleLogEntry.Method, Is.EqualTo("log")); @@ -58,7 +58,7 @@ public async Task CanListenToConsoleLog() [Test] public async Task CanListenToJavascriptLog() { - TaskCompletionSource tcs = new(); + TaskCompletionSource tcs = new(); await using var subscription = await context.Log.OnEntryAddedAsync(tcs.SetResult); @@ -73,13 +73,13 @@ public async Task CanListenToJavascriptLog() Assert.That(logEntry.Source.Realm, Is.Not.Null); Assert.That(logEntry.Text, Is.EqualTo("Error: Not working")); Assert.That(logEntry.Level, Is.EqualTo(Level.Error)); - Assert.That(logEntry, Is.AssignableFrom()); + Assert.That(logEntry, Is.AssignableFrom()); } [Test] public async Task CanRetrieveStacktrace() { - TaskCompletionSource tcs = new(); + TaskCompletionSource tcs = new(); await using var subscription = await bidi.Log.OnEntryAddedAsync(tcs.SetResult); diff --git a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs index 6c5e6e3b6cfb1..e5448fcaefd6a 100644 --- a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs +++ b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs @@ -93,7 +93,7 @@ public async Task CanAddPreloadScript() Assert.That(preloadScript, Is.Not.Null); - TaskCompletionSource tcs = new(); + TaskCompletionSource tcs = new(); await context.Log.OnEntryAddedAsync(tcs.SetResult); From d81807c2d7010f5308f031891b806fe411be9c15 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 17 Mar 2025 18:38:38 +0300 Subject: [PATCH 2/2] Update LogEntry.cs --- dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs b/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs index 3f2249ececad9..5022ed0a5c6b7 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Log/LogEntry.cs @@ -24,8 +24,8 @@ namespace OpenQA.Selenium.BiDi.Modules.Log; // https://github.com/dotnet/runtime/issues/72604 //[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] -//[JsonDerivedType(typeof(Console), "console")] -//[JsonDerivedType(typeof(Javascript), "javascript")] +//[JsonDerivedType(typeof(ConsoleLogEntry), "console")] +//[JsonDerivedType(typeof(JavascriptLogEntry), "javascript")] public abstract record LogEntry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp) : EventArgs(BiDi) {