Skip to content

Commit 767c7c0

Browse files
committed
Simplify
1 parent 9d68245 commit 767c7c0

File tree

1 file changed

+20
-56
lines changed

1 file changed

+20
-56
lines changed

dotnet/src/webdriver/CookieJar.cs

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,77 +23,45 @@
2323

2424
namespace OpenQA.Selenium
2525
{
26-
/// <summary>
27-
/// Defines an interface allowing the user to manipulate cookies on the current page.
28-
/// </summary>
29-
internal class CookieJar : ICookieJar
26+
internal class CookieJar(WebDriver driver) : ICookieJar
3027
{
31-
private WebDriver driver;
28+
public ReadOnlyCollection<Cookie> AllCookies => GetAllCookies();
3229

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-
42-
/// <summary>
43-
/// Gets all cookies defined for the current page.
44-
/// </summary>
45-
public ReadOnlyCollection<Cookie> AllCookies
46-
{
47-
get { return this.GetAllCookies(); }
48-
}
49-
50-
/// <summary>
51-
/// Method for creating a cookie in the browser
52-
/// </summary>
53-
/// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
5430
public void AddCookie(Cookie cookie)
5531
{
56-
Dictionary<string, object> parameters = new Dictionary<string, object>();
57-
parameters.Add("cookie", cookie);
58-
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
32+
if (cookie is null)
33+
{
34+
throw new ArgumentNullException(nameof(cookie));
35+
}
36+
37+
driver.InternalExecute(DriverCommand.AddCookie, new() { { "cookie", cookie } });
5938
}
6039

61-
/// <summary>
62-
/// Delete the cookie by passing in the name of the cookie
63-
/// </summary>
64-
/// <param name="name">The name of the cookie that is in the browser</param>
6540
public void DeleteCookieNamed(string name)
6641
{
67-
Dictionary<string, object> parameters = new Dictionary<string, object>();
68-
parameters.Add("name", name);
69-
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
42+
if (name is null)
43+
{
44+
throw new ArgumentNullException(nameof(name));
45+
}
46+
47+
driver.InternalExecute(DriverCommand.DeleteCookie, new() { { "name", name } });
7048
}
7149

72-
/// <summary>
73-
/// Delete a cookie in the browser by passing in a copy of a cookie
74-
/// </summary>
75-
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
7650
public void DeleteCookie(Cookie cookie)
7751
{
78-
if (cookie != null)
52+
if (cookie is null)
7953
{
80-
this.DeleteCookieNamed(cookie.Name);
54+
throw new ArgumentNullException(nameof(cookie));
8155
}
56+
57+
DeleteCookieNamed(cookie.Name);
8258
}
8359

84-
/// <summary>
85-
/// Delete All Cookies that are present in the browser
86-
/// </summary>
8760
public void DeleteAllCookies()
8861
{
89-
this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
62+
driver.InternalExecute(DriverCommand.DeleteAllCookies, null);
9063
}
9164

92-
/// <summary>
93-
/// Method for returning a getting a cookie by name
94-
/// </summary>
95-
/// <param name="name">name of the cookie that needs to be returned</param>
96-
/// <returns>A Cookie from the name</returns>
9765
public Cookie GetCookieNamed(string name)
9866
{
9967
Cookie cookieToReturn = null;
@@ -113,14 +81,10 @@ public Cookie GetCookieNamed(string name)
11381
return cookieToReturn;
11482
}
11583

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>
12084
private ReadOnlyCollection<Cookie> GetAllCookies()
12185
{
12286
List<Cookie> toReturn = new List<Cookie>();
123-
object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
87+
object returned = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value;
12488

12589
try
12690
{

0 commit comments

Comments
 (0)