Skip to content

Commit

Permalink
[dotnet] Json serializer gen context for SM output (#14481)
Browse files Browse the repository at this point in the history
* [dotnet] Json serializer gen context for SM output

* Add System.Text.Encodings.Web dep for json context

* Update bazel dotnet rules to 0.16
  • Loading branch information
nvborisenko authored Oct 7, 2024
1 parent 64ed1e1 commit 40bfc5e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bazel_dep(name = "protobuf", version = "21.7", dev_dependency = True, repo_name
# Required for rules_rust to import the crates properly
bazel_dep(name = "rules_cc", version = "0.0.9", dev_dependency = True)

bazel_dep(name = "rules_dotnet", version = "0.15.1")
bazel_dep(name = "rules_dotnet", version = "0.16.0")
bazel_dep(name = "rules_java", version = "7.11.1")
bazel_dep(name = "rules_jvm_external", version = "6.3")
bazel_dep(name = "rules_nodejs", version = "6.2.0")
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ csharp_library(
framework("nuget", "Microsoft.Bcl.AsyncInterfaces"),
framework("nuget", "System.Threading.Tasks.Extensions"),
framework("nuget", "System.Memory"),
framework("nuget", "System.Text.Encodings.Web"),
framework("nuget", "System.Text.Json"),
],
)
Expand Down Expand Up @@ -119,6 +120,7 @@ csharp_library(
framework("nuget", "Microsoft.Bcl.AsyncInterfaces"),
framework("nuget", "System.Threading.Tasks.Extensions"),
framework("nuget", "System.Memory"),
framework("nuget", "System.Text.Encodings.Web"),
framework("nuget", "System.Text.Json"),
],
)
Expand Down
67 changes: 50 additions & 17 deletions dotnet/src/webdriver/SeleniumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium
{
Expand All @@ -38,6 +38,8 @@ public static class SeleniumManager

private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");

private static readonly JsonSerializerOptions _serializerOptions = new() { PropertyNameCaseInsensitive = true, TypeInfoResolver = SeleniumManagerSerializerContext.Default };

static SeleniumManager()
{

Expand Down Expand Up @@ -86,10 +88,12 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
argsBuilder.Append(" --debug");
}

var output = RunCommand(BinaryFullPath, argsBuilder.ToString());
Dictionary<string, string> binaryPaths = new Dictionary<string, string>();
binaryPaths.Add("browser_path", (string)output["browser_path"]);
binaryPaths.Add("driver_path", (string)output["driver_path"]);
var smCommandResult = RunCommand(BinaryFullPath, argsBuilder.ToString());
Dictionary<string, string> binaryPaths = new()
{
{ "browser_path", smCommandResult.BrowserPath },
{ "driver_path", smCommandResult.DriverPath }
};

if (_logger.IsEnabled(LogEventLevel.Trace))
{
Expand All @@ -108,7 +112,7 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
/// <returns>
/// the standard output of the execution.
/// </returns>
private static JsonNode RunCommand(string fileName, string arguments)
private static SeleniumManagerResponse.ResultResponse RunCommand(string fileName, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = BinaryFullPath;
Expand Down Expand Up @@ -174,47 +178,76 @@ private static JsonNode RunCommand(string fileName, string arguments)
}

string output = outputBuilder.ToString().Trim();
JsonNode resultJsonNode;

SeleniumManagerResponse jsonResponse;

try
{
var deserializedOutput = JsonSerializer.Deserialize<Dictionary<string, JsonNode>>(output);
resultJsonNode = deserializedOutput["result"];
jsonResponse = JsonSerializer.Deserialize<SeleniumManagerResponse>(output, _serializerOptions);
}
catch (Exception ex)
{
throw new WebDriverException($"Error deserializing Selenium Manager's response: {output}", ex);
}

if (resultJsonNode["logs"] is not null)
if (jsonResponse.Logs is not null)
{
var logs = resultJsonNode["logs"];
foreach (var entry in logs.AsArray())
foreach (var entry in jsonResponse.Logs)
{
switch (entry.GetPropertyName())
switch (entry.Level)
{
case "WARN":
if (_logger.IsEnabled(LogEventLevel.Warn))
{
_logger.Warn(entry.GetValue<string>());
_logger.Warn(entry.Message);
}
break;
case "DEBUG":
if (_logger.IsEnabled(LogEventLevel.Debug))
{
_logger.Debug(entry.GetValue<string>());
_logger.Debug(entry.Message);
}
break;
case "INFO":
if (_logger.IsEnabled(LogEventLevel.Info))
{
_logger.Info(entry.GetValue<string>());
_logger.Info(entry.Message);
}
break;
}
}
}

return resultJsonNode;
return jsonResponse.Result;
}
}

internal class SeleniumManagerResponse
{
public IReadOnlyList<LogEntryResponse> Logs { get; set; }

public ResultResponse Result { get; set; }

public class LogEntryResponse
{
public string Level { get; set; }

public string Message { get; set; }
}

public class ResultResponse
{
[JsonPropertyName("driver_path")]
public string DriverPath { get; set; }

[JsonPropertyName("browser_path")]
public string BrowserPath { get; set; }
}
}

[JsonSerializable(typeof(SeleniumManagerResponse))]
internal partial class SeleniumManagerSerializerContext : JsonSerializerContext
{

}
}

0 comments on commit 40bfc5e

Please sign in to comment.