Skip to content

Commit 42d7dee

Browse files
[dotnet] Annotate nullability on Navigate() and SwitchTo() (#15211)
1 parent 07872eb commit 42d7dee

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

dotnet/src/webdriver/INavigation.cs

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.Threading.Tasks;
2222

23+
#nullable enable
24+
2325
namespace OpenQA.Selenium
2426
{
2527
/// <summary>
@@ -64,13 +66,15 @@ public interface INavigation
6466
/// should the underlying page change while your test is executing the results of
6567
/// future calls against this interface will be against the freshly loaded page.
6668
/// </remarks>
69+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
6770
void GoToUrl(string url);
6871

6972
/// <summary>
7073
/// Navigate to a url as an asynchronous task.
7174
/// </summary>
7275
/// <param name="url">String of where you want the browser to go.</param>
7376
/// <returns>A task object representing the asynchronous operation.</returns>
77+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
7478
Task GoToUrlAsync(string url);
7579

7680
/// <summary>
@@ -86,13 +90,15 @@ public interface INavigation
8690
/// should the underlying page change while your test is executing the results of
8791
/// future calls against this interface will be against the freshly loaded page.
8892
/// </remarks>
93+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
8994
void GoToUrl(Uri url);
9095

9196
/// <summary>
9297
/// Navigate to a url as an asynchronous task.
9398
/// </summary>
9499
/// <param name="url">Uri object of where you want the browser to go.</param>
95100
/// <returns>A task object representing the asynchronous operation.</returns>
101+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
96102
Task GoToUrlAsync(Uri url);
97103

98104
/// <summary>

dotnet/src/webdriver/ITargetLocator.cs

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

20+
#nullable enable
21+
22+
using System;
23+
2024
namespace OpenQA.Selenium
2125
{
2226
/// <summary>
@@ -38,6 +42,7 @@ public interface ITargetLocator
3842
/// <param name="frameName">The name of the frame to select.</param>
3943
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
4044
/// <exception cref="NoSuchFrameException">If the frame cannot be found.</exception>
45+
/// <exception cref="ArgumentNullException">If <paramref name="frameName"/> is <see langword="null"/>.</exception>
4146
IWebDriver Frame(string frameName);
4247

4348
/// <summary>
@@ -47,6 +52,8 @@ public interface ITargetLocator
4752
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
4853
/// <exception cref="NoSuchFrameException">If the element is neither a FRAME nor an IFRAME element.</exception>
4954
/// <exception cref="StaleElementReferenceException">If the element is no longer valid.</exception>
55+
/// <exception cref="ArgumentNullException">If <paramref name="frameElement"/> is <see langword="null"/>.</exception>
56+
/// <exception cref="ArgumentException">If <paramref name="frameElement"/> cannot be converted to an <see cref="IWebDriverObjectReference"/>.</exception>
5057
IWebDriver Frame(IWebElement frameElement);
5158

5259
/// <summary>
@@ -61,6 +68,7 @@ public interface ITargetLocator
6168
/// <param name="windowName">The name of the window to select.</param>
6269
/// <returns>An <see cref="IWebDriver"/> instance focused on the given window.</returns>
6370
/// <exception cref="NoSuchWindowException">If the window cannot be found.</exception>
71+
/// <exception cref="ArgumentNullException">If <paramref name="windowName"/> is <see langword="null"/>.</exception>
6472
IWebDriver Window(string windowName);
6573

6674
/// <summary>

dotnet/src/webdriver/Navigator.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121
using System.Collections.Generic;
2222
using System.Threading.Tasks;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium
2527
{
2628
/// <summary>
2729
/// Provides a mechanism for Navigating with the driver.
2830
/// </summary>
29-
internal class Navigator : INavigation
31+
internal sealed class Navigator : INavigation
3032
{
31-
private WebDriver driver;
33+
private readonly WebDriver driver;
3234

3335
/// <summary>
3436
/// Initializes a new instance of the <see cref="Navigator"/> class
3537
/// </summary>
3638
/// <param name="driver">Driver in use</param>
39+
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is null.</exception>
3740
public Navigator(WebDriver driver)
3841
{
39-
this.driver = driver;
42+
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
4043
}
4144

4245
/// <summary>
@@ -83,6 +86,7 @@ public async Task ForwardAsync()
8386
/// Navigate to a url.
8487
/// </summary>
8588
/// <param name="url">String of where you want the browser to go to</param>
89+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
8690
public void GoToUrl(string url)
8791
{
8892
Task.Run(async delegate
@@ -96,6 +100,7 @@ public void GoToUrl(string url)
96100
/// </summary>
97101
/// <param name="url">String of where you want the browser to go.</param>
98102
/// <returns>A task object representing the asynchronous operation.</returns>
103+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
99104
public async Task GoToUrlAsync(string url)
100105
{
101106
if (url == null)
@@ -114,6 +119,7 @@ public async Task GoToUrlAsync(string url)
114119
/// Navigate to a url.
115120
/// </summary>
116121
/// <param name="url">Uri object of where you want the browser to go.</param>
122+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
117123
public void GoToUrl(Uri url)
118124
{
119125
Task.Run(async delegate
@@ -127,6 +133,7 @@ public void GoToUrl(Uri url)
127133
/// </summary>
128134
/// <param name="url">Uri object of where you want the browser to go.</param>
129135
/// <returns>A task object representing the asynchronous operation.</returns>
136+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
130137
public async Task GoToUrlAsync(Uri url)
131138
{
132139
if (url == null)

dotnet/src/webdriver/TargetLocator.cs

+25-11
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
using System.Collections.ObjectModel;
2424
using System.Text.RegularExpressions;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium
2729
{
2830
/// <summary>
2931
/// Provides a mechanism for finding elements on the page with locators.
3032
/// </summary>
31-
internal class TargetLocator : ITargetLocator
33+
internal sealed class TargetLocator : ITargetLocator
3234
{
33-
private WebDriver driver;
35+
private readonly WebDriver driver;
3436

3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="TargetLocator"/> class
3739
/// </summary>
3840
/// <param name="driver">The driver that is currently in use</param>
41+
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
3942
public TargetLocator(WebDriver driver)
4043
{
41-
this.driver = driver;
44+
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
4245
}
4346

4447
/// <summary>
@@ -59,6 +62,7 @@ public IWebDriver Frame(int frameIndex)
5962
/// </summary>
6063
/// <param name="frameName">name of the frame</param>
6164
/// <returns>A WebDriver instance that is currently in use</returns>
65+
/// <exception cref="ArgumentNullException">If <paramref name="frameName"/> is <see langword="null"/>.</exception>
6266
public IWebDriver Frame(string frameName)
6367
{
6468
if (frameName == null)
@@ -85,26 +89,27 @@ public IWebDriver Frame(string frameName)
8589
/// </summary>
8690
/// <param name="frameElement">a previously found FRAME or IFRAME element.</param>
8791
/// <returns>A WebDriver instance that is currently in use.</returns>
92+
/// <exception cref="ArgumentNullException">If <paramref name="frameElement"/> is <see langword="null"/>.</exception>
93+
/// <exception cref="ArgumentException">If <paramref name="frameElement"/> cannot be converted to an <see cref="IWebDriverObjectReference"/>.</exception>
8894
public IWebDriver Frame(IWebElement frameElement)
8995
{
9096
if (frameElement == null)
9197
{
9298
throw new ArgumentNullException(nameof(frameElement), "Frame element cannot be null");
9399
}
94100

95-
IWebDriverObjectReference elementReference = frameElement as IWebDriverObjectReference;
101+
IWebDriverObjectReference? elementReference = frameElement as IWebDriverObjectReference;
96102
if (elementReference == null)
97103
{
98-
IWrapsElement elementWrapper = frameElement as IWrapsElement;
99-
if (elementWrapper != null)
104+
if (frameElement is IWrapsElement elementWrapper)
100105
{
101106
elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference;
102107
}
103108
}
104109

105110
if (elementReference == null)
106111
{
107-
throw new ArgumentException("frameElement cannot be converted to IWebElementReference", nameof(frameElement));
112+
throw new ArgumentException($"{nameof(frameElement)} cannot be converted to {nameof(IWebDriverObjectReference)}", nameof(frameElement));
108113
}
109114

110115
Dictionary<string, object> elementDictionary = elementReference.ToDictionary();
@@ -131,8 +136,14 @@ public IWebDriver ParentFrame()
131136
/// </summary>
132137
/// <param name="windowHandleOrName">Window handle or name of the window that you wish to move to</param>
133138
/// <returns>A WebDriver instance that is currently in use</returns>
139+
/// <exception cref="ArgumentNullException">If <paramref name="windowHandleOrName"/> is <see langword="null"/>.</exception>
134140
public IWebDriver Window(string windowHandleOrName)
135141
{
142+
if (windowHandleOrName is null)
143+
{
144+
throw new ArgumentNullException(nameof(windowHandleOrName));
145+
}
146+
136147
Dictionary<string, object> parameters = new Dictionary<string, object>();
137148
parameters.Add("handle", windowHandleOrName);
138149
try
@@ -143,7 +154,7 @@ public IWebDriver Window(string windowHandleOrName)
143154
catch (NoSuchWindowException)
144155
{
145156
// simulate search by name
146-
string original = null;
157+
string? original = null;
147158
try
148159
{
149160
original = this.driver.CurrentWindowHandle;
@@ -183,10 +194,13 @@ public IWebDriver NewWindow(WindowType typeHint)
183194
{
184195
Dictionary<string, object> parameters = new Dictionary<string, object>();
185196
parameters.Add("type", typeHint.ToString().ToLowerInvariant());
197+
186198
Response response = this.driver.InternalExecute(DriverCommand.NewWindow, parameters);
187-
Dictionary<string, object> result = response.Value as Dictionary<string, object>;
188-
string newWindowHandle = result["handle"].ToString();
199+
200+
Dictionary<string, object> result = (Dictionary<string, object>)response.Value!;
201+
string newWindowHandle = result["handle"].ToString()!;
189202
this.Window(newWindowHandle);
203+
190204
return this.driver;
191205
}
192206

@@ -196,7 +210,7 @@ public IWebDriver NewWindow(WindowType typeHint)
196210
/// <returns>Element of the default</returns>
197211
public IWebDriver DefaultContent()
198212
{
199-
Dictionary<string, object> parameters = new Dictionary<string, object>();
213+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
200214
parameters.Add("id", null);
201215
this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
202216
return this.driver;

0 commit comments

Comments
 (0)