2020using System ;
2121using System . Collections . Generic ;
2222using System . Diagnostics . CodeAnalysis ;
23- using System . Globalization ;
2423using System . IO ;
25- using System . Text ;
24+ using OpenQA . Selenium . Manager ;
2625
2726namespace OpenQA . Selenium ;
2827
@@ -32,6 +31,9 @@ namespace OpenQA.Selenium;
3231/// </summary>
3332public 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}
0 commit comments