28
28
using System . Diagnostics . CodeAnalysis ;
29
29
using OpenQA . Selenium . Uia ;
30
30
using Gravity . Abstraction . Uia ;
31
+ using System . Net . Http ;
32
+ using System . Text ;
31
33
32
34
namespace Gravity . Abstraction . WebDriver
33
35
{
@@ -94,9 +96,9 @@ public IWebDriver Create()
94
96
}
95
97
catch ( Exception e )
96
98
{
97
-
99
+ Trace . TraceError ( $ " { e . GetBaseException ( ) } " ) ;
98
100
throw ;
99
- }
101
+ }
100
102
}
101
103
#endregion
102
104
@@ -210,7 +212,7 @@ private IWebDriver GetRemoteAndroid(string driverBinaries)
210
212
[ SuppressMessage ( "CodeQuality" , "IDE0051:Remove unused private members" , Justification = "Used by reflection." ) ]
211
213
private IWebDriver GetRemoteIos ( string driverBinaries )
212
214
{
213
- return GetMobile < AppiumOptions , AppiumOptionsParams , IOSDriver < IWebElement > > ( driverBinaries , "iOS" ) ;
215
+ return GetMobile < AppiumOptions , AppiumOptionsParams , IOSDriver < IWebElement > > ( driverBinaries , string . Empty ) ;
214
216
}
215
217
216
218
[ DriverMethod ( Driver = Driver . Safari , RemoteDriver = true ) ]
@@ -229,13 +231,65 @@ private IWebDriver GetRemoteDriver(string driverBinaries)
229
231
var capabilities = GetCapabilities ( options , _capabilities ) ;
230
232
options . BrowserVersion = string . IsNullOrEmpty ( options . BrowserVersion ) ? "1.0" : options . BrowserVersion ;
231
233
232
-
233
234
// factor web driver
234
235
var executor = _commandTimeout == 0
235
236
? new UiaCommandExecutor ( new Uri ( driverBinaries ) , TimeSpan . FromSeconds ( 60 ) )
236
237
: new UiaCommandExecutor ( new Uri ( driverBinaries ) , TimeSpan . FromSeconds ( _commandTimeout ) ) ;
237
238
return new UiaDriver ( executor , capabilities ) ;
238
239
}
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
+ }
239
293
#endregion
240
294
241
295
// TODO: add default constructor options when there are no binaries to load
@@ -291,14 +345,14 @@ private DriverOptions GetOptions<TOptions, TParams>(string platformName)
291
345
where TParams : DriverOptionsParams , IOptionable < TOptions >
292
346
{
293
347
// parse token
294
- var isOptions = this . _options . Keys . Count > 0 ;
348
+ var isOptions = _options . Keys . Count > 0 ;
295
349
296
350
// null validation
297
351
if ( ! isOptions )
298
352
{
299
353
return new TOptions
300
354
{
301
- PlatformName = platformName
355
+ PlatformName = string . IsNullOrEmpty ( platformName ) ? null : platformName
302
356
} ;
303
357
}
304
358
@@ -309,7 +363,7 @@ private DriverOptions GetOptions<TOptions, TParams>(string platformName)
309
363
var options = paramsObj . ToDriverOptions ( ) ;
310
364
if ( ! string . IsNullOrEmpty ( platformName ) )
311
365
{
312
- options . PlatformName = platformName ;
366
+ options . PlatformName = string . IsNullOrEmpty ( platformName ) ? null : platformName ;
313
367
}
314
368
return options ;
315
369
}
0 commit comments