Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Simplify testing driver factory #15245

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public StableChannelEdgeDriver(EdgeDriverService service, EdgeOptions options)
: base(service, options)
{
}

public static EdgeOptions DefaultOptions
{
get { return new EdgeOptions(); }
Expand Down
81 changes: 20 additions & 61 deletions dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,115 +22,74 @@
using NUnit.Framework.Internal;
using OpenQA.Selenium.Environment;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class IgnoreBrowserAttribute : NUnitAttribute, IApplyToTest
{
private readonly Browser browser;
private readonly string ignoreReason = string.Empty;

public IgnoreBrowserAttribute(Browser browser)
{
this.browser = browser;
this.Value = browser;
}

public IgnoreBrowserAttribute(Browser browser, string reason)
: this(browser)
{
this.ignoreReason = reason;
this.Reason = reason;
}

public Browser Value
{
get { return browser; }
}
public Browser Value { get; }

public string Reason
{
get { return ignoreReason; }
}
public string Reason { get; } = string.Empty;

public void ApplyToTest(Test test)
{
if (test.RunState != RunState.NotRunnable)
{
List<Attribute> ignoreAttributes = new List<Attribute>();
Attribute[] ignoreAttributes;
if (test.IsSuite)
{
Attribute[] ignoreClassAttributes = test.TypeInfo.GetCustomAttributes<IgnoreBrowserAttribute>(true);
if (ignoreClassAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreClassAttributes);
}
ignoreAttributes = test.TypeInfo.GetCustomAttributes<IgnoreBrowserAttribute>(true);
}
else
{
IgnoreBrowserAttribute[] ignoreMethodAttributes = test.Method.GetCustomAttributes<IgnoreBrowserAttribute>(true);
if (ignoreMethodAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreMethodAttributes);
}
ignoreAttributes = test.Method.GetCustomAttributes<IgnoreBrowserAttribute>(true);
}

foreach (Attribute attr in ignoreAttributes)
{
IgnoreBrowserAttribute browserToIgnoreAttr = attr as IgnoreBrowserAttribute;
if (browserToIgnoreAttr != null && IgnoreTestForBrowser(browserToIgnoreAttr.Value))
if (attr is IgnoreBrowserAttribute browserToIgnoreAttr
&& IgnoreTestForBrowser(browserToIgnoreAttr.Value))
{
string ignoreReason = "Ignoring browser " + EnvironmentManager.Instance.Browser.ToString() + ".";
string ignoreReason = $"Ignoring browser {EnvironmentManager.Instance.Browser}.";
if (!string.IsNullOrEmpty(browserToIgnoreAttr.Reason))
{
ignoreReason = ignoreReason + " " + browserToIgnoreAttr.Reason;
}

test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, browserToIgnoreAttr.Reason);
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
}
}
}
}

private bool IgnoreTestForBrowser(Browser browserToIgnore)
private static bool IgnoreTestForBrowser(Browser browserToIgnore)
{
return browserToIgnore.Equals(EnvironmentManager.Instance.Browser) || browserToIgnore.Equals(Browser.All) || IsRemoteInstanceOfBrowser(browserToIgnore);
}

private bool IsRemoteInstanceOfBrowser(Browser desiredBrowser)
private static bool IsRemoteInstanceOfBrowser(Browser desiredBrowser)
{
bool isRemoteInstance = false;
switch (desiredBrowser)
return (desiredBrowser, EnvironmentManager.Instance.RemoteCapabilities) switch
{
case Browser.IE:
if (EnvironmentManager.Instance.RemoteCapabilities == "internet explorer")
{
isRemoteInstance = true;
}
break;

case Browser.Firefox:
if (EnvironmentManager.Instance.RemoteCapabilities == "firefox")
{
isRemoteInstance = true;
}
break;

case Browser.Chrome:
if (EnvironmentManager.Instance.RemoteCapabilities == "chrome")
{
isRemoteInstance = true;
}
break;
case Browser.Edge:
if (EnvironmentManager.Instance.RemoteCapabilities == "MicrosoftEdge")
{
isRemoteInstance = true;
}
break;
}
return isRemoteInstance;
(Browser.IE, "internet explorer") => true,
(Browser.Firefox, "firefox") => true,
(Browser.Chrome, "chrome") => true,
(Browser.Edge, "MicrosoftEdge") => true,
_ => false,
};
}
}
}
67 changes: 24 additions & 43 deletions dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,105 +22,86 @@
using NUnit.Framework.Internal;
using OpenQA.Selenium.Environment;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using OSPlatform = System.Runtime.InteropServices.OSPlatform;


namespace OpenQA.Selenium
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class IgnorePlatformAttribute : NUnitAttribute, IApplyToTest
{
private readonly String platform;
private readonly string ignoreReason = string.Empty;
public const string Windows = nameof(Windows);
public const string Linux = nameof(Linux);
public const string Mac = nameof(Mac);

public IgnorePlatformAttribute(string platform)
{
this.platform = platform.ToLower();
this.Value = platform.ToLowerInvariant();
}

public IgnorePlatformAttribute(string platform, string reason)
: this(platform)
{
this.ignoreReason = reason;
this.Reason = reason;
}

public string Value
{
get { return platform; }
}
public string Value { get; }

public string Reason
{
get { return ignoreReason; }
}
public string Reason { get; } = string.Empty;

public void ApplyToTest(Test test)
{
if (test.RunState != RunState.NotRunnable)
{
List<Attribute> ignoreAttributes = new List<Attribute>();
Attribute[] ignoreAttributes;
if (test.IsSuite)
{
Attribute[] ignoreClassAttributes =
test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
if (ignoreClassAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreClassAttributes);
}
ignoreAttributes = test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
}
else
{
IgnorePlatformAttribute[] ignoreMethodAttributes =
test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
if (ignoreMethodAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreMethodAttributes);
}
ignoreAttributes = test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
}

foreach (Attribute attr in ignoreAttributes)
{
IgnorePlatformAttribute platformToIgnoreAttr = attr as IgnorePlatformAttribute;
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
if (attr is IgnorePlatformAttribute platformToIgnoreAttr
&& IgnoreTestForPlatform(platformToIgnoreAttr.Value))
{
string ignoreReason =
"Ignoring platform " + EnvironmentManager.Instance.Browser.ToString() + ".";
string ignoreReason = $"Ignoring platform {EnvironmentManager.Instance.Browser}.";
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
{
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
}

test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
}
}
}
}

private bool IgnoreTestForPlatform(string platformToIgnore)
private static bool IgnoreTestForPlatform(string platformToIgnore)
{
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
return platformToIgnore.Equals(CurrentPlatform(), StringComparison.OrdinalIgnoreCase);
}

private string CurrentPlatform()
private static string CurrentPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
return "windows";
return Windows;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
else if (OperatingSystem.IsLinux())
{
return "linux";
return Linux;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (OperatingSystem.IsMacOS())
{
return "mac";
return Mac;
}
else
{
throw new WebDriverException("Selenium Manager did not find supported operating system");
throw new PlatformNotSupportedException($"Selenium Manager did not find supported operating system: {RuntimeInformation.OSDescription}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
{
public IgnoreTargetAttribute(string target)
{
this.Value = target.ToLower();
this.Value = target.ToLowerInvariant();
}

public IgnoreTargetAttribute(string target, string reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,24 @@ namespace OpenQA.Selenium
{
public class NeedsFreshDriverAttribute : TestActionAttribute
{
private bool isCreatedBeforeTest = false;
private bool isCreatedAfterTest = false;
public bool IsCreatedBeforeTest { get; set; } = false;

public bool IsCreatedBeforeTest
{
get { return isCreatedBeforeTest; }
set { isCreatedBeforeTest = value; }
}

public bool IsCreatedAfterTest
{
get { return isCreatedAfterTest; }
set { isCreatedAfterTest = value; }
}
public bool IsCreatedAfterTest { get; set; } = false;

public override void BeforeTest(ITest test)
{
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
if (fixtureInstance != null && this.isCreatedBeforeTest)
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedBeforeTest)
{
EnvironmentManager.Instance.CreateFreshDriver();
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
}

base.BeforeTest(test);
}

public override void AfterTest(ITest test)
{
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
if (fixtureInstance != null && this.isCreatedAfterTest)
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedAfterTest)
{
EnvironmentManager.Instance.CreateFreshDriver();
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
Expand Down
4 changes: 2 additions & 2 deletions dotnet/test/common/DevTools/DevToolsPerformanceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ await domains.Performance.SetTimeDomain(new CurrentCdpVersion.Performance.SetTim
}

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

[Test]
[IgnorePlatform("Windows", "Thread time is not supported on this platform")]
[IgnorePlatform(IgnorePlatformAttribute.Windows, "Thread time is not supported on this platform")]
[IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
[IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
[IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]
Expand Down
Loading
Loading