22
22
using System . Collections . Generic ;
23
23
using System . Diagnostics ;
24
24
using System . Globalization ;
25
+ using System . IO ;
25
26
using System . Reflection ;
26
27
27
28
#if ! NET45 && ! NET46 && ! NET47
@@ -38,8 +39,40 @@ namespace OpenQA.Selenium
38
39
/// </summary>
39
40
public static class SeleniumManager
40
41
{
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
+ }
43
76
44
77
/// <summary>
45
78
/// Determines the location of the correct driver.
@@ -50,9 +83,6 @@ public static class SeleniumManager
50
83
/// </returns>
51
84
public static string DriverPath ( DriverOptions options )
52
85
{
53
- var binaryFile = Binary ;
54
- if ( binaryFile == null ) return null ;
55
-
56
86
StringBuilder argsBuilder = new StringBuilder ( ) ;
57
87
argsBuilder . AppendFormat ( CultureInfo . InvariantCulture , " --browser \" {0}\" " , options . BrowserName ) ;
58
88
argsBuilder . Append ( " --output json" ) ;
@@ -68,8 +98,7 @@ public static string DriverPath(DriverOptions options)
68
98
argsBuilder . AppendFormat ( CultureInfo . InvariantCulture , " --browser-path \" {0}\" " , browserBinary ) ;
69
99
}
70
100
71
-
72
- return RunCommand ( binaryFile , argsBuilder . ToString ( ) ) ;
101
+ return RunCommand ( binaryFullPath , argsBuilder . ToString ( ) ) ;
73
102
}
74
103
75
104
@@ -96,40 +125,6 @@ private static string BrowserBinary(DriverOptions options)
96
125
return null ;
97
126
}
98
127
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
- }
133
128
134
129
/// <summary>
135
130
/// Executes a process with the given arguments.
@@ -142,7 +137,7 @@ private static string Binary
142
137
private static string RunCommand ( string fileName , string arguments )
143
138
{
144
139
Process process = new Process ( ) ;
145
- process . StartInfo . FileName = $ " { basePath } / { fileName } " ;
140
+ process . StartInfo . FileName = binaryFullPath ;
146
141
process . StartInfo . Arguments = arguments ;
147
142
process . StartInfo . UseShellExecute = false ;
148
143
process . StartInfo . StandardErrorEncoding = Encoding . UTF8 ;
@@ -151,7 +146,6 @@ private static string RunCommand(string fileName, string arguments)
151
146
process . StartInfo . RedirectStandardError = true ;
152
147
153
148
StringBuilder outputBuilder = new StringBuilder ( ) ;
154
- int processExitCode ;
155
149
156
150
DataReceivedEventHandler outputHandler = ( sender , e ) => outputBuilder . AppendLine ( e . Data ) ;
157
151
@@ -164,14 +158,20 @@ private static string RunCommand(string fileName, string arguments)
164
158
process . BeginOutputReadLine ( ) ;
165
159
166
160
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
+ }
167
168
}
168
169
catch ( Exception ex )
169
170
{
170
171
throw new WebDriverException ( $ "Error starting process: { fileName } { arguments } ", ex ) ;
171
172
}
172
173
finally
173
174
{
174
- processExitCode = process . ExitCode ;
175
175
process . OutputDataReceived -= outputHandler ;
176
176
}
177
177
@@ -188,13 +188,6 @@ private static string RunCommand(string fileName, string arguments)
188
188
throw new WebDriverException ( $ "Error deserializing Selenium Manager's response: { output } ", ex ) ;
189
189
}
190
190
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
-
198
191
return result ;
199
192
}
200
193
}
0 commit comments