Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using OpenQA.Selenium.Internal.Logging;
using OpenQA.Selenium.DevTools.Json;
using System;
using System.Collections.Concurrent;
using System.Globalization;
Expand Down Expand Up @@ -60,6 +61,11 @@ public class DevToolsSession : IDevToolsSession

private readonly static ILogger logger = Internal.Logging.Log.GetLogger<DevToolsSession>();

private static readonly JsonSerializerOptions s_devToolsSerializerOptions = new()
{
TypeInfoResolver = CdpSerializationContext.Default,
};

/// <summary>
/// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint.
/// </summary>
Expand Down Expand Up @@ -189,7 +195,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new ArgumentNullException(nameof(command));
}

var result = await SendCommand(command.CommandName, sessionId, JsonSerializer.SerializeToNode(command), cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);
var result = await SendCommand(command.CommandName, sessionId, JsonSerializer.SerializeToNode(command, s_devToolsSerializerOptions), cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);

if (result == null)
{
Expand All @@ -201,7 +207,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
}

return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
return (ICommandResponse<TCommand>)result.Value.Deserialize(commandResponseType, s_devToolsSerializerOptions);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevTools.Json;

[JsonSerializable(typeof(ICommand))]
[JsonSerializable(typeof(ICommandResponse<>))]
[JsonSourceGenerationOptions(Converters = [typeof(StringConverter)])]
internal sealed partial class CdpSerializationContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ namespace {{rootNamespace}}.{{domain.Name}}
{{/each}}
}
}

namespace OpenQA.Selenium.DevTools.Json
{
[global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{rootNamespace}}.{{domain.Name}}.{{className}}CommandSettings))]
[global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{rootNamespace}}.{{domain.Name}}.{{className}}CommandResponse))]
internal sealed partial class CdpSerializationContext;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 options:

  • Having only one single CdpSerializationContext for types from all cdp namespaces (v130, v131, v132) (and we know how to resolve collisions)
  • Or separate json context per cdp version

I am not deep dived yet. Please suggest which option would be more friendly for us.

}
Loading