|
25 | 25 |
|
26 | 26 | namespace OpenQA.Selenium |
27 | 27 | { |
28 | | - /// <summary> |
29 | | - /// Defines an interface allowing the user to manipulate cookies on the current page. |
30 | | - /// </summary> |
31 | | - internal sealed class CookieJar : ICookieJar |
| 28 | + internal sealed class CookieJar(WebDriver driver) : ICookieJar |
32 | 29 | { |
33 | | - private readonly WebDriver driver; |
34 | | - |
35 | | - /// <summary> |
36 | | - /// Initializes a new instance of the <see cref="CookieJar"/> class. |
37 | | - /// </summary> |
38 | | - /// <param name="driver">The driver that is currently in use</param> |
39 | | - /// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception> |
40 | | - public CookieJar(WebDriver driver) |
41 | | - { |
42 | | - this.driver = driver ?? throw new ArgumentNullException(nameof(driver)); |
43 | | - } |
44 | | - |
45 | 30 | /// <summary> |
46 | 31 | /// Gets all cookies defined for the current page. |
47 | 32 | /// </summary> |
48 | 33 | public ReadOnlyCollection<Cookie> AllCookies |
49 | 34 | { |
50 | 35 | get |
51 | 36 | { |
52 | | - object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()).Value; |
| 37 | + Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>()); |
53 | 38 |
|
54 | 39 | try |
55 | 40 | { |
56 | 41 | List<Cookie> toReturn = new List<Cookie>(); |
57 | | - if (returned is object?[] cookies) |
| 42 | + if (response.Value is object?[] cookies) |
58 | 43 | { |
59 | 44 | foreach (object? rawCookie in cookies) |
60 | 45 | { |
@@ -89,66 +74,72 @@ public void AddCookie(Cookie cookie) |
89 | 74 |
|
90 | 75 | Dictionary<string, object> parameters = new Dictionary<string, object>(); |
91 | 76 | parameters.Add("cookie", cookie); |
92 | | - this.driver.InternalExecute(DriverCommand.AddCookie, parameters); |
| 77 | + driver.InternalExecute(DriverCommand.AddCookie, parameters); |
93 | 78 | } |
94 | 79 |
|
95 | | -#nullable disable |
96 | | - |
97 | 80 | /// <summary> |
98 | 81 | /// Delete the cookie by passing in the name of the cookie |
99 | 82 | /// </summary> |
100 | 83 | /// <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> |
101 | 85 | public void DeleteCookieNamed(string name) |
102 | 86 | { |
| 87 | + if (name is null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException(nameof(name)); |
| 90 | + } |
| 91 | + |
103 | 92 | Dictionary<string, object> parameters = new Dictionary<string, object>(); |
104 | 93 | parameters.Add("name", name); |
105 | | - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); |
| 94 | + driver.InternalExecute(DriverCommand.DeleteCookie, parameters); |
106 | 95 | } |
107 | 96 |
|
108 | | -#nullable enable |
109 | | - |
110 | 97 | /// <summary> |
111 | 98 | /// Delete a cookie in the browser by passing in a copy of a cookie |
112 | 99 | /// </summary> |
113 | 100 | /// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param> |
114 | | - public void DeleteCookie(Cookie? cookie) |
| 101 | + /// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception> |
| 102 | + public void DeleteCookie(Cookie cookie) |
115 | 103 | { |
116 | | - if (cookie != null) |
| 104 | + if (cookie is null) |
117 | 105 | { |
118 | | - this.DeleteCookieNamed(cookie.Name); |
| 106 | + throw new ArgumentNullException(nameof(cookie)); |
119 | 107 | } |
| 108 | + |
| 109 | + this.DeleteCookieNamed(cookie.Name); |
120 | 110 | } |
121 | 111 |
|
122 | 112 | /// <summary> |
123 | 113 | /// Delete All Cookies that are present in the browser |
124 | 114 | /// </summary> |
125 | 115 | public void DeleteAllCookies() |
126 | 116 | { |
127 | | - this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null); |
| 117 | + driver.InternalExecute(DriverCommand.DeleteAllCookies, null); |
128 | 118 | } |
129 | 119 |
|
130 | 120 | /// <summary> |
131 | 121 | /// Method for returning a getting a cookie by name |
132 | 122 | /// </summary> |
133 | 123 | /// <param name="name">name of the cookie that needs to be returned</param> |
134 | | - /// <returns>A Cookie from the name</returns> |
135 | | - 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) |
136 | 126 | { |
137 | | - Cookie? cookieToReturn = null; |
138 | | - if (name is not null) |
| 127 | + if (name is null) |
139 | 128 | { |
140 | | - ReadOnlyCollection<Cookie> allCookies = this.AllCookies; |
141 | | - foreach (Cookie currentCookie in allCookies) |
| 129 | + throw new ArgumentNullException(nameof(name)); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + foreach (Cookie currentCookie in this.AllCookies) |
| 134 | + { |
| 135 | + if (name.Equals(currentCookie.Name)) |
142 | 136 | { |
143 | | - if (name.Equals(currentCookie.Name)) |
144 | | - { |
145 | | - cookieToReturn = currentCookie; |
146 | | - break; |
147 | | - } |
| 137 | + return currentCookie; |
148 | 138 | } |
| 139 | + |
149 | 140 | } |
150 | 141 |
|
151 | | - return cookieToReturn; |
| 142 | + return null; |
152 | 143 | } |
153 | 144 | } |
154 | 145 | } |
0 commit comments