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
93 changes: 49 additions & 44 deletions dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,72 @@
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 : ICookieJar
{
private WebDriver driver;
private readonly 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>
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
public CookieJar(WebDriver driver)
{
this.driver = driver;
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
}

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

try
{
List<Cookie> toReturn = new List<Cookie>();
if (returned 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);
Expand All @@ -62,18 +96,21 @@ public void AddCookie(Cookie cookie)
/// 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>
public void DeleteCookieNamed(string name)
public void DeleteCookieNamed(string? name)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", name);
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
if (name is not null)
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", name);
this.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>
public void DeleteCookie(Cookie cookie)
public void DeleteCookie(Cookie? cookie)
{
if (cookie != null)
{
Expand All @@ -94,10 +131,10 @@ public void DeleteAllCookies()
/// </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)
public Cookie? GetCookieNamed(string? name)
{
Cookie cookieToReturn = null;
if (name != null)
Cookie? cookieToReturn = null;
if (name is not null)
{
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
foreach (Cookie currentCookie in allCookies)
Expand All @@ -112,37 +149,5 @@ public Cookie GetCookieNamed(string 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
{
object[] cookies = returned as object[];
if (cookies != null)
{
foreach (object rawCookie in cookies)
{
Dictionary<string, object> cookieDictionary = rawCookie as Dictionary<string, object>;
if (rawCookie != null)
{
toReturn.Add(Cookie.FromDictionary(cookieDictionary));
}
}
}

return new ReadOnlyCollection<Cookie>(toReturn);
}
catch (Exception e)
{
throw new WebDriverException("Unexpected problem getting cookies", e);
}
}
}
}
10 changes: 7 additions & 3 deletions 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,19 +47,19 @@ 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);
Cookie? GetCookieNamed(string? name);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Deletes the specified cookie from the page.
/// </summary>
/// <param name="cookie">The <see cref="Cookie"/> to be deleted.</param>
void DeleteCookie(Cookie cookie);
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>
void DeleteCookieNamed(string name);
void DeleteCookieNamed(string? name);

/// <summary>
/// Deletes all cookies from the page.
Expand Down
Loading