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 4 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
5 changes: 5 additions & 0 deletions dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public DefaultSafariDriver(SafariOptions options)
{
}

public DefaultSafariDriver(SafariDriverService service)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it is in runtime. As I remember we always use ctor with service and options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it was included in your PR #14662 so it would be simpler to include it here and shrink that PR.

If we don't use it, then that's OK. I think it doesn't hurt anything, and makes potential future refactorings simpler, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think code, which is not used, makes life easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

: base(service)
{
}

public DefaultSafariDriver(SafariDriverService service, SafariOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public DevChannelChromeDriver(ChromeOptions options)
{
}

public DevChannelChromeDriver(ChromeDriverService service)
: base(service)
{
}

public DevChannelChromeDriver(ChromeDriverService service, ChromeOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public DevChannelEdgeDriver(EdgeOptions options)
{
}

public DevChannelEdgeDriver(EdgeDriverService service)
: base(service)
{
}

public DevChannelEdgeDriver(EdgeDriverService service, EdgeOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public EdgeInternetExplorerModeDriver(InternetExplorerOptions options)
{
}

public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service)
: base(service)
{
}

public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service, InternetExplorerOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public NightlyChannelFirefoxDriver(FirefoxOptions options)
{
}

public NightlyChannelFirefoxDriver(FirefoxDriverService service)
: base(service)
{
}

public NightlyChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public SafariTechnologyPreviewDriver(SafariOptions options)
{
}

public SafariTechnologyPreviewDriver(SafariDriverService service)
: base(service)
{
}

public SafariTechnologyPreviewDriver(SafariDriverService service, SafariOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public StableChannelChromeDriver(ChromeOptions options)
{
}

public StableChannelChromeDriver(ChromeDriverService service)
: base(service)
{
}

public StableChannelChromeDriver(ChromeDriverService service, ChromeOptions options)
: base(service, options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ public StableChannelEdgeDriver(EdgeOptions options)
{
}

public StableChannelEdgeDriver(EdgeDriverService service)
: base(service)
{
}

public StableChannelEdgeDriver(EdgeDriverService service, EdgeOptions options)
: base(service, options)
{
}

public static EdgeOptions DefaultOptions
{
get { return new EdgeOptions(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public StableChannelFirefoxDriver(FirefoxOptions options)
{
}

public StableChannelFirefoxDriver(FirefoxDriverService service)
: base(service)
{
}

public StableChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options)
: base(service, options)
{
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
Loading
Loading