2222using System . Collections . Generic ;
2323using System . Diagnostics ;
2424using System . Globalization ;
25+ using System . IO ;
2526using System . Reflection ;
2627
2728#if ! NET45 && ! NET46 && ! NET47
@@ -38,8 +39,40 @@ namespace OpenQA.Selenium
3839 /// </summary>
3940 public static class SeleniumManager
4041 {
41- private static string basePath = System . IO . Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
42- private static string binary ;
42+ private readonly static string binaryFullPath ;
43+
44+ static SeleniumManager ( )
45+ {
46+ #if NET45
47+ var currentDirectory = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
48+ #else
49+ var currentDirectory = AppContext . BaseDirectory ;
50+ #endif
51+
52+ string binary ;
53+ #if NET45 || NET46 || NET47
54+ binary = "selenium-manager/windows/selenium-manager.exe" ;
55+ #else
56+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
57+ {
58+ binary = "selenium-manager/windows/selenium-manager.exe" ;
59+ }
60+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
61+ {
62+ binary = "selenium-manager/linux/selenium-manager" ;
63+ }
64+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
65+ {
66+ binary = "selenium-manager/macos/selenium-manager" ;
67+ }
68+ else
69+ {
70+ throw new WebDriverException ( "Selenium Manager did not find supported operating system" ) ;
71+ }
72+ #endif
73+
74+ binaryFullPath = Path . Combine ( currentDirectory , binary ) ;
75+ }
4376
4477 /// <summary>
4578 /// Determines the location of the correct driver.
@@ -50,9 +83,6 @@ public static class SeleniumManager
5083 /// </returns>
5184 public static string DriverPath ( DriverOptions options )
5285 {
53- var binaryFile = Binary ;
54- if ( binaryFile == null ) return null ;
55-
5686 StringBuilder argsBuilder = new StringBuilder ( ) ;
5787 argsBuilder . AppendFormat ( CultureInfo . InvariantCulture , " --browser \" {0}\" " , options . BrowserName ) ;
5888 argsBuilder . Append ( " --output json" ) ;
@@ -68,8 +98,7 @@ public static string DriverPath(DriverOptions options)
6898 argsBuilder . AppendFormat ( CultureInfo . InvariantCulture , " --browser-path \" {0}\" " , browserBinary ) ;
6999 }
70100
71-
72- return RunCommand ( binaryFile , argsBuilder . ToString ( ) ) ;
101+ return RunCommand ( binaryFullPath , argsBuilder . ToString ( ) ) ;
73102 }
74103
75104
@@ -96,40 +125,6 @@ private static string BrowserBinary(DriverOptions options)
96125 return null ;
97126 }
98127
99- /// <summary>
100- /// Gets the location of the correct Selenium Manager binary.
101- /// </summary>
102- private static string Binary
103- {
104- get
105- {
106- if ( string . IsNullOrEmpty ( binary ) )
107- {
108- #if NET45 || NET46 || NET47
109- binary = "selenium-manager/windows/selenium-manager.exe" ;
110- #else
111- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
112- {
113- binary = "selenium-manager/windows/selenium-manager.exe" ;
114- }
115- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
116- {
117- binary = "selenium-manager/linux/selenium-manager" ;
118- }
119- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
120- {
121- binary = "selenium-manager/macos/selenium-manager" ;
122- }
123- else
124- {
125- throw new WebDriverException ( "Selenium Manager did not find supported operating system" ) ;
126- }
127- #endif
128- }
129-
130- return binary ;
131- }
132- }
133128
134129 /// <summary>
135130 /// Executes a process with the given arguments.
@@ -142,7 +137,7 @@ private static string Binary
142137 private static string RunCommand ( string fileName , string arguments )
143138 {
144139 Process process = new Process ( ) ;
145- process . StartInfo . FileName = $ " { basePath } / { fileName } " ;
140+ process . StartInfo . FileName = binaryFullPath ;
146141 process . StartInfo . Arguments = arguments ;
147142 process . StartInfo . UseShellExecute = false ;
148143 process . StartInfo . StandardErrorEncoding = Encoding . UTF8 ;
@@ -151,7 +146,6 @@ private static string RunCommand(string fileName, string arguments)
151146 process . StartInfo . RedirectStandardError = true ;
152147
153148 StringBuilder outputBuilder = new StringBuilder ( ) ;
154- int processExitCode ;
155149
156150 DataReceivedEventHandler outputHandler = ( sender , e ) => outputBuilder . AppendLine ( e . Data ) ;
157151
@@ -164,14 +158,20 @@ private static string RunCommand(string fileName, string arguments)
164158 process . BeginOutputReadLine ( ) ;
165159
166160 process . WaitForExit ( ) ;
161+
162+ if ( process . ExitCode != 0 )
163+ {
164+ // We do not log any warnings coming from Selenium Manager like the other bindings as we don't have any logging in the .NET bindings
165+
166+ throw new WebDriverException ( $ "Selenium Manager process exited abnormally with { process . ExitCode } code: { fileName } { arguments } \n { outputBuilder } ") ;
167+ }
167168 }
168169 catch ( Exception ex )
169170 {
170171 throw new WebDriverException ( $ "Error starting process: { fileName } { arguments } ", ex ) ;
171172 }
172173 finally
173174 {
174- processExitCode = process . ExitCode ;
175175 process . OutputDataReceived -= outputHandler ;
176176 }
177177
@@ -188,13 +188,6 @@ private static string RunCommand(string fileName, string arguments)
188188 throw new WebDriverException ( $ "Error deserializing Selenium Manager's response: { output } ", ex ) ;
189189 }
190190
191- // We do not log any warnings coming from Selenium Manager like the other bindings as we don't have any logging in the .NET bindings
192-
193- if ( processExitCode != 0 )
194- {
195- throw new WebDriverException ( $ "Invalid response from process (code { processExitCode } ): { fileName } { arguments } \n { result } ") ;
196- }
197-
198191 return result ;
199192 }
200193 }
0 commit comments