Skip to content

Commit 4fe2a56

Browse files
[dotnet] Add nullability annotations to ShadowRoot (#14812)
1 parent e71f0db commit 4fe2a56

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

Diff for: dotnet/src/webdriver/ISearchContext.cs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.Collections.ObjectModel;
2222

23+
#nullable enable
24+
2325
namespace OpenQA.Selenium
2426
{
2527
/// <summary>

Diff for: dotnet/src/webdriver/IWrapsDriver.cs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium
2123
{
2224
/// <summary>

Diff for: dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System.Collections.Generic;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal
2325
{
2426
/// <summary>

Diff for: dotnet/src/webdriver/ShadowRoot.cs

+31-26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Collections.ObjectModel;
24+
using System.Diagnostics.CodeAnalysis;
25+
26+
#nullable enable
2427

2528
namespace OpenQA.Selenium
2629
{
@@ -34,68 +37,67 @@ public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReferenc
3437
/// </summary>
3538
public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf";
3639

37-
private WebDriver driver;
38-
private string shadowRootId;
40+
private readonly WebDriver driver;
41+
private readonly string shadowRootId;
3942

4043
/// <summary>
4144
/// Initializes a new instance of the <see cref="ShadowRoot"/> class.
4245
/// </summary>
4346
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this shadow root.</param>
4447
/// <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>
4549
public ShadowRoot(WebDriver parentDriver, string id)
4650
{
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));
4953
}
5054

5155
/// <summary>
5256
/// Gets the <see cref="IWebDriver"/> driving this shadow root.
5357
/// </summary>
54-
public IWebDriver WrappedDriver
55-
{
56-
get { return this.driver; }
57-
}
58+
public IWebDriver WrappedDriver => this.driver;
5859

5960
/// <summary>
6061
/// Gets the internal ID for this ShadowRoot.
6162
/// </summary>
62-
string IWebDriverObjectReference.ObjectReferenceId
63-
{
64-
get { return this.shadowRootId; }
65-
}
63+
string IWebDriverObjectReference.ObjectReferenceId => this.shadowRootId;
6664

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)
6866
{
69-
if (shadowRootDictionary == null)
67+
if (shadowRootDictionary is null)
7068
{
7169
throw new ArgumentNullException(nameof(shadowRootDictionary), "The dictionary containing the shadow root reference cannot be null");
7270
}
7371

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+
}
7677

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;
8080
}
8181

8282
/// <summary>
8383
/// Finds the first <see cref="IWebElement"/> using the given method.
8484
/// </summary>
8585
/// <param name="by">The locating mechanism to use.</param>
8686
/// <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>
8788
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
8889
public IWebElement FindElement(By by)
8990
{
90-
if (by == null)
91+
if (by is null)
9192
{
92-
throw new ArgumentNullException(nameof(@by), "by cannot be null");
93+
throw new ArgumentNullException(nameof(by), "by cannot be null");
9394
}
9495

9596
Dictionary<string, object> parameters = new Dictionary<string, object>();
9697
parameters.Add("id", this.shadowRootId);
9798
parameters.Add("using", by.Mechanism);
9899
parameters.Add("value", by.Criteria);
100+
99101
Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters);
100102
return this.driver.GetElementFromResponse(commandResponse);
101103
}
@@ -107,26 +109,29 @@ public IWebElement FindElement(By by)
107109
/// <param name="by">The locating mechanism to use.</param>
108110
/// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see>
109111
/// 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>
110113
public ReadOnlyCollection<IWebElement> FindElements(By by)
111114
{
112-
if (by == null)
115+
if (by is null)
113116
{
114-
throw new ArgumentNullException(nameof(@by), "by cannot be null");
117+
throw new ArgumentNullException(nameof(by), "by cannot be null");
115118
}
116119

117120
Dictionary<string, object> parameters = new Dictionary<string, object>();
118121
parameters.Add("id", this.shadowRootId);
119122
parameters.Add("using", by.Mechanism);
120123
parameters.Add("value", by.Criteria);
124+
121125
Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters);
122126
return this.driver.GetElementsFromResponse(commandResponse);
123127
}
124128

125129
Dictionary<string, object> IWebDriverObjectReference.ToDictionary()
126130
{
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+
};
130135
}
131136
}
132137
}

Diff for: dotnet/src/webdriver/WebDriver.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,9 @@ private object ParseJavaScriptReturnValue(object responseValue)
980980
{
981981
returnValue = this.elementFactory.CreateElement(resultAsDictionary);
982982
}
983-
else if (ShadowRoot.ContainsShadowRootReference(resultAsDictionary))
983+
else if (ShadowRoot.TryCreate(this, resultAsDictionary, out ShadowRoot shadowRoot))
984984
{
985-
returnValue = ShadowRoot.FromDictionary(this, resultAsDictionary);
985+
returnValue = shadowRoot;
986986
}
987987
else
988988
{

0 commit comments

Comments
 (0)