@@ -50,10 +50,20 @@ public Response()
5050 /// <param name="sessionId">Session ID in use</param>
5151 public Response ( SessionId sessionId )
5252 {
53- if ( sessionId != null )
54- {
55- this . SessionId = sessionId . ToString ( ) ;
56- }
53+ this . SessionId = sessionId ? . ToString ( ) ;
54+ }
55+
56+ /// <summary>
57+ /// Initializes a new instance of the <see cref="Response"/> class
58+ /// </summary>
59+ /// <param name="sessionId">The Session ID in use, if any.</param>
60+ /// <param name="value">The JSON payload of the response.</param>
61+ /// <param name="status">The WebDriver result status of the response.</param>
62+ public Response ( string sessionId , object value , WebDriverResult status )
63+ {
64+ this . SessionId = sessionId ;
65+ this . Value = value ;
66+ this . Status = status ;
5767 }
5868
5969 /// <summary>
@@ -66,59 +76,57 @@ public static Response FromJson(string value)
6676 Dictionary < string , object > rawResponse = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions )
6777 ?? throw new WebDriverException ( "JSON success response returned \" null\" value" ) ;
6878
69- var response = new Response ( ) ;
79+ object contents ;
80+ string sessionId = null ;
7081
71- if ( rawResponse . ContainsKey ( "sessionId" ) )
82+ if ( rawResponse . TryGetValue ( "sessionId" , out var s ) && s is not null )
7283 {
73- if ( rawResponse [ "sessionId" ] != null )
74- {
75- response . SessionId = rawResponse [ "sessionId" ] . ToString ( ) ;
76- }
84+ sessionId = s . ToString ( ) ;
7785 }
7886
7987 if ( rawResponse . TryGetValue ( "value" , out object valueObj ) )
8088 {
81- response . Value = valueObj ;
89+ contents = valueObj ;
8290 }
83-
84- // If the returned object does *not* have a "value" property
85- // the response value should be the entirety of the response.
86- // TODO: Remove this if statement altogether; there should
87- // never be a spec-compliant response that does not contain a
88- // value property.
89- if ( ! rawResponse . ContainsKey ( "value" ) && response . Value == null )
91+ else
9092 {
93+ // If the returned object does *not* have a "value" property
94+ // the response value should be the entirety of the response.
95+ // TODO: Remove this if statement altogether; there should
96+ // never be a spec-compliant response that does not contain a
97+ // value property.
98+
9199 // Special-case for the new session command, where the "capabilities"
92100 // property of the response is the actual value we're interested in.
93- if ( rawResponse . ContainsKey ( "capabilities" ) )
101+ if ( rawResponse . TryGetValue ( "capabilities" , out var capabilities ) )
94102 {
95- response . Value = rawResponse [ " capabilities" ] ;
103+ contents = capabilities ;
96104 }
97105 else
98106 {
99- response . Value = rawResponse ;
107+ contents = rawResponse ;
100108 }
101109 }
102110
103- if ( response . Value is Dictionary < string , object > valueDictionary )
111+ if ( contents is Dictionary < string , object > valueDictionary )
104112 {
105113 // Special case code for the new session command. If the response contains
106114 // sessionId and capabilities properties, fix up the session ID and value members.
107- if ( valueDictionary . ContainsKey ( "sessionId" ) )
115+ if ( valueDictionary . TryGetValue ( "sessionId" , out var session ) )
108116 {
109- response . SessionId = valueDictionary [ "sessionId" ] . ToString ( ) ;
117+ sessionId = session . ToString ( ) ;
110118 if ( valueDictionary . TryGetValue ( "capabilities" , out object capabilities ) )
111119 {
112- response . Value = capabilities ;
120+ contents = capabilities ;
113121 }
114122 else
115123 {
116- response . Value = valueDictionary [ "value" ] ;
124+ contents = valueDictionary [ "value" ] ;
117125 }
118126 }
119127 }
120128
121- return response ;
129+ return new Response ( sessionId , contents , WebDriverResult . Success ) ;
122130 }
123131
124132 /// <summary>
@@ -147,8 +155,6 @@ public static Response FromErrorJson(string value)
147155 var deserializedResponse = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions )
148156 ?? throw new WebDriverException ( "JSON error response returned \" null\" value" ) ;
149157
150- var response = new Response ( ) ;
151-
152158 if ( ! deserializedResponse . TryGetValue ( "value" , out var valueObject ) )
153159 {
154160 throw new WebDriverException ( $ "The 'value' property was not found in the response:{ Environment . NewLine } { value } ") ;
@@ -159,8 +165,6 @@ public static Response FromErrorJson(string value)
159165 throw new WebDriverException ( $ "The 'value' property is not a dictionary of <string, object>{ Environment . NewLine } { value } ") ;
160166 }
161167
162- response . Value = valueDictionary ;
163-
164168 if ( ! valueDictionary . TryGetValue ( "error" , out var errorObject ) )
165169 {
166170 throw new WebDriverException ( $ "The 'value > error' property was not found in the response:{ Environment . NewLine } { value } ") ;
@@ -171,11 +175,9 @@ public static Response FromErrorJson(string value)
171175 throw new WebDriverException ( $ "The 'value > error' property is not a string{ Environment . NewLine } { value } ") ;
172176 }
173177
174- response . Value = deserializedResponse [ "value" ] ;
175-
176- response . Status = WebDriverError . ResultFromError ( errorString ) ;
178+ WebDriverResult status = WebDriverError . ResultFromError ( errorString ) ;
177179
178- return response ;
180+ return new Response ( sessionId : null , valueDictionary , status ) ;
179181 }
180182
181183 /// <summary>
0 commit comments