Skip to content

Commit 3c94dff

Browse files
committed
Modernize more testing infrastructure
1 parent 2773fe4 commit 3c94dff

File tree

10 files changed

+102
-180
lines changed

10 files changed

+102
-180
lines changed

dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,115 +22,92 @@
2222
using NUnit.Framework.Internal;
2323
using OpenQA.Selenium.Environment;
2424
using System;
25-
using System.Collections.Generic;
2625

2726
namespace OpenQA.Selenium
2827
{
2928
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
3029
public class IgnoreBrowserAttribute : NUnitAttribute, IApplyToTest
3130
{
32-
private readonly Browser browser;
33-
private readonly string ignoreReason = string.Empty;
34-
3531
public IgnoreBrowserAttribute(Browser browser)
3632
{
37-
this.browser = browser;
33+
this.Value = browser;
3834
}
3935

4036
public IgnoreBrowserAttribute(Browser browser, string reason)
4137
: this(browser)
4238
{
43-
this.ignoreReason = reason;
39+
this.Reason = reason;
4440
}
4541

46-
public Browser Value
47-
{
48-
get { return browser; }
49-
}
42+
public Browser Value { get; }
5043

51-
public string Reason
52-
{
53-
get { return ignoreReason; }
54-
}
44+
public string Reason { get; } = string.Empty;
5545

5646
public void ApplyToTest(Test test)
5747
{
5848
if (test.RunState != RunState.NotRunnable)
5949
{
60-
List<Attribute> ignoreAttributes = new List<Attribute>();
50+
Attribute[] ignoreAttributes;
6151
if (test.IsSuite)
6252
{
63-
Attribute[] ignoreClassAttributes = test.TypeInfo.GetCustomAttributes<IgnoreBrowserAttribute>(true);
64-
if (ignoreClassAttributes.Length > 0)
65-
{
66-
ignoreAttributes.AddRange(ignoreClassAttributes);
67-
}
53+
ignoreAttributes = test.TypeInfo.GetCustomAttributes<IgnoreBrowserAttribute>(true);
6854
}
6955
else
7056
{
71-
IgnoreBrowserAttribute[] ignoreMethodAttributes = test.Method.GetCustomAttributes<IgnoreBrowserAttribute>(true);
72-
if (ignoreMethodAttributes.Length > 0)
73-
{
74-
ignoreAttributes.AddRange(ignoreMethodAttributes);
75-
}
57+
ignoreAttributes = test.Method.GetCustomAttributes<IgnoreBrowserAttribute>(true);
7658
}
7759

7860
foreach (Attribute attr in ignoreAttributes)
7961
{
80-
IgnoreBrowserAttribute browserToIgnoreAttr = attr as IgnoreBrowserAttribute;
81-
if (browserToIgnoreAttr != null && IgnoreTestForBrowser(browserToIgnoreAttr.Value))
62+
if (attr is IgnoreBrowserAttribute browserToIgnoreAttr
63+
&& IgnoreTestForBrowser(browserToIgnoreAttr.Value))
8264
{
83-
string ignoreReason = "Ignoring browser " + EnvironmentManager.Instance.Browser.ToString() + ".";
65+
string ignoreReason = $"Ignoring browser {EnvironmentManager.Instance.Browser}.";
8466
if (!string.IsNullOrEmpty(browserToIgnoreAttr.Reason))
8567
{
8668
ignoreReason = ignoreReason + " " + browserToIgnoreAttr.Reason;
8769
}
8870

8971
test.RunState = RunState.Ignored;
90-
test.Properties.Set(PropertyNames.SkipReason, browserToIgnoreAttr.Reason);
72+
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
9173
}
9274
}
9375
}
9476
}
9577

96-
private bool IgnoreTestForBrowser(Browser browserToIgnore)
78+
private static bool IgnoreTestForBrowser(Browser browserToIgnore)
9779
{
9880
return browserToIgnore.Equals(EnvironmentManager.Instance.Browser) || browserToIgnore.Equals(Browser.All) || IsRemoteInstanceOfBrowser(browserToIgnore);
9981
}
10082

101-
private bool IsRemoteInstanceOfBrowser(Browser desiredBrowser)
83+
private static bool IsRemoteInstanceOfBrowser(Browser desiredBrowser)
10284
{
103-
bool isRemoteInstance = false;
104-
switch (desiredBrowser)
85+
return (desiredBrowser, EnvironmentManager.Instance.RemoteCapabilities) switch
10586
{
106-
case Browser.IE:
107-
if (EnvironmentManager.Instance.RemoteCapabilities == "internet explorer")
108-
{
109-
isRemoteInstance = true;
110-
}
111-
break;
87+
(Browser.IE, "internet explorer") => true,
88+
(Browser.Firefox, "firefox") => true,
89+
(Browser.Chrome, "chrome") => true,
90+
(Browser.Edge, "MicrosoftEdge") => true,
91+
_ => false,
92+
};
93+
}
94+
}
95+
}
11296

113-
case Browser.Firefox:
114-
if (EnvironmentManager.Instance.RemoteCapabilities == "firefox")
115-
{
116-
isRemoteInstance = true;
117-
}
118-
break;
97+
class C
98+
{
99+
public static bool AreDefault(int i, string j)
100+
{
101+
switch (i, j)
102+
{
103+
case (0, _):
104+
return true;
119105

120-
case Browser.Chrome:
121-
if (EnvironmentManager.Instance.RemoteCapabilities == "chrome")
122-
{
123-
isRemoteInstance = true;
124-
}
125-
break;
126-
case Browser.Edge:
127-
if (EnvironmentManager.Instance.RemoteCapabilities == "MicrosoftEdge")
128-
{
129-
isRemoteInstance = true;
130-
}
131-
break;
132-
}
133-
return isRemoteInstance;
106+
case (_, null):
107+
return true;
108+
109+
default:
110+
return false;
134111
}
135112
}
136113
}

dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,105 +22,86 @@
2222
using NUnit.Framework.Internal;
2323
using OpenQA.Selenium.Environment;
2424
using System;
25-
using System.Collections.Generic;
2625
using System.Runtime.InteropServices;
27-
using OSPlatform = System.Runtime.InteropServices.OSPlatform;
28-
2926

3027
namespace OpenQA.Selenium
3128
{
3229
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
3330
public class IgnorePlatformAttribute : NUnitAttribute, IApplyToTest
3431
{
35-
private readonly String platform;
36-
private readonly string ignoreReason = string.Empty;
32+
public const string Windows = nameof(Windows);
33+
public const string Linux = nameof(Linux);
34+
public const string Mac = nameof(Mac);
3735

3836
public IgnorePlatformAttribute(string platform)
3937
{
40-
this.platform = platform.ToLower();
38+
this.Value = platform.ToLowerInvariant();
4139
}
4240

4341
public IgnorePlatformAttribute(string platform, string reason)
4442
: this(platform)
4543
{
46-
this.ignoreReason = reason;
44+
this.Reason = reason;
4745
}
4846

49-
public string Value
50-
{
51-
get { return platform; }
52-
}
47+
public string Value { get; }
5348

54-
public string Reason
55-
{
56-
get { return ignoreReason; }
57-
}
49+
public string Reason { get; } = string.Empty;
5850

5951
public void ApplyToTest(Test test)
6052
{
6153
if (test.RunState != RunState.NotRunnable)
6254
{
63-
List<Attribute> ignoreAttributes = new List<Attribute>();
55+
Attribute[] ignoreAttributes;
6456
if (test.IsSuite)
6557
{
66-
Attribute[] ignoreClassAttributes =
67-
test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
68-
if (ignoreClassAttributes.Length > 0)
69-
{
70-
ignoreAttributes.AddRange(ignoreClassAttributes);
71-
}
58+
ignoreAttributes = test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
7259
}
7360
else
7461
{
75-
IgnorePlatformAttribute[] ignoreMethodAttributes =
76-
test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
77-
if (ignoreMethodAttributes.Length > 0)
78-
{
79-
ignoreAttributes.AddRange(ignoreMethodAttributes);
80-
}
62+
ignoreAttributes = test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
8163
}
8264

8365
foreach (Attribute attr in ignoreAttributes)
8466
{
85-
IgnorePlatformAttribute platformToIgnoreAttr = attr as IgnorePlatformAttribute;
86-
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
67+
if (attr is IgnorePlatformAttribute platformToIgnoreAttr
68+
&& IgnoreTestForPlatform(platformToIgnoreAttr.Value))
8769
{
88-
string ignoreReason =
89-
"Ignoring platform " + EnvironmentManager.Instance.Browser.ToString() + ".";
70+
string ignoreReason = $"Ignoring platform {EnvironmentManager.Instance.Browser}.";
9071
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
9172
{
9273
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
9374
}
9475

9576
test.RunState = RunState.Ignored;
96-
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
77+
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
9778
}
9879
}
9980
}
10081
}
10182

102-
private bool IgnoreTestForPlatform(string platformToIgnore)
83+
private static bool IgnoreTestForPlatform(string platformToIgnore)
10384
{
104-
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
85+
return platformToIgnore.Equals(CurrentPlatform(), StringComparison.OrdinalIgnoreCase);
10586
}
10687

107-
private string CurrentPlatform()
88+
private static string CurrentPlatform()
10889
{
109-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
90+
if (OperatingSystem.IsWindows())
11091
{
111-
return "windows";
92+
return Windows;
11293
}
113-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
94+
else if (OperatingSystem.IsLinux())
11495
{
115-
return "linux";
96+
return Linux;
11697
}
117-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
98+
else if (OperatingSystem.IsMacOS())
11899
{
119-
return "mac";
100+
return Mac;
120101
}
121102
else
122103
{
123-
throw new WebDriverException("Selenium Manager did not find supported operating system");
104+
throw new PlatformNotSupportedException($"Selenium Manager did not find supported operating system: {RuntimeInformation.OSDescription}");
124105
}
125106
}
126107
}

dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
3232
{
3333
public IgnoreTargetAttribute(string target)
3434
{
35-
this.Value = target.ToLower();
35+
this.Value = target.ToLowerInvariant();
3636
}
3737

3838
public IgnoreTargetAttribute(string target, string reason)

dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,24 @@ namespace OpenQA.Selenium
2525
{
2626
public class NeedsFreshDriverAttribute : TestActionAttribute
2727
{
28-
private bool isCreatedBeforeTest = false;
29-
private bool isCreatedAfterTest = false;
28+
public bool IsCreatedBeforeTest { get; set; } = false;
3029

31-
public bool IsCreatedBeforeTest
32-
{
33-
get { return isCreatedBeforeTest; }
34-
set { isCreatedBeforeTest = value; }
35-
}
36-
37-
public bool IsCreatedAfterTest
38-
{
39-
get { return isCreatedAfterTest; }
40-
set { isCreatedAfterTest = value; }
41-
}
30+
public bool IsCreatedAfterTest { get; set; } = false;
4231

4332
public override void BeforeTest(ITest test)
4433
{
45-
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
46-
if (fixtureInstance != null && this.isCreatedBeforeTest)
34+
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedBeforeTest)
4735
{
4836
EnvironmentManager.Instance.CreateFreshDriver();
4937
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
5038
}
39+
5140
base.BeforeTest(test);
5241
}
5342

5443
public override void AfterTest(ITest test)
5544
{
56-
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
57-
if (fixtureInstance != null && this.isCreatedAfterTest)
45+
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedAfterTest)
5846
{
5947
EnvironmentManager.Instance.CreateFreshDriver();
6048
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();

dotnet/test/common/DevTools/DevToolsPerformanceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ await domains.Performance.SetTimeDomain(new CurrentCdpVersion.Performance.SetTim
6969
}
7070

7171
[Test]
72-
[IgnorePlatform("Windows", "Thread time is not supported on this platform")]
72+
[IgnorePlatform(IgnorePlatformAttribute.Windows, "Thread time is not supported on this platform")]
7373
[IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
7474
[IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
7575
[IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]
@@ -107,7 +107,7 @@ await domains.Performance.SetTimeDomain(new CurrentCdpVersion.Performance.SetTim
107107
}
108108

109109
[Test]
110-
[IgnorePlatform("Windows", "Thread time is not supported on this platform")]
110+
[IgnorePlatform(IgnorePlatformAttribute.Windows, "Thread time is not supported on this platform")]
111111
[IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
112112
[IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
113113
[IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]

dotnet/test/common/Environment/DriverStartingEventArgs.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ namespace OpenQA.Selenium.Environment
2121
{
2222
public class DriverStartingEventArgs
2323
{
24-
DriverService service;
25-
DriverOptions options;
26-
2724
public DriverStartingEventArgs(DriverService service, DriverOptions options)
2825
{
2926
this.Service = service;
3027
this.Options = options;
3128
}
3229

33-
public DriverService Service { get => service; set => service = value; }
30+
public DriverService Service { get; set; }
3431

35-
public DriverOptions Options { get => options; set => options = value; }
32+
public DriverOptions Options { get; set; }
3633
}
3734
}

dotnet/test/common/Environment/InlinePage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ namespace OpenQA.Selenium.Environment
2525
public class InlinePage
2626
{
2727
private string title = string.Empty;
28-
private List<string> scripts = new List<string>();
29-
private List<string> styles = new List<string>();
30-
private List<string> bodyParts = new List<string>();
28+
private readonly List<string> scripts = new List<string>();
29+
private readonly List<string> styles = new List<string>();
30+
private readonly List<string> bodyParts = new List<string>();
3131
private string onLoad;
3232
private string onBeforeUnload;
3333

dotnet/test/common/Environment/RemoteSeleniumServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class RemoteSeleniumServer
3030
{
3131
private Process webserverProcess;
3232
private string serverJarName = @"java/src/org/openqa/selenium/grid/selenium_server_deploy.jar";
33-
private string projectRootPath;
34-
private bool autoStart;
33+
private readonly string projectRootPath;
34+
private readonly bool autoStart;
3535

3636
public RemoteSeleniumServer(string projectRoot, bool autoStartServer)
3737
{

0 commit comments

Comments
 (0)