Skip to content

Commit c584c4a

Browse files
authored
[dotnet] Stream Selenium Manager output to internal logging (#17024)
1 parent 7278252 commit c584c4a

7 files changed

Lines changed: 495 additions & 380 deletions

File tree

dotnet/src/webdriver/DriverFinder.cs

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
using System;
2121
using System.Collections.Generic;
2222
using System.Diagnostics.CodeAnalysis;
23-
using System.Globalization;
2423
using System.IO;
25-
using System.Text;
24+
using OpenQA.Selenium.Manager;
2625

2726
namespace OpenQA.Selenium;
2827

@@ -32,6 +31,9 @@ namespace OpenQA.Selenium;
3231
/// </summary>
3332
public class DriverFinder
3433
{
34+
private const string DriverPathKey = "driver_path";
35+
private const string BrowserPathKey = "browser_path";
36+
3537
private readonly DriverOptions options;
3638
private readonly Dictionary<string, string> paths = new Dictionary<string, string>();
3739

@@ -52,7 +54,7 @@ public DriverFinder(DriverOptions options)
5254
/// </returns>
5355
public string GetBrowserPath()
5456
{
55-
return BinaryPaths()[SeleniumManager.BrowserPathKey];
57+
return BinaryPaths()[BrowserPathKey];
5658
}
5759

5860
/// <summary>
@@ -63,7 +65,7 @@ public string GetBrowserPath()
6365
/// </returns>
6466
public string GetDriverPath()
6567
{
66-
return BinaryPaths()[SeleniumManager.DriverPathKey];
68+
return BinaryPaths()[DriverPathKey];
6769
}
6870

6971
/// <summary>
@@ -102,18 +104,29 @@ public bool TryGetBrowserPath([NotNullWhen(true)] out string? browserPath)
102104
/// <exception cref="NoSuchDriverException">If one of the paths does not exist.</exception>
103105
private Dictionary<string, string> BinaryPaths()
104106
{
105-
if (paths.ContainsKey(SeleniumManager.DriverPathKey) && !string.IsNullOrWhiteSpace(paths[SeleniumManager.DriverPathKey]))
107+
if (paths.TryGetValue(DriverPathKey, out string? cachedDriverPath) && !string.IsNullOrWhiteSpace(cachedDriverPath))
106108
{
107109
return paths;
108110
}
109111

110-
Dictionary<string, string> binaryPaths = SeleniumManager.BinaryPaths(CreateArguments());
111-
string driverPath = binaryPaths[SeleniumManager.DriverPathKey];
112-
string browserPath = binaryPaths[SeleniumManager.BrowserPathKey];
112+
if (string.IsNullOrWhiteSpace(options.BrowserName))
113+
{
114+
throw new NoSuchDriverException("Browser name must be specified to find the driver using Selenium Manager.");
115+
}
116+
117+
BrowserDiscoveryResult smResult = SeleniumManager.DiscoverBrowser(options.BrowserName, new BrowserDiscoveryOptions
118+
{
119+
BrowserVersion = options.BrowserVersion,
120+
BrowserPath = options.BinaryLocation,
121+
Proxy = options.Proxy?.SslProxy ?? options.Proxy?.HttpProxy
122+
});
123+
124+
string driverPath = smResult.DriverPath;
125+
string browserPath = smResult.BrowserPath;
113126

114127
if (File.Exists(driverPath))
115128
{
116-
paths.Add(SeleniumManager.DriverPathKey, driverPath);
129+
paths.Add(DriverPathKey, driverPath);
117130
}
118131
else
119132
{
@@ -122,7 +135,7 @@ private Dictionary<string, string> BinaryPaths()
122135

123136
if (File.Exists(browserPath))
124137
{
125-
paths.Add(SeleniumManager.BrowserPathKey, browserPath);
138+
paths.Add(BrowserPathKey, browserPath);
126139
}
127140
else
128141
{
@@ -131,43 +144,4 @@ private Dictionary<string, string> BinaryPaths()
131144

132145
return paths;
133146
}
134-
135-
/// <summary>
136-
/// Create arguments to invoke Selenium Manager
137-
/// </summary>
138-
/// <returns>
139-
/// A string with all arguments to invoke Selenium Manager
140-
/// </returns>
141-
/// <exception cref="NoSuchDriverException"></exception>
142-
private string CreateArguments()
143-
{
144-
StringBuilder argsBuilder = new StringBuilder();
145-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser \"{0}\"", options.BrowserName);
146-
147-
if (!string.IsNullOrEmpty(options.BrowserVersion))
148-
{
149-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-version {0}", options.BrowserVersion);
150-
}
151-
152-
string? browserBinary = options.BinaryLocation;
153-
if (!string.IsNullOrEmpty(browserBinary))
154-
{
155-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary);
156-
}
157-
158-
if (options.Proxy != null)
159-
{
160-
if (options.Proxy.SslProxy != null)
161-
{
162-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.SslProxy);
163-
}
164-
else if (options.Proxy.HttpProxy != null)
165-
{
166-
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.HttpProxy);
167-
}
168-
}
169-
170-
return argsBuilder.ToString();
171-
}
172-
173147
}

dotnet/src/webdriver/Internal/Logging/ILogContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ public interface ILogContext : IDisposable
6565
/// Emits a log message using the specified logger, log level, and message.
6666
/// </summary>
6767
/// <param name="logger">The logger to emit the log message.</param>
68+
/// <param name="timestamp">The timestamp of the log event.</param>
6869
/// <param name="level">The log level of the message.</param>
6970
/// <param name="message">The log message.</param>
70-
internal void EmitMessage(ILogger logger, LogEventLevel level, string message);
71+
internal void EmitMessage(ILogger logger, DateTimeOffset timestamp, LogEventLevel level, string message);
7172

7273
/// <summary>
7374
/// Sets the minimum log level for the current context.

dotnet/src/webdriver/Internal/Logging/ILogger.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ internal interface ILogger
5656
/// <param name="message">The log message.</param>
5757
void Error(string message);
5858

59+
/// <summary>
60+
/// Writes a log message with a specific timestamp and log level.
61+
/// </summary>
62+
/// <param name="timestamp">The timestamp of the log event.</param>
63+
/// <param name="level">The severity level of the log message.</param>
64+
/// <param name="message">The log message.</param>
65+
void LogMessage(DateTimeOffset timestamp, LogEventLevel level, string message);
66+
5967
/// <summary>
6068
/// Gets or sets the log event level.
6169
/// </summary>

dotnet/src/webdriver/Internal/Logging/LogContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public bool IsEnabled(ILogger logger, LogEventLevel level)
9797
return Handlers != null && level >= _level && (_loggers?.TryGetValue(logger.Issuer, out var loggerEntry) != true || level >= loggerEntry?.Level);
9898
}
9999

100-
public void EmitMessage(ILogger logger, LogEventLevel level, string message)
100+
public void EmitMessage(ILogger logger, DateTimeOffset timestamp, LogEventLevel level, string message)
101101
{
102102
if (IsEnabled(logger, level))
103103
{
104-
message = TruncateMessage(message, _truncationLength);
104+
string truncatedMessage = TruncateMessage(message, _truncationLength);
105105

106-
var logEvent = new LogEvent(logger.Issuer, DateTimeOffset.Now, level, message);
106+
var logEvent = new LogEvent(logger.Issuer, timestamp, level, truncatedMessage);
107107

108108
foreach (var handler in Handlers)
109109
{

dotnet/src/webdriver/Internal/Logging/Logger.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ public bool IsEnabled(LogEventLevel level)
6767
return Log.CurrentContext.IsEnabled(this, level);
6868
}
6969

70+
public void LogMessage(DateTimeOffset timestamp, LogEventLevel level, string message)
71+
{
72+
Log.CurrentContext.EmitMessage(this, timestamp.ToLocalTime(), level, message);
73+
}
74+
7075
private void LogMessage(LogEventLevel level, string message)
7176
{
72-
Log.CurrentContext.EmitMessage(this, level, message);
77+
LogMessage(DateTimeOffset.Now, level, message);
7378
}
7479
}

0 commit comments

Comments
 (0)