21
21
using System ;
22
22
using System . Collections . Generic ;
23
23
using System . Collections . ObjectModel ;
24
+ using System . Diagnostics . CodeAnalysis ;
25
+
26
+ #nullable enable
24
27
25
28
namespace OpenQA . Selenium
26
29
{
@@ -34,68 +37,67 @@ public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReferenc
34
37
/// </summary>
35
38
public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf" ;
36
39
37
- private WebDriver driver ;
38
- private string shadowRootId ;
40
+ private readonly WebDriver driver ;
41
+ private readonly string shadowRootId ;
39
42
40
43
/// <summary>
41
44
/// Initializes a new instance of the <see cref="ShadowRoot"/> class.
42
45
/// </summary>
43
46
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this shadow root.</param>
44
47
/// <param name="id">The ID value provided to identify the shadow root.</param>
48
+ /// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> or <paramref name="id"/> are <see langword="null"/>.</exception>
45
49
public ShadowRoot ( WebDriver parentDriver , string id )
46
50
{
47
- this . driver = parentDriver ;
48
- this . shadowRootId = id ;
51
+ this . driver = parentDriver ?? throw new ArgumentNullException ( nameof ( parentDriver ) ) ;
52
+ this . shadowRootId = id ?? throw new ArgumentNullException ( nameof ( id ) ) ;
49
53
}
50
54
51
55
/// <summary>
52
56
/// Gets the <see cref="IWebDriver"/> driving this shadow root.
53
57
/// </summary>
54
- public IWebDriver WrappedDriver
55
- {
56
- get { return this . driver ; }
57
- }
58
+ public IWebDriver WrappedDriver => this . driver ;
58
59
59
60
/// <summary>
60
61
/// Gets the internal ID for this ShadowRoot.
61
62
/// </summary>
62
- string IWebDriverObjectReference . ObjectReferenceId
63
- {
64
- get { return this . shadowRootId ; }
65
- }
63
+ string IWebDriverObjectReference . ObjectReferenceId => this . shadowRootId ;
66
64
67
- internal static bool ContainsShadowRootReference ( Dictionary < string , object > shadowRootDictionary )
65
+ internal static bool TryCreate ( WebDriver parentDriver , Dictionary < string , object ? > shadowRootDictionary , [ NotNullWhen ( true ) ] out ShadowRoot ? shadowRoot )
68
66
{
69
- if ( shadowRootDictionary == null )
67
+ if ( shadowRootDictionary is null )
70
68
{
71
69
throw new ArgumentNullException ( nameof ( shadowRootDictionary ) , "The dictionary containing the shadow root reference cannot be null" ) ;
72
70
}
73
71
74
- return shadowRootDictionary . ContainsKey ( ShadowRootReferencePropertyName ) ;
75
- }
72
+ if ( shadowRootDictionary . TryGetValue ( ShadowRootReferencePropertyName , out object ? shadowRootValue ) )
73
+ {
74
+ shadowRoot = new ShadowRoot ( parentDriver , shadowRootValue ? . ToString ( ) ! ) ;
75
+ return true ;
76
+ }
76
77
77
- internal static ShadowRoot FromDictionary ( WebDriver driver , Dictionary < string , object > shadowRootDictionary )
78
- {
79
- return new ShadowRoot ( driver , shadowRootDictionary [ ShadowRoot . ShadowRootReferencePropertyName ] . ToString ( ) ) ;
78
+ shadowRoot = null ;
79
+ return false ;
80
80
}
81
81
82
82
/// <summary>
83
83
/// Finds the first <see cref="IWebElement"/> using the given method.
84
84
/// </summary>
85
85
/// <param name="by">The locating mechanism to use.</param>
86
86
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
87
+ /// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
87
88
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
88
89
public IWebElement FindElement ( By by )
89
90
{
90
- if ( by == null )
91
+ if ( by is null )
91
92
{
92
- throw new ArgumentNullException ( nameof ( @ by) , "by cannot be null" ) ;
93
+ throw new ArgumentNullException ( nameof ( by ) , "by cannot be null" ) ;
93
94
}
94
95
95
96
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
96
97
parameters . Add ( "id" , this . shadowRootId ) ;
97
98
parameters . Add ( "using" , by . Mechanism ) ;
98
99
parameters . Add ( "value" , by . Criteria ) ;
100
+
99
101
Response commandResponse = this . driver . InternalExecute ( DriverCommand . FindShadowChildElement , parameters ) ;
100
102
return this . driver . GetElementFromResponse ( commandResponse ) ;
101
103
}
@@ -107,26 +109,29 @@ public IWebElement FindElement(By by)
107
109
/// <param name="by">The locating mechanism to use.</param>
108
110
/// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see>
109
111
/// matching the current criteria, or an empty list if nothing matches.</returns>
112
+ /// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
110
113
public ReadOnlyCollection < IWebElement > FindElements ( By by )
111
114
{
112
- if ( by == null )
115
+ if ( by is null )
113
116
{
114
- throw new ArgumentNullException ( nameof ( @ by) , "by cannot be null" ) ;
117
+ throw new ArgumentNullException ( nameof ( by ) , "by cannot be null" ) ;
115
118
}
116
119
117
120
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
118
121
parameters . Add ( "id" , this . shadowRootId ) ;
119
122
parameters . Add ( "using" , by . Mechanism ) ;
120
123
parameters . Add ( "value" , by . Criteria ) ;
124
+
121
125
Response commandResponse = this . driver . InternalExecute ( DriverCommand . FindShadowChildElements , parameters ) ;
122
126
return this . driver . GetElementsFromResponse ( commandResponse ) ;
123
127
}
124
128
125
129
Dictionary < string , object > IWebDriverObjectReference . ToDictionary ( )
126
130
{
127
- Dictionary < string , object > shadowRootDictionary = new Dictionary < string , object > ( ) ;
128
- shadowRootDictionary . Add ( ShadowRootReferencePropertyName , this . shadowRootId ) ;
129
- return shadowRootDictionary ;
131
+ return new Dictionary < string , object >
132
+ {
133
+ [ ShadowRootReferencePropertyName ] = this . shadowRootId
134
+ } ;
130
135
}
131
136
}
132
137
}
0 commit comments