2828using System . Diagnostics . CodeAnalysis ;
2929using OpenQA . Selenium . Uia ;
3030using Gravity . Abstraction . Uia ;
31+ using System . Net . Http ;
32+ using System . Text ;
3133
3234namespace Gravity . Abstraction . WebDriver
3335{
@@ -94,9 +96,9 @@ public IWebDriver Create()
9496 }
9597 catch ( Exception e )
9698 {
97-
99+ Trace . TraceError ( $ " { e . GetBaseException ( ) } " ) ;
98100 throw ;
99- }
101+ }
100102 }
101103 #endregion
102104
@@ -210,7 +212,7 @@ private IWebDriver GetRemoteAndroid(string driverBinaries)
210212 [ SuppressMessage ( "CodeQuality" , "IDE0051:Remove unused private members" , Justification = "Used by reflection." ) ]
211213 private IWebDriver GetRemoteIos ( string driverBinaries )
212214 {
213- return GetMobile < AppiumOptions , AppiumOptionsParams , IOSDriver < IWebElement > > ( driverBinaries , "iOS" ) ;
215+ return GetMobile < AppiumOptions , AppiumOptionsParams , IOSDriver < IWebElement > > ( driverBinaries , string . Empty ) ;
214216 }
215217
216218 [ DriverMethod ( Driver = Driver . Safari , RemoteDriver = true ) ]
@@ -229,13 +231,65 @@ private IWebDriver GetRemoteDriver(string driverBinaries)
229231 var capabilities = GetCapabilities ( options , _capabilities ) ;
230232 options . BrowserVersion = string . IsNullOrEmpty ( options . BrowserVersion ) ? "1.0" : options . BrowserVersion ;
231233
232-
233234 // factor web driver
234235 var executor = _commandTimeout == 0
235236 ? new UiaCommandExecutor ( new Uri ( driverBinaries ) , TimeSpan . FromSeconds ( 60 ) )
236237 : new UiaCommandExecutor ( new Uri ( driverBinaries ) , TimeSpan . FromSeconds ( _commandTimeout ) ) ;
237238 return new UiaDriver ( executor , capabilities ) ;
238239 }
240+
241+ [ DriverMethod ( Driver = "RemoteWebDriver" , RemoteDriver = true ) ]
242+ [ SuppressMessage ( "CodeQuality" , "IDE0051:Remove unused private members" , Justification = "Used by reflection." ) ]
243+ private IWebDriver GetRemoteUiaDriver ( string driverBinaries )
244+ {
245+ // setup
246+ var capabilities = new CapabilitiesModel
247+ {
248+ DesiredCapabilities = _capabilities ,
249+ Capabilities = new ( )
250+ {
251+ FirstMatch = new [ ]
252+ {
253+ _capabilities
254+ }
255+ }
256+ } ;
257+
258+ // build
259+ using var client = new HttpClient ( ) ;
260+ var requestBody = JsonSerializer . Serialize ( capabilities , new JsonSerializerOptions
261+ {
262+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase
263+ } ) ;
264+ var content = new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) ;
265+ var request = new HttpRequestMessage ( HttpMethod . Post , $ "{ driverBinaries } /session")
266+ {
267+ Content = content
268+ } ;
269+
270+ // invoke
271+ var response = client . SendAsync ( request ) . Result ;
272+
273+ // internal server error
274+ if ( ! response . IsSuccessStatusCode )
275+ {
276+ throw new WebDriverException ( response . ReasonPhrase ) ;
277+ }
278+
279+ // extract id
280+ var responseContent = response . Content . ReadAsStringAsync ( ) . Result ;
281+ var responseObject = JsonSerializer . Deserialize < IDictionary < string , object > > ( responseContent ) ;
282+ var sessionId = ( ( JsonElement ) responseObject [ "value" ] ) . GetProperty ( "sessionId" ) . GetString ( ) ;
283+
284+ // setup: get information to initialize driver two
285+ var addressOfRemoteServer = new Uri ( driverBinaries ) ;
286+ var timeout = TimeSpan . FromSeconds ( 60 ) ;
287+ var commandExecutor = new LocalExecutor ( sessionId , addressOfRemoteServer , timeout ) ;
288+ var localCapabilities = new Dictionary < string , object > ( _capabilities ) ;
289+
290+ // get
291+ return new RemoteWebDriver ( commandExecutor , new DesiredCapabilities ( localCapabilities ) ) ;
292+ }
239293 #endregion
240294
241295 // TODO: add default constructor options when there are no binaries to load
@@ -291,14 +345,14 @@ private DriverOptions GetOptions<TOptions, TParams>(string platformName)
291345 where TParams : DriverOptionsParams , IOptionable < TOptions >
292346 {
293347 // parse token
294- var isOptions = this . _options . Keys . Count > 0 ;
348+ var isOptions = _options . Keys . Count > 0 ;
295349
296350 // null validation
297351 if ( ! isOptions )
298352 {
299353 return new TOptions
300354 {
301- PlatformName = platformName
355+ PlatformName = string . IsNullOrEmpty ( platformName ) ? null : platformName
302356 } ;
303357 }
304358
@@ -309,7 +363,7 @@ private DriverOptions GetOptions<TOptions, TParams>(string platformName)
309363 var options = paramsObj . ToDriverOptions ( ) ;
310364 if ( ! string . IsNullOrEmpty ( platformName ) )
311365 {
312- options . PlatformName = platformName ;
366+ options . PlatformName = string . IsNullOrEmpty ( platformName ) ? null : platformName ;
313367 }
314368 return options ;
315369 }
0 commit comments