Skip to content

Commit b3c3903

Browse files
committed
[dotnet] add test attributes to guard for platforms and targets
1 parent 8fb8fd7 commit b3c3903

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using NUnit.Framework;
2+
using NUnit.Framework.Interfaces;
3+
using System;
4+
using NUnit.Framework.Internal;
5+
using System.Collections.Generic;
6+
using System.Runtime.InteropServices;
7+
using OpenQA.Selenium.Environment;
8+
9+
#if !NET45 && !NET46 && !NET47
10+
using System.Runtime.InteropServices;
11+
using OSPlatform = System.Runtime.InteropServices.OSPlatform;
12+
#endif
13+
14+
15+
namespace OpenQA.Selenium
16+
{
17+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
18+
public class IgnorePlatformAttribute : NUnitAttribute, IApplyToTest
19+
{
20+
private readonly String platform;
21+
private readonly string ignoreReason = string.Empty;
22+
23+
public IgnorePlatformAttribute(string platform)
24+
{
25+
this.platform = platform.ToLower();
26+
}
27+
28+
public IgnorePlatformAttribute(string platform, string reason)
29+
: this(platform)
30+
{
31+
this.ignoreReason = reason;
32+
}
33+
34+
public string Value
35+
{
36+
get { return platform; }
37+
}
38+
39+
public string Reason
40+
{
41+
get { return ignoreReason; }
42+
}
43+
44+
public void ApplyToTest(Test test)
45+
{
46+
if (test.RunState != RunState.NotRunnable)
47+
{
48+
List<Attribute> ignoreAttributes = new List<Attribute>();
49+
if (test.IsSuite)
50+
{
51+
Attribute[] ignoreClassAttributes =
52+
test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
53+
if (ignoreClassAttributes.Length > 0)
54+
{
55+
ignoreAttributes.AddRange(ignoreClassAttributes);
56+
}
57+
}
58+
else
59+
{
60+
IgnorePlatformAttribute[] ignoreMethodAttributes =
61+
test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
62+
if (ignoreMethodAttributes.Length > 0)
63+
{
64+
ignoreAttributes.AddRange(ignoreMethodAttributes);
65+
}
66+
}
67+
68+
foreach (Attribute attr in ignoreAttributes)
69+
{
70+
IgnorePlatformAttribute platformToIgnoreAttr = attr as IgnorePlatformAttribute;
71+
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
72+
{
73+
string ignoreReason =
74+
"Ignoring platform " + EnvironmentManager.Instance.Browser.ToString() + ".";
75+
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
76+
{
77+
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
78+
}
79+
80+
test.RunState = RunState.Ignored;
81+
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
82+
}
83+
}
84+
}
85+
}
86+
87+
private bool IgnoreTestForPlatform(string platformToIgnore)
88+
{
89+
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
90+
}
91+
92+
private string CurrentPlatform()
93+
{
94+
#if NET45 || NET46 || NET47
95+
return null;
96+
#else
97+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
98+
{
99+
return "windows";
100+
}
101+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
102+
{
103+
return "linux";
104+
}
105+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
106+
{
107+
return "mac";
108+
}
109+
else
110+
{
111+
throw new WebDriverException("Selenium Manager did not find supported operating system");
112+
}
113+
#endif
114+
}
115+
}
116+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using NUnit.Framework;
2+
using NUnit.Framework.Interfaces;
3+
using System;
4+
using NUnit.Framework.Internal;
5+
using System.Collections.Generic;
6+
using OpenQA.Selenium.Environment;
7+
8+
9+
namespace OpenQA.Selenium
10+
{
11+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
12+
public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
13+
{
14+
private readonly String target;
15+
private readonly string ignoreReason = string.Empty;
16+
17+
public IgnoreTargetAttribute(string target)
18+
{
19+
this.target = target.ToLower();
20+
}
21+
22+
public IgnoreTargetAttribute(string target, string reason)
23+
: this(target)
24+
{
25+
this.ignoreReason = reason;
26+
}
27+
28+
public string Value
29+
{
30+
get { return target; }
31+
}
32+
33+
public string Reason
34+
{
35+
get { return ignoreReason; }
36+
}
37+
38+
public void ApplyToTest(Test test)
39+
{
40+
if (test.RunState != RunState.NotRunnable)
41+
{
42+
List<Attribute> ignoreAttributes = new List<Attribute>();
43+
if (test.IsSuite)
44+
{
45+
Attribute[] ignoreClassAttributes =
46+
test.TypeInfo.GetCustomAttributes<IgnoreTargetAttribute>(true);
47+
if (ignoreClassAttributes.Length > 0)
48+
{
49+
ignoreAttributes.AddRange(ignoreClassAttributes);
50+
}
51+
}
52+
else
53+
{
54+
IgnoreTargetAttribute[] ignoreMethodAttributes =
55+
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(true);
56+
if (ignoreMethodAttributes.Length > 0)
57+
{
58+
ignoreAttributes.AddRange(ignoreMethodAttributes);
59+
}
60+
}
61+
62+
foreach (Attribute attr in ignoreAttributes)
63+
{
64+
IgnoreTargetAttribute platformToIgnoreAttr = attr as IgnoreTargetAttribute;
65+
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
66+
{
67+
string ignoreReason =
68+
"Ignoring target " + EnvironmentManager.Instance.Browser.ToString() + ".";
69+
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
70+
{
71+
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
72+
}
73+
74+
test.RunState = RunState.Ignored;
75+
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
76+
}
77+
}
78+
}
79+
}
80+
81+
private bool IgnoreTestForPlatform(string platformToIgnore)
82+
{
83+
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
84+
}
85+
86+
private string CurrentPlatform()
87+
{
88+
#if NET6_0
89+
return "net6";
90+
#endif
91+
#if NETCOREAPP3_1
92+
return "netcore3";
93+
#endif
94+
#if NET48
95+
return "net48";
96+
#endif
97+
return null;
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)