Skip to content

Commit 60e450d

Browse files
authored
Merge branch 'trunk' into js_nodev_update
2 parents d1df441 + 7d8068d commit 60e450d

File tree

6 files changed

+119
-76
lines changed

6 files changed

+119
-76
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
}

dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs

+34-49
Original file line numberDiff line numberDiff line change
@@ -22,89 +22,74 @@
2222
using NUnit.Framework.Internal;
2323
using OpenQA.Selenium.Environment;
2424
using System;
25-
using System.Collections.Generic;
2625

26+
#nullable enable
2727

2828
namespace OpenQA.Selenium
2929
{
3030
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
3131
public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
3232
{
33-
private readonly String target;
34-
private readonly string ignoreReason = string.Empty;
35-
3633
public IgnoreTargetAttribute(string target)
3734
{
38-
this.target = target.ToLower();
35+
this.Value = target.ToLower();
3936
}
4037

4138
public IgnoreTargetAttribute(string target, string reason)
4239
: this(target)
4340
{
44-
this.ignoreReason = reason;
41+
this.Reason = reason;
4542
}
4643

47-
public string Value
48-
{
49-
get { return target; }
50-
}
44+
public string Value { get; }
5145

52-
public string Reason
53-
{
54-
get { return ignoreReason; }
55-
}
46+
public string Reason { get; } = string.Empty;
5647

5748
public void ApplyToTest(Test test)
5849
{
59-
if (test.RunState != RunState.NotRunnable)
50+
if (test.RunState is RunState.NotRunnable)
6051
{
61-
List<Attribute> ignoreAttributes = new List<Attribute>();
62-
if (test.IsSuite)
63-
{
64-
Attribute[] ignoreClassAttributes =
65-
test.TypeInfo.GetCustomAttributes<IgnoreTargetAttribute>(true);
66-
if (ignoreClassAttributes.Length > 0)
67-
{
68-
ignoreAttributes.AddRange(ignoreClassAttributes);
69-
}
70-
}
71-
else
52+
return;
53+
}
54+
IgnoreTargetAttribute[] ignoreAttributes;
55+
if (test.IsSuite)
56+
{
57+
ignoreAttributes = test.TypeInfo!.GetCustomAttributes<IgnoreTargetAttribute>(true);
58+
}
59+
else
60+
{
61+
ignoreAttributes = test.Method!.GetCustomAttributes<IgnoreTargetAttribute>(true);
62+
}
63+
64+
foreach (IgnoreTargetAttribute platformToIgnoreAttr in ignoreAttributes)
65+
{
66+
if (IgnoreTestForPlatform(platformToIgnoreAttr.Value))
7267
{
73-
IgnoreTargetAttribute[] ignoreMethodAttributes =
74-
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(true);
75-
if (ignoreMethodAttributes.Length > 0)
68+
string ignoreReason = $"Ignoring target {EnvironmentManager.Instance.Browser}";
69+
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
7670
{
77-
ignoreAttributes.AddRange(ignoreMethodAttributes);
71+
ignoreReason = ignoreReason + ": " + platformToIgnoreAttr.Reason;
7872
}
79-
}
8073

81-
foreach (Attribute attr in ignoreAttributes)
82-
{
83-
IgnoreTargetAttribute platformToIgnoreAttr = attr as IgnoreTargetAttribute;
84-
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
85-
{
86-
string ignoreReason =
87-
"Ignoring target " + EnvironmentManager.Instance.Browser.ToString() + ".";
88-
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
89-
{
90-
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
91-
}
74+
test.RunState = RunState.Ignored;
75+
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
9276

93-
test.RunState = RunState.Ignored;
94-
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
95-
}
9677
}
9778
}
9879
}
9980

100-
private bool IgnoreTestForPlatform(string platformToIgnore)
81+
private static bool IgnoreTestForPlatform(string platformToIgnore)
10182
{
102-
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
83+
return CurrentPlatform().Equals(platformToIgnore, StringComparison.OrdinalIgnoreCase);
10384
}
10485

105-
private string CurrentPlatform()
86+
private static string CurrentPlatform()
10687
{
107-
return "net6";
88+
#if NET8_0
89+
return "net8";
90+
#else
91+
#error Update IgnoreTargetAttribute.CurrentPlatform to the current TFM
92+
#endif
10893
}
10994
}
11095
}

0 commit comments

Comments
 (0)