diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs
index e4f681e11908a..8ae324fa6961f 100644
--- a/dotnet/src/webdriver/CookieJar.cs
+++ b/dotnet/src/webdriver/CookieJar.cs
@@ -21,64 +21,92 @@
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(WebDriver driver) : ICookieJar
{
- private WebDriver driver;
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The driver that is currently in use
- public CookieJar(WebDriver driver)
- {
- this.driver = driver;
- }
-
///
/// Gets all cookies defined for the current page.
///
public ReadOnlyCollection AllCookies
{
- get { return this.GetAllCookies(); }
+ get
+ {
+ Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary());
+
+ try
+ {
+ List toReturn = new List();
+ if (response.Value 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);
+ driver.InternalExecute(DriverCommand.AddCookie, parameters);
}
///
/// 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);
}
///
/// 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
+ /// 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);
}
///
@@ -86,63 +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 != null)
+ if (name is null)
{
- ReadOnlyCollection allCookies = this.AllCookies;
- foreach (Cookie currentCookie in allCookies)
- {
- if (name.Equals(currentCookie.Name))
- {
- cookieToReturn = currentCookie;
- break;
- }
- }
+ throw new ArgumentNullException(nameof(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
+ foreach (Cookie currentCookie in this.AllCookies)
{
- object[] cookies = returned as object[];
- if (cookies != null)
+ if (name.Equals(currentCookie.Name))
{
- foreach (object rawCookie in cookies)
- {
- Dictionary cookieDictionary = rawCookie as Dictionary;
- if (rawCookie != null)
- {
- toReturn.Add(Cookie.FromDictionary(cookieDictionary));
- }
- }
+ return currentCookie;
}
- return new ReadOnlyCollection(toReturn);
- }
- catch (Exception e)
- {
- throw new WebDriverException("Unexpected problem getting cookies", e);
}
+
+ return null;
}
}
}
diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs
index a27c472a78c41..07594bf8f3173 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,18 +47,21 @@ 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.
+ /// 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);
///