18
18
using System . Collections . Generic ;
19
19
using System . Diagnostics ;
20
20
using System . IO ;
21
+ using System . Linq ;
21
22
using System . Net ;
22
23
using System . Runtime . CompilerServices ;
23
24
@@ -32,6 +33,7 @@ public class AppiumLocalService : ICommandServer
32
33
private readonly TimeSpan InitializationTimeout ;
33
34
private readonly IDictionary < string , string > EnvironmentForProcess ;
34
35
private Process Service ;
36
+ private List < string > ArgsList ;
35
37
36
38
/// <summary>
37
39
/// Creates an instance of AppiumLocalService without special settings
@@ -40,11 +42,11 @@ public class AppiumLocalService : ICommandServer
40
42
public static AppiumLocalService BuildDefaultService ( ) => new AppiumServiceBuilder ( ) . Build ( ) ;
41
43
42
44
internal AppiumLocalService (
43
- FileInfo nodeJS ,
44
- string arguments ,
45
- IPAddress ip ,
45
+ FileInfo nodeJS ,
46
+ string arguments ,
47
+ IPAddress ip ,
46
48
int port ,
47
- TimeSpan initializationTimeout ,
49
+ TimeSpan initializationTimeout ,
48
50
IDictionary < string , string > environmentForProcess )
49
51
{
50
52
NodeJS = nodeJS ;
@@ -58,7 +60,7 @@ internal AppiumLocalService(
58
60
/// <summary>
59
61
/// The base URL for the managed appium server.
60
62
/// </summary>
61
- public Uri ServiceUrl => new Uri ( $ "http://{ IP . ToString ( ) } :{ Convert . ToString ( Port ) } /wd/hub ") ;
63
+ public Uri ServiceUrl => new Uri ( $ "http://{ IP } :{ Convert . ToString ( Port ) } ") ;
62
64
63
65
/// <summary>
64
66
/// Event that can be used to capture the output of the service
@@ -131,7 +133,7 @@ private void DestroyProcess()
131
133
{
132
134
Service . Kill ( ) ;
133
135
}
134
- catch ( Exception ignored )
136
+ catch
135
137
{
136
138
}
137
139
finally
@@ -175,26 +177,80 @@ public bool IsRunning
175
177
}
176
178
}
177
179
178
- private bool Ping ( TimeSpan span )
180
+ private string GetArgsValue ( string argStr )
179
181
{
180
- bool pinged = false ;
182
+ int idx ;
183
+ idx = ArgsList . IndexOf ( argStr ) ;
184
+ return ArgsList [ idx + 1 ] ;
185
+ }
181
186
182
- Uri status ;
187
+ private string ParseBasePath ( )
188
+ {
189
+ if ( ArgsList . Contains ( "--base-path" ) )
190
+ {
191
+ return GetArgsValue ( "--base-path" ) ;
192
+ }
193
+ else if ( ArgsList . Contains ( "-pa" ) )
194
+ {
195
+ return GetArgsValue ( "-pa" ) ;
196
+ }
197
+ return AppiumServiceConstants . DefaultBasePath ;
198
+ }
183
199
200
+ private void GenerateArgsList ( )
201
+ {
202
+ ArgsList = Arguments . Split ( ' ' ) . ToList ( ) ;
203
+ }
204
+ private Uri CreateStatusUrl ( )
205
+ {
206
+ Uri status ;
184
207
Uri service = ServiceUrl ;
208
+
209
+ if ( ArgsList == null )
210
+ {
211
+ GenerateArgsList ( ) ;
212
+ }
213
+ string basePath = ParseBasePath ( ) ;
214
+ bool defBasePath = basePath . Equals ( AppiumServiceConstants . DefaultBasePath ) ;
215
+
185
216
if ( service . IsLoopback || IP . ToString ( ) . Equals ( AppiumServiceConstants . DefaultLocalIPAddress ) )
186
217
{
187
- status = new Uri ( "http://localhost:" + Convert . ToString ( Port ) + "/wd/hub/status" ) ;
218
+ var tmpStatus = "http://localhost:" + Convert . ToString ( Port ) ;
219
+ if ( defBasePath )
220
+ {
221
+ status = new Uri ( tmpStatus + AppiumServiceConstants . StatusUrl ) ;
222
+ }
223
+ else
224
+ {
225
+ status = new Uri ( tmpStatus + basePath + AppiumServiceConstants . StatusUrl ) ;
226
+ }
188
227
}
189
228
else
190
229
{
191
- status = new Uri ( service . ToString ( ) + "/status" ) ;
230
+ if ( defBasePath )
231
+ {
232
+ status = new Uri ( service , AppiumServiceConstants . StatusUrl ) ;
233
+ }
234
+ else
235
+ {
236
+ status = new Uri ( service , basePath + AppiumServiceConstants . StatusUrl ) ;
237
+ }
192
238
}
239
+ return status ;
240
+ }
241
+
242
+ private bool Ping ( TimeSpan span )
243
+ {
244
+ bool pinged = false ;
245
+
246
+ Uri status ;
247
+
248
+ status = CreateStatusUrl ( ) ;
193
249
194
- DateTime endTime = DateTime . Now . Add ( this . InitializationTimeout ) ;
250
+ DateTime endTime = DateTime . Now . Add ( span ) ;
195
251
while ( ! pinged & DateTime . Now < endTime )
196
252
{
197
- HttpWebRequest request = ( HttpWebRequest ) HttpWebRequest . Create ( status ) ;
253
+ HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( status ) ;
198
254
HttpWebResponse response = null ;
199
255
try
200
256
{
@@ -203,7 +259,7 @@ private bool Ping(TimeSpan span)
203
259
pinged = true ;
204
260
}
205
261
}
206
- catch ( Exception e )
262
+ catch
207
263
{
208
264
pinged = false ;
209
265
}
0 commit comments