Skip to content
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
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/DevTools/DevToolsCommandData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
Expand Down Expand Up @@ -90,7 +91,7 @@ public DevToolsCommandData(long commandId, string sessionId, string commandName,
/// Get or sets the result of the command execution.
/// </summary>
[JsonIgnore]
public JsonNode Result { get; set; }
public JsonElement Result { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the command resulted in an error response.
Expand Down
36 changes: 20 additions & 16 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new InvalidOperationException($"Type {command.GetType()} does not correspond to a known command response type.");
}

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

/// <summary>
Expand Down Expand Up @@ -201,7 +201,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.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
}

/// <summary>
Expand Down Expand Up @@ -230,7 +230,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
return default(TCommandResponse);
}

return result.Deserialize<TCommandResponse>();
return result.Value.Deserialize<TCommandResponse>();
}

/// <summary>
Expand All @@ -243,7 +243,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
//[DebuggerStepThrough]
public async Task<JsonNode> SendCommand(string commandName, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
public async Task<JsonElement?> SendCommand(string commandName, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
{
if (this.attachedTargetId == null)
{
Expand All @@ -265,7 +265,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
//[DebuggerStepThrough]
public async Task<JsonNode> SendCommand(string commandName, string sessionId, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
public async Task<JsonElement?> SendCommand(string commandName, string sessionId, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
{
if (millisecondsTimeout.HasValue == false)
{
Expand Down Expand Up @@ -298,8 +298,8 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
{
if (modified.IsError)
{
var errorMessage = modified.Result["message"].GetValue<string>();
var errorData = modified.Result["data"]?.GetValue<string>();
var errorMessage = modified.Result.GetProperty("message").GetString();
var errorData = modified.Result.TryGetProperty("data", out var data) ? data.GetString() : null;

var exceptionMessage = $"{commandName}: {errorMessage}";
if (!string.IsNullOrWhiteSpace(errorData))
Expand All @@ -310,7 +310,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
LogTrace("Recieved Error Response {0}: {1} {2}", modified.CommandId, message, errorData);
throw new CommandResponseException(exceptionMessage)
{
Code = modified.Result["code"]?.GetValue<long>() ?? -1
Code = modified.Result.TryGetProperty("code", out var code) ? code.GetInt64() : -1
};
}

Expand Down Expand Up @@ -559,23 +559,27 @@ private void ProcessMessage(string message)
logger.Trace($"CDP RCV << {message}");
}

var messageObject = JsonObject.Parse(message).AsObject();
JsonElement messageObject;
using (var doc = JsonDocument.Parse(message))
{
messageObject = doc.RootElement.Clone();
}

if (messageObject.TryGetPropertyValue("id", out var idProperty))
if (messageObject.TryGetProperty("id", out var idProperty))
{
long commandId = (long)idProperty;
long commandId = idProperty.GetInt64();

DevToolsCommandData commandInfo;
if (this.pendingCommands.TryGetValue(commandId, out commandInfo))
{
if (messageObject.TryGetPropertyValue("error", out var errorProperty))
if (messageObject.TryGetProperty("error", out var errorProperty))
{
commandInfo.IsError = true;
commandInfo.Result = errorProperty;
}
else
{
commandInfo.Result = messageObject["result"];
commandInfo.Result = messageObject.GetProperty("result");
LogTrace("Recieved Response {0}: {1}", commandId, commandInfo.Result.ToString());
}

Expand All @@ -594,11 +598,11 @@ private void ProcessMessage(string message)
return;
}

if (messageObject.TryGetPropertyValue("method", out var methodProperty))
if (messageObject.TryGetProperty("method", out var methodProperty))
{
var method = (string)methodProperty;
var method = methodProperty.GetString();
var methodParts = method.Split(new char[] { '.' }, 2);
var eventData = messageObject["params"];
var eventData = messageObject.GetProperty("params");

LogTrace("Recieved Event {0}: {1}", method, eventData.ToString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools
Expand All @@ -33,7 +34,7 @@ public class DevToolsEventReceivedEventArgs : EventArgs
/// <param name="domainName">The domain on which the event is to be raised.</param>
/// <param name="eventName">The name of the event to be raised.</param>
/// <param name="eventData">The data for the event to be raised.</param>
public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonNode eventData)
public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonElement eventData)
{
DomainName = domainName;
EventName = eventName;
Expand All @@ -53,6 +54,6 @@ public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonN
/// <summary>
/// Gets the data with which the event is to be raised.
/// </summary>
public JsonNode EventData { get; private set; }
public JsonElement EventData { get; private set; }
}
}
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/DevTools/IDevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -84,6 +85,6 @@ Task<TCommandResponse> SendCommand<TCommand, TCommandResponse>(TCommand command,
/// <param name="millisecondsTimeout">The execution timeout of the command in milliseconds.</param>
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
Task<JsonNode> SendCommand(string commandName, JsonNode @params, CancellationToken cancellationToken, int? millisecondsTimeout, bool throwExceptionIfResponseNotReceived);
Task<JsonElement?> SendCommand(string commandName, JsonNode @params, CancellationToken cancellationToken, int? millisecondsTimeout, bool throwExceptionIfResponseNotReceived);
}
}
Loading