Skip to content

[dotnet] [bidi] Make LogEntry as not nested #15432

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

Merged
merged 2 commits into from
Mar 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -157,7 +157,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
[JsonSerializable(typeof(Modules.Script.RealmDestroyedEventArgs))]
[JsonSerializable(typeof(IReadOnlyList<Modules.Script.RealmInfo>))]

[JsonSerializable(typeof(Modules.Log.Entry))]
[JsonSerializable(typeof(Modules.Log.LogEntry))]

[JsonSerializable(typeof(Modules.Storage.GetCookiesCommand))]
[JsonSerializable(typeof(Modules.Storage.GetCookiesResult))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Polymorphic;

// https://github.com/dotnet/runtime/issues/72604
internal class LogEntryConverter : JsonConverter<Modules.Log.Entry>
internal class LogEntryConverter : JsonConverter<Modules.Log.LogEntry>
{
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<Modules.Log.Entry.Console>(options),
"javascript" => jsonDocument.Deserialize<Modules.Log.Entry.Javascript>(options),
"console" => jsonDocument.Deserialize<ConsoleLogEntry>(options),
"javascript" => jsonDocument.Deserialize<JavascriptLogEntry>(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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;

public class BrowsingContextLogModule(BrowsingContext context, LogModule logModule)
{
public Task<Subscription> OnEntryAddedAsync(Func<Entry, Task> handler, SubscriptionOptions? options = null)
public Task<Subscription> OnEntryAddedAsync(Func<Log.LogEntry, Task> handler, SubscriptionOptions? options = null)
{
return logModule.OnEntryAddedAsync(async args =>
{
Expand All @@ -36,7 +36,7 @@ public Task<Subscription> OnEntryAddedAsync(Func<Entry, Task> handler, Subscript
}, options);
}

public Task<Subscription> OnEntryAddedAsync(Action<Entry> handler, SubscriptionOptions? options = null)
public Task<Subscription> OnEntryAddedAsync(Action<Log.LogEntry> handler, SubscriptionOptions? options = null)
{
return logModule.OnEntryAddedAsync(args =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="Entry.cs" company="Selenium Committers">
// <copyright file="LogEntry.cs" company="Selenium Committers">
// 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
Expand All @@ -24,19 +24,19 @@ namespace OpenQA.Selenium.BiDi.Modules.Log;

// https://github.com/dotnet/runtime/issues/72604
//[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)
//[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)
{
public Script.StackTrace? StackTrace { get; set; }
}

public record Console(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp, string Method, IReadOnlyList<Script.RemoteValue> Args)
: Entry(BiDi, Level, Source, Text, Timestamp);
public record ConsoleLogEntry(BiDi BiDi, Level Level, Script.Source Source, string Text, DateTimeOffset Timestamp, string Method, IReadOnlyList<Script.RemoteValue> 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
{
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/BiDi/Modules/Log/LogModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ namespace OpenQA.Selenium.BiDi.Modules.Log;

public sealed class LogModule(Broker broker) : Module(broker)
{
public async Task<Subscription> OnEntryAddedAsync(Func<Entry, Task> handler, SubscriptionOptions? options = null)
public async Task<Subscription> OnEntryAddedAsync(Func<LogEntry, Task> handler, SubscriptionOptions? options = null)
{
return await Broker.SubscribeAsync("log.entryAdded", handler, options).ConfigureAwait(false);
}

public async Task<Subscription> OnEntryAddedAsync(Action<Entry> handler, SubscriptionOptions? options = null)
public async Task<Subscription> OnEntryAddedAsync(Action<LogEntry> handler, SubscriptionOptions? options = null)
{
return await Broker.SubscribeAsync("log.entryAdded", handler, options).ConfigureAwait(false);
}
Expand Down
12 changes: 6 additions & 6 deletions dotnet/test/common/BiDi/Log/LogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LogTest : BiDiTestFixture
[Test]
public async Task CanListenToConsoleLog()
{
TaskCompletionSource<Entry> tcs = new();
TaskCompletionSource<Modules.Log.LogEntry> tcs = new();

await using var subscription = await context.Log.OnEntryAddedAsync(tcs.SetResult);

Expand All @@ -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<Entry.Console>());
Assert.That(logEntry, Is.AssignableFrom<ConsoleLogEntry>());

var consoleLogEntry = logEntry as Entry.Console;
var consoleLogEntry = logEntry as ConsoleLogEntry;

Assert.That(consoleLogEntry.Method, Is.EqualTo("log"));

Expand All @@ -58,7 +58,7 @@ public async Task CanListenToConsoleLog()
[Test]
public async Task CanListenToJavascriptLog()
{
TaskCompletionSource<Entry> tcs = new();
TaskCompletionSource<Modules.Log.LogEntry> tcs = new();

await using var subscription = await context.Log.OnEntryAddedAsync(tcs.SetResult);

Expand All @@ -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<Entry.Javascript>());
Assert.That(logEntry, Is.AssignableFrom<JavascriptLogEntry>());
}

[Test]
public async Task CanRetrieveStacktrace()
{
TaskCompletionSource<Entry> tcs = new();
TaskCompletionSource<Modules.Log.LogEntry> tcs = new();

await using var subscription = await bidi.Log.OnEntryAddedAsync(tcs.SetResult);

Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task CanAddPreloadScript()

Assert.That(preloadScript, Is.Not.Null);

TaskCompletionSource<Modules.Log.Entry> tcs = new();
TaskCompletionSource<Modules.Log.LogEntry> tcs = new();

await context.Log.OnEntryAddedAsync(tcs.SetResult);

Expand Down
Loading