Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Add nullability to CookieJar #14874

Merged
merged 7 commits into from
Dec 11, 2024
119 changes: 58 additions & 61 deletions dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,128 +21,125 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Defines an interface allowing the user to manipulate cookies on the current page.
/// </summary>
internal class CookieJar : ICookieJar
internal sealed class CookieJar(WebDriver driver) : ICookieJar
{
private WebDriver driver;

/// <summary>
/// Initializes a new instance of the <see cref="CookieJar"/> class.
/// </summary>
/// <param name="driver">The driver that is currently in use</param>
public CookieJar(WebDriver driver)
{
this.driver = driver;
}

/// <summary>
/// Gets all cookies defined for the current page.
/// </summary>
public ReadOnlyCollection<Cookie> AllCookies
{
get { return this.GetAllCookies(); }
get
{
Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>());

try
{
List<Cookie> toReturn = new List<Cookie>();
if (response.Value is object?[] cookies)
{
foreach (object? rawCookie in cookies)
{
if (rawCookie != null)
{
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
toReturn.Add(newCookie);
}
}
}

return new ReadOnlyCollection<Cookie>(toReturn);
}
catch (Exception e)
{
throw new WebDriverException("Unexpected problem getting cookies", e);
}
}
}

/// <summary>
/// Method for creating a cookie in the browser
/// </summary>
/// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
public void AddCookie(Cookie cookie)
{
if (cookie is null)
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(nameof(cookie));
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("cookie", cookie);
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
driver.InternalExecute(DriverCommand.AddCookie, parameters);
}

/// <summary>
/// Delete the cookie by passing in the name of the cookie
/// </summary>
/// <param name="name">The name of the cookie that is in the browser</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
public void DeleteCookieNamed(string name)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", name);
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
}

/// <summary>
/// Delete a cookie in the browser by passing in a copy of a cookie
/// </summary>
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
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);
}

/// <summary>
/// Delete All Cookies that are present in the browser
/// </summary>
public void DeleteAllCookies()
{
this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
}

/// <summary>
/// Method for returning a getting a cookie by name
/// </summary>
/// <param name="name">name of the cookie that needs to be returned</param>
/// <returns>A Cookie from the name</returns>
public Cookie GetCookieNamed(string name)
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
public Cookie? GetCookieNamed(string name)
{
Cookie cookieToReturn = null;
if (name != null)
if (name is null)
{
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
foreach (Cookie currentCookie in allCookies)
{
if (name.Equals(currentCookie.Name))
{
cookieToReturn = currentCookie;
break;
}
}
throw new ArgumentNullException(nameof(name));
}

return cookieToReturn;
}

/// <summary>
/// Method for getting a Collection of Cookies that are present in the browser
/// </summary>
/// <returns>ReadOnlyCollection of Cookies in the browser</returns>
private ReadOnlyCollection<Cookie> GetAllCookies()
{
List<Cookie> toReturn = new List<Cookie>();
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).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<string, object> cookieDictionary = rawCookie as Dictionary<string, object>;
if (rawCookie != null)
{
toReturn.Add(Cookie.FromDictionary(cookieDictionary));
}
}
return currentCookie;
}

return new ReadOnlyCollection<Cookie>(toReturn);
}
catch (Exception e)
{
throw new WebDriverException("Unexpected problem getting cookies", e);
}

return null;
}
}
}
9 changes: 8 additions & 1 deletion dotnet/src/webdriver/ICookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
// under the License.
// </copyright>

using System;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -35,6 +38,7 @@ public interface ICookieJar
/// Adds a cookie to the current page.
/// </summary>
/// <param name="cookie">The <see cref="Cookie"/> object to be added.</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
void AddCookie(Cookie cookie);

/// <summary>
Expand All @@ -43,18 +47,21 @@ public interface ICookieJar
/// <param name="name">The name of the cookie to retrieve.</param>
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
/// if no cookie with the specified name is found.</returns>
Cookie GetCookieNamed(string name);
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
Cookie? GetCookieNamed(string name);

/// <summary>
/// Deletes the specified cookie from the page.
/// </summary>
/// <param name="cookie">The <see cref="Cookie"/> to be deleted.</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
void DeleteCookie(Cookie cookie);

/// <summary>
/// Deletes the cookie with the specified name from the page.
/// </summary>
/// <param name="name">The name of the cookie to be deleted.</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
void DeleteCookieNamed(string name);

/// <summary>
Expand Down
Loading