Skip to content

Commit 28e9a3a

Browse files
[dotnet] Add nullability to CookieJar (#14874)
1 parent 3f3fd78 commit 28e9a3a

File tree

2 files changed

+66
-62
lines changed

2 files changed

+66
-62
lines changed

dotnet/src/webdriver/CookieJar.cs

+58-61
Original file line numberDiff line numberDiff line change
@@ -21,128 +21,125 @@
2121
using System.Collections.Generic;
2222
using System.Collections.ObjectModel;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium
2527
{
26-
/// <summary>
27-
/// Defines an interface allowing the user to manipulate cookies on the current page.
28-
/// </summary>
29-
internal class CookieJar : ICookieJar
28+
internal sealed class CookieJar(WebDriver driver) : ICookieJar
3029
{
31-
private WebDriver driver;
32-
33-
/// <summary>
34-
/// Initializes a new instance of the <see cref="CookieJar"/> class.
35-
/// </summary>
36-
/// <param name="driver">The driver that is currently in use</param>
37-
public CookieJar(WebDriver driver)
38-
{
39-
this.driver = driver;
40-
}
41-
4230
/// <summary>
4331
/// Gets all cookies defined for the current page.
4432
/// </summary>
4533
public ReadOnlyCollection<Cookie> AllCookies
4634
{
47-
get { return this.GetAllCookies(); }
35+
get
36+
{
37+
Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>());
38+
39+
try
40+
{
41+
List<Cookie> toReturn = new List<Cookie>();
42+
if (response.Value is object?[] cookies)
43+
{
44+
foreach (object? rawCookie in cookies)
45+
{
46+
if (rawCookie != null)
47+
{
48+
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
49+
toReturn.Add(newCookie);
50+
}
51+
}
52+
}
53+
54+
return new ReadOnlyCollection<Cookie>(toReturn);
55+
}
56+
catch (Exception e)
57+
{
58+
throw new WebDriverException("Unexpected problem getting cookies", e);
59+
}
60+
}
4861
}
4962

5063
/// <summary>
5164
/// Method for creating a cookie in the browser
5265
/// </summary>
5366
/// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
67+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
5468
public void AddCookie(Cookie cookie)
5569
{
70+
if (cookie is null)
71+
{
72+
throw new ArgumentNullException(nameof(cookie));
73+
}
74+
5675
Dictionary<string, object> parameters = new Dictionary<string, object>();
5776
parameters.Add("cookie", cookie);
58-
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
77+
driver.InternalExecute(DriverCommand.AddCookie, parameters);
5978
}
6079

6180
/// <summary>
6281
/// Delete the cookie by passing in the name of the cookie
6382
/// </summary>
6483
/// <param name="name">The name of the cookie that is in the browser</param>
84+
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
6585
public void DeleteCookieNamed(string name)
6686
{
87+
if (name is null)
88+
{
89+
throw new ArgumentNullException(nameof(name));
90+
}
91+
6792
Dictionary<string, object> parameters = new Dictionary<string, object>();
6893
parameters.Add("name", name);
69-
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
94+
driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
7095
}
7196

7297
/// <summary>
7398
/// Delete a cookie in the browser by passing in a copy of a cookie
7499
/// </summary>
75100
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
101+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
76102
public void DeleteCookie(Cookie cookie)
77103
{
78-
if (cookie != null)
104+
if (cookie is null)
79105
{
80-
this.DeleteCookieNamed(cookie.Name);
106+
throw new ArgumentNullException(nameof(cookie));
81107
}
108+
109+
this.DeleteCookieNamed(cookie.Name);
82110
}
83111

84112
/// <summary>
85113
/// Delete All Cookies that are present in the browser
86114
/// </summary>
87115
public void DeleteAllCookies()
88116
{
89-
this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
117+
driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
90118
}
91119

92120
/// <summary>
93121
/// Method for returning a getting a cookie by name
94122
/// </summary>
95123
/// <param name="name">name of the cookie that needs to be returned</param>
96-
/// <returns>A Cookie from the name</returns>
97-
public Cookie GetCookieNamed(string name)
124+
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
125+
public Cookie? GetCookieNamed(string name)
98126
{
99-
Cookie cookieToReturn = null;
100-
if (name != null)
127+
if (name is null)
101128
{
102-
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
103-
foreach (Cookie currentCookie in allCookies)
104-
{
105-
if (name.Equals(currentCookie.Name))
106-
{
107-
cookieToReturn = currentCookie;
108-
break;
109-
}
110-
}
129+
throw new ArgumentNullException(nameof(name));
111130
}
112131

113-
return cookieToReturn;
114-
}
115132

116-
/// <summary>
117-
/// Method for getting a Collection of Cookies that are present in the browser
118-
/// </summary>
119-
/// <returns>ReadOnlyCollection of Cookies in the browser</returns>
120-
private ReadOnlyCollection<Cookie> GetAllCookies()
121-
{
122-
List<Cookie> toReturn = new List<Cookie>();
123-
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
124-
125-
try
133+
foreach (Cookie currentCookie in this.AllCookies)
126134
{
127-
object[] cookies = returned as object[];
128-
if (cookies != null)
135+
if (name.Equals(currentCookie.Name))
129136
{
130-
foreach (object rawCookie in cookies)
131-
{
132-
Dictionary<string, object> cookieDictionary = rawCookie as Dictionary<string, object>;
133-
if (rawCookie != null)
134-
{
135-
toReturn.Add(Cookie.FromDictionary(cookieDictionary));
136-
}
137-
}
137+
return currentCookie;
138138
}
139139

140-
return new ReadOnlyCollection<Cookie>(toReturn);
141-
}
142-
catch (Exception e)
143-
{
144-
throw new WebDriverException("Unexpected problem getting cookies", e);
145140
}
141+
142+
return null;
146143
}
147144
}
148145
}

dotnet/src/webdriver/ICookieJar.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Collections.ObjectModel;
2122

23+
#nullable enable
24+
2225
namespace OpenQA.Selenium
2326
{
2427
/// <summary>
@@ -35,6 +38,7 @@ public interface ICookieJar
3538
/// Adds a cookie to the current page.
3639
/// </summary>
3740
/// <param name="cookie">The <see cref="Cookie"/> object to be added.</param>
41+
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
3842
void AddCookie(Cookie cookie);
3943

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

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

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

6067
/// <summary>

0 commit comments

Comments
 (0)