2424using System . Runtime . InteropServices ;
2525using System . Text ;
2626using System . Text . Json ;
27- using System . Text . Json . Nodes ;
27+ using System . Text . Json . Serialization ;
2828
2929namespace OpenQA . Selenium
3030{
@@ -38,6 +38,8 @@ public static class SeleniumManager
3838
3939 private static readonly string BinaryFullPath = Environment . GetEnvironmentVariable ( "SE_MANAGER_PATH" ) ;
4040
41+ private static readonly JsonSerializerOptions _serializerOptions = new ( ) { PropertyNameCaseInsensitive = true , TypeInfoResolver = SeleniumManagerSerializerContext . Default } ;
42+
4143 static SeleniumManager ( )
4244 {
4345
@@ -86,10 +88,12 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
8688 argsBuilder . Append ( " --debug" ) ;
8789 }
8890
89- var output = RunCommand ( BinaryFullPath , argsBuilder . ToString ( ) ) ;
90- Dictionary < string , string > binaryPaths = new Dictionary < string , string > ( ) ;
91- binaryPaths . Add ( "browser_path" , ( string ) output [ "browser_path" ] ) ;
92- binaryPaths . Add ( "driver_path" , ( string ) output [ "driver_path" ] ) ;
91+ var smCommandResult = RunCommand ( BinaryFullPath , argsBuilder . ToString ( ) ) ;
92+ Dictionary < string , string > binaryPaths = new ( )
93+ {
94+ { "browser_path" , smCommandResult . BrowserPath } ,
95+ { "driver_path" , smCommandResult . DriverPath }
96+ } ;
9397
9498 if ( _logger . IsEnabled ( LogEventLevel . Trace ) )
9599 {
@@ -108,7 +112,7 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
108112 /// <returns>
109113 /// the standard output of the execution.
110114 /// </returns>
111- private static JsonNode RunCommand ( string fileName , string arguments )
115+ private static SeleniumManagerResponse . ResultResponse RunCommand ( string fileName , string arguments )
112116 {
113117 Process process = new Process ( ) ;
114118 process . StartInfo . FileName = BinaryFullPath ;
@@ -174,47 +178,76 @@ private static JsonNode RunCommand(string fileName, string arguments)
174178 }
175179
176180 string output = outputBuilder . ToString ( ) . Trim ( ) ;
177- JsonNode resultJsonNode ;
181+
182+ SeleniumManagerResponse jsonResponse ;
183+
178184 try
179185 {
180- var deserializedOutput = JsonSerializer . Deserialize < Dictionary < string , JsonNode > > ( output ) ;
181- resultJsonNode = deserializedOutput [ "result" ] ;
186+ jsonResponse = JsonSerializer . Deserialize < SeleniumManagerResponse > ( output , _serializerOptions ) ;
182187 }
183188 catch ( Exception ex )
184189 {
185190 throw new WebDriverException ( $ "Error deserializing Selenium Manager's response: { output } ", ex ) ;
186191 }
187192
188- if ( resultJsonNode [ "logs" ] is not null )
193+ if ( jsonResponse . Logs is not null )
189194 {
190- var logs = resultJsonNode [ "logs" ] ;
191- foreach ( var entry in logs . AsArray ( ) )
195+ foreach ( var entry in jsonResponse . Logs )
192196 {
193- switch ( entry . GetPropertyName ( ) )
197+ switch ( entry . Level )
194198 {
195199 case "WARN" :
196200 if ( _logger . IsEnabled ( LogEventLevel . Warn ) )
197201 {
198- _logger . Warn ( entry . GetValue < string > ( ) ) ;
202+ _logger . Warn ( entry . Message ) ;
199203 }
200204 break ;
201205 case "DEBUG" :
202206 if ( _logger . IsEnabled ( LogEventLevel . Debug ) )
203207 {
204- _logger . Debug ( entry . GetValue < string > ( ) ) ;
208+ _logger . Debug ( entry . Message ) ;
205209 }
206210 break ;
207211 case "INFO" :
208212 if ( _logger . IsEnabled ( LogEventLevel . Info ) )
209213 {
210- _logger . Info ( entry . GetValue < string > ( ) ) ;
214+ _logger . Info ( entry . Message ) ;
211215 }
212216 break ;
213217 }
214218 }
215219 }
216220
217- return resultJsonNode ;
221+ return jsonResponse . Result ;
222+ }
223+ }
224+
225+ internal class SeleniumManagerResponse
226+ {
227+ public IReadOnlyList < LogEntryResponse > Logs { get ; set ; }
228+
229+ public ResultResponse Result { get ; set ; }
230+
231+ public class LogEntryResponse
232+ {
233+ public string Level { get ; set ; }
234+
235+ public string Message { get ; set ; }
236+ }
237+
238+ public class ResultResponse
239+ {
240+ [ JsonPropertyName ( "driver_path" ) ]
241+ public string DriverPath { get ; set ; }
242+
243+ [ JsonPropertyName ( "browser_path" ) ]
244+ public string BrowserPath { get ; set ; }
218245 }
219246 }
247+
248+ [ JsonSerializable ( typeof ( SeleniumManagerResponse ) ) ]
249+ internal partial class SeleniumManagerSerializerContext : JsonSerializerContext
250+ {
251+
252+ }
220253}
0 commit comments