diff --git a/dotnet/src/webdriver/CookieJar.cs b/dotnet/src/webdriver/CookieJar.cs index e4f681e11908a..934c50892cc3c 100644 --- a/dotnet/src/webdriver/CookieJar.cs +++ b/dotnet/src/webdriver/CookieJar.cs @@ -23,104 +23,61 @@ namespace OpenQA.Selenium { - /// - /// Defines an interface allowing the user to manipulate cookies on the current page. - /// - internal class CookieJar : ICookieJar + internal class CookieJar(WebDriver driver) : ICookieJar { - private WebDriver driver; + public ReadOnlyCollection AllCookies => GetAllCookies(); - /// - /// Initializes a new instance of the class. - /// - /// The driver that is currently in use - public CookieJar(WebDriver driver) - { - this.driver = driver; - } - - /// - /// Gets all cookies defined for the current page. - /// - public ReadOnlyCollection AllCookies - { - get { return this.GetAllCookies(); } - } - - /// - /// Method for creating a cookie in the browser - /// - /// that represents a cookie in the browser public void AddCookie(Cookie cookie) { - Dictionary parameters = new Dictionary(); - parameters.Add("cookie", cookie); - this.driver.InternalExecute(DriverCommand.AddCookie, parameters); + if (cookie is null) + { + throw new ArgumentNullException(nameof(cookie)); + } + + driver.InternalExecute(DriverCommand.AddCookie, new() { { "cookie", cookie } }); } - /// - /// Delete the cookie by passing in the name of the cookie - /// - /// The name of the cookie that is in the browser public void DeleteCookieNamed(string name) { - Dictionary parameters = new Dictionary(); - parameters.Add("name", name); - this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters); + if (name is null) + { + throw new ArgumentNullException(nameof(name)); + } + + driver.InternalExecute(DriverCommand.DeleteCookie, new() { { "name", name } }); } - /// - /// Delete a cookie in the browser by passing in a copy of a cookie - /// - /// An object that represents a copy of the cookie that needs to be deleted public void DeleteCookie(Cookie cookie) { - if (cookie != null) + if (cookie is null) { - this.DeleteCookieNamed(cookie.Name); + throw new ArgumentNullException(nameof(cookie)); } + + DeleteCookieNamed(cookie.Name); } - /// - /// Delete All Cookies that are present in the browser - /// public void DeleteAllCookies() { - this.driver.InternalExecute(DriverCommand.DeleteAllCookies, null); + driver.InternalExecute(DriverCommand.DeleteAllCookies, null); } - /// - /// Method for returning a getting a cookie by name - /// - /// name of the cookie that needs to be returned - /// A Cookie from the name public Cookie GetCookieNamed(string name) { - Cookie cookieToReturn = null; - if (name != null) + if (name is null) { - ReadOnlyCollection allCookies = this.AllCookies; - foreach (Cookie currentCookie in allCookies) - { - if (name.Equals(currentCookie.Name)) - { - cookieToReturn = currentCookie; - break; - } - } + throw new ArgumentNullException(nameof(name)); } - return cookieToReturn; + var rawCookie = driver.InternalExecute(DriverCommand.GetCookie, new() { { "name", name } }).Value; + + return Cookie.FromDictionary((Dictionary)rawCookie); } - /// - /// Method for getting a Collection of Cookies that are present in the browser - /// - /// ReadOnlyCollection of Cookies in the browser private ReadOnlyCollection GetAllCookies() { List toReturn = new List(); - object returned = this.driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()).Value; + object returned = driver.InternalExecute(DriverCommand.GetAllCookies, new Dictionary()).Value; try { diff --git a/dotnet/src/webdriver/ICookieJar.cs b/dotnet/src/webdriver/ICookieJar.cs index a27c472a78c41..c0e1eab1d824e 100644 --- a/dotnet/src/webdriver/ICookieJar.cs +++ b/dotnet/src/webdriver/ICookieJar.cs @@ -17,6 +17,7 @@ // under the License. // +using System; using System.Collections.ObjectModel; namespace OpenQA.Selenium @@ -35,26 +36,30 @@ public interface ICookieJar /// Adds a cookie to the current page. /// /// The object to be added. + /// If is . void AddCookie(Cookie cookie); /// /// Gets a cookie with the specified name. /// /// The name of the cookie to retrieve. - /// The containing the name. Returns - /// if no cookie with the specified name is found. + /// The containing the name. + /// If is . + /// If is not found. Cookie GetCookieNamed(string name); /// /// Deletes the specified cookie from the page. /// /// The to be deleted. + /// If is . void DeleteCookie(Cookie cookie); /// /// Deletes the cookie with the specified name from the page. /// /// The name of the cookie to be deleted. + /// If is . void DeleteCookieNamed(string name); /// diff --git a/dotnet/src/webdriver/NoSuchCookieException.cs b/dotnet/src/webdriver/NoSuchCookieException.cs new file mode 100644 index 0000000000000..067a89eba6465 --- /dev/null +++ b/dotnet/src/webdriver/NoSuchCookieException.cs @@ -0,0 +1,63 @@ +// +// 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. +// + +using System; + +#nullable enable + +namespace OpenQA.Selenium +{ + /// + /// The exception that is thrown when a cookie is not found. + /// + [Serializable] + public class NoSuchCookieException : NotFoundException + { + /// + /// Initializes a new instance of the class. + /// + public NoSuchCookieException() + : base() + { + } + + /// + /// Initializes a new instance of the class with + /// a specified error message. + /// + /// The message that describes the error. + public NoSuchCookieException(string? message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with + /// a specified error message and a reference to the inner exception that is the + /// cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or if no inner exception is specified. + public NoSuchCookieException(string? message, Exception? innerException) + : base(message, innerException) + { + } + } +} diff --git a/dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs b/dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs index 8c51b36fd6035..352194e7bca41 100644 --- a/dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs +++ b/dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs @@ -108,7 +108,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")); diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index 9bcca8b28f8b3..f887ce42bd432 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -853,6 +853,9 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command case WebDriverResult.InsecureCertificate: throw new InsecureCertificateException(errorMessage); + case WebDriverResult.NoSuchCookie: + throw new NoSuchCookieException(errorMessage); + default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status)); } diff --git a/dotnet/test/common/CookieImplementationTest.cs b/dotnet/test/common/CookieImplementationTest.cs index a329895d79b7d..6b60192252ec1 100644 --- a/dotnet/test/common/CookieImplementationTest.cs +++ b/dotnet/test/common/CookieImplementationTest.cs @@ -553,8 +553,7 @@ public void SettingACookieThatExpiredInThePast() IOptions options = driver.Manage(); options.Cookies.AddCookie(cookie); - cookie = options.Cookies.GetCookieNamed("expired"); - Assert.That(cookie, Is.Null, "Cookie expired before it was set, so nothing should be returned: " + cookie); + Assert.That(() => options.Cookies.GetCookieNamed("expired"), Throws.InstanceOf()); } [Test] @@ -579,9 +578,8 @@ public void CanSetCookieWithoutOptionalFieldsSet() public void DeleteNotExistedCookie() { String key = GenerateUniqueKey(); - AssertCookieIsNotPresentWithName(key); - driver.Manage().Cookies.DeleteCookieNamed(key); + Assert.That(() => driver.Manage().Cookies.DeleteCookieNamed(key), Throws.Nothing); } [Test]