Skip to content

Commit 98b22f8

Browse files
[dotnet] Add nullability to Manage() (#15210)
* [dotnet] Add nullability to `Manage()` * Reference langword null on navigation
1 parent 9a3390d commit 98b22f8

File tree

8 files changed

+55
-64
lines changed

8 files changed

+55
-64
lines changed

dotnet/src/webdriver/INavigation.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public interface INavigation
6666
/// should the underlying page change while your test is executing the results of
6767
/// future calls against this interface will be against the freshly loaded page.
6868
/// </remarks>
69-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
69+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
7070
void GoToUrl(string url);
7171

7272
/// <summary>
7373
/// Navigate to a url as an asynchronous task.
7474
/// </summary>
7575
/// <param name="url">String of where you want the browser to go.</param>
7676
/// <returns>A task object representing the asynchronous operation.</returns>
77-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
77+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
7878
Task GoToUrlAsync(string url);
7979

8080
/// <summary>
@@ -90,15 +90,15 @@ public interface INavigation
9090
/// should the underlying page change while your test is executing the results of
9191
/// future calls against this interface will be against the freshly loaded page.
9292
/// </remarks>
93-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
93+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
9494
void GoToUrl(Uri url);
9595

9696
/// <summary>
9797
/// Navigate to a url as an asynchronous task.
9898
/// </summary>
9999
/// <param name="url">Uri object of where you want the browser to go.</param>
100100
/// <returns>A task object representing the asynchronous operation.</returns>
101-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
101+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
102102
Task GoToUrlAsync(Uri url);
103103

104104
/// <summary>

dotnet/src/webdriver/IOptions.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>

dotnet/src/webdriver/ITimeouts.cs

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

2020
using System;
2121

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

dotnet/src/webdriver/IWindow.cs

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

2020
using System.Drawing;
2121

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

dotnet/src/webdriver/Navigator.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task ForwardAsync()
8686
/// Navigate to a url.
8787
/// </summary>
8888
/// <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>
89+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
9090
public void GoToUrl(string url)
9191
{
9292
Task.Run(async delegate
@@ -100,7 +100,7 @@ public void GoToUrl(string url)
100100
/// </summary>
101101
/// <param name="url">String of where you want the browser to go.</param>
102102
/// <returns>A task object representing the asynchronous operation.</returns>
103-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
103+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
104104
public async Task GoToUrlAsync(string url)
105105
{
106106
if (url == null)
@@ -119,7 +119,7 @@ public async Task GoToUrlAsync(string url)
119119
/// Navigate to a url.
120120
/// </summary>
121121
/// <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>
122+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
123123
public void GoToUrl(Uri url)
124124
{
125125
Task.Run(async delegate
@@ -133,7 +133,7 @@ public void GoToUrl(Uri url)
133133
/// </summary>
134134
/// <param name="url">Uri object of where you want the browser to go.</param>
135135
/// <returns>A task object representing the asynchronous operation.</returns>
136-
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is null.</exception>
136+
/// <exception cref="ArgumentNullException">If <paramref name="url"/> is <see langword="null"/>.</exception>
137137
public async Task GoToUrlAsync(Uri url)
138138
{
139139
if (url == null)

dotnet/src/webdriver/OptionsManager.cs

+9-19
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,42 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium
2123
{
2224
/// <summary>
2325
/// Provides a mechanism for setting options needed for the driver during the test.
2426
/// </summary>
25-
internal class OptionsManager : IOptions
27+
internal sealed class OptionsManager : IOptions
2628
{
27-
private WebDriver driver;
29+
private readonly WebDriver driver;
2830

2931
/// <summary>
3032
/// Initializes a new instance of the <see cref="OptionsManager"/> class
3133
/// </summary>
3234
/// <param name="driver">Instance of the driver currently in use</param>
3335
public OptionsManager(WebDriver driver)
3436
{
35-
this.driver = driver;
37+
this.driver = driver ?? throw new System.ArgumentNullException(nameof(driver));
3638
}
3739

3840
/// <summary>
3941
/// Gets an object allowing the user to manipulate cookies on the page.
4042
/// </summary>
41-
public ICookieJar Cookies
42-
{
43-
get { return new CookieJar(this.driver); }
44-
}
43+
public ICookieJar Cookies => new CookieJar(this.driver);
4544

4645
/// <summary>
4746
/// Gets an object allowing the user to manipulate the currently-focused browser window.
4847
/// </summary>
4948
/// <remarks>"Currently-focused" is defined as the browser window having the window handle
5049
/// returned when IWebDriver.CurrentWindowHandle is called.</remarks>
51-
public IWindow Window
52-
{
53-
get { return new Window(this.driver); }
54-
}
50+
public IWindow Window => new Window(this.driver);
5551

5652
/// <summary>
5753
/// Gets an object allowing the user to examine the logs of the current driver instance.
5854
/// </summary>
59-
public ILogs Logs
60-
{
61-
get { return new Logs(this.driver); }
62-
}
55+
public ILogs Logs => new Logs(this.driver);
6356

6457
/// <summary>
6558
/// Provides access to the timeouts defined for this driver.
@@ -70,9 +63,6 @@ public ITimeouts Timeouts()
7063
return new Timeouts(this.driver);
7164
}
7265

73-
public INetwork Network
74-
{
75-
get { return this.driver.Network; }
76-
}
66+
public INetwork Network => this.driver.Network;
7767
}
7868
}

dotnet/src/webdriver/Timeouts.cs

+18-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using System.Globalization;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium
2527
{
2628
/// <summary>
@@ -33,19 +35,19 @@ internal class Timeouts : ITimeouts
3335
private const string PageLoadTimeoutName = "pageLoad";
3436
private const string LegacyPageLoadTimeoutName = "page load";
3537

36-
private readonly TimeSpan DefaultImplicitWaitTimeout = TimeSpan.FromSeconds(0);
37-
private readonly TimeSpan DefaultAsyncScriptTimeout = TimeSpan.FromSeconds(30);
38-
private readonly TimeSpan DefaultPageLoadTimeout = TimeSpan.FromSeconds(300);
38+
private static readonly TimeSpan DefaultImplicitWaitTimeout = TimeSpan.FromSeconds(0);
39+
private static readonly TimeSpan DefaultAsyncScriptTimeout = TimeSpan.FromSeconds(30);
40+
private static readonly TimeSpan DefaultPageLoadTimeout = TimeSpan.FromSeconds(300);
3941

40-
private WebDriver driver;
42+
private readonly WebDriver driver;
4143

4244
/// <summary>
4345
/// Initializes a new instance of the <see cref="Timeouts"/> class
4446
/// </summary>
4547
/// <param name="driver">The driver that is currently in use</param>
4648
public Timeouts(WebDriver driver)
4749
{
48-
this.driver = driver;
50+
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
4951
}
5052

5153
/// <summary>
@@ -70,8 +72,8 @@ public Timeouts(WebDriver driver)
7072
/// </remarks>
7173
public TimeSpan ImplicitWait
7274
{
73-
get { return this.ExecuteGetTimeout(ImplicitTimeoutName); }
74-
set { this.ExecuteSetTimeout(ImplicitTimeoutName, value); }
75+
get => this.ExecuteGetTimeout(ImplicitTimeoutName);
76+
set => this.ExecuteSetTimeout(ImplicitTimeoutName, value);
7577
}
7678

7779
/// <summary>
@@ -87,8 +89,8 @@ public TimeSpan ImplicitWait
8789
/// </remarks>
8890
public TimeSpan AsynchronousJavaScript
8991
{
90-
get { return this.ExecuteGetTimeout(AsyncScriptTimeoutName); }
91-
set { this.ExecuteSetTimeout(AsyncScriptTimeoutName, value); }
92+
get => this.ExecuteGetTimeout(AsyncScriptTimeoutName);
93+
set => this.ExecuteSetTimeout(AsyncScriptTimeoutName, value);
9294
}
9395

9496
/// <summary>
@@ -103,29 +105,21 @@ public TimeSpan AsynchronousJavaScript
103105
/// </remarks>
104106
public TimeSpan PageLoad
105107
{
106-
get
107-
{
108-
string timeoutName = PageLoadTimeoutName;
109-
return this.ExecuteGetTimeout(timeoutName);
110-
}
111-
112-
set
113-
{
114-
string timeoutName = PageLoadTimeoutName;
115-
this.ExecuteSetTimeout(timeoutName, value);
116-
}
108+
get => this.ExecuteGetTimeout(PageLoadTimeoutName);
109+
set => this.ExecuteSetTimeout(PageLoadTimeoutName, value);
117110
}
118111

119112
private TimeSpan ExecuteGetTimeout(string timeoutType)
120113
{
121114
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetTimeouts, null);
122-
Dictionary<string, object> responseValue = (Dictionary<string, object>)commandResponse.Value;
123-
if (!responseValue.ContainsKey(timeoutType))
115+
116+
Dictionary<string, object?> responseValue = (Dictionary<string, object?>)commandResponse.Value!;
117+
if (!responseValue.TryGetValue(timeoutType, out object? timeout))
124118
{
125119
throw new WebDriverException("Specified timeout type not defined");
126120
}
127121

128-
return TimeSpan.FromMilliseconds(Convert.ToDouble(responseValue[timeoutType], CultureInfo.InvariantCulture));
122+
return TimeSpan.FromMilliseconds(Convert.ToDouble(timeout, CultureInfo.InvariantCulture));
129123
}
130124

131125
private void ExecuteSetTimeout(string timeoutType, TimeSpan timeToWait)
@@ -149,6 +143,7 @@ private void ExecuteSetTimeout(string timeoutType, TimeSpan timeToWait)
149143

150144
Dictionary<string, object> parameters = new Dictionary<string, object>();
151145
parameters.Add(timeoutType, Convert.ToInt64(milliseconds));
146+
152147
this.driver.InternalExecute(DriverCommand.SetTimeouts, parameters);
153148
}
154149
}

dotnet/src/webdriver/Window.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
using System.Drawing;
2323
using System.Globalization;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium
2628
{
2729
/// <summary>
2830
/// Defines the interface through which the user can manipulate the browser window.
2931
/// </summary>
30-
internal class Window : IWindow
32+
internal sealed class Window : IWindow
3133
{
3234
private WebDriver driver;
3335

@@ -37,7 +39,7 @@ internal class Window : IWindow
3739
/// <param name="driver">Instance of the driver currently in use</param>
3840
public Window(WebDriver driver)
3941
{
40-
this.driver = driver;
42+
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
4143
}
4244

4345
/// <summary>
@@ -48,12 +50,12 @@ public Point Position
4850
{
4951
get
5052
{
51-
Response commandResponse;
52-
commandResponse = this.driver.InternalExecute(DriverCommand.GetWindowRect, null);
53+
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetWindowRect, null);
5354

54-
Dictionary<string, object> rawPosition = (Dictionary<string, object>)commandResponse.Value;
55+
Dictionary<string, object?> rawPosition = (Dictionary<string, object?>)commandResponse.Value!;
5556
int x = Convert.ToInt32(rawPosition["x"], CultureInfo.InvariantCulture);
5657
int y = Convert.ToInt32(rawPosition["y"], CultureInfo.InvariantCulture);
58+
5759
return new Point(x, y);
5860
}
5961

@@ -74,11 +76,12 @@ public Size Size
7476
{
7577
get
7678
{
77-
Response commandResponse;
78-
commandResponse = this.driver.InternalExecute(DriverCommand.GetWindowRect, null);
79-
Dictionary<string, object> rawPosition = (Dictionary<string, object>)commandResponse.Value;
79+
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetWindowRect, null);
80+
81+
Dictionary<string, object?> rawPosition = (Dictionary<string, object?>)commandResponse.Value!;
8082
int height = Convert.ToInt32(rawPosition["height"], CultureInfo.InvariantCulture);
8183
int width = Convert.ToInt32(rawPosition["width"], CultureInfo.InvariantCulture);
84+
8285
return new Size(width, height);
8386
}
8487

@@ -96,26 +99,23 @@ public Size Size
9699
/// </summary>
97100
public void Maximize()
98101
{
99-
Dictionary<string, object> parameters = null;
100-
this.driver.InternalExecute(DriverCommand.MaximizeWindow, parameters);
102+
this.driver.InternalExecute(DriverCommand.MaximizeWindow, null);
101103
}
102104

103105
/// <summary>
104106
/// Minimizes the current window if it is not already minimized.
105107
/// </summary>
106108
public void Minimize()
107109
{
108-
Dictionary<string, object> parameters = null;
109-
this.driver.InternalExecute(DriverCommand.MinimizeWindow, parameters);
110+
this.driver.InternalExecute(DriverCommand.MinimizeWindow, null);
110111
}
111112

112113
/// <summary>
113114
/// Sets the current window to full screen if it is not already in that state.
114115
/// </summary>
115116
public void FullScreen()
116117
{
117-
Dictionary<string, object> parameters = null;
118-
this.driver.InternalExecute(DriverCommand.FullScreenWindow, parameters);
118+
this.driver.InternalExecute(DriverCommand.FullScreenWindow, null);
119119
}
120120
}
121121
}

0 commit comments

Comments
 (0)