1818using  System . Collections . Generic ; 
1919using  System . Diagnostics ; 
2020using  System . IO ; 
21+ using  System . Linq ; 
2122using  System . Net ; 
2223using  System . Runtime . CompilerServices ; 
2324
@@ -32,6 +33,7 @@ public class AppiumLocalService : ICommandServer
3233        private  readonly  TimeSpan  InitializationTimeout ; 
3334        private  readonly  IDictionary < string ,  string >  EnvironmentForProcess ; 
3435        private  Process  Service ; 
36+         private  List < string >  ArgsList ; 
3537
3638        /// <summary> 
3739        /// Creates an instance of AppiumLocalService without special settings 
@@ -40,11 +42,11 @@ public class AppiumLocalService : ICommandServer
4042        public  static AppiumLocalService  BuildDefaultService ( )  =>  new  AppiumServiceBuilder ( ) . Build ( ) ; 
4143
4244        internal  AppiumLocalService ( 
43-             FileInfo  nodeJS ,   
44-             string  arguments ,   
45-             IPAddress  ip ,   
45+             FileInfo  nodeJS , 
46+             string  arguments , 
47+             IPAddress  ip , 
4648            int  port , 
47-             TimeSpan  initializationTimeout ,   
49+             TimeSpan  initializationTimeout , 
4850            IDictionary < string ,  string >  environmentForProcess ) 
4951        { 
5052            NodeJS  =  nodeJS ; 
@@ -58,7 +60,7 @@ internal AppiumLocalService(
5860        /// <summary> 
5961        /// The base URL for the managed appium server. 
6062        /// </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 ) } ") ; 
6264
6365        /// <summary> 
6466        /// Event that can be used to capture the output of the service 
@@ -131,7 +133,7 @@ private void DestroyProcess()
131133            { 
132134                Service . Kill ( ) ; 
133135            } 
134-             catch   ( Exception   ignored ) 
136+             catch 
135137            { 
136138            } 
137139            finally 
@@ -175,26 +177,80 @@ public bool IsRunning
175177            } 
176178        } 
177179
178-         private  bool   Ping ( TimeSpan   span ) 
180+         private  string   GetArgsValue ( string   argStr ) 
179181        { 
180-             bool  pinged  =  false ; 
182+             int  idx ; 
183+             idx =  ArgsList . IndexOf ( argStr ) ; 
184+             return  ArgsList [ idx  +  1 ] ; 
185+         } 
181186
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+         } 
183199
200+         private  void  GenerateArgsList ( ) 
201+         { 
202+            ArgsList  =  Arguments . Split ( ' ' ) . ToList ( ) ; 
203+         } 
204+         private  Uri  CreateStatusUrl ( ) 
205+         { 
206+             Uri  status ; 
184207            Uri  service  =  ServiceUrl ; 
208+ 
209+             if  ( ArgsList  ==  null ) 
210+             { 
211+                 GenerateArgsList ( ) ; 
212+             } 
213+             string  basePath  =  ParseBasePath ( ) ; 
214+             bool  defBasePath  =  basePath . Equals ( AppiumServiceConstants . DefaultBasePath ) ; 
215+ 
185216            if  ( service . IsLoopback  ||  IP . ToString ( ) . Equals ( AppiumServiceConstants . DefaultLocalIPAddress ) ) 
186217            { 
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+                 } 
188227            } 
189228            else 
190229            { 
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+                 } 
192238            } 
239+             return  status ; 
240+         } 
241+ 
242+         private  bool  Ping ( TimeSpan  span ) 
243+         { 
244+             bool  pinged  =  false ; 
245+ 
246+             Uri  status ; 
247+ 
248+             status  =  CreateStatusUrl ( ) ; 
193249
194-             DateTime  endTime  =  DateTime . Now . Add ( this . InitializationTimeout ) ; 
250+             DateTime  endTime  =  DateTime . Now . Add ( span ) ; 
195251            while  ( ! pinged  &  DateTime . Now  <  endTime ) 
196252            { 
197-                 HttpWebRequest  request  =  ( HttpWebRequest )   HttpWebRequest . Create ( status ) ; 
253+                 HttpWebRequest  request  =  ( HttpWebRequest ) WebRequest . Create ( status ) ; 
198254                HttpWebResponse  response  =  null ; 
199255                try 
200256                { 
@@ -203,7 +259,7 @@ private bool Ping(TimeSpan span)
203259                        pinged  =  true ; 
204260                    } 
205261                } 
206-                 catch   ( Exception   e ) 
262+                 catch 
207263                { 
208264                    pinged  =  false ; 
209265                } 
0 commit comments