From 0353a656a5a19567295e95b40b06900047623739 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 28 Oct 2024 18:05:43 -0400 Subject: [PATCH 01/14] [dotnet] Add NRT to `Manage()` and friends --- dotnet/src/webdriver/Alert.cs | 7 ++- dotnet/src/webdriver/Cookie.cs | 49 ++++++++++--------- dotnet/src/webdriver/CookieJar.cs | 26 ++++++---- dotnet/src/webdriver/ICookieJar.cs | 10 ++-- dotnet/src/webdriver/ILogs.cs | 4 ++ dotnet/src/webdriver/INetwork.cs | 7 ++- dotnet/src/webdriver/IOptions.cs | 2 + dotnet/src/webdriver/ITargetLocator.cs | 3 ++ dotnet/src/webdriver/ITimeouts.cs | 2 + dotnet/src/webdriver/IWindow.cs | 2 + .../src/webdriver/Internal/ReturnedCookie.cs | 6 ++- dotnet/src/webdriver/LogEntry.cs | 21 +++++--- dotnet/src/webdriver/Logs.cs | 16 ++++-- dotnet/src/webdriver/NetworkManager.cs | 11 +++-- dotnet/src/webdriver/OptionsManager.cs | 2 + dotnet/src/webdriver/TargetLocator.cs | 26 +++++++--- dotnet/src/webdriver/Timeouts.cs | 2 + dotnet/src/webdriver/Window.cs | 8 +-- 18 files changed, 140 insertions(+), 64 deletions(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index 843372dad9834..cac46bd8cee8f 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -19,6 +19,8 @@ using System; using System.Collections.Generic; +#nullable enable + namespace OpenQA.Selenium { /// @@ -26,7 +28,7 @@ namespace OpenQA.Selenium /// internal class Alert : IAlert { - private WebDriver driver; + private readonly WebDriver driver; /// /// Initializes a new instance of the class. @@ -45,7 +47,7 @@ public string Text get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return commandResponse.Value.ToString(); + return commandResponse.Value.ToString()!; } } @@ -69,6 +71,7 @@ public void Accept() /// Sends keys to the alert. /// /// The keystrokes to send. + /// If is null. public void SendKeys(string keysToSend) { if (keysToSend == null) diff --git a/dotnet/src/webdriver/Cookie.cs b/dotnet/src/webdriver/Cookie.cs index 5b28cd46b7ff6..08e2d7a111b20 100644 --- a/dotnet/src/webdriver/Cookie.cs +++ b/dotnet/src/webdriver/Cookie.cs @@ -23,6 +23,8 @@ using System.Linq; using System.Text.Json.Serialization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -33,9 +35,9 @@ public class Cookie { private string cookieName; private string cookieValue; - private string cookiePath; - private string cookieDomain; - private string sameSite; + private string? cookiePath; + private string? cookieDomain; + private string? sameSite; private bool isHttpOnly; private bool secure; private DateTime? cookieExpiry; @@ -64,7 +66,7 @@ public Cookie(string name, string value) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string path) + public Cookie(string name, string value, string? path) : this(name, value, path, null) { } @@ -80,7 +82,7 @@ public Cookie(string name, string value, string path) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string path, DateTime? expiry) + public Cookie(string name, string value, string? path, DateTime? expiry) : this(name, value, null, path, expiry) { } @@ -97,7 +99,7 @@ public Cookie(string name, string value, string path, DateTime? expiry) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string domain, string path, DateTime? expiry) + public Cookie(string name, string value, string? domain, string? path, DateTime? expiry) : this(name, value, domain, path, expiry, false, false, null) { } @@ -117,7 +119,7 @@ public Cookie(string name, string value, string domain, string path, DateTime? e /// If the name and value are both an empty string, /// if the name contains a semi-colon, or if same site value is not valid. /// If the name, value or currentUrl is . - public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite) + public Cookie(string name, string value, string? domain, string? path, DateTime? expiry, bool secure, bool isHttpOnly, string? sameSite) { if (name == null) { @@ -190,7 +192,7 @@ public string Value /// [JsonPropertyName("domain")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string Domain + public string? Domain { get { return this.cookieDomain; } } @@ -200,7 +202,7 @@ public string Domain /// [JsonPropertyName("path")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public virtual string Path + public virtual string? Path { get { return this.cookiePath; } } @@ -229,7 +231,7 @@ public virtual bool IsHttpOnly /// [JsonPropertyName("sameSite")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public virtual string SameSite + public virtual string? SameSite { get { return this.sameSite; } } @@ -272,6 +274,7 @@ internal long? ExpirySeconds /// /// The Dictionary object containing the cookie parameters. /// A object with the proper parameters set. + /// If is null. public static Cookie FromDictionary(Dictionary rawCookie) { if (rawCookie == null) @@ -279,20 +282,20 @@ public static Cookie FromDictionary(Dictionary rawCookie) throw new ArgumentNullException(nameof(rawCookie), "Dictionary cannot be null"); } - string name = rawCookie["name"].ToString(); + string name = rawCookie["name"].ToString()!; string value = string.Empty; if (rawCookie["value"] != null) { - value = rawCookie["value"].ToString(); + value = rawCookie["value"].ToString()!; } - string path = "/"; + string? path = "/"; if (rawCookie.ContainsKey("path") && rawCookie["path"] != null) { path = rawCookie["path"].ToString(); } - string domain = string.Empty; + string? domain = string.Empty; if (rawCookie.ContainsKey("domain") && rawCookie["domain"] != null) { domain = rawCookie["domain"].ToString(); @@ -307,16 +310,16 @@ public static Cookie FromDictionary(Dictionary rawCookie) bool secure = false; if (rawCookie.ContainsKey("secure") && rawCookie["secure"] != null) { - secure = bool.Parse(rawCookie["secure"].ToString()); + secure = bool.Parse(rawCookie["secure"].ToString()!); } bool isHttpOnly = false; if (rawCookie.ContainsKey("httpOnly") && rawCookie["httpOnly"] != null) { - isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString()); + isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString()!); } - string sameSite = null; + string? sameSite = null; if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null) { sameSite = rawCookie["sameSite"].ToString(); @@ -347,10 +350,10 @@ public override string ToString() /// if the specified Object /// is equal to the current Object; otherwise, /// . - public override bool Equals(object obj) + public override bool Equals(object? obj) { // Two cookies are equal if the name and value match - Cookie cookie = obj as Cookie; + Cookie? cookie = obj as Cookie; if (this == obj) { @@ -367,7 +370,7 @@ public override bool Equals(object obj) return false; } - return !(this.cookieValue != null ? !this.cookieValue.Equals(cookie.cookieValue) : cookie.Value != null); + return string.Equals(this.cookieValue, cookie.cookieValue); } /// @@ -379,12 +382,12 @@ public override int GetHashCode() return this.cookieName.GetHashCode(); } - private static string StripPort(string domain) + private static string? StripPort(string? domain) { - return string.IsNullOrEmpty(domain) ? null : domain.Split(':')[0]; + return string.IsNullOrEmpty(domain) ? null : domain!.Split(':')[0]; } - private static DateTime? ConvertExpirationTime(string expirationTime) + private static DateTime? ConvertExpirationTime(string? expirationTime) { DateTime? expires = null; double seconds = 0; diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index be698efa93a11..d6aca00e3e200 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -20,6 +20,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -27,7 +29,7 @@ namespace OpenQA.Selenium /// internal class CookieJar : ICookieJar { - private WebDriver driver; + private readonly WebDriver driver; /// /// Initializes a new instance of the class. @@ -50,8 +52,14 @@ public ReadOnlyCollection AllCookies /// Method for creating a cookie in the browser /// /// that represents a cookie in the browser + /// If is . public void AddCookie(Cookie cookie) { + if (cookie == null) + { + throw new ArgumentNullException(nameof(cookie)); + } + Dictionary parameters = new Dictionary(); parameters.Add("cookie", cookie); this.driver.InternalExecute(DriverCommand.AddCookie, parameters); @@ -61,9 +69,9 @@ public void AddCookie(Cookie cookie) /// Delete the cookie by passing in the name of the cookie /// /// The name of the cookie that is in the browser - public void DeleteCookieNamed(string name) + public void DeleteCookieNamed(string? name) { - Dictionary parameters = new Dictionary(); + Dictionary parameters = new Dictionary(); parameters.Add("name", name); this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); } @@ -72,7 +80,7 @@ public void DeleteCookieNamed(string name) /// Delete a cookie in the browser by passing in a copy of a cookie /// /// An object that represents a copy of the cookie that needs to be deleted - public void DeleteCookie(Cookie cookie) + public void DeleteCookie(Cookie? cookie) { if (cookie != null) { @@ -92,10 +100,10 @@ public void DeleteAllCookies() /// Method for returning a getting a cookie by name /// /// name of the cookie that needs to be returned - /// A Cookie from the name - public Cookie GetCookieNamed(string name) + /// A Cookie from the name, or if not found. + public Cookie? GetCookieNamed(string? name) { - Cookie cookieToReturn = null; + Cookie? cookieToReturn = null; if (name != null) { ReadOnlyCollection allCookies = this.AllCookies; @@ -123,12 +131,12 @@ private ReadOnlyCollection GetAllCookies() try { - object[] cookies = returned as object[]; + object[]? cookies = returned as object[]; if (cookies != null) { foreach (object rawCookie in cookies) { - Dictionary cookieDictionary = rawCookie as Dictionary; + Dictionary cookieDictionary = (Dictionary)rawCookie; if (rawCookie != null) { toReturn.Add(Cookie.FromDictionary(cookieDictionary)); diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index 82b919a33959e..3655b801382ef 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -16,8 +16,11 @@ // limitations under the License. // +using System; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -34,6 +37,7 @@ public interface ICookieJar /// Adds a cookie to the current page. /// /// The object to be added. + /// If is . void AddCookie(Cookie cookie); /// @@ -42,19 +46,19 @@ public interface ICookieJar /// The name of the cookie to retrieve. /// The containing the name. Returns /// if no cookie with the specified name is found. - Cookie GetCookieNamed(string name); + Cookie? GetCookieNamed(string? name); /// /// Deletes the specified cookie from the page. /// /// The to be deleted. - void DeleteCookie(Cookie cookie); + void DeleteCookie(Cookie? cookie); /// /// Deletes the cookie with the specified name from the page. /// /// The name of the cookie to be deleted. - void DeleteCookieNamed(string name); + void DeleteCookieNamed(string? name); /// /// Deletes all cookies from the page. diff --git a/dotnet/src/webdriver/ILogs.cs b/dotnet/src/webdriver/ILogs.cs index 89a00aa0a39f5..ab752b7b2c697 100644 --- a/dotnet/src/webdriver/ILogs.cs +++ b/dotnet/src/webdriver/ILogs.cs @@ -16,8 +16,11 @@ // limitations under the License. // +using System; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -36,6 +39,7 @@ public interface ILogs /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. + /// If is . ReadOnlyCollection GetLog(string logKind); } } diff --git a/dotnet/src/webdriver/INetwork.cs b/dotnet/src/webdriver/INetwork.cs index a9a4eb2f0b2b4..0dea1843ee110 100644 --- a/dotnet/src/webdriver/INetwork.cs +++ b/dotnet/src/webdriver/INetwork.cs @@ -19,6 +19,8 @@ using System; using System.Threading.Tasks; +#nullable enable + namespace OpenQA.Selenium { /// @@ -29,18 +31,19 @@ public interface INetwork /// /// Occurs when a browser sends a network request. /// - event EventHandler NetworkRequestSent; + event EventHandler? NetworkRequestSent; /// /// Occurs when a browser receives a network response. /// - event EventHandler NetworkResponseReceived; + event EventHandler? NetworkResponseReceived; /// /// Adds a to examine incoming network requests, /// and optionally modify the request or provide a response. /// /// The to add. + /// If is . void AddRequestHandler(NetworkRequestHandler handler); /// diff --git a/dotnet/src/webdriver/IOptions.cs b/dotnet/src/webdriver/IOptions.cs index 2cac7344a7265..347ee5ebd2fb3 100644 --- a/dotnet/src/webdriver/IOptions.cs +++ b/dotnet/src/webdriver/IOptions.cs @@ -16,6 +16,8 @@ // limitations under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/ITargetLocator.cs b/dotnet/src/webdriver/ITargetLocator.cs index ed299ef8e8c25..62407a1c77c09 100644 --- a/dotnet/src/webdriver/ITargetLocator.cs +++ b/dotnet/src/webdriver/ITargetLocator.cs @@ -16,6 +16,8 @@ // limitations under the License. // +using System; + namespace OpenQA.Selenium { /// @@ -59,6 +61,7 @@ public interface ITargetLocator /// /// The name of the window to select. /// An instance focused on the given window. + /// If is . /// If the window cannot be found. IWebDriver Window(string windowName); diff --git a/dotnet/src/webdriver/ITimeouts.cs b/dotnet/src/webdriver/ITimeouts.cs index 40e64a4bf7b60..f1c35f4d55aea 100644 --- a/dotnet/src/webdriver/ITimeouts.cs +++ b/dotnet/src/webdriver/ITimeouts.cs @@ -18,6 +18,8 @@ using System; +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/IWindow.cs b/dotnet/src/webdriver/IWindow.cs index 362cc99d574a3..c1101dd17413f 100644 --- a/dotnet/src/webdriver/IWindow.cs +++ b/dotnet/src/webdriver/IWindow.cs @@ -18,6 +18,8 @@ using System.Drawing; +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Internal/ReturnedCookie.cs b/dotnet/src/webdriver/Internal/ReturnedCookie.cs index cc2283f505da1..0902bf8b6efd3 100644 --- a/dotnet/src/webdriver/Internal/ReturnedCookie.cs +++ b/dotnet/src/webdriver/Internal/ReturnedCookie.cs @@ -19,6 +19,8 @@ using System; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium.Internal { /// @@ -41,7 +43,7 @@ public class ReturnedCookie : Cookie /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value or currentUrl is . - public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly) + public ReturnedCookie(string name, string value, string? domain, string? path, DateTime? expiry, bool isSecure, bool isHttpOnly) : this(name, value, domain, path, expiry, isSecure, isHttpOnly, null) { } @@ -61,7 +63,7 @@ public ReturnedCookie(string name, string value, string domain, string path, Dat /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value or currentUrl is . - public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly, string sameSite) + public ReturnedCookie(string name, string value, string? domain, string? path, DateTime? expiry, bool isSecure, bool isHttpOnly, string? sameSite) : base(name, value, domain, path, expiry, isSecure, isHttpOnly, sameSite) { diff --git a/dotnet/src/webdriver/LogEntry.cs b/dotnet/src/webdriver/LogEntry.cs index a33ad6ba866db..84ccf5961e00e 100644 --- a/dotnet/src/webdriver/LogEntry.cs +++ b/dotnet/src/webdriver/LogEntry.cs @@ -20,6 +20,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -29,7 +31,7 @@ public class LogEntry { private LogLevel level = LogLevel.All; private DateTime timestamp = DateTime.MinValue; - private string message = string.Empty; + private string? message = string.Empty; /// /// Initializes a new instance of the class. @@ -57,7 +59,7 @@ public LogLevel Level /// /// Gets the message of the log entry. /// - public string Message + public string? Message { get { return this.message; } } @@ -77,8 +79,14 @@ public override string ToString() /// The from /// which to create the . /// A with the values in the dictionary. + /// If is . internal static LogEntry FromDictionary(Dictionary entryDictionary) { + if (entryDictionary == null) + { + throw new ArgumentNullException(nameof(entryDictionary)); + } + LogEntry entry = new LogEntry(); if (entryDictionary.ContainsKey("message")) { @@ -94,12 +102,13 @@ internal static LogEntry FromDictionary(Dictionary entryDictiona if (entryDictionary.ContainsKey("level")) { - string levelValue = entryDictionary["level"].ToString(); - try + string? levelValue = entryDictionary["level"].ToString(); + + if (Enum.TryParse(levelValue, ignoreCase: true, out LogLevel level)) { - entry.level = (LogLevel)Enum.Parse(typeof(LogLevel), levelValue, true); + entry.level = level; } - catch (ArgumentException) + else { // If the requested log level string is not a valid log level, // ignore it and use LogLevel.All. diff --git a/dotnet/src/webdriver/Logs.cs b/dotnet/src/webdriver/Logs.cs index 6c903dccc9efd..3894e8bae82e8 100644 --- a/dotnet/src/webdriver/Logs.cs +++ b/dotnet/src/webdriver/Logs.cs @@ -20,6 +20,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -49,12 +51,12 @@ public ReadOnlyCollection AvailableLogTypes try { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAvailableLogTypes, null); - object[] responseValue = commandResponse.Value as object[]; + object[]? responseValue = commandResponse.Value as object[]; if (responseValue != null) { foreach (object logKind in responseValue) { - availableLogTypes.Add(logKind.ToString()); + availableLogTypes.Add(logKind.ToString()!); } } } @@ -73,20 +75,26 @@ public ReadOnlyCollection AvailableLogTypes /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. + /// If is null. public ReadOnlyCollection GetLog(string logKind) { + if (logKind == null) + { + throw new ArgumentNullException(nameof(logKind)); + } + List entries = new List(); Dictionary parameters = new Dictionary(); parameters.Add("type", logKind); Response commandResponse = this.driver.InternalExecute(DriverCommand.GetLog, parameters); - object[] responseValue = commandResponse.Value as object[]; + object[]? responseValue = commandResponse.Value as object[]; if (responseValue != null) { foreach (object rawEntry in responseValue) { - Dictionary entryDictionary = rawEntry as Dictionary; + Dictionary entryDictionary = (Dictionary)rawEntry; if (entryDictionary != null) { entries.Add(LogEntry.FromDictionary(entryDictionary)); diff --git a/dotnet/src/webdriver/NetworkManager.cs b/dotnet/src/webdriver/NetworkManager.cs index 7d3131c7a6114..2ee46c46e784b 100644 --- a/dotnet/src/webdriver/NetworkManager.cs +++ b/dotnet/src/webdriver/NetworkManager.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Threading.Tasks; +#nullable enable + namespace OpenQA.Selenium { /// @@ -44,7 +46,7 @@ public NetworkManager(IWebDriver driver) // StartMonitoring(). this.session = new Lazy(() => { - IDevTools devToolsDriver = driver as IDevTools; + IDevTools? devToolsDriver = driver as IDevTools; if (devToolsDriver == null) { throw new WebDriverException("Driver must implement IDevTools to use these features"); @@ -57,12 +59,12 @@ public NetworkManager(IWebDriver driver) /// /// Occurs when a browser sends a network request. /// - public event EventHandler NetworkRequestSent; + public event EventHandler? NetworkRequestSent; /// /// Occurs when a browser receives a network response. /// - public event EventHandler NetworkResponseReceived; + public event EventHandler? NetworkResponseReceived; /// /// Asynchronously starts monitoring for network traffic. @@ -95,6 +97,7 @@ public async Task StopMonitoring() /// and optionally modify the request or provide a response. /// /// The to add. + /// If is null. public void AddRequestHandler(NetworkRequestHandler handler) { if (handler == null) @@ -199,7 +202,7 @@ private async Task OnAuthRequired(object sender, AuthRequiredEventArgs e) { if (authenticationHandler.UriMatcher.Invoke(uri)) { - PasswordCredentials credentials = authenticationHandler.Credentials as PasswordCredentials; + PasswordCredentials credentials = (PasswordCredentials)authenticationHandler.Credentials; await this.session.Value.Domains.Network.ContinueWithAuth(e.RequestId, credentials.UserName, credentials.Password).ConfigureAwait(false); successfullyAuthenticated = true; break; diff --git a/dotnet/src/webdriver/OptionsManager.cs b/dotnet/src/webdriver/OptionsManager.cs index 6f0b6155b2875..e14e5c517b5b5 100644 --- a/dotnet/src/webdriver/OptionsManager.cs +++ b/dotnet/src/webdriver/OptionsManager.cs @@ -16,6 +16,8 @@ // limitations under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/TargetLocator.cs b/dotnet/src/webdriver/TargetLocator.cs index 60652a620b232..9bf2d0e7f5849 100644 --- a/dotnet/src/webdriver/TargetLocator.cs +++ b/dotnet/src/webdriver/TargetLocator.cs @@ -22,6 +22,8 @@ using System.Collections.ObjectModel; using System.Text.RegularExpressions; +#nullable enable + namespace OpenQA.Selenium { /// @@ -58,6 +60,8 @@ public IWebDriver Frame(int frameIndex) /// /// name of the frame /// A WebDriver instance that is currently in use + /// If is null. + /// If a non-existent iFrame is queried for. public IWebDriver Frame(string frameName) { if (frameName == null) @@ -84,6 +88,8 @@ public IWebDriver Frame(string frameName) /// /// a previously found FRAME or IFRAME element. /// A WebDriver instance that is currently in use. + /// If is null. + /// If is an invalid object reference. public IWebDriver Frame(IWebElement frameElement) { if (frameElement == null) @@ -91,10 +97,10 @@ public IWebDriver Frame(IWebElement frameElement) throw new ArgumentNullException(nameof(frameElement), "Frame element cannot be null"); } - IWebDriverObjectReference elementReference = frameElement as IWebDriverObjectReference; + IWebDriverObjectReference? elementReference = frameElement as IWebDriverObjectReference; if (elementReference == null) { - IWrapsElement elementWrapper = frameElement as IWrapsElement; + IWrapsElement? elementWrapper = frameElement as IWrapsElement; if (elementWrapper != null) { elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; @@ -130,8 +136,15 @@ public IWebDriver ParentFrame() /// /// Window handle or name of the window that you wish to move to /// A WebDriver instance that is currently in use + /// If is . + /// If no window matching the provided handle or name exists. public IWebDriver Window(string windowHandleOrName) { + if (windowHandleOrName == null) + { + throw new ArgumentNullException(nameof(windowHandleOrName)); + } + Dictionary parameters = new Dictionary(); parameters.Add("handle", windowHandleOrName); try @@ -142,7 +155,7 @@ public IWebDriver Window(string windowHandleOrName) catch (NoSuchWindowException) { // simulate search by name - string original = null; + string? original = null; try { original = this.driver.CurrentWindowHandle; @@ -183,8 +196,8 @@ public IWebDriver NewWindow(WindowType typeHint) Dictionary parameters = new Dictionary(); parameters.Add("type", typeHint.ToString().ToLowerInvariant()); Response response = this.driver.InternalExecute(DriverCommand.NewWindow, parameters); - Dictionary result = response.Value as Dictionary; - string newWindowHandle = result["handle"].ToString(); + Dictionary result = (Dictionary)response.Value; + string newWindowHandle = result["handle"].ToString()!; this.Window(newWindowHandle); return this.driver; } @@ -195,7 +208,7 @@ public IWebDriver NewWindow(WindowType typeHint) /// Element of the default public IWebDriver DefaultContent() { - Dictionary parameters = new Dictionary(); + Dictionary parameters = new Dictionary(); parameters.Add("id", null); this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters); return this.driver; @@ -215,6 +228,7 @@ public IWebElement ActiveElement() /// Switches to the currently active modal dialog for this particular driver instance. /// /// A handle to the dialog. + /// No alert is present. public IAlert Alert() { // N.B. We only execute the GetAlertText command to be able to throw diff --git a/dotnet/src/webdriver/Timeouts.cs b/dotnet/src/webdriver/Timeouts.cs index ee83696c0401a..bafa40a5e6317 100644 --- a/dotnet/src/webdriver/Timeouts.cs +++ b/dotnet/src/webdriver/Timeouts.cs @@ -20,6 +20,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Window.cs b/dotnet/src/webdriver/Window.cs index 036f00a7da0ae..32f0f8f5ab6a3 100644 --- a/dotnet/src/webdriver/Window.cs +++ b/dotnet/src/webdriver/Window.cs @@ -21,6 +21,8 @@ using System.Drawing; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -95,7 +97,7 @@ public Size Size /// public void Maximize() { - Dictionary parameters = null; + Dictionary? parameters = null; this.driver.InternalExecute(DriverCommand.MaximizeWindow, parameters); } @@ -104,7 +106,7 @@ public void Maximize() /// public void Minimize() { - Dictionary parameters = null; + Dictionary? parameters = null; this.driver.InternalExecute(DriverCommand.MinimizeWindow, parameters); } @@ -113,7 +115,7 @@ public void Minimize() /// public void FullScreen() { - Dictionary parameters = null; + Dictionary? parameters = null; this.driver.InternalExecute(DriverCommand.FullScreenWindow, parameters); } } From 184bd6a5b9748f6c3e750f0351af03f2402e55dd Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 28 Oct 2024 18:06:34 -0400 Subject: [PATCH 02/14] Fix XML DOC --- dotnet/src/webdriver/Alert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index cac46bd8cee8f..74f8ac67e904d 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -71,7 +71,7 @@ public void Accept() /// Sends keys to the alert. /// /// The keystrokes to send. - /// If is null. + /// If is . public void SendKeys(string keysToSend) { if (keysToSend == null) From fff58e43a82b9fe4c135859e903c6aff49316bed Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 28 Oct 2024 18:18:23 -0400 Subject: [PATCH 03/14] Fix CookieJar.GetAllCookies() --- dotnet/src/webdriver/CookieJar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index d6aca00e3e200..d9928a9532ab0 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -136,9 +136,9 @@ private ReadOnlyCollection GetAllCookies() { foreach (object rawCookie in cookies) { - Dictionary cookieDictionary = (Dictionary)rawCookie; if (rawCookie != null) { + Dictionary cookieDictionary = (Dictionary)rawCookie; toReturn.Add(Cookie.FromDictionary(cookieDictionary)); } } From eaae0e69e410daf20372ed3ee4ee31e620238928 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 28 Oct 2024 18:19:55 -0400 Subject: [PATCH 04/14] Fix Logs.GetLog(string) --- dotnet/src/webdriver/Logs.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Logs.cs b/dotnet/src/webdriver/Logs.cs index 3894e8bae82e8..0be03a6a88b35 100644 --- a/dotnet/src/webdriver/Logs.cs +++ b/dotnet/src/webdriver/Logs.cs @@ -94,7 +94,7 @@ public ReadOnlyCollection GetLog(string logKind) { foreach (object rawEntry in responseValue) { - Dictionary entryDictionary = (Dictionary)rawEntry; + Dictionary? entryDictionary = rawEntry as Dictionary; if (entryDictionary != null) { entries.Add(LogEntry.FromDictionary(entryDictionary)); From b0a1401a1c2169fdca265b7b8219ef09861291cd Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 00:45:38 -0500 Subject: [PATCH 05/14] Fix nullability of `Alert` --- dotnet/src/webdriver/Alert.cs | 4 ++-- dotnet/src/webdriver/IAlert.cs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index f1f99eded9ee3..7a3e643d8b436 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -43,12 +43,12 @@ public Alert(WebDriver driver) /// /// Gets the text of the alert. /// - public string Text + public string? Text { get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return commandResponse.Value.ToString()!; + return (string?)commandResponse.Value; } } diff --git a/dotnet/src/webdriver/IAlert.cs b/dotnet/src/webdriver/IAlert.cs index 6dd0c8db63e99..78c15d992c1b3 100644 --- a/dotnet/src/webdriver/IAlert.cs +++ b/dotnet/src/webdriver/IAlert.cs @@ -17,6 +17,10 @@ // under the License. // +using System; + +#nullable enable + namespace OpenQA.Selenium { /// @@ -27,7 +31,7 @@ public interface IAlert /// /// Gets the text of the alert. /// - string Text { get; } + string? Text { get; } /// /// Dismisses the alert. @@ -43,6 +47,7 @@ public interface IAlert /// Sends keys to the alert. /// /// The keystrokes to send. + /// If is . void SendKeys(string keysToSend); } } From c29cb54ca9a829a17cd5b80292f98f8c070fd2f9 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 00:46:34 -0500 Subject: [PATCH 06/14] Use ToString() instead of cast --- dotnet/src/webdriver/Alert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index 7a3e643d8b436..500e56e3acdce 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -48,7 +48,7 @@ public string? Text get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return (string?)commandResponse.Value; + return commandResponse.Value?.ToString(); } } From bdf4338cf2013037a8c4bc9828b4de09fcf6d071 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 01:05:44 -0500 Subject: [PATCH 07/14] `IAlert.Text` cannot be null --- dotnet/src/webdriver/Alert.cs | 6 +++--- dotnet/src/webdriver/IAlert.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index 500e56e3acdce..756acf8d9205c 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -43,12 +43,12 @@ public Alert(WebDriver driver) /// /// Gets the text of the alert. /// - public string? Text + public string Text { get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return commandResponse.Value?.ToString(); + return (string)commandResponse.Value!; } } @@ -75,7 +75,7 @@ public void Accept() /// If is . public void SendKeys(string keysToSend) { - if (keysToSend == null) + if (keysToSend is null) { throw new ArgumentNullException(nameof(keysToSend), "Keys to send must not be null."); } diff --git a/dotnet/src/webdriver/IAlert.cs b/dotnet/src/webdriver/IAlert.cs index 78c15d992c1b3..0ee3b576b101d 100644 --- a/dotnet/src/webdriver/IAlert.cs +++ b/dotnet/src/webdriver/IAlert.cs @@ -31,7 +31,7 @@ public interface IAlert /// /// Gets the text of the alert. /// - string? Text { get; } + string Text { get; } /// /// Dismisses the alert. From b7307239ec31d2be5210f7240818a0865d50424c Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 01:42:16 -0500 Subject: [PATCH 08/14] remove nullability from `CookieJar` --- dotnet/src/webdriver/CookieJar.cs | 26 +++++++++----------------- dotnet/src/webdriver/ICookieJar.cs | 10 +++------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index 44cf484f37352..e4f681e11908a 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -21,8 +21,6 @@ using System.Collections.Generic; using System.Collections.ObjectModel; -#nullable enable - namespace OpenQA.Selenium { /// @@ -30,7 +28,7 @@ namespace OpenQA.Selenium /// internal class CookieJar : ICookieJar { - private readonly WebDriver driver; + private WebDriver driver; /// /// Initializes a new instance of the class. @@ -53,14 +51,8 @@ public ReadOnlyCollection AllCookies /// Method for creating a cookie in the browser /// /// that represents a cookie in the browser - /// If is . public void AddCookie(Cookie cookie) { - if (cookie == null) - { - throw new ArgumentNullException(nameof(cookie)); - } - Dictionary parameters = new Dictionary(); parameters.Add("cookie", cookie); this.driver.InternalExecute(DriverCommand.AddCookie, parameters); @@ -70,9 +62,9 @@ public void AddCookie(Cookie cookie) /// Delete the cookie by passing in the name of the cookie /// /// The name of the cookie that is in the browser - public void DeleteCookieNamed(string? name) + public void DeleteCookieNamed(string name) { - Dictionary parameters = new Dictionary(); + Dictionary parameters = new Dictionary(); parameters.Add("name", name); this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); } @@ -81,7 +73,7 @@ public void DeleteCookieNamed(string? name) /// Delete a cookie in the browser by passing in a copy of a cookie /// /// An object that represents a copy of the cookie that needs to be deleted - public void DeleteCookie(Cookie? cookie) + public void DeleteCookie(Cookie cookie) { if (cookie != null) { @@ -101,10 +93,10 @@ public void DeleteAllCookies() /// Method for returning a getting a cookie by name /// /// name of the cookie that needs to be returned - /// A Cookie from the name, or if not found. - public Cookie? GetCookieNamed(string? name) + /// A Cookie from the name + public Cookie GetCookieNamed(string name) { - Cookie? cookieToReturn = null; + Cookie cookieToReturn = null; if (name != null) { ReadOnlyCollection allCookies = this.AllCookies; @@ -132,14 +124,14 @@ private ReadOnlyCollection GetAllCookies() try { - object[]? cookies = returned as object[]; + object[] cookies = returned as object[]; if (cookies != null) { foreach (object rawCookie in cookies) { + Dictionary cookieDictionary = rawCookie as Dictionary; if (rawCookie != null) { - Dictionary cookieDictionary = (Dictionary)rawCookie; toReturn.Add(Cookie.FromDictionary(cookieDictionary)); } } diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index eaf924937a0f7..a27c472a78c41 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -17,11 +17,8 @@ // under the License. // -using System; using System.Collections.ObjectModel; -#nullable enable - namespace OpenQA.Selenium { /// @@ -38,7 +35,6 @@ public interface ICookieJar /// Adds a cookie to the current page. /// /// The object to be added. - /// If is . void AddCookie(Cookie cookie); /// @@ -47,19 +43,19 @@ public interface ICookieJar /// The name of the cookie to retrieve. /// The containing the name. Returns /// if no cookie with the specified name is found. - Cookie? GetCookieNamed(string? name); + Cookie GetCookieNamed(string name); /// /// Deletes the specified cookie from the page. /// /// The to be deleted. - void DeleteCookie(Cookie? cookie); + void DeleteCookie(Cookie cookie); /// /// Deletes the cookie with the specified name from the page. /// /// The name of the cookie to be deleted. - void DeleteCookieNamed(string? name); + void DeleteCookieNamed(string name); /// /// Deletes all cookies from the page. From 8782a6c714bd7bd5b9f344d2f6565269b37739bc Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 02:01:41 -0500 Subject: [PATCH 09/14] Remove changes from logs --- dotnet/src/webdriver/ILogs.cs | 4 ---- dotnet/src/webdriver/LogEntry.cs | 21 ++++++--------------- dotnet/src/webdriver/Logs.cs | 16 ++++------------ 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/dotnet/src/webdriver/ILogs.cs b/dotnet/src/webdriver/ILogs.cs index d3f236f36c05e..1224f6d4f3666 100644 --- a/dotnet/src/webdriver/ILogs.cs +++ b/dotnet/src/webdriver/ILogs.cs @@ -17,11 +17,8 @@ // under the License. // -using System; using System.Collections.ObjectModel; -#nullable enable - namespace OpenQA.Selenium { /// @@ -40,7 +37,6 @@ public interface ILogs /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. - /// If is . ReadOnlyCollection GetLog(string logKind); } } diff --git a/dotnet/src/webdriver/LogEntry.cs b/dotnet/src/webdriver/LogEntry.cs index 8f3de652f3515..1f43ac5df8a7e 100644 --- a/dotnet/src/webdriver/LogEntry.cs +++ b/dotnet/src/webdriver/LogEntry.cs @@ -21,8 +21,6 @@ using System.Collections.Generic; using System.Globalization; -#nullable enable - namespace OpenQA.Selenium { /// @@ -32,7 +30,7 @@ public class LogEntry { private LogLevel level = LogLevel.All; private DateTime timestamp = DateTime.MinValue; - private string? message = string.Empty; + private string message = string.Empty; /// /// Initializes a new instance of the class. @@ -60,7 +58,7 @@ public LogLevel Level /// /// Gets the message of the log entry. /// - public string? Message + public string Message { get { return this.message; } } @@ -80,14 +78,8 @@ public override string ToString() /// The from /// which to create the . /// A with the values in the dictionary. - /// If is . internal static LogEntry FromDictionary(Dictionary entryDictionary) { - if (entryDictionary == null) - { - throw new ArgumentNullException(nameof(entryDictionary)); - } - LogEntry entry = new LogEntry(); if (entryDictionary.ContainsKey("message")) { @@ -103,13 +95,12 @@ internal static LogEntry FromDictionary(Dictionary entryDictiona if (entryDictionary.ContainsKey("level")) { - string? levelValue = entryDictionary["level"].ToString(); - - if (Enum.TryParse(levelValue, ignoreCase: true, out LogLevel level)) + string levelValue = entryDictionary["level"].ToString(); + try { - entry.level = level; + entry.level = (LogLevel)Enum.Parse(typeof(LogLevel), levelValue, true); } - else + catch (ArgumentException) { // If the requested log level string is not a valid log level, // ignore it and use LogLevel.All. diff --git a/dotnet/src/webdriver/Logs.cs b/dotnet/src/webdriver/Logs.cs index 899be1dcd32ce..74fa3102977d5 100644 --- a/dotnet/src/webdriver/Logs.cs +++ b/dotnet/src/webdriver/Logs.cs @@ -21,8 +21,6 @@ using System.Collections.Generic; using System.Collections.ObjectModel; -#nullable enable - namespace OpenQA.Selenium { /// @@ -52,12 +50,12 @@ public ReadOnlyCollection AvailableLogTypes try { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAvailableLogTypes, null); - object[]? responseValue = commandResponse.Value as object[]; + object[] responseValue = commandResponse.Value as object[]; if (responseValue != null) { foreach (object logKind in responseValue) { - availableLogTypes.Add(logKind.ToString()!); + availableLogTypes.Add(logKind.ToString()); } } } @@ -76,26 +74,20 @@ public ReadOnlyCollection AvailableLogTypes /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. - /// If is null. public ReadOnlyCollection GetLog(string logKind) { - if (logKind == null) - { - throw new ArgumentNullException(nameof(logKind)); - } - List entries = new List(); Dictionary parameters = new Dictionary(); parameters.Add("type", logKind); Response commandResponse = this.driver.InternalExecute(DriverCommand.GetLog, parameters); - object[]? responseValue = commandResponse.Value as object[]; + object[] responseValue = commandResponse.Value as object[]; if (responseValue != null) { foreach (object rawEntry in responseValue) { - Dictionary? entryDictionary = rawEntry as Dictionary; + Dictionary entryDictionary = rawEntry as Dictionary; if (entryDictionary != null) { entries.Add(LogEntry.FromDictionary(entryDictionary)); From c0c0f415ea6545e227a1c011d0f02b828cfb0d26 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 02:06:19 -0500 Subject: [PATCH 10/14] Remove changes to `SwitchTo()` --- dotnet/src/webdriver/ITargetLocator.cs | 3 --- dotnet/src/webdriver/TargetLocator.cs | 26 ++++++-------------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/dotnet/src/webdriver/ITargetLocator.cs b/dotnet/src/webdriver/ITargetLocator.cs index 77dc42e0f4306..933e9d928ce6f 100644 --- a/dotnet/src/webdriver/ITargetLocator.cs +++ b/dotnet/src/webdriver/ITargetLocator.cs @@ -17,8 +17,6 @@ // under the License. // -using System; - namespace OpenQA.Selenium { /// @@ -62,7 +60,6 @@ public interface ITargetLocator /// /// The name of the window to select. /// An instance focused on the given window. - /// If is . /// If the window cannot be found. IWebDriver Window(string windowName); diff --git a/dotnet/src/webdriver/TargetLocator.cs b/dotnet/src/webdriver/TargetLocator.cs index 92e77917bac59..c93a783493b90 100644 --- a/dotnet/src/webdriver/TargetLocator.cs +++ b/dotnet/src/webdriver/TargetLocator.cs @@ -23,8 +23,6 @@ using System.Collections.ObjectModel; using System.Text.RegularExpressions; -#nullable enable - namespace OpenQA.Selenium { /// @@ -61,8 +59,6 @@ public IWebDriver Frame(int frameIndex) /// /// name of the frame /// A WebDriver instance that is currently in use - /// If is null. - /// If a non-existent iFrame is queried for. public IWebDriver Frame(string frameName) { if (frameName == null) @@ -89,8 +85,6 @@ public IWebDriver Frame(string frameName) /// /// a previously found FRAME or IFRAME element. /// A WebDriver instance that is currently in use. - /// If is null. - /// If is an invalid object reference. public IWebDriver Frame(IWebElement frameElement) { if (frameElement == null) @@ -98,10 +92,10 @@ public IWebDriver Frame(IWebElement frameElement) throw new ArgumentNullException(nameof(frameElement), "Frame element cannot be null"); } - IWebDriverObjectReference? elementReference = frameElement as IWebDriverObjectReference; + IWebDriverObjectReference elementReference = frameElement as IWebDriverObjectReference; if (elementReference == null) { - IWrapsElement? elementWrapper = frameElement as IWrapsElement; + IWrapsElement elementWrapper = frameElement as IWrapsElement; if (elementWrapper != null) { elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; @@ -137,15 +131,8 @@ public IWebDriver ParentFrame() /// /// Window handle or name of the window that you wish to move to /// A WebDriver instance that is currently in use - /// If is . - /// If no window matching the provided handle or name exists. public IWebDriver Window(string windowHandleOrName) { - if (windowHandleOrName == null) - { - throw new ArgumentNullException(nameof(windowHandleOrName)); - } - Dictionary parameters = new Dictionary(); parameters.Add("handle", windowHandleOrName); try @@ -156,7 +143,7 @@ public IWebDriver Window(string windowHandleOrName) catch (NoSuchWindowException) { // simulate search by name - string? original = null; + string original = null; try { original = this.driver.CurrentWindowHandle; @@ -197,8 +184,8 @@ public IWebDriver NewWindow(WindowType typeHint) Dictionary parameters = new Dictionary(); parameters.Add("type", typeHint.ToString().ToLowerInvariant()); Response response = this.driver.InternalExecute(DriverCommand.NewWindow, parameters); - Dictionary result = (Dictionary)response.Value; - string newWindowHandle = result["handle"].ToString()!; + Dictionary result = response.Value as Dictionary; + string newWindowHandle = result["handle"].ToString(); this.Window(newWindowHandle); return this.driver; } @@ -209,7 +196,7 @@ public IWebDriver NewWindow(WindowType typeHint) /// Element of the default public IWebDriver DefaultContent() { - Dictionary parameters = new Dictionary(); + Dictionary parameters = new Dictionary(); parameters.Add("id", null); this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters); return this.driver; @@ -229,7 +216,6 @@ public IWebElement ActiveElement() /// Switches to the currently active modal dialog for this particular driver instance. /// /// A handle to the dialog. - /// No alert is present. public IAlert Alert() { // N.B. We only execute the GetAlertText command to be able to throw From 8c84516911f3e9d523e06922f09b3b01d70a33b5 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 9 Dec 2024 10:08:55 -0500 Subject: [PATCH 11/14] Revert most cookie changes --- dotnet/src/webdriver/Cookie.cs | 49 +++++++++---------- .../src/webdriver/Internal/ReturnedCookie.cs | 6 +-- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/dotnet/src/webdriver/Cookie.cs b/dotnet/src/webdriver/Cookie.cs index c6a2e738d4bb0..b15dca6d196fe 100644 --- a/dotnet/src/webdriver/Cookie.cs +++ b/dotnet/src/webdriver/Cookie.cs @@ -24,8 +24,6 @@ using System.Linq; using System.Text.Json.Serialization; -#nullable enable - namespace OpenQA.Selenium { /// @@ -36,9 +34,9 @@ public class Cookie { private string cookieName; private string cookieValue; - private string? cookiePath; - private string? cookieDomain; - private string? sameSite; + private string cookiePath; + private string cookieDomain; + private string sameSite; private bool isHttpOnly; private bool secure; private DateTime? cookieExpiry; @@ -67,7 +65,7 @@ public Cookie(string name, string value) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string? path) + public Cookie(string name, string value, string path) : this(name, value, path, null) { } @@ -83,7 +81,7 @@ public Cookie(string name, string value, string? path) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string? path, DateTime? expiry) + public Cookie(string name, string value, string path, DateTime? expiry) : this(name, value, null, path, expiry) { } @@ -100,7 +98,7 @@ public Cookie(string name, string value, string? path, DateTime? expiry) /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value is . - public Cookie(string name, string value, string? domain, string? path, DateTime? expiry) + public Cookie(string name, string value, string domain, string path, DateTime? expiry) : this(name, value, domain, path, expiry, false, false, null) { } @@ -120,7 +118,7 @@ public Cookie(string name, string value, string? domain, string? path, DateTime? /// If the name and value are both an empty string, /// if the name contains a semi-colon, or if same site value is not valid. /// If the name, value or currentUrl is . - public Cookie(string name, string value, string? domain, string? path, DateTime? expiry, bool secure, bool isHttpOnly, string? sameSite) + public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite) { if (name == null) { @@ -193,7 +191,7 @@ public string Value /// [JsonPropertyName("domain")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Domain + public string Domain { get { return this.cookieDomain; } } @@ -203,7 +201,7 @@ public string? Domain /// [JsonPropertyName("path")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public virtual string? Path + public virtual string Path { get { return this.cookiePath; } } @@ -232,7 +230,7 @@ public virtual bool IsHttpOnly /// [JsonPropertyName("sameSite")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public virtual string? SameSite + public virtual string SameSite { get { return this.sameSite; } } @@ -275,7 +273,6 @@ internal long? ExpirySeconds /// /// The Dictionary object containing the cookie parameters. /// A object with the proper parameters set. - /// If is null. public static Cookie FromDictionary(Dictionary rawCookie) { if (rawCookie == null) @@ -283,20 +280,20 @@ public static Cookie FromDictionary(Dictionary rawCookie) throw new ArgumentNullException(nameof(rawCookie), "Dictionary cannot be null"); } - string name = rawCookie["name"].ToString()!; + string name = rawCookie["name"].ToString(); string value = string.Empty; if (rawCookie["value"] != null) { - value = rawCookie["value"].ToString()!; + value = rawCookie["value"].ToString(); } - string? path = "/"; + string path = "/"; if (rawCookie.ContainsKey("path") && rawCookie["path"] != null) { path = rawCookie["path"].ToString(); } - string? domain = string.Empty; + string domain = string.Empty; if (rawCookie.ContainsKey("domain") && rawCookie["domain"] != null) { domain = rawCookie["domain"].ToString(); @@ -311,16 +308,16 @@ public static Cookie FromDictionary(Dictionary rawCookie) bool secure = false; if (rawCookie.ContainsKey("secure") && rawCookie["secure"] != null) { - secure = bool.Parse(rawCookie["secure"].ToString()!); + secure = bool.Parse(rawCookie["secure"].ToString()); } bool isHttpOnly = false; if (rawCookie.ContainsKey("httpOnly") && rawCookie["httpOnly"] != null) { - isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString()!); + isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString()); } - string? sameSite = null; + string sameSite = null; if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null) { sameSite = rawCookie["sameSite"].ToString(); @@ -351,17 +348,15 @@ public override string ToString() /// if the specified Object /// is equal to the current Object; otherwise, /// . - public override bool Equals(object? obj) + public override bool Equals(object obj) { // Two cookies are equal if the name and value match - Cookie? cookie = obj as Cookie; - if (this == obj) { return true; } - if (cookie == null) + if (obj is not Cookie cookie) { return false; } @@ -383,12 +378,12 @@ public override int GetHashCode() return this.cookieName.GetHashCode(); } - private static string? StripPort(string? domain) + private static string StripPort(string domain) { - return string.IsNullOrEmpty(domain) ? null : domain!.Split(':')[0]; + return string.IsNullOrEmpty(domain) ? null : domain.Split(':')[0]; } - private static DateTime? ConvertExpirationTime(string? expirationTime) + private static DateTime? ConvertExpirationTime(string expirationTime) { DateTime? expires = null; double seconds = 0; diff --git a/dotnet/src/webdriver/Internal/ReturnedCookie.cs b/dotnet/src/webdriver/Internal/ReturnedCookie.cs index bd217fefffdc6..bef50faa49662 100644 --- a/dotnet/src/webdriver/Internal/ReturnedCookie.cs +++ b/dotnet/src/webdriver/Internal/ReturnedCookie.cs @@ -20,8 +20,6 @@ using System; using System.Globalization; -#nullable enable - namespace OpenQA.Selenium.Internal { /// @@ -44,7 +42,7 @@ public class ReturnedCookie : Cookie /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value or currentUrl is . - public ReturnedCookie(string name, string value, string? domain, string? path, DateTime? expiry, bool isSecure, bool isHttpOnly) + public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly) : this(name, value, domain, path, expiry, isSecure, isHttpOnly, null) { } @@ -64,7 +62,7 @@ public ReturnedCookie(string name, string value, string? domain, string? path, D /// If the name is or an empty string, /// or if it contains a semi-colon. /// If the value or currentUrl is . - public ReturnedCookie(string name, string value, string? domain, string? path, DateTime? expiry, bool isSecure, bool isHttpOnly, string? sameSite) + public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly, string sameSite) : base(name, value, domain, path, expiry, isSecure, isHttpOnly, sameSite) { From d7a2f154e1a9083769722da0e04c93cac48dd0c2 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 9 Dec 2024 10:12:33 -0500 Subject: [PATCH 12/14] revert a bunch of changes --- dotnet/src/webdriver/INetwork.cs | 7 ++----- dotnet/src/webdriver/IOptions.cs | 2 -- dotnet/src/webdriver/ITimeouts.cs | 2 -- dotnet/src/webdriver/IWindow.cs | 2 -- dotnet/src/webdriver/NetworkManager.cs | 11 ++++------- dotnet/src/webdriver/OptionsManager.cs | 2 -- dotnet/src/webdriver/Timeouts.cs | 2 -- dotnet/src/webdriver/Window.cs | 8 +++----- 8 files changed, 9 insertions(+), 27 deletions(-) diff --git a/dotnet/src/webdriver/INetwork.cs b/dotnet/src/webdriver/INetwork.cs index f9ad12bbc9b3c..34e6fa3a6b9f5 100644 --- a/dotnet/src/webdriver/INetwork.cs +++ b/dotnet/src/webdriver/INetwork.cs @@ -20,8 +20,6 @@ using System; using System.Threading.Tasks; -#nullable enable - namespace OpenQA.Selenium { /// @@ -32,19 +30,18 @@ public interface INetwork /// /// Occurs when a browser sends a network request. /// - event EventHandler? NetworkRequestSent; + event EventHandler NetworkRequestSent; /// /// Occurs when a browser receives a network response. /// - event EventHandler? NetworkResponseReceived; + event EventHandler NetworkResponseReceived; /// /// Adds a to examine incoming network requests, /// and optionally modify the request or provide a response. /// /// The to add. - /// If is . void AddRequestHandler(NetworkRequestHandler handler); /// diff --git a/dotnet/src/webdriver/IOptions.cs b/dotnet/src/webdriver/IOptions.cs index 6c704000879e2..78fd14514ba37 100644 --- a/dotnet/src/webdriver/IOptions.cs +++ b/dotnet/src/webdriver/IOptions.cs @@ -17,8 +17,6 @@ // under the License. // -#nullable enable - namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/ITimeouts.cs b/dotnet/src/webdriver/ITimeouts.cs index a0a4594e6088e..2e39ace5227f4 100644 --- a/dotnet/src/webdriver/ITimeouts.cs +++ b/dotnet/src/webdriver/ITimeouts.cs @@ -19,8 +19,6 @@ using System; -#nullable enable - namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/IWindow.cs b/dotnet/src/webdriver/IWindow.cs index e5202ed629319..c09ddd7341b22 100644 --- a/dotnet/src/webdriver/IWindow.cs +++ b/dotnet/src/webdriver/IWindow.cs @@ -19,8 +19,6 @@ using System.Drawing; -#nullable enable - namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/NetworkManager.cs b/dotnet/src/webdriver/NetworkManager.cs index 2e151d2d16251..b508c32140ca0 100644 --- a/dotnet/src/webdriver/NetworkManager.cs +++ b/dotnet/src/webdriver/NetworkManager.cs @@ -22,8 +22,6 @@ using System.Collections.Generic; using System.Threading.Tasks; -#nullable enable - namespace OpenQA.Selenium { /// @@ -47,7 +45,7 @@ public NetworkManager(IWebDriver driver) // StartMonitoring(). this.session = new Lazy(() => { - IDevTools? devToolsDriver = driver as IDevTools; + IDevTools devToolsDriver = driver as IDevTools; if (devToolsDriver == null) { throw new WebDriverException("Driver must implement IDevTools to use these features"); @@ -60,12 +58,12 @@ public NetworkManager(IWebDriver driver) /// /// Occurs when a browser sends a network request. /// - public event EventHandler? NetworkRequestSent; + public event EventHandler NetworkRequestSent; /// /// Occurs when a browser receives a network response. /// - public event EventHandler? NetworkResponseReceived; + public event EventHandler NetworkResponseReceived; /// /// Asynchronously starts monitoring for network traffic. @@ -98,7 +96,6 @@ public async Task StopMonitoring() /// and optionally modify the request or provide a response. /// /// The to add. - /// If is null. public void AddRequestHandler(NetworkRequestHandler handler) { if (handler == null) @@ -203,7 +200,7 @@ private async Task OnAuthRequired(object sender, AuthRequiredEventArgs e) { if (authenticationHandler.UriMatcher.Invoke(uri)) { - PasswordCredentials credentials = (PasswordCredentials)authenticationHandler.Credentials; + PasswordCredentials credentials = authenticationHandler.Credentials as PasswordCredentials; await this.session.Value.Domains.Network.ContinueWithAuth(e.RequestId, credentials.UserName, credentials.Password).ConfigureAwait(false); successfullyAuthenticated = true; break; diff --git a/dotnet/src/webdriver/OptionsManager.cs b/dotnet/src/webdriver/OptionsManager.cs index e7ce717eb3c6c..4fc01194dc1c5 100644 --- a/dotnet/src/webdriver/OptionsManager.cs +++ b/dotnet/src/webdriver/OptionsManager.cs @@ -17,8 +17,6 @@ // under the License. // -#nullable enable - namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Timeouts.cs b/dotnet/src/webdriver/Timeouts.cs index 3929d184bde8a..63422d21db6f9 100644 --- a/dotnet/src/webdriver/Timeouts.cs +++ b/dotnet/src/webdriver/Timeouts.cs @@ -21,8 +21,6 @@ using System.Collections.Generic; using System.Globalization; -#nullable enable - namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Window.cs b/dotnet/src/webdriver/Window.cs index aa4adda3f2131..6283f1ba3ef77 100644 --- a/dotnet/src/webdriver/Window.cs +++ b/dotnet/src/webdriver/Window.cs @@ -22,8 +22,6 @@ using System.Drawing; using System.Globalization; -#nullable enable - namespace OpenQA.Selenium { /// @@ -98,7 +96,7 @@ public Size Size /// public void Maximize() { - Dictionary? parameters = null; + Dictionary parameters = null; this.driver.InternalExecute(DriverCommand.MaximizeWindow, parameters); } @@ -107,7 +105,7 @@ public void Maximize() /// public void Minimize() { - Dictionary? parameters = null; + Dictionary parameters = null; this.driver.InternalExecute(DriverCommand.MinimizeWindow, parameters); } @@ -116,7 +114,7 @@ public void Minimize() /// public void FullScreen() { - Dictionary? parameters = null; + Dictionary parameters = null; this.driver.InternalExecute(DriverCommand.FullScreenWindow, parameters); } } From f03f0f90409d2e4f9229ebfc9b61470961b93828 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 11 Dec 2024 14:31:25 -0500 Subject: [PATCH 13/14] Mark `IAlert.Text` as nullable --- dotnet/src/webdriver/Alert.cs | 4 ++-- dotnet/src/webdriver/IAlert.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index 756acf8d9205c..1c7fd49655831 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -43,12 +43,12 @@ public Alert(WebDriver driver) /// /// Gets the text of the alert. /// - public string Text + public string? Text { get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return (string)commandResponse.Value!; + return (string)commandResponse.Value; } } diff --git a/dotnet/src/webdriver/IAlert.cs b/dotnet/src/webdriver/IAlert.cs index 0ee3b576b101d..78c15d992c1b3 100644 --- a/dotnet/src/webdriver/IAlert.cs +++ b/dotnet/src/webdriver/IAlert.cs @@ -31,7 +31,7 @@ public interface IAlert /// /// Gets the text of the alert. /// - string Text { get; } + string? Text { get; } /// /// Dismisses the alert. From 43b3250464962145513367ad0e3121253fd45ec0 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:03:58 +0300 Subject: [PATCH 14/14] Update dotnet/src/webdriver/Alert.cs --- dotnet/src/webdriver/Alert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Alert.cs b/dotnet/src/webdriver/Alert.cs index 1c7fd49655831..d7637b7465a6b 100644 --- a/dotnet/src/webdriver/Alert.cs +++ b/dotnet/src/webdriver/Alert.cs @@ -48,7 +48,7 @@ public string? Text get { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null); - return (string)commandResponse.Value; + return (string?)commandResponse.Value; } }