Skip to content

Commit 83a7150

Browse files
committed
implement custom test class in dotnet to filter examples based on OS
1 parent 8397879 commit 83a7150

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

examples/dotnet/SeleniumDocs/GettingStarted/InstallDriversTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
using OpenQA.Selenium.Edge;
44
using OpenQA.Selenium.Firefox;
55
using OpenQA.Selenium.IE;
6+
using SeleniumDocs.TestSupport;
67
using WebDriverManager;
78
using WebDriverManager.DriverConfigs.Impl;
89

910
namespace SeleniumDocs.GettingStarted
1011
{
11-
[TestClass]
12+
[TestClassCustom]
1213
public class InstallDriversTest
1314
{
1415
[TestMethod]
@@ -21,7 +22,6 @@ public void ChromeSession()
2122
driver.Quit();
2223
}
2324

24-
[Ignore] // WebDriverManager.Net does not resolve msedgedriver in Linux
2525
[TestMethod]
2626
public void EdgeSession()
2727
{
@@ -42,7 +42,7 @@ public void FirefoxSession()
4242
driver.Quit();
4343
}
4444

45-
[Ignore] // Only runs on Windows
45+
[EnabledOnOs("WINDOWS")]
4646
[TestMethod]
4747
public void InternetExplorerSession()
4848
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace SeleniumDocs.TestSupport
6+
{
7+
// Solution based on — https://matt.kotsenas.com/posts/ignoreif-mstest
8+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
9+
public class EnabledOnOsAttribute : Attribute
10+
{
11+
public OSPlatform EnabledOnOs { get; set; }
12+
13+
public EnabledOnOsAttribute(String platform)
14+
{
15+
EnabledOnOs = OSPlatform.Create(platform);
16+
}
17+
18+
internal bool ShouldIgnore(ITestMethod testMethod)
19+
{
20+
return !RuntimeInformation.IsOSPlatform(this.EnabledOnOs);
21+
}
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace SeleniumDocs.TestSupport
4+
{
5+
// Solution based on — https://matt.kotsenas.com/posts/ignoreif-mstest
6+
public class TestClassCustomAttribute : TestClassAttribute
7+
{
8+
public override TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute testMethodAttribute)
9+
{
10+
if (testMethodAttribute is TestMethodCustomAttribute)
11+
{
12+
return testMethodAttribute;
13+
}
14+
return new TestMethodCustomAttribute();
15+
}
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace SeleniumDocs.TestSupport
6+
{
7+
// Solution based on — https://matt.kotsenas.com/posts/ignoreif-mstest
8+
public class TestMethodCustomAttribute : TestMethodAttribute
9+
{
10+
public override TestResult[] Execute(ITestMethod testMethod)
11+
{
12+
var enabledOsAttributes = FindEnabledAttributes(testMethod);
13+
14+
foreach (var enabledOsAttribute in enabledOsAttributes)
15+
{
16+
if (enabledOsAttribute.ShouldIgnore(testMethod))
17+
{
18+
var message = $"Test not executed. Only enabled on '{enabledOsAttribute.EnabledOnOs}' OS.";
19+
return new[]
20+
{
21+
new TestResult
22+
{
23+
Outcome = UnitTestOutcome.Inconclusive,
24+
TestFailureException = new AssertInconclusiveException(message)
25+
}
26+
};
27+
}
28+
}
29+
return base.Execute(testMethod);
30+
}
31+
32+
private IEnumerable<EnabledOnOsAttribute> FindEnabledAttributes(ITestMethod testMethod)
33+
{
34+
var enabledOsAttributes = new List<EnabledOnOsAttribute>();
35+
enabledOsAttributes.AddRange(testMethod.GetAttributes<EnabledOnOsAttribute>(inherit: true));
36+
37+
var type = testMethod.MethodInfo.DeclaringType;
38+
while (type != null)
39+
{
40+
enabledOsAttributes.AddRange(type.GetCustomAttributes<EnabledOnOsAttribute>(inherit: true));
41+
type = type.DeclaringType;
42+
}
43+
return enabledOsAttributes;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)