11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4- using Newtonsoft . Json ;
4+ using System . Text . Json ;
55
66namespace Appium . Net . Integration . Tests . helpers
77{
@@ -10,32 +10,45 @@ public class Env
1010 public static TimeSpan InitTimeoutSec = TimeSpan . FromSeconds ( 180 ) ;
1111 public static TimeSpan ImplicitTimeoutSec = TimeSpan . FromSeconds ( 10 ) ;
1212
13- private static Dictionary < string , string > _env ;
13+ private static Dictionary < string , JsonElement > _env ;
1414 private static bool _initialized ;
1515
1616 private static void Init ( )
1717 {
18+ _env = new Dictionary < string , JsonElement >
19+ {
20+ { "DEV" , JsonDocument . Parse ( "true" ) . RootElement } ,
21+ { "isRemoteAppiumServer" , JsonDocument . Parse ( "false" ) . RootElement } ,
22+ { "remoteAppiumServerUri" , JsonDocument . Parse ( "\" http://localhost:4723\" " ) . RootElement }
23+ } ;
24+
25+ if ( _initialized ) return ;
26+
1827 try
1928 {
20- if ( ! _initialized )
29+ _initialized = true ;
30+ var path = AppDomain . CurrentDomain . BaseDirectory ;
31+ var sr = new StreamReader ( path + "env.json" ) ;
32+ var jsonString = sr . ReadToEnd ( ) ;
33+ _env = JsonSerializer . Deserialize < Dictionary < string , JsonElement > > ( jsonString , new JsonSerializerOptions
2134 {
22- _initialized = true ;
23- var path = AppDomain . CurrentDomain . BaseDirectory ;
24- var sr = new StreamReader ( path + "env.json" ) ;
25- var jsonString = sr . ReadToEnd ( ) ;
26- _env = JsonConvert . DeserializeObject < Dictionary < string , string > > ( jsonString ) ;
27- }
35+ PropertyNameCaseInsensitive = true
36+ } ) ;
2837 }
29- catch
38+ catch ( JsonException jsonEx )
3039 {
31- _env = new Dictionary < string , string > ( ) ;
40+ Console . WriteLine ( $ "Error parsing JSON: { jsonEx . Message } ") ;
41+ }
42+ catch ( Exception ex )
43+ {
44+ Console . WriteLine ( $ "Error initializing environment: { ex . Message } ") ;
3245 }
3346 }
3447
35- private static bool IsTrue ( string val )
48+ private static bool IsTrue ( object val )
3649 {
37- val = val ? . ToLower ( ) . Trim ( ) ;
38- return ( val == "true" ) || ( val == "1" ) ;
50+ val = val ? . ToString ( ) . ToLower ( ) . Trim ( ) ;
51+ return val . Equals ( "true" ) || val . Equals ( "1" ) ;
3952 }
4053
4154 public static bool ServerIsRemote ( )
@@ -47,16 +60,25 @@ public static bool ServerIsRemote()
4760 public static bool ServerIsLocal ( )
4861 {
4962 Init ( ) ;
50- return _env . ContainsKey ( "DEV" ) && IsTrue ( _env [ "DEV" ] ) || IsTrue ( Environment . GetEnvironmentVariable ( "DEV" ) ) ;
63+ return ( _env . ContainsKey ( "DEV" ) && IsTrue ( _env [ "DEV" ] ) ) || IsTrue ( Environment . GetEnvironmentVariable ( "DEV" ) ) ;
5164 }
5265
5366 public static string GetEnvVar ( string name )
5467 {
55- if ( _env . ContainsKey ( name ) && ( _env [ name ] != null ) )
68+ if ( _env . ContainsKey ( name ) )
5669 {
57- return _env [ name ] ;
70+ JsonElement element = _env [ name ] ;
71+
72+ return element . ValueKind switch
73+ {
74+ JsonValueKind . String => element . GetString ( ) ,
75+ JsonValueKind . Number => element . GetRawText ( ) ,
76+ JsonValueKind . True or JsonValueKind . False => element . GetRawText ( ) ,
77+ JsonValueKind . Null => null ,
78+ _ => element . GetRawText ( )
79+ } ;
5880 }
59- return Environment . GetEnvironmentVariable ( name ) ;
81+ return Environment . GetEnvironmentVariable ( name ) ;
6082 }
6183 }
6284}
0 commit comments