Skip to content
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

[dotnet] Json serializer gen context for SM output #14481

Merged
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
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
{

}
}
Loading