@@ -40,6 +40,8 @@ public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable,
40
40
/// </summary>
41
41
public const string ElementReferencePropertyName = "element-6066-11e4-a52e-4f735466cecf" ;
42
42
43
+ #nullable enable
44
+
43
45
private readonly WebDriver driver ;
44
46
45
47
/// <summary>
@@ -77,7 +79,8 @@ public virtual string TagName
77
79
78
80
Response commandResponse = this . Execute ( DriverCommand . GetElementTagName , parameters ) ;
79
81
80
- return commandResponse . Value . ToString ( ) ;
82
+ commandResponse . EnsureValueIsNotNull ( ) ;
83
+ return commandResponse . Value . ToString ( ) ! ;
81
84
}
82
85
}
83
86
@@ -95,7 +98,8 @@ public virtual string Text
95
98
96
99
Response commandResponse = this . Execute ( DriverCommand . GetElementText , parameters ) ;
97
100
98
- return commandResponse . Value . ToString ( ) ;
101
+ commandResponse . EnsureValueIsNotNull ( ) ;
102
+ return commandResponse . Value . ToString ( ) ! ;
99
103
}
100
104
}
101
105
@@ -151,7 +155,11 @@ public virtual Point Location
151
155
152
156
Response commandResponse = this . Execute ( DriverCommand . GetElementRect , parameters ) ;
153
157
154
- Dictionary < string , object > rawPoint = ( Dictionary < string , object > ) commandResponse . Value ;
158
+ if ( commandResponse . Value is not Dictionary < string , object ? > rawPoint )
159
+ {
160
+ throw new WebDriverException ( $ "GetElementRect command was successful, but response was not an object: { commandResponse . Value } ") ;
161
+ }
162
+
155
163
int x = Convert . ToInt32 ( rawPoint [ "x" ] , CultureInfo . InvariantCulture ) ;
156
164
int y = Convert . ToInt32 ( rawPoint [ "y" ] , CultureInfo . InvariantCulture ) ;
157
165
return new Point ( x , y ) ;
@@ -171,7 +179,11 @@ public virtual Size Size
171
179
172
180
Response commandResponse = this . Execute ( DriverCommand . GetElementRect , parameters ) ;
173
181
174
- Dictionary < string , object > rawSize = ( Dictionary < string , object > ) commandResponse . Value ;
182
+ if ( commandResponse . Value is not Dictionary < string , object ? > rawSize )
183
+ {
184
+ throw new WebDriverException ( $ "GetElementRect command was successful, but response was not an object: { commandResponse . Value } ") ;
185
+ }
186
+
175
187
int width = Convert . ToInt32 ( rawSize [ "width" ] , CultureInfo . InvariantCulture ) ;
176
188
int height = Convert . ToInt32 ( rawSize [ "height" ] , CultureInfo . InvariantCulture ) ;
177
189
return new Size ( width , height ) ;
@@ -207,7 +219,7 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
207
219
{
208
220
get
209
221
{
210
- object scriptResponse = this . driver . ExecuteScript ( "var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};" , this ) ;
222
+ object scriptResponse = this . driver . ExecuteScript ( "var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};" , this ) ! ;
211
223
212
224
Dictionary < string , object > rawLocation = ( Dictionary < string , object > ) scriptResponse ;
213
225
@@ -229,7 +241,8 @@ public virtual string ComputedAccessibleLabel
229
241
230
242
Response commandResponse = this . Execute ( DriverCommand . GetComputedAccessibleLabel , parameters ) ;
231
243
232
- return commandResponse . Value . ToString ( ) ;
244
+ commandResponse . EnsureValueIsNotNull ( ) ;
245
+ return commandResponse . Value . ToString ( ) ! ;
233
246
}
234
247
}
235
248
@@ -240,16 +253,18 @@ public virtual string ComputedAccessibleRole
240
253
{
241
254
get
242
255
{
243
- // TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
244
- // needs to be updated to more thoroughly document the structure of what is returned
245
- // by this command. Once that is done, a type-safe class will be created, and will
246
- // be returned by this property.
247
256
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
248
257
parameters . Add ( "id" , this . Id ) ;
249
258
250
259
Response commandResponse = this . Execute ( DriverCommand . GetComputedAccessibleRole , parameters ) ;
251
260
261
+ #nullable disable
262
+ // TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
263
+ // needs to be updated to more thoroughly document the structure of what is returned
264
+ // by this command. Once that is done, a type-safe class will be created, and will
265
+ // be returned by this property.
252
266
return commandResponse . Value . ToString ( ) ;
267
+ #nullable enable
253
268
}
254
269
}
255
270
@@ -312,11 +327,14 @@ public virtual void Click()
312
327
this . Execute ( DriverCommand . ClickElement , parameters ) ;
313
328
}
314
329
330
+ #nullable restore
331
+
315
332
/// <summary>
316
333
/// Finds the first <see cref="IWebElement"/> using the given method.
317
334
/// </summary>
318
335
/// <param name="by">The locating mechanism to use.</param>
319
336
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
337
+ /// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
320
338
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
321
339
public virtual IWebElement FindElement ( By by )
322
340
{
@@ -328,6 +346,8 @@ public virtual IWebElement FindElement(By by)
328
346
return by . FindElement ( this ) ;
329
347
}
330
348
349
+ #nullable enable
350
+
331
351
/// <summary>
332
352
/// Finds a child element matching the given mechanism and value.
333
353
/// </summary>
@@ -346,6 +366,8 @@ public virtual IWebElement FindElement(string mechanism, string value)
346
366
return this . driver . GetElementFromResponse ( commandResponse ) ;
347
367
}
348
368
369
+ #nullable restore
370
+
349
371
/// <summary>
350
372
/// Finds all <see cref="IWebElement">IWebElements</see> within the current context
351
373
/// using the given mechanism.
@@ -363,6 +385,8 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(By by)
363
385
return by . FindElements ( this ) ;
364
386
}
365
387
388
+ #nullable enable
389
+
366
390
/// <summary>
367
391
/// Finds all child elements matching the given mechanism and value.
368
392
/// </summary>
@@ -418,15 +442,14 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, st
418
442
/// via JavaScript.
419
443
/// </remarks>
420
444
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
421
- public virtual string GetAttribute ( string attributeName )
445
+ public virtual string ? GetAttribute ( string attributeName )
422
446
{
423
- Response commandResponse = null ;
424
- string attributeValue = string . Empty ;
425
447
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
426
448
string atom = GetAtom ( "get-attribute.js" ) ;
427
449
parameters . Add ( "script" , atom ) ;
428
450
parameters . Add ( "args" , new object [ ] { ( ( IWebDriverObjectReference ) this ) . ToDictionary ( ) , attributeName } ) ;
429
- commandResponse = this . Execute ( DriverCommand . ExecuteScript , parameters ) ;
451
+
452
+ Response commandResponse = Execute ( DriverCommand . ExecuteScript , parameters ) ;
430
453
431
454
432
455
// Normalize string values of boolean results as lowercase.
@@ -451,7 +474,7 @@ public virtual string GetAttribute(string attributeName)
451
474
/// of an IDL property of the element, either use the <see cref="GetAttribute(string)"/>
452
475
/// method or the <see cref="GetDomProperty(string)"/> method.
453
476
/// </remarks>
454
- public virtual string GetDomAttribute ( string attributeName )
477
+ public virtual string ? GetDomAttribute ( string attributeName )
455
478
{
456
479
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
457
480
parameters . Add ( "id" , this . Id ) ;
@@ -469,7 +492,7 @@ public virtual string GetDomAttribute(string attributeName)
469
492
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
470
493
/// value is not set or the property does not exist.</returns>
471
494
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
472
- public virtual string GetDomProperty ( string propertyName )
495
+ public virtual string ? GetDomProperty ( string propertyName )
473
496
{
474
497
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
475
498
parameters . Add ( "id" , this . Id ) ;
@@ -492,12 +515,12 @@ public virtual ISearchContext GetShadowRoot()
492
515
parameters . Add ( "id" , this . Id ) ;
493
516
494
517
Response commandResponse = this . Execute ( DriverCommand . GetElementShadowRoot , parameters ) ;
495
- if ( commandResponse . Value is not Dictionary < string , object > shadowRootDictionary )
518
+ if ( commandResponse . Value is not Dictionary < string , object ? > shadowRootDictionary )
496
519
{
497
520
throw new WebDriverException ( "Get shadow root command succeeded, but response value does not represent a shadow root." ) ;
498
521
}
499
522
500
- if ( ! ShadowRoot . TryCreate ( this . driver , shadowRootDictionary , out ShadowRoot shadowRoot ) )
523
+ if ( ! ShadowRoot . TryCreate ( this . driver , shadowRootDictionary , out ShadowRoot ? shadowRoot ) )
501
524
{
502
525
throw new WebDriverException ( "Get shadow root command succeeded, but response value does not have a shadow root key value." ) ;
503
526
}
@@ -523,7 +546,9 @@ public virtual string GetCssValue(string propertyName)
523
546
parameters . Add ( "name" , propertyName ) ;
524
547
525
548
Response commandResponse = this . Execute ( DriverCommand . GetElementValueOfCssProperty , parameters ) ;
526
- return commandResponse . Value . ToString ( ) ;
549
+
550
+ commandResponse . EnsureValueIsNotNull ( ) ;
551
+ return commandResponse . Value . ToString ( ) ! ;
527
552
}
528
553
529
554
/// <summary>
@@ -537,7 +562,9 @@ public virtual Screenshot GetScreenshot()
537
562
538
563
// Get the screenshot as base64.
539
564
Response screenshotResponse = this . Execute ( DriverCommand . ElementScreenshot , parameters ) ;
540
- string base64 = screenshotResponse . Value . ToString ( ) ;
565
+
566
+ screenshotResponse . EnsureValueIsNotNull ( ) ;
567
+ string base64 = screenshotResponse . Value . ToString ( ) ! ;
541
568
542
569
// ... and convert it.
543
570
return new Screenshot ( base64 ) ;
@@ -595,7 +622,7 @@ public virtual void SendKeys(string text)
595
622
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
596
623
public virtual void Submit ( )
597
624
{
598
- string elementType = this . GetAttribute ( "type" ) ;
625
+ string ? elementType = this . GetAttribute ( "type" ) ;
599
626
if ( elementType != null && elementType == "submit" )
600
627
{
601
628
this . Click ( ) ;
@@ -639,7 +666,7 @@ public override int GetHashCode()
639
666
/// </summary>
640
667
/// <param name="obj">Object to compare against</param>
641
668
/// <returns>A boolean if it is equal or not</returns>
642
- public override bool Equals ( object obj )
669
+ public override bool Equals ( object ? obj )
643
670
{
644
671
if ( obj is not IWebElement other )
645
672
{
@@ -674,6 +701,8 @@ Dictionary<string, object> IWebDriverObjectReference.ToDictionary()
674
701
return elementDictionary ;
675
702
}
676
703
704
+ #nullable restore
705
+
677
706
/// <summary>
678
707
/// Executes a command on this element using the specified parameters.
679
708
/// </summary>
@@ -685,6 +714,8 @@ protected virtual Response Execute(string commandToExecute, Dictionary<string, o
685
714
return this . driver . InternalExecute ( commandToExecute , parameters ) ;
686
715
}
687
716
717
+ #nullable enable
718
+
688
719
private static string GetAtom ( string atomResourceName )
689
720
{
690
721
string atom = string . Empty ;
@@ -719,7 +750,9 @@ private string UploadFile(string localFile)
719
750
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
720
751
parameters . Add ( "file" , base64zip ) ;
721
752
Response response = this . Execute ( DriverCommand . UploadFile , parameters ) ;
722
- return response . Value . ToString ( ) ;
753
+
754
+ response . EnsureValueIsNotNull ( ) ;
755
+ return response . Value . ToString ( ) ! ;
723
756
}
724
757
catch ( IOException e )
725
758
{
0 commit comments