@@ -40,28 +40,24 @@ public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable,
40
40
/// </summary>
41
41
public const string ElementReferencePropertyName = "element-6066-11e4-a52e-4f735466cecf" ;
42
42
43
- private WebDriver driver ;
44
- private string elementId ;
43
+ private readonly WebDriver driver ;
45
44
46
45
/// <summary>
47
46
/// Initializes a new instance of the <see cref="WebElement"/> class.
48
47
/// </summary>
49
48
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this element.</param>
50
49
/// <param name="id">The ID value provided to identify the element.</param>
51
- /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langword="null"/>.</exception>
50
+ /// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> or <paramref name=" id"/> are <see langword="null"/>.</exception>
52
51
public WebElement ( WebDriver parentDriver , string id )
53
52
{
54
- this . driver = parentDriver ;
55
- this . elementId = id ?? throw new ArgumentNullException ( nameof ( id ) ) ;
53
+ this . driver = parentDriver ?? throw new ArgumentNullException ( nameof ( parentDriver ) ) ;
54
+ this . Id = id ?? throw new ArgumentNullException ( nameof ( id ) ) ;
56
55
}
57
56
58
57
/// <summary>
59
58
/// Gets the <see cref="IWebDriver"/> driving this element.
60
59
/// </summary>
61
- public IWebDriver WrappedDriver
62
- {
63
- get { return this . driver ; }
64
- }
60
+ public IWebDriver WrappedDriver => this . driver ;
65
61
66
62
/// <summary>
67
63
/// Gets the tag name of this element.
@@ -77,8 +73,10 @@ public virtual string TagName
77
73
get
78
74
{
79
75
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
80
- parameters . Add ( "id" , this . elementId ) ;
76
+ parameters . Add ( "id" , this . Id ) ;
77
+
81
78
Response commandResponse = this . Execute ( DriverCommand . GetElementTagName , parameters ) ;
79
+
82
80
return commandResponse . Value . ToString ( ) ;
83
81
}
84
82
}
@@ -93,8 +91,10 @@ public virtual string Text
93
91
get
94
92
{
95
93
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
96
- parameters . Add ( "id" , this . elementId ) ;
94
+ parameters . Add ( "id" , this . Id ) ;
95
+
97
96
Response commandResponse = this . Execute ( DriverCommand . GetElementText , parameters ) ;
97
+
98
98
return commandResponse . Value . ToString ( ) ;
99
99
}
100
100
}
@@ -110,9 +110,11 @@ public virtual bool Enabled
110
110
get
111
111
{
112
112
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
113
- parameters . Add ( "id" , this . elementId ) ;
113
+ parameters . Add ( "id" , this . Id ) ;
114
+
114
115
Response commandResponse = this . Execute ( DriverCommand . IsElementEnabled , parameters ) ;
115
- return ( bool ) Convert . ChangeType ( commandResponse . Value , typeof ( bool ) ) ;
116
+
117
+ return Convert . ToBoolean ( commandResponse . Value ) ;
116
118
}
117
119
}
118
120
@@ -127,9 +129,11 @@ public virtual bool Selected
127
129
get
128
130
{
129
131
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
130
- parameters . Add ( "id" , this . elementId ) ;
132
+ parameters . Add ( "id" , this . Id ) ;
133
+
131
134
Response commandResponse = this . Execute ( DriverCommand . IsElementSelected , parameters ) ;
132
- return ( bool ) Convert . ChangeType ( commandResponse . Value , typeof ( bool ) ) ;
135
+
136
+ return Convert . ToBoolean ( commandResponse . Value ) ;
133
137
}
134
138
}
135
139
@@ -142,10 +146,11 @@ public virtual Point Location
142
146
{
143
147
get
144
148
{
145
- string getLocationCommand = DriverCommand . GetElementRect ;
146
149
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
147
150
parameters . Add ( "id" , this . Id ) ;
148
- Response commandResponse = this . Execute ( getLocationCommand , parameters ) ;
151
+
152
+ Response commandResponse = this . Execute ( DriverCommand . GetElementRect , parameters ) ;
153
+
149
154
Dictionary < string , object > rawPoint = ( Dictionary < string , object > ) commandResponse . Value ;
150
155
int x = Convert . ToInt32 ( rawPoint [ "x" ] , CultureInfo . InvariantCulture ) ;
151
156
int y = Convert . ToInt32 ( rawPoint [ "y" ] , CultureInfo . InvariantCulture ) ;
@@ -161,10 +166,11 @@ public virtual Size Size
161
166
{
162
167
get
163
168
{
164
- string getSizeCommand = DriverCommand . GetElementRect ;
165
169
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
166
170
parameters . Add ( "id" , this . Id ) ;
167
- Response commandResponse = this . Execute ( getSizeCommand , parameters ) ;
171
+
172
+ Response commandResponse = this . Execute ( DriverCommand . GetElementRect , parameters ) ;
173
+
168
174
Dictionary < string , object > rawSize = ( Dictionary < string , object > ) commandResponse . Value ;
169
175
int width = Convert . ToInt32 ( rawSize [ "width" ] , CultureInfo . InvariantCulture ) ;
170
176
int height = Convert . ToInt32 ( rawSize [ "height" ] , CultureInfo . InvariantCulture ) ;
@@ -183,14 +189,14 @@ public virtual bool Displayed
183
189
{
184
190
get
185
191
{
186
- Response commandResponse = null ;
187
192
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
188
193
string atom = GetAtom ( "is-displayed.js" ) ;
189
194
parameters . Add ( "script" , atom ) ;
190
- parameters . Add ( "args" , new object [ ] { this . ToElementReference ( ) . ToDictionary ( ) } ) ;
191
- commandResponse = this . Execute ( DriverCommand . ExecuteScript , parameters ) ;
195
+ parameters . Add ( "args" , new object [ ] { ( ( IWebDriverObjectReference ) this ) . ToDictionary ( ) } ) ;
192
196
193
- return ( bool ) Convert . ChangeType ( commandResponse . Value , typeof ( bool ) ) ;
197
+ Response commandResponse = Execute ( DriverCommand . ExecuteScript , parameters ) ;
198
+
199
+ return Convert . ToBoolean ( commandResponse . Value ) ;
194
200
}
195
201
}
196
202
@@ -201,9 +207,9 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
201
207
{
202
208
get
203
209
{
204
- Dictionary < string , object > rawLocation ;
205
210
object scriptResponse = this . driver . ExecuteScript ( "var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};" , this ) ;
206
- rawLocation = scriptResponse as Dictionary < string , object > ;
211
+
212
+ Dictionary < string , object > rawLocation = ( Dictionary < string , object > ) scriptResponse ;
207
213
208
214
int x = Convert . ToInt32 ( rawLocation [ "x" ] , CultureInfo . InvariantCulture ) ;
209
215
int y = Convert . ToInt32 ( rawLocation [ "y" ] , CultureInfo . InvariantCulture ) ;
@@ -220,7 +226,9 @@ public virtual string ComputedAccessibleLabel
220
226
{
221
227
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
222
228
parameters . Add ( "id" , this . Id ) ;
229
+
223
230
Response commandResponse = this . Execute ( DriverCommand . GetComputedAccessibleLabel , parameters ) ;
231
+
224
232
return commandResponse . Value . ToString ( ) ;
225
233
}
226
234
}
@@ -233,12 +241,14 @@ public virtual string ComputedAccessibleRole
233
241
get
234
242
{
235
243
// TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
236
- // needs to be updated to more throughly document the structure of what is returned
244
+ // needs to be updated to more thoroughly document the structure of what is returned
237
245
// by this command. Once that is done, a type-safe class will be created, and will
238
246
// be returned by this property.
239
247
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
240
248
parameters . Add ( "id" , this . Id ) ;
249
+
241
250
Response commandResponse = this . Execute ( DriverCommand . GetComputedAccessibleRole , parameters ) ;
251
+
242
252
return commandResponse . Value . ToString ( ) ;
243
253
}
244
254
}
@@ -247,18 +257,12 @@ public virtual string ComputedAccessibleRole
247
257
/// Gets the coordinates identifying the location of this element using
248
258
/// various frames of reference.
249
259
/// </summary>
250
- public virtual ICoordinates Coordinates
251
- {
252
- get { return new ElementCoordinates ( this ) ; }
253
- }
260
+ public virtual ICoordinates Coordinates => new ElementCoordinates ( this ) ;
254
261
255
262
/// <summary>
256
263
/// Gets the internal ID of the element.
257
264
/// </summary>
258
- string IWebDriverObjectReference . ObjectReferenceId
259
- {
260
- get { return this . elementId ; }
261
- }
265
+ string IWebDriverObjectReference . ObjectReferenceId => this . Id ;
262
266
263
267
/// <summary>
264
268
/// Gets the ID of the element
@@ -270,10 +274,7 @@ string IWebDriverObjectReference.ObjectReferenceId
270
274
/// and the parent driver hosting the element have a need to access the
271
275
/// internal element ID. Therefore, we have two properties returning the
272
276
/// same value, one scoped as internal, the other as protected.</remarks>
273
- protected string Id
274
- {
275
- get { return this . elementId ; }
276
- }
277
+ protected string Id { get ; }
277
278
278
279
/// <summary>
279
280
/// Clears the content of this element.
@@ -285,7 +286,8 @@ protected string Id
285
286
public virtual void Clear ( )
286
287
{
287
288
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
288
- parameters . Add ( "id" , this . elementId ) ;
289
+ parameters . Add ( "id" , this . Id ) ;
290
+
289
291
this . Execute ( DriverCommand . ClearElement , parameters ) ;
290
292
}
291
293
@@ -306,7 +308,8 @@ public virtual void Clear()
306
308
public virtual void Click ( )
307
309
{
308
310
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
309
- parameters . Add ( "id" , this . elementId ) ;
311
+ parameters . Add ( "id" , this . Id ) ;
312
+
310
313
this . Execute ( DriverCommand . ClickElement , parameters ) ;
311
314
}
312
315
@@ -335,10 +338,12 @@ public virtual IWebElement FindElement(By by)
335
338
public virtual IWebElement FindElement ( string mechanism , string value )
336
339
{
337
340
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
338
- parameters . Add ( "id" , this . elementId ) ;
341
+ parameters . Add ( "id" , this . Id ) ;
339
342
parameters . Add ( "using" , mechanism ) ;
340
343
parameters . Add ( "value" , value ) ;
344
+
341
345
Response commandResponse = this . Execute ( DriverCommand . FindChildElement , parameters ) ;
346
+
342
347
return this . driver . GetElementFromResponse ( commandResponse ) ;
343
348
}
344
349
@@ -368,10 +373,12 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(By by)
368
373
public virtual ReadOnlyCollection < IWebElement > FindElements ( string mechanism , string value )
369
374
{
370
375
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
371
- parameters . Add ( "id" , this . elementId ) ;
376
+ parameters . Add ( "id" , this . Id ) ;
372
377
parameters . Add ( "using" , mechanism ) ;
373
378
parameters . Add ( "value" , value ) ;
379
+
374
380
Response commandResponse = this . Execute ( DriverCommand . FindChildElements , parameters ) ;
381
+
375
382
return this . driver . GetElementsFromResponse ( commandResponse ) ;
376
383
}
377
384
@@ -419,25 +426,17 @@ public virtual string GetAttribute(string attributeName)
419
426
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
420
427
string atom = GetAtom ( "get-attribute.js" ) ;
421
428
parameters . Add ( "script" , atom ) ;
422
- parameters . Add ( "args" , new object [ ] { this . ToElementReference ( ) . ToDictionary ( ) , attributeName } ) ;
429
+ parameters . Add ( "args" , new object [ ] { ( ( IWebDriverObjectReference ) this ) . ToDictionary ( ) , attributeName } ) ;
423
430
commandResponse = this . Execute ( DriverCommand . ExecuteScript , parameters ) ;
424
431
425
- if ( commandResponse . Value == null )
426
- {
427
- attributeValue = null ;
428
- }
429
- else
430
- {
431
- attributeValue = commandResponse . Value . ToString ( ) ;
432
432
433
- // Normalize string values of boolean results as lowercase.
434
- if ( commandResponse . Value is bool )
435
- {
436
- attributeValue = attributeValue . ToLowerInvariant ( ) ;
437
- }
433
+ // Normalize string values of boolean results as lowercase.
434
+ if ( commandResponse . Value is bool b )
435
+ {
436
+ return b ? "true" : "false" ;
438
437
}
439
438
440
- return attributeValue ;
439
+ return commandResponse . Value ? . ToString ( ) ;
441
440
}
442
441
443
442
/// <summary>
@@ -455,22 +454,13 @@ public virtual string GetAttribute(string attributeName)
455
454
/// </remarks>
456
455
public virtual string GetDomAttribute ( string attributeName )
457
456
{
458
- string attributeValue = string . Empty ;
459
457
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
460
458
parameters . Add ( "id" , this . Id ) ;
461
459
parameters . Add ( "name" , attributeName ) ;
462
460
463
461
Response commandResponse = this . Execute ( DriverCommand . GetElementAttribute , parameters ) ;
464
- if ( commandResponse . Value == null )
465
- {
466
- attributeValue = null ;
467
- }
468
- else
469
- {
470
- attributeValue = commandResponse . Value . ToString ( ) ;
471
- }
472
462
473
- return attributeValue ;
463
+ return commandResponse . Value ? . ToString ( ) ;
474
464
}
475
465
476
466
/// <summary>
@@ -482,22 +472,13 @@ public virtual string GetDomAttribute(string attributeName)
482
472
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
483
473
public virtual string GetDomProperty ( string propertyName )
484
474
{
485
- string propertyValue = string . Empty ;
486
475
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
487
476
parameters . Add ( "id" , this . Id ) ;
488
477
parameters . Add ( "name" , propertyName ) ;
489
478
490
479
Response commandResponse = this . Execute ( DriverCommand . GetElementProperty , parameters ) ;
491
- if ( commandResponse . Value == null )
492
- {
493
- propertyValue = null ;
494
- }
495
- else
496
- {
497
- propertyValue = commandResponse . Value . ToString ( ) ;
498
- }
499
480
500
- return propertyValue ;
481
+ return commandResponse . Value ? . ToString ( ) ;
501
482
}
502
483
503
484
/// <summary>
@@ -512,19 +493,17 @@ public virtual ISearchContext GetShadowRoot()
512
493
parameters . Add ( "id" , this . Id ) ;
513
494
514
495
Response commandResponse = this . Execute ( DriverCommand . GetElementShadowRoot , parameters ) ;
515
- Dictionary < string , object > shadowRootDictionary = commandResponse . Value as Dictionary < string , object > ;
516
- if ( shadowRootDictionary == null )
496
+ if ( commandResponse . Value is not Dictionary < string , object > shadowRootDictionary )
517
497
{
518
498
throw new WebDriverException ( "Get shadow root command succeeded, but response value does not represent a shadow root." ) ;
519
499
}
520
500
521
- if ( ! shadowRootDictionary . ContainsKey ( ShadowRoot . ShadowRootReferencePropertyName ) )
501
+ if ( ! ShadowRoot . TryCreate ( this . driver , shadowRootDictionary , out ShadowRoot shadowRoot ) )
522
502
{
523
503
throw new WebDriverException ( "Get shadow root command succeeded, but response value does not have a shadow root key value." ) ;
524
504
}
525
505
526
- string shadowRootId = shadowRootDictionary [ ShadowRoot . ShadowRootReferencePropertyName ] . ToString ( ) ;
527
- return new ShadowRoot ( this . driver , shadowRootId ) ;
506
+ return shadowRoot ;
528
507
}
529
508
530
509
/// <summary>
@@ -555,7 +534,7 @@ public virtual string GetCssValue(string propertyName)
555
534
public virtual Screenshot GetScreenshot ( )
556
535
{
557
536
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
558
- parameters . Add ( "id" , this . elementId ) ;
537
+ parameters . Add ( "id" , this . Id ) ;
559
538
560
539
// Get the screenshot as base64.
561
540
Response screenshotResponse = this . Execute ( DriverCommand . ElementScreenshot , parameters ) ;
@@ -601,7 +580,7 @@ public virtual void SendKeys(string text)
601
580
// TODO: Remove either "keysToSend" or "value" property, whichever is not the
602
581
// appropriate one for spec compliance.
603
582
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
604
- parameters . Add ( "id" , this . elementId ) ;
583
+ parameters . Add ( "id" , this . Id ) ;
605
584
parameters . Add ( "text" , text ) ;
606
585
parameters . Add ( "value" , text . ToCharArray ( ) ) ;
607
586
@@ -625,7 +604,7 @@ public virtual void Submit()
625
604
}
626
605
else
627
606
{
628
- String script = "/* submitForm */var form = arguments[0];\n " +
607
+ string script = "/* submitForm */var form = arguments[0];\n " +
629
608
"while (form.nodeName != \" FORM\" && form.parentNode) {\n " +
630
609
" form = form.parentNode;\n " +
631
610
"}\n " +
@@ -645,7 +624,7 @@ public virtual void Submit()
645
624
/// <returns>A string that represents the current <see cref="WebElement"/>.</returns>
646
625
public override string ToString ( )
647
626
{
648
- return string . Format ( CultureInfo . InvariantCulture , "Element (id = {0})" , this . elementId ) ;
627
+ return string . Format ( CultureInfo . InvariantCulture , "Element (id = {0})" , this . Id ) ;
649
628
}
650
629
651
630
/// <summary>
@@ -654,7 +633,7 @@ public override string ToString()
654
633
/// <returns>Integer of the hash code for the element</returns>
655
634
public override int GetHashCode ( )
656
635
{
657
- return this . elementId . GetHashCode ( ) ;
636
+ return this . Id . GetHashCode ( ) ;
658
637
}
659
638
660
639
/// <summary>
@@ -664,25 +643,22 @@ public override int GetHashCode()
664
643
/// <returns>A boolean if it is equal or not</returns>
665
644
public override bool Equals ( object obj )
666
645
{
667
- IWebElement other = obj as IWebElement ;
668
- if ( other == null )
646
+ if ( obj is not IWebElement other )
669
647
{
670
648
return false ;
671
649
}
672
650
673
- IWrapsElement objAsWrapsElement = obj as IWrapsElement ;
674
- if ( objAsWrapsElement != null )
651
+ if ( obj is IWrapsElement objAsWrapsElement )
675
652
{
676
653
other = objAsWrapsElement . WrappedElement ;
677
654
}
678
655
679
- WebElement otherAsElement = other as WebElement ;
680
- if ( otherAsElement == null )
656
+ if ( other is not WebElement otherAsElement )
681
657
{
682
658
return false ;
683
659
}
684
660
685
- if ( this . elementId == otherAsElement . Id )
661
+ if ( this . Id == otherAsElement . Id )
686
662
{
687
663
// For drivers that implement ID equality, we can check for equal IDs
688
664
// here, and expect them to be equal. There is a potential danger here
@@ -696,7 +672,7 @@ public override bool Equals(object obj)
696
672
Dictionary < string , object > IWebDriverObjectReference . ToDictionary ( )
697
673
{
698
674
Dictionary < string , object > elementDictionary = new Dictionary < string , object > ( ) ;
699
- elementDictionary . Add ( ElementReferencePropertyName , this . elementId ) ;
675
+ elementDictionary . Add ( ElementReferencePropertyName , this . Id ) ;
700
676
return elementDictionary ;
701
677
}
702
678
@@ -729,7 +705,7 @@ private static string GetAtom(string atomResourceName)
729
705
730
706
private string UploadFile ( string localFile )
731
707
{
732
- string base64zip = string . Empty ;
708
+ string base64zip ;
733
709
try
734
710
{
735
711
using ( MemoryStream fileUploadMemoryStream = new MemoryStream ( ) )
@@ -752,10 +728,5 @@ private string UploadFile(string localFile)
752
728
throw new WebDriverException ( "Cannot upload " + localFile , e ) ;
753
729
}
754
730
}
755
-
756
- private IWebDriverObjectReference ToElementReference ( )
757
- {
758
- return this as IWebDriverObjectReference ;
759
- }
760
731
}
761
732
}
0 commit comments