Skip to content

Commit 7d8068d

Browse files
authored
[dotnet] Utilize dedicated WebDriver Spec endpoint to get named cookie (#14957)
1 parent 85b8164 commit 7d8068d

File tree

5 files changed

+85
-27
lines changed

5 files changed

+85
-27
lines changed

dotnet/src/webdriver/CookieJar.cs

+17-25
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,20 @@ public ReadOnlyCollection<Cookie> AllCookies
3636
{
3737
Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>());
3838

39-
try
39+
List<Cookie> toReturn = new List<Cookie>();
40+
if (response.Value is object?[] cookies)
4041
{
41-
List<Cookie> toReturn = new List<Cookie>();
42-
if (response.Value is object?[] cookies)
42+
foreach (object? rawCookie in cookies)
4343
{
44-
foreach (object? rawCookie in cookies)
44+
if (rawCookie != null)
4545
{
46-
if (rawCookie != null)
47-
{
48-
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
49-
toReturn.Add(newCookie);
50-
}
46+
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
47+
toReturn.Add(newCookie);
5148
}
5249
}
53-
54-
return new ReadOnlyCollection<Cookie>(toReturn);
55-
}
56-
catch (Exception e)
57-
{
58-
throw new WebDriverException("Unexpected problem getting cookies", e);
5950
}
51+
52+
return new ReadOnlyCollection<Cookie>(toReturn);
6053
}
6154
}
6255

@@ -124,22 +117,21 @@ public void DeleteAllCookies()
124117
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
125118
public Cookie? GetCookieNamed(string name)
126119
{
127-
if (name is null)
120+
if (string.IsNullOrWhiteSpace(name))
128121
{
129-
throw new ArgumentNullException(nameof(name));
122+
throw new ArgumentException("Cookie name cannot be empty", nameof(name));
130123
}
131124

132-
133-
foreach (Cookie currentCookie in this.AllCookies)
125+
try
134126
{
135-
if (name.Equals(currentCookie.Name))
136-
{
137-
return currentCookie;
138-
}
127+
var rawCookie = driver.InternalExecute(DriverCommand.GetCookie, new() { { "name", name } }).Value;
139128

129+
return Cookie.FromDictionary((Dictionary<string, object>)rawCookie);
130+
}
131+
catch (NoSuchCookieException)
132+
{
133+
return null;
140134
}
141-
142-
return null;
143135
}
144136
}
145137
}

dotnet/src/webdriver/ICookieJar.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public interface ICookieJar
4747
/// <param name="name">The name of the cookie to retrieve.</param>
4848
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
4949
/// if no cookie with the specified name is found.</returns>
50-
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
50+
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
5151
Cookie? GetCookieNamed(string name);
5252

5353
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// <copyright file="NoSuchCookieException.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System;
21+
22+
#nullable enable
23+
24+
namespace OpenQA.Selenium
25+
{
26+
/// <summary>
27+
/// The exception that is thrown when a cookie is not found.
28+
/// </summary>
29+
[Serializable]
30+
public class NoSuchCookieException : NotFoundException
31+
{
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class.
34+
/// </summary>
35+
public NoSuchCookieException()
36+
: base()
37+
{
38+
}
39+
40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class with
42+
/// a specified error message.
43+
/// </summary>
44+
/// <param name="message">The message that describes the error.</param>
45+
public NoSuchCookieException(string? message)
46+
: base(message)
47+
{
48+
}
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class with
52+
/// a specified error message and a reference to the inner exception that is the
53+
/// cause of this exception.
54+
/// </summary>
55+
/// <param name="message">The error message that explains the reason for the exception.</param>
56+
/// <param name="innerException">The exception that is the cause of the current exception,
57+
/// or <see langword="null"/> if no inner exception is specified.</param>
58+
public NoSuchCookieException(string? message, Exception? innerException)
59+
: base(message, innerException)
60+
{
61+
}
62+
}
63+
}

dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected override void InitializeCommandDictionary()
104104
this.TryAddCommand(DriverCommand.ExecuteScript, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/execute/sync"));
105105
this.TryAddCommand(DriverCommand.ExecuteAsyncScript, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/execute/async"));
106106
this.TryAddCommand(DriverCommand.GetAllCookies, new HttpCommandInfo(HttpCommandInfo.GetCommand, "/session/{sessionId}/cookie"));
107-
this.TryAddCommand(DriverCommand.GetCookie, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/cookie/{name}"));
107+
this.TryAddCommand(DriverCommand.GetCookie, new HttpCommandInfo(HttpCommandInfo.GetCommand, "/session/{sessionId}/cookie/{name}"));
108108
this.TryAddCommand(DriverCommand.AddCookie, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/cookie"));
109109
this.TryAddCommand(DriverCommand.DeleteCookie, new HttpCommandInfo(HttpCommandInfo.DeleteCommand, "/session/{sessionId}/cookie/{name}"));
110110
this.TryAddCommand(DriverCommand.DeleteAllCookies, new HttpCommandInfo(HttpCommandInfo.DeleteCommand, "/session/{sessionId}/cookie"));

dotnet/src/webdriver/WebDriver.cs

+3
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,9 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command
847847
case WebDriverResult.UnsupportedOperation:
848848
throw new UnsupportedOperationException(errorMessage);
849849

850+
case WebDriverResult.NoSuchCookie:
851+
throw new NoSuchCookieException(errorMessage);
852+
850853
default:
851854
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
852855
}

0 commit comments

Comments
 (0)