1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
- using Newtonsoft . Json ;
4
+ using System . Text . Json ;
5
5
6
6
namespace Appium . Net . Integration . Tests . helpers
7
7
{
@@ -10,32 +10,45 @@ public class Env
10
10
public static TimeSpan InitTimeoutSec = TimeSpan . FromSeconds ( 180 ) ;
11
11
public static TimeSpan ImplicitTimeoutSec = TimeSpan . FromSeconds ( 10 ) ;
12
12
13
- private static Dictionary < string , string > _env ;
13
+ private static Dictionary < string , JsonElement > _env ;
14
14
private static bool _initialized ;
15
15
16
16
private static void Init ( )
17
17
{
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
+
18
27
try
19
28
{
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
21
34
{
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
+ } ) ;
28
37
}
29
- catch
38
+ catch ( JsonException jsonEx )
30
39
{
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 } ") ;
32
45
}
33
46
}
34
47
35
- private static bool IsTrue ( string val )
48
+ private static bool IsTrue ( object val )
36
49
{
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" ) ;
39
52
}
40
53
41
54
public static bool ServerIsRemote ( )
@@ -47,16 +60,25 @@ public static bool ServerIsRemote()
47
60
public static bool ServerIsLocal ( )
48
61
{
49
62
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" ) ) ;
51
64
}
52
65
53
66
public static string GetEnvVar ( string name )
54
67
{
55
- if ( _env . ContainsKey ( name ) && ( _env [ name ] != null ) )
68
+ if ( _env . ContainsKey ( name ) )
56
69
{
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
+ } ;
58
80
}
59
- return Environment . GetEnvironmentVariable ( name ) ;
81
+ return Environment . GetEnvironmentVariable ( name ) ;
60
82
}
61
83
}
62
84
}
0 commit comments