|
22 | 22 | using NUnit.Framework.Internal; |
23 | 23 | using OpenQA.Selenium.Environment; |
24 | 24 | using System; |
25 | | -using System.Collections.Generic; |
26 | 25 |
|
| 26 | +#nullable enable |
27 | 27 |
|
28 | 28 | namespace OpenQA.Selenium |
29 | 29 | { |
30 | 30 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] |
31 | 31 | public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest |
32 | 32 | { |
33 | | - private readonly String target; |
34 | | - private readonly string ignoreReason = string.Empty; |
35 | | - |
36 | 33 | public IgnoreTargetAttribute(string target) |
37 | 34 | { |
38 | | - this.target = target.ToLower(); |
| 35 | + this.Value = target.ToLower(); |
39 | 36 | } |
40 | 37 |
|
41 | 38 | public IgnoreTargetAttribute(string target, string reason) |
42 | 39 | : this(target) |
43 | 40 | { |
44 | | - this.ignoreReason = reason; |
| 41 | + this.Reason = reason; |
45 | 42 | } |
46 | 43 |
|
47 | | - public string Value |
48 | | - { |
49 | | - get { return target; } |
50 | | - } |
| 44 | + public string Value { get; } |
51 | 45 |
|
52 | | - public string Reason |
53 | | - { |
54 | | - get { return ignoreReason; } |
55 | | - } |
| 46 | + public string Reason { get; } = string.Empty; |
56 | 47 |
|
57 | 48 | public void ApplyToTest(Test test) |
58 | 49 | { |
59 | | - if (test.RunState != RunState.NotRunnable) |
| 50 | + if (test.RunState is RunState.NotRunnable) |
60 | 51 | { |
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)) |
72 | 67 | { |
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)) |
76 | 70 | { |
77 | | - ignoreAttributes.AddRange(ignoreMethodAttributes); |
| 71 | + ignoreReason = ignoreReason + ": " + platformToIgnoreAttr.Reason; |
78 | 72 | } |
79 | | - } |
80 | 73 |
|
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); |
92 | 76 |
|
93 | | - test.RunState = RunState.Ignored; |
94 | | - test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason); |
95 | | - } |
96 | 77 | } |
97 | 78 | } |
98 | 79 | } |
99 | 80 |
|
100 | | - private bool IgnoreTestForPlatform(string platformToIgnore) |
| 81 | + private static bool IgnoreTestForPlatform(string platformToIgnore) |
101 | 82 | { |
102 | | - return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform()); |
| 83 | + return CurrentPlatform().Equals(platformToIgnore, StringComparison.OrdinalIgnoreCase); |
103 | 84 | } |
104 | 85 |
|
105 | | - private string CurrentPlatform() |
| 86 | + private static string CurrentPlatform() |
106 | 87 | { |
107 | | - return "net6"; |
| 88 | +#if NET8_0 |
| 89 | + return "net8"; |
| 90 | +#else |
| 91 | +#error Update IgnoreTargetAttribute.CurrentPlatform to the current TFM |
| 92 | +#endif |
108 | 93 | } |
109 | 94 | } |
110 | 95 | } |
0 commit comments