|
21 | 21 | using System.Collections.Generic;
|
22 | 22 | using System.Collections.ObjectModel;
|
23 | 23 |
|
| 24 | +#nullable enable |
| 25 | + |
24 | 26 | namespace OpenQA.Selenium
|
25 | 27 | {
|
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 |
30 | 29 | {
|
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 |
| - |
42 | 30 | /// <summary>
|
43 | 31 | /// Gets all cookies defined for the current page.
|
44 | 32 | /// </summary>
|
45 | 33 | public ReadOnlyCollection<Cookie> AllCookies
|
46 | 34 | {
|
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 | + } |
48 | 61 | }
|
49 | 62 |
|
50 | 63 | /// <summary>
|
51 | 64 | /// Method for creating a cookie in the browser
|
52 | 65 | /// </summary>
|
53 | 66 | /// <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> |
54 | 68 | public void AddCookie(Cookie cookie)
|
55 | 69 | {
|
| 70 | + if (cookie is null) |
| 71 | + { |
| 72 | + throw new ArgumentNullException(nameof(cookie)); |
| 73 | + } |
| 74 | + |
56 | 75 | Dictionary<string, object> parameters = new Dictionary<string, object>();
|
57 | 76 | parameters.Add("cookie", cookie);
|
58 |
| - this.driver.InternalExecute(DriverCommand.AddCookie, parameters); |
| 77 | + driver.InternalExecute(DriverCommand.AddCookie, parameters); |
59 | 78 | }
|
60 | 79 |
|
61 | 80 | /// <summary>
|
62 | 81 | /// Delete the cookie by passing in the name of the cookie
|
63 | 82 | /// </summary>
|
64 | 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> |
65 | 85 | public void DeleteCookieNamed(string name)
|
66 | 86 | {
|
| 87 | + if (name is null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException(nameof(name)); |
| 90 | + } |
| 91 | + |
67 | 92 | Dictionary<string, object> parameters = new Dictionary<string, object>();
|
68 | 93 | parameters.Add("name", name);
|
69 |
| - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); |
| 94 | + driver.InternalExecute(DriverCommand.DeleteCookie, parameters); |
70 | 95 | }
|
71 | 96 |
|
72 | 97 | /// <summary>
|
73 | 98 | /// Delete a cookie in the browser by passing in a copy of a cookie
|
74 | 99 | /// </summary>
|
75 | 100 | /// <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> |
76 | 102 | public void DeleteCookie(Cookie cookie)
|
77 | 103 | {
|
78 |
| - if (cookie != null) |
| 104 | + if (cookie is null) |
79 | 105 | {
|
80 |
| - this.DeleteCookieNamed(cookie.Name); |
| 106 | + throw new ArgumentNullException(nameof(cookie)); |
81 | 107 | }
|
| 108 | + |
| 109 | + this.DeleteCookieNamed(cookie.Name); |
82 | 110 | }
|
83 | 111 |
|
84 | 112 | /// <summary>
|
85 | 113 | /// Delete All Cookies that are present in the browser
|
86 | 114 | /// </summary>
|
87 | 115 | public void DeleteAllCookies()
|
88 | 116 | {
|
89 |
| - this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null); |
| 117 | + driver.InternalExecute(DriverCommand.DeleteAllCookies, null); |
90 | 118 | }
|
91 | 119 |
|
92 | 120 | /// <summary>
|
93 | 121 | /// Method for returning a getting a cookie by name
|
94 | 122 | /// </summary>
|
95 | 123 | /// <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) |
98 | 126 | {
|
99 |
| - Cookie cookieToReturn = null; |
100 |
| - if (name != null) |
| 127 | + if (name is null) |
101 | 128 | {
|
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)); |
111 | 130 | }
|
112 | 131 |
|
113 |
| - return cookieToReturn; |
114 |
| - } |
115 | 132 |
|
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) |
126 | 134 | {
|
127 |
| - object[] cookies = returned as object[]; |
128 |
| - if (cookies != null) |
| 135 | + if (name.Equals(currentCookie.Name)) |
129 | 136 | {
|
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; |
138 | 138 | }
|
139 | 139 |
|
140 |
| - return new ReadOnlyCollection<Cookie>(toReturn); |
141 |
| - } |
142 |
| - catch (Exception e) |
143 |
| - { |
144 |
| - throw new WebDriverException("Unexpected problem getting cookies", e); |
145 | 140 | }
|
| 141 | + |
| 142 | + return null; |
146 | 143 | }
|
147 | 144 | }
|
148 | 145 | }
|
0 commit comments