Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,20 @@ public ReadOnlyCollection<Cookie> AllCookies
{
Response response = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary<string, object>());

try
List<Cookie> toReturn = new List<Cookie>();
if (response.Value is object?[] cookies)
{
List<Cookie> toReturn = new List<Cookie>();
if (response.Value is object?[] cookies)
foreach (object? rawCookie in cookies)
{
foreach (object? rawCookie in cookies)
if (rawCookie != null)
{
if (rawCookie != null)
{
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
toReturn.Add(newCookie);
}
Cookie newCookie = Cookie.FromDictionary((Dictionary<string, object?>)rawCookie);
toReturn.Add(newCookie);
}
}

return new ReadOnlyCollection<Cookie>(toReturn);
}
catch (Exception e)
{
throw new WebDriverException("Unexpected problem getting cookies", e);
}

return new ReadOnlyCollection<Cookie>(toReturn);
}
}

Expand Down Expand Up @@ -124,22 +117,21 @@ public void DeleteAllCookies()
/// <returns>A Cookie from the name; or <see langword="null"/> if not found.</returns>
public Cookie? GetCookieNamed(string name)
{
if (name is null)
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
throw new ArgumentException("Cookie name cannot be empty", nameof(name));
}


foreach (Cookie currentCookie in this.AllCookies)
try
{
if (name.Equals(currentCookie.Name))
{
return currentCookie;
}
var rawCookie = driver.InternalExecute(DriverCommand.GetCookie, new() { { "name", name } }).Value;

return Cookie.FromDictionary((Dictionary<string, object>)rawCookie);
}
catch (NoSuchCookieException)
{
return null;
}

return null;
}
}
}
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/ICookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface ICookieJar
/// <param name="name">The name of the cookie to retrieve.</param>
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
/// if no cookie with the specified name is found.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="name"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
Cookie? GetCookieNamed(string name);

/// <summary>
Expand Down
63 changes: 63 additions & 0 deletions dotnet/src/webdriver/NoSuchCookieException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// <copyright file="NoSuchCookieException.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// The exception that is thrown when a cookie is not found.
/// </summary>
[Serializable]
public class NoSuchCookieException : NotFoundException
{
/// <summary>
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class.
/// </summary>
public NoSuchCookieException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class with
/// a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public NoSuchCookieException(string? message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NoSuchCookieException"/> class with
/// a specified error message and a reference to the inner exception that is the
/// cause of this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception,
/// or <see langword="null"/> if no inner exception is specified.</param>
public NoSuchCookieException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected override void InitializeCommandDictionary()
this.TryAddCommand(DriverCommand.ExecuteScript, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/execute/sync"));
this.TryAddCommand(DriverCommand.ExecuteAsyncScript, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/execute/async"));
this.TryAddCommand(DriverCommand.GetAllCookies, new HttpCommandInfo(HttpCommandInfo.GetCommand, "/session/{sessionId}/cookie"));
this.TryAddCommand(DriverCommand.GetCookie, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/cookie/{name}"));
this.TryAddCommand(DriverCommand.GetCookie, new HttpCommandInfo(HttpCommandInfo.GetCommand, "/session/{sessionId}/cookie/{name}"));
this.TryAddCommand(DriverCommand.AddCookie, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/cookie"));
this.TryAddCommand(DriverCommand.DeleteCookie, new HttpCommandInfo(HttpCommandInfo.DeleteCommand, "/session/{sessionId}/cookie/{name}"));
this.TryAddCommand(DriverCommand.DeleteAllCookies, new HttpCommandInfo(HttpCommandInfo.DeleteCommand, "/session/{sessionId}/cookie"));
Expand Down
3 changes: 3 additions & 0 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,9 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command
case WebDriverResult.UnsupportedOperation:
throw new UnsupportedOperationException(errorMessage);

case WebDriverResult.NoSuchCookie:
throw new NoSuchCookieException(errorMessage);

default:
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
}
Expand Down
Loading