From 1b25a75de7750b4165e5888423d3d9e9527f330d Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 01:36:41 -0500 Subject: [PATCH 1/4] [dotnet] Add nullability to `CookieJar` --- dotnet/src/webdriver/CookieJar.cs | 93 ++++++++++++++++-------------- dotnet/src/webdriver/ICookieJar.cs | 10 +++- 2 files changed, 56 insertions(+), 47 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index e4f681e11908a..4d3639f2403fe 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -21,22 +21,25 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// /// Defines an interface allowing the user to manipulate cookies on the current page. /// - internal class CookieJar : ICookieJar + internal sealed class CookieJar : ICookieJar { - private WebDriver driver; + private readonly WebDriver driver; /// /// Initializes a new instance of the class. /// /// The driver that is currently in use + /// If is . public CookieJar(WebDriver driver) { - this.driver = driver; + this.driver = driver ?? throw new ArgumentNullException(nameof(driver)); } /// @@ -44,15 +47,46 @@ public CookieJar(WebDriver driver) /// public ReadOnlyCollection AllCookies { - get { return this.GetAllCookies(); } + get + { + object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()).Value; + + try + { + List toReturn = new List(); + if (returned is object?[] cookies) + { + foreach (object? rawCookie in cookies) + { + if (rawCookie != null) + { + Cookie newCookie = Cookie.FromDictionary((Dictionary)rawCookie); + toReturn.Add(newCookie); + } + } + } + + return new ReadOnlyCollection(toReturn); + } + catch (Exception e) + { + throw new WebDriverException("Unexpected problem getting cookies", e); + } + } } /// /// Method for creating a cookie in the browser /// /// that represents a cookie in the browser + /// If is . public void AddCookie(Cookie cookie) { + if (cookie is null) + { + throw new ArgumentNullException(nameof(cookie)); + } + Dictionary parameters = new Dictionary(); parameters.Add("cookie", cookie); this.driver.InternalExecute(DriverCommand.AddCookie, parameters); @@ -62,18 +96,21 @@ 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(); - parameters.Add("name", name); - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); + if (name is not null) + { + Dictionary parameters = new Dictionary(); + parameters.Add("name", name); + this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); + } } /// /// 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) { @@ -94,10 +131,10 @@ public void DeleteAllCookies() /// /// name of the cookie that needs to be returned /// A Cookie from the name - public Cookie GetCookieNamed(string name) + public Cookie? GetCookieNamed(string? name) { - Cookie cookieToReturn = null; - if (name != null) + Cookie? cookieToReturn = null; + if (name is not null) { ReadOnlyCollection allCookies = this.AllCookies; foreach (Cookie currentCookie in allCookies) @@ -112,37 +149,5 @@ public Cookie GetCookieNamed(string name) return cookieToReturn; } - - /// - /// Method for getting a Collection of Cookies that are present in the browser - /// - /// ReadOnlyCollection of Cookies in the browser - private ReadOnlyCollection GetAllCookies() - { - List toReturn = new List(); - object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()).Value; - - try - { - object[] cookies = returned as object[]; - if (cookies != null) - { - foreach (object rawCookie in cookies) - { - Dictionary cookieDictionary = rawCookie as Dictionary; - if (rawCookie != null) - { - toReturn.Add(Cookie.FromDictionary(cookieDictionary)); - } - } - } - - return new ReadOnlyCollection(toReturn); - } - catch (Exception e) - { - throw new WebDriverException("Unexpected problem getting cookies", e); - } - } } } diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index a27c472a78c41..eaf924937a0f7 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -17,8 +17,11 @@ // under the License. // +using System; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -35,6 +38,7 @@ public interface ICookieJar /// Adds a cookie to the current page. /// /// The object to be added. + /// If is . void AddCookie(Cookie cookie); /// @@ -43,19 +47,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 7c8d30d4588dcc9b4a4974dc25b4a67cf000e0bd Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 14:04:10 -0500 Subject: [PATCH 2/4] Remove nullability from `ICookieJar.DeleteCookieNamed(string)` --- dotnet/src/webdriver/CookieJar.cs | 6 +++++- dotnet/src/webdriver/ICookieJar.cs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index 4d3639f2403fe..17ca003a5e860 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -92,11 +92,13 @@ public void AddCookie(Cookie cookie) this.driver.InternalExecute(DriverCommand.AddCookie, parameters); } +#nullable disable + /// /// 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) { if (name is not null) { @@ -106,6 +108,8 @@ public void DeleteCookieNamed(string? name) } } +#nullable enable + /// /// Delete a cookie in the browser by passing in a copy of a cookie /// diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index eaf924937a0f7..be94867296153 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -55,11 +55,15 @@ public interface ICookieJar /// The to be deleted. void DeleteCookie(Cookie? cookie); +#nullable disable + /// /// 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); + +#nullable enable /// /// Deletes all cookies from the page. From 4c1d6e3af7687b10a099f1b9b490661b576be332 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 8 Dec 2024 14:05:57 -0500 Subject: [PATCH 3/4] remove any changse from `CookieJar.DeleteCookieNamed(string)` --- dotnet/src/webdriver/CookieJar.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index 17ca003a5e860..f30e3d2fd5d74 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -100,12 +100,9 @@ public void AddCookie(Cookie cookie) /// The name of the cookie that is in the browser public void DeleteCookieNamed(string name) { - if (name is not null) - { - Dictionary parameters = new Dictionary(); - parameters.Add("name", name); - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); - } + Dictionary parameters = new Dictionary(); + parameters.Add("name", name); + this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); } #nullable enable From 74701ce02c556fa731f49cff74135befb0e71c05 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 11 Dec 2024 00:36:17 -0500 Subject: [PATCH 4/4] Update `ICookieJar` contract and implementation with null-handling parameter checks --- dotnet/src/webdriver/CookieJar.cs | 71 +++++++++++++----------------- dotnet/src/webdriver/ICookieJar.cs | 11 +++-- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index f30e3d2fd5d74..8ae324fa6961f 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -25,23 +25,8 @@ namespace OpenQA.Selenium { - /// - /// Defines an interface allowing the user to manipulate cookies on the current page. - /// - internal sealed class CookieJar : ICookieJar + internal sealed class CookieJar(WebDriver driver) : ICookieJar { - private readonly WebDriver driver; - - /// - /// Initializes a new instance of the class. - /// - /// The driver that is currently in use - /// If is . - public CookieJar(WebDriver driver) - { - this.driver = driver ?? throw new ArgumentNullException(nameof(driver)); - } - /// /// Gets all cookies defined for the current page. /// @@ -49,12 +34,12 @@ public ReadOnlyCollection AllCookies { get { - object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()).Value; + Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()); try { List toReturn = new List(); - if (returned is object?[] cookies) + if (response.Value is object?[] cookies) { foreach (object? rawCookie in cookies) { @@ -89,34 +74,39 @@ public void AddCookie(Cookie cookie) Dictionary parameters = new Dictionary(); parameters.Add("cookie", cookie); - this.driver.InternalExecute(DriverCommand.AddCookie, parameters); + driver.InternalExecute(DriverCommand.AddCookie, parameters); } -#nullable disable - /// /// Delete the cookie by passing in the name of the cookie /// /// The name of the cookie that is in the browser + /// If is . public void DeleteCookieNamed(string name) { + if (name is null) + { + throw new ArgumentNullException(nameof(name)); + } + Dictionary parameters = new Dictionary(); parameters.Add("name", name); - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); + driver.InternalExecute(DriverCommand.DeleteCookie, parameters); } -#nullable enable - /// /// 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) + /// If is . + public void DeleteCookie(Cookie cookie) { - if (cookie != null) + if (cookie is null) { - this.DeleteCookieNamed(cookie.Name); + throw new ArgumentNullException(nameof(cookie)); } + + this.DeleteCookieNamed(cookie.Name); } /// @@ -124,31 +114,32 @@ public void DeleteCookie(Cookie? cookie) /// public void DeleteAllCookies() { - this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null); + driver.InternalExecute(DriverCommand.DeleteAllCookies, null); } /// /// 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; - if (name is not null) + if (name is null) { - ReadOnlyCollection allCookies = this.AllCookies; - foreach (Cookie currentCookie in allCookies) + throw new ArgumentNullException(nameof(name)); + } + + + foreach (Cookie currentCookie in this.AllCookies) + { + if (name.Equals(currentCookie.Name)) { - if (name.Equals(currentCookie.Name)) - { - cookieToReturn = currentCookie; - break; - } + return currentCookie; } + } - return cookieToReturn; + return null; } } } diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index be94867296153..07594bf8f3173 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -47,24 +47,23 @@ 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); + /// If is . + Cookie? GetCookieNamed(string name); /// /// Deletes the specified cookie from the page. /// /// The to be deleted. - void DeleteCookie(Cookie? cookie); - -#nullable disable + /// If is . + void DeleteCookie(Cookie cookie); /// /// Deletes the cookie with the specified name from the page. /// /// The name of the cookie to be deleted. + /// If is . void DeleteCookieNamed(string name); -#nullable enable - /// /// Deletes all cookies from the page. ///