/
IgnoreTargetAttribute.cs
97 lines (86 loc) · 3.09 KB
/
IgnoreTargetAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using NUnit.Framework.Internal;
using System.Collections.Generic;
using OpenQA.Selenium.Environment;
namespace OpenQA.Selenium
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
{
private readonly String target;
private readonly string ignoreReason = string.Empty;
public IgnoreTargetAttribute(string target)
{
this.target = target.ToLower();
}
public IgnoreTargetAttribute(string target, string reason)
: this(target)
{
this.ignoreReason = reason;
}
public string Value
{
get { return target; }
}
public string Reason
{
get { return ignoreReason; }
}
public void ApplyToTest(Test test)
{
if (test.RunState != RunState.NotRunnable)
{
List<Attribute> ignoreAttributes = new List<Attribute>();
if (test.IsSuite)
{
Attribute[] ignoreClassAttributes =
test.TypeInfo.GetCustomAttributes<IgnoreTargetAttribute>(true);
if (ignoreClassAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreClassAttributes);
}
}
else
{
IgnoreTargetAttribute[] ignoreMethodAttributes =
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(true);
if (ignoreMethodAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreMethodAttributes);
}
}
foreach (Attribute attr in ignoreAttributes)
{
IgnoreTargetAttribute platformToIgnoreAttr = attr as IgnoreTargetAttribute;
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
{
string ignoreReason =
"Ignoring target " + EnvironmentManager.Instance.Browser.ToString() + ".";
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
{
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
}
test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
}
}
}
}
private bool IgnoreTestForPlatform(string platformToIgnore)
{
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
}
private string CurrentPlatform()
{
#if NET6_0
return "net6";
#endif
#if NET48
return "net48";
#endif
return null;
}
}
}