From c321a466bed183ae2b11ce43f080a1594096ebf2 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 6 Feb 2025 01:05:56 -0500 Subject: [PATCH 1/4] [dotnet] Simplify testing driver factory --- .../DefaultSafariDriver.cs | 5 ++ .../DevChannelChromeDriver.cs | 5 ++ .../DevChannelEdgeDriver.cs | 5 ++ .../EdgeInternetExplorerModeDriver.cs | 5 ++ .../NightlyChannelFirefoxDriver.cs | 5 ++ .../SafariTechnologyPreviewDriver.cs | 5 ++ .../StableChannelChromeDriver.cs | 5 ++ .../StableChannelEdgeDriver.cs | 6 ++ .../StableChannelFirefoxDriver.cs | 5 ++ .../test/common/Environment/DriverFactory.cs | 76 ++++++---------- .../common/Environment/EnvironmentManager.cs | 86 ++++--------------- 11 files changed, 89 insertions(+), 119 deletions(-) diff --git a/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs b/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs index 311a2832a8e2c..4f1a9801d902c 100644 --- a/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs @@ -30,6 +30,11 @@ public DefaultSafariDriver(SafariOptions options) { } + public DefaultSafariDriver(SafariDriverService service) + : base(service) + { + } + public DefaultSafariDriver(SafariDriverService service, SafariOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs b/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs index 1f0a17e677583..5c712eaa23166 100644 --- a/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs @@ -32,6 +32,11 @@ public DevChannelChromeDriver(ChromeOptions options) { } + public DevChannelChromeDriver(ChromeDriverService service) + : base(service) + { + } + public DevChannelChromeDriver(ChromeDriverService service, ChromeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs b/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs index 0fbcbf0189093..a444c1e942bd2 100644 --- a/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs @@ -32,6 +32,11 @@ public DevChannelEdgeDriver(EdgeOptions options) { } + public DevChannelEdgeDriver(EdgeDriverService service) + : base(service) + { + } + public DevChannelEdgeDriver(EdgeDriverService service, EdgeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs b/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs index 09c97ba2ea82c..199ce89ec41e3 100644 --- a/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs @@ -35,6 +35,11 @@ public EdgeInternetExplorerModeDriver(InternetExplorerOptions options) { } + public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service) + : base(service) + { + } + public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service, InternetExplorerOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs b/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs index a551057fa4b92..346c720678e64 100644 --- a/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs @@ -35,6 +35,11 @@ public NightlyChannelFirefoxDriver(FirefoxOptions options) { } + public NightlyChannelFirefoxDriver(FirefoxDriverService service) + : base(service) + { + } + public NightlyChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs b/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs index 5143122fb416b..a9df1982ab316 100644 --- a/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs @@ -35,6 +35,11 @@ public SafariTechnologyPreviewDriver(SafariOptions options) { } + public SafariTechnologyPreviewDriver(SafariDriverService service) + : base(service) + { + } + public SafariTechnologyPreviewDriver(SafariDriverService service, SafariOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs index 0f9edb86f98cf..234e68cb57d4d 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs @@ -32,6 +32,11 @@ public StableChannelChromeDriver(ChromeOptions options) { } + public StableChannelChromeDriver(ChromeDriverService service) + : base(service) + { + } + public StableChannelChromeDriver(ChromeDriverService service, ChromeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs index f73a1c14faae0..a54b768ee9553 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs @@ -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(); } diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs index ae487e6e3de35..c9f747b48471a 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs @@ -35,6 +35,11 @@ public StableChannelFirefoxDriver(FirefoxOptions options) { } + public StableChannelFirefoxDriver(FirefoxDriverService service) + : base(service) + { + } + public StableChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options) : base(service, options) { diff --git a/dotnet/test/common/Environment/DriverFactory.cs b/dotnet/test/common/Environment/DriverFactory.cs index c993a5d37ab5b..7a68723545a63 100644 --- a/dotnet/test/common/Environment/DriverFactory.cs +++ b/dotnet/test/common/Environment/DriverFactory.cs @@ -32,10 +32,10 @@ namespace OpenQA.Selenium.Environment { public class DriverFactory { - string driverPath; - string browserBinaryLocation; - private Dictionary serviceTypes = new Dictionary(); - private Dictionary optionsTypes = new Dictionary(); + private readonly string driverPath; + private readonly string browserBinaryLocation; + private readonly Dictionary serviceTypes = new Dictionary(); + private readonly Dictionary optionsTypes = new Dictionary(); public DriverFactory(string driverPath, string browserBinaryLocation) { @@ -71,17 +71,15 @@ public IWebDriver CreateDriver(Type driverType, bool logging = false) return CreateDriverWithOptions(driverType, null, logging); } - public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions, bool logging = false) + public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions, bool enableLogging = false) { Console.WriteLine($"Creating new driver of {driverType} type..."); Browser browser = Browser.All; DriverService service = null; DriverOptions options = null; - bool enableLogging = logging; - List constructorArgTypeList = new List(); - IWebDriver driver = null; + IWebDriver driver; if (typeof(ChromeDriver).IsAssignableFrom(driverType)) { browser = Browser.Chrome; @@ -149,7 +147,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO service = CreateService(); } - if (!String.IsNullOrEmpty(this.driverPath) && service != null) + if (!string.IsNullOrEmpty(this.driverPath) && service != null) { service.DriverServicePath = Path.GetDirectoryName(this.driverPath); service.DriverServiceExecutableName = Path.GetFileName(this.driverPath); @@ -159,12 +157,10 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO if (browser != Browser.All) { - constructorArgTypeList.Add(this.serviceTypes[browser]); - constructorArgTypeList.Add(this.optionsTypes[browser]); - ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray()); + ConstructorInfo ctorInfo = driverType.GetConstructor([this.serviceTypes[browser], this.optionsTypes[browser]]); if (ctorInfo != null) { - return (IWebDriver)ctorInfo.Invoke(new object[] { service, options }); + return (IWebDriver)ctorInfo.Invoke([service, options]); } } @@ -176,20 +172,23 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options) { if (this.DriverStarting != null) { - DriverStartingEventArgs args = new DriverStartingEventArgs(service, options); - this.DriverStarting(this, args); + this.DriverStarting(this, new DriverStartingEventArgs(service, options)); } } - private T GetDriverOptions(Type driverType, DriverOptions overriddenOptions) where T : DriverOptions, new() + private TOptions GetDriverOptions(Type driverType, DriverOptions overriddenOptions) + where TOptions : DriverOptions, new() { - T options = new T(); - Type optionsType = typeof(T); + TOptions options; PropertyInfo defaultOptionsProperty = driverType.GetProperty("DefaultOptions", BindingFlags.Public | BindingFlags.Static); - if (defaultOptionsProperty != null && defaultOptionsProperty.PropertyType == optionsType) + if (defaultOptionsProperty != null && defaultOptionsProperty.PropertyType == typeof(TOptions)) { - options = (T)defaultOptionsProperty.GetValue(null, null); + options = (TOptions)defaultOptionsProperty.GetValue(null, null); + } + else + { + options = new TOptions(); } if (overriddenOptions != null) @@ -208,41 +207,16 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options) return options; } - - private T MergeOptions(object baseOptions, DriverOptions overriddenOptions) where T : DriverOptions, new() - { - // If the driver type has a static DefaultOptions property, - // get the value of that property, which should be a valid - // options of the generic type (T). Otherwise, create a new - // instance of the browser-specific options class. - T mergedOptions = new T(); - if (baseOptions != null && baseOptions is T) - { - mergedOptions = (T)baseOptions; - } - - if (overriddenOptions != null) - { - mergedOptions.PageLoadStrategy = overriddenOptions.PageLoadStrategy; - mergedOptions.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior; - mergedOptions.Proxy = overriddenOptions.Proxy; - } - - return mergedOptions; - } - - private T CreateService() where T : DriverService + private TService CreateService() + where TService : DriverService { - T service = default(T); - Type serviceType = typeof(T); - - MethodInfo createDefaultServiceMethod = serviceType.GetMethod("CreateDefaultService", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null); - if (createDefaultServiceMethod != null && createDefaultServiceMethod.ReturnType == serviceType) + MethodInfo createDefaultServiceMethod = typeof(TService).GetMethod("CreateDefaultService", BindingFlags.Public | BindingFlags.Static, null, [], null); + if (createDefaultServiceMethod != null && createDefaultServiceMethod.ReturnType == typeof(TService)) { - service = (T)createDefaultServiceMethod.Invoke(null, new object[] { }); + return (TService)createDefaultServiceMethod.Invoke(null, []); } - return service; + return default; } } } diff --git a/dotnet/test/common/Environment/EnvironmentManager.cs b/dotnet/test/common/Environment/EnvironmentManager.cs index 9c0b53b90c56c..fcdf1c2a58545 100644 --- a/dotnet/test/common/Environment/EnvironmentManager.cs +++ b/dotnet/test/common/Environment/EnvironmentManager.cs @@ -31,14 +31,9 @@ namespace OpenQA.Selenium.Environment public class EnvironmentManager { private static EnvironmentManager instance; - private Type driverType; - private Browser browser; + private readonly Type driverType; private IWebDriver driver; - private UrlBuilder urlBuilder; - private TestWebServer webServer; - private DriverFactory driverFactory; - private RemoteSeleniumServer remoteServer; - private string remoteCapabilities; + private readonly DriverFactory driverFactory; private EnvironmentManager() { @@ -91,10 +86,10 @@ private EnvironmentManager() throw new ArgumentOutOfRangeException($"Unable to find driver type {driverConfig.DriverTypeName}"); } - browser = driverConfig.BrowserValue; - remoteCapabilities = driverConfig.RemoteCapabilities; + Browser = driverConfig.BrowserValue; + RemoteCapabilities = driverConfig.RemoteCapabilities; - urlBuilder = new UrlBuilder(websiteConfig); + UrlBuilder = new UrlBuilder(websiteConfig); // When run using the `bazel test` command, the following environment // variable will be set. If not set, we're running from a build system @@ -185,48 +180,28 @@ private EnvironmentManager() // Use the default one. } - webServer = new TestWebServer(projectRoot, webServerConfig); + WebServer = new TestWebServer(projectRoot, webServerConfig); bool autoStartRemoteServer = false; - if (browser == Browser.Remote) + if (Browser == Browser.Remote) { autoStartRemoteServer = driverConfig.AutoStartRemoteServer; } - remoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer); + RemoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer); } ~EnvironmentManager() { - if (remoteServer != null) - { - remoteServer.StopAsync().Wait(); - } - if (webServer != null) - { - webServer.StopAsync().Wait(); - } + RemoteServer?.StopAsync().Wait(); + WebServer?.StopAsync().Wait(); CloseCurrentDriver(); } public event EventHandler DriverStarting; - public static EnvironmentManager Instance - { - get - { - if (instance == null) - { - instance = new EnvironmentManager(); - } - - return instance; - } - } + public static EnvironmentManager Instance => instance ??= new EnvironmentManager(); - public Browser Browser - { - get { return browser; } - } + public Browser Browser { get; } public string CurrentDirectory { @@ -242,39 +217,17 @@ public string CurrentDirectory } } - public TestWebServer WebServer - { - get { return webServer; } - } + public TestWebServer WebServer { get; } - public RemoteSeleniumServer RemoteServer - { - get { return remoteServer; } - } + public RemoteSeleniumServer RemoteServer { get; } - public string RemoteCapabilities - { - get { return remoteCapabilities; } - } + public string RemoteCapabilities { get; } - public UrlBuilder UrlBuilder - { - get - { - return urlBuilder; - } - } + public UrlBuilder UrlBuilder { get; } public IWebDriver GetCurrentDriver() { - if (driver != null) - { - return driver; - } - else - { - return CreateFreshDriver(); - } + return driver ?? CreateFreshDriver(); } public IWebDriver CreateDriverInstance() @@ -296,10 +249,7 @@ public IWebDriver CreateFreshDriver() public void CloseCurrentDriver() { - if (driver != null) - { - driver.Quit(); - } + driver?.Quit(); driver = null; } From 3c94dffc6a1b9761c97cc9104c9799e1337e66aa Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 6 Feb 2025 10:48:51 -0500 Subject: [PATCH 2/4] Modernize more testing infrastructure --- .../IgnoreBrowserAttribute.cs | 95 +++++++------------ .../IgnorePlatformAttribute.cs | 67 +++++-------- .../IgnoreTargetAttribute.cs | 2 +- .../NeedsFreshDriverAttribute.cs | 22 +---- .../DevTools/DevToolsPerformanceTest.cs | 4 +- .../Environment/DriverStartingEventArgs.cs | 7 +- dotnet/test/common/Environment/InlinePage.cs | 6 +- .../Environment/RemoteSeleniumServer.cs | 4 +- .../test/common/Environment/TestWebServer.cs | 9 +- dotnet/test/common/Environment/UrlBuilder.cs | 66 ++++--------- 10 files changed, 102 insertions(+), 180 deletions(-) diff --git a/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs b/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs index ff3bc71d50522..675e49c672ccc 100644 --- a/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs @@ -22,115 +22,92 @@ 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 ignoreAttributes = new List(); + Attribute[] ignoreAttributes; if (test.IsSuite) { - Attribute[] ignoreClassAttributes = test.TypeInfo.GetCustomAttributes(true); - if (ignoreClassAttributes.Length > 0) - { - ignoreAttributes.AddRange(ignoreClassAttributes); - } + ignoreAttributes = test.TypeInfo.GetCustomAttributes(true); } else { - IgnoreBrowserAttribute[] ignoreMethodAttributes = test.Method.GetCustomAttributes(true); - if (ignoreMethodAttributes.Length > 0) - { - ignoreAttributes.AddRange(ignoreMethodAttributes); - } + ignoreAttributes = test.Method.GetCustomAttributes(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; + (Browser.IE, "internet explorer") => true, + (Browser.Firefox, "firefox") => true, + (Browser.Chrome, "chrome") => true, + (Browser.Edge, "MicrosoftEdge") => true, + _ => false, + }; + } + } +} - case Browser.Firefox: - if (EnvironmentManager.Instance.RemoteCapabilities == "firefox") - { - isRemoteInstance = true; - } - break; +class C +{ + public static bool AreDefault(int i, string j) + { + switch (i, j) + { + case (0, _): + return true; - case Browser.Chrome: - if (EnvironmentManager.Instance.RemoteCapabilities == "chrome") - { - isRemoteInstance = true; - } - break; - case Browser.Edge: - if (EnvironmentManager.Instance.RemoteCapabilities == "MicrosoftEdge") - { - isRemoteInstance = true; - } - break; - } - return isRemoteInstance; + case (_, null): + return true; + + default: + return false; } } } diff --git a/dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs b/dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs index 0e6622ebc4ade..9d12ebc487f93 100644 --- a/dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs @@ -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 ignoreAttributes = new List(); + Attribute[] ignoreAttributes; if (test.IsSuite) { - Attribute[] ignoreClassAttributes = - test.TypeInfo.GetCustomAttributes(true); - if (ignoreClassAttributes.Length > 0) - { - ignoreAttributes.AddRange(ignoreClassAttributes); - } + ignoreAttributes = test.TypeInfo.GetCustomAttributes(true); } else { - IgnorePlatformAttribute[] ignoreMethodAttributes = - test.Method.GetCustomAttributes(true); - if (ignoreMethodAttributes.Length > 0) - { - ignoreAttributes.AddRange(ignoreMethodAttributes); - } + ignoreAttributes = test.Method.GetCustomAttributes(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}"); } } } diff --git a/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs b/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs index 152e71cc946bf..5bfc336906bcb 100644 --- a/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs @@ -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) diff --git a/dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs b/dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs index 669dd82d01db4..a0c6daa49da71 100644 --- a/dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs @@ -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(); diff --git a/dotnet/test/common/DevTools/DevToolsPerformanceTest.cs b/dotnet/test/common/DevTools/DevToolsPerformanceTest.cs index 9995818f1a82b..fc5153f83db6c 100644 --- a/dotnet/test/common/DevTools/DevToolsPerformanceTest.cs +++ b/dotnet/test/common/DevTools/DevToolsPerformanceTest.cs @@ -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")] @@ -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")] diff --git a/dotnet/test/common/Environment/DriverStartingEventArgs.cs b/dotnet/test/common/Environment/DriverStartingEventArgs.cs index 2b37ba3c2cbc2..0cc1441d353df 100644 --- a/dotnet/test/common/Environment/DriverStartingEventArgs.cs +++ b/dotnet/test/common/Environment/DriverStartingEventArgs.cs @@ -21,17 +21,14 @@ namespace OpenQA.Selenium.Environment { public class DriverStartingEventArgs { - DriverService service; - DriverOptions options; - public DriverStartingEventArgs(DriverService service, DriverOptions options) { this.Service = service; this.Options = options; } - public DriverService Service { get => service; set => service = value; } + public DriverService Service { get; set; } - public DriverOptions Options { get => options; set => options = value; } + public DriverOptions Options { get; set; } } } diff --git a/dotnet/test/common/Environment/InlinePage.cs b/dotnet/test/common/Environment/InlinePage.cs index 660f959a46e01..cbe4ef87d3093 100644 --- a/dotnet/test/common/Environment/InlinePage.cs +++ b/dotnet/test/common/Environment/InlinePage.cs @@ -25,9 +25,9 @@ namespace OpenQA.Selenium.Environment public class InlinePage { private string title = string.Empty; - private List scripts = new List(); - private List styles = new List(); - private List bodyParts = new List(); + private readonly List scripts = new List(); + private readonly List styles = new List(); + private readonly List bodyParts = new List(); private string onLoad; private string onBeforeUnload; diff --git a/dotnet/test/common/Environment/RemoteSeleniumServer.cs b/dotnet/test/common/Environment/RemoteSeleniumServer.cs index 8c12943c928f3..16326f5df9ca6 100644 --- a/dotnet/test/common/Environment/RemoteSeleniumServer.cs +++ b/dotnet/test/common/Environment/RemoteSeleniumServer.cs @@ -30,8 +30,8 @@ public class RemoteSeleniumServer { private Process webserverProcess; private string serverJarName = @"java/src/org/openqa/selenium/grid/selenium_server_deploy.jar"; - private string projectRootPath; - private bool autoStart; + private readonly string projectRootPath; + private readonly bool autoStart; public RemoteSeleniumServer(string projectRoot, bool autoStartServer) { diff --git a/dotnet/test/common/Environment/TestWebServer.cs b/dotnet/test/common/Environment/TestWebServer.cs index f5fcd0f4ff0c6..c9060e834b0a3 100644 --- a/dotnet/test/common/Environment/TestWebServer.cs +++ b/dotnet/test/common/Environment/TestWebServer.cs @@ -169,8 +169,13 @@ public async Task StartAsync() output = webserverProcess.StandardOutput.ReadToEnd(); } - string errorMessage = string.Format("Could not start the test web server in {0} seconds.\nWorking directory: {1}\nProcess Args: {2}\nstdout: {3}\nstderr: {4}", timeout.TotalSeconds, projectRootPath, processArgsBuilder, output, error); - throw new TimeoutException(errorMessage); + throw new TimeoutException($""" + Could not start the test web server in {timeout.TotalSeconds} seconds. + Working directory: {projectRootPath} + Process Args: {processArgsBuilder} + stdout: {output} + stderr: {error} + """); } } } diff --git a/dotnet/test/common/Environment/UrlBuilder.cs b/dotnet/test/common/Environment/UrlBuilder.cs index 2bbe6d1c0351c..c7ab537964981 100644 --- a/dotnet/test/common/Environment/UrlBuilder.cs +++ b/dotnet/test/common/Environment/UrlBuilder.cs @@ -29,38 +29,26 @@ namespace OpenQA.Selenium.Environment { public class UrlBuilder { - string protocol; - string hostName; - string port; - string securePort; - string path; - string alternateHostName; - - public string AlternateHostName - { - get { return alternateHostName; } - } + private readonly string protocol; + private readonly string port; + private readonly string securePort; - public string HostName - { - get { return hostName; } - } + public string AlternateHostName { get; } - public string Path - { - get { return path; } - } + public string HostName { get; } + + public string Path { get; } public UrlBuilder(WebsiteConfig config) { protocol = config.Protocol; - hostName = config.HostName; + HostName = config.HostName; port = config.Port; securePort = config.SecurePort; - path = config.Folder; + Path = config.Folder; //Use the first IPv4 address that we find IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); - foreach (IPAddress ip in Dns.GetHostEntry(hostName).AddressList) + foreach (IPAddress ip in Dns.GetHostEntry(HostName).AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { @@ -68,31 +56,22 @@ public UrlBuilder(WebsiteConfig config) break; } } - alternateHostName = ipAddress.ToString(); + AlternateHostName = ipAddress.ToString(); } public string LocalWhereIs(string page) { - string location = string.Empty; - location = "http://localhost:" + port + "/" + path + "/" + page; - - return location; + return $"http://localhost:{port}/{Path}/{page}"; } public string WhereIs(string page) { - string location = string.Empty; - location = "http://" + hostName + ":" + port + "/" + path + "/" + page; - - return location; + return $"http://{HostName}:{port}/{Path}/{page}"; } public string WhereElseIs(string page) { - string location = string.Empty; - location = "http://" + alternateHostName + ":" + port + "/" + path + "/" + page; - - return location; + return $"http://{AlternateHostName}:{port}/{Path}/{page}"; } public string WhereIsViaNonLoopbackAddress(string page) @@ -108,19 +87,14 @@ public string WhereIsViaNonLoopbackAddress(string page) } } - string location = string.Empty; - location = "http://" + hostNameAsIPAddress + ":" + port + "/" + path + "/" + page; - - return location; + return $"http://{hostNameAsIPAddress}:{port}/{Path}/{page}"; } public string WhereIsSecure(string page) { - string location = string.Empty; - location = "https://" + hostName + ":" + securePort + "/" + path + "/" + page; - - return location; + return $"https://{HostName}:{securePort}/{Path}/{page}"; } + public string CreateInlinePage(InlinePage page) { Uri createPageUri = new Uri(new Uri(WhereIs(string.Empty)), "createPage"); @@ -142,14 +116,14 @@ public string CreateInlinePage(InlinePage page) // The response string from the Java remote server has trailing null // characters. This is due to the fix for issue 288. - if (responseString.IndexOf('\0') >= 0) + if (responseString.Contains('\0')) { - responseString = responseString.Substring(0, responseString.IndexOf('\0')); + responseString = responseString[..responseString.IndexOf('\0')]; } if (responseString.Contains("localhost")) { - responseString = responseString.Replace("localhost", this.hostName); + responseString = responseString.Replace("localhost", this.HostName); } return responseString; From 3e21caf68bdff5e41e428bcdeda52d6e4460f39f Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 6 Feb 2025 10:50:20 -0500 Subject: [PATCH 3/4] revert accidental push --- .../IgnoreBrowserAttribute.cs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs b/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs index 675e49c672ccc..06248ac3a3e25 100644 --- a/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/IgnoreBrowserAttribute.cs @@ -93,21 +93,3 @@ private static bool IsRemoteInstanceOfBrowser(Browser desiredBrowser) } } } - -class C -{ - public static bool AreDefault(int i, string j) - { - switch (i, j) - { - case (0, _): - return true; - - case (_, null): - return true; - - default: - return false; - } - } -} From eb4e13518c81860bdfac410d5a2ff451512fec74 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 7 Feb 2025 11:37:54 -0500 Subject: [PATCH 4/4] Remove unnecessary changes --- .../test/common/CustomDriverConfigs/DefaultSafariDriver.cs | 5 ----- .../common/CustomDriverConfigs/DevChannelChromeDriver.cs | 5 ----- .../test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs | 5 ----- .../CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs | 5 ----- .../CustomDriverConfigs/NightlyChannelFirefoxDriver.cs | 5 ----- .../CustomDriverConfigs/SafariTechnologyPreviewDriver.cs | 5 ----- .../common/CustomDriverConfigs/StableChannelChromeDriver.cs | 5 ----- .../common/CustomDriverConfigs/StableChannelEdgeDriver.cs | 5 ----- .../common/CustomDriverConfigs/StableChannelFirefoxDriver.cs | 5 ----- 9 files changed, 45 deletions(-) diff --git a/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs b/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs index 4f1a9801d902c..311a2832a8e2c 100644 --- a/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs @@ -30,11 +30,6 @@ public DefaultSafariDriver(SafariOptions options) { } - public DefaultSafariDriver(SafariDriverService service) - : base(service) - { - } - public DefaultSafariDriver(SafariDriverService service, SafariOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs b/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs index 5c712eaa23166..1f0a17e677583 100644 --- a/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs @@ -32,11 +32,6 @@ public DevChannelChromeDriver(ChromeOptions options) { } - public DevChannelChromeDriver(ChromeDriverService service) - : base(service) - { - } - public DevChannelChromeDriver(ChromeDriverService service, ChromeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs b/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs index a444c1e942bd2..0fbcbf0189093 100644 --- a/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs @@ -32,11 +32,6 @@ public DevChannelEdgeDriver(EdgeOptions options) { } - public DevChannelEdgeDriver(EdgeDriverService service) - : base(service) - { - } - public DevChannelEdgeDriver(EdgeDriverService service, EdgeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs b/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs index 199ce89ec41e3..09c97ba2ea82c 100644 --- a/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs @@ -35,11 +35,6 @@ public EdgeInternetExplorerModeDriver(InternetExplorerOptions options) { } - public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service) - : base(service) - { - } - public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service, InternetExplorerOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs b/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs index 346c720678e64..a551057fa4b92 100644 --- a/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs @@ -35,11 +35,6 @@ public NightlyChannelFirefoxDriver(FirefoxOptions options) { } - public NightlyChannelFirefoxDriver(FirefoxDriverService service) - : base(service) - { - } - public NightlyChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs b/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs index a9df1982ab316..5143122fb416b 100644 --- a/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs @@ -35,11 +35,6 @@ public SafariTechnologyPreviewDriver(SafariOptions options) { } - public SafariTechnologyPreviewDriver(SafariDriverService service) - : base(service) - { - } - public SafariTechnologyPreviewDriver(SafariDriverService service, SafariOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs index 234e68cb57d4d..0f9edb86f98cf 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs @@ -32,11 +32,6 @@ public StableChannelChromeDriver(ChromeOptions options) { } - public StableChannelChromeDriver(ChromeDriverService service) - : base(service) - { - } - public StableChannelChromeDriver(ChromeDriverService service, ChromeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs index a54b768ee9553..e433b02f9d4ee 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs @@ -33,11 +33,6 @@ public StableChannelEdgeDriver(EdgeOptions options) { } - public StableChannelEdgeDriver(EdgeDriverService service) - : base(service) - { - } - public StableChannelEdgeDriver(EdgeDriverService service, EdgeOptions options) : base(service, options) { diff --git a/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs b/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs index c9f747b48471a..ae487e6e3de35 100644 --- a/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs +++ b/dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs @@ -35,11 +35,6 @@ public StableChannelFirefoxDriver(FirefoxOptions options) { } - public StableChannelFirefoxDriver(FirefoxDriverService service) - : base(service) - { - } - public StableChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options) : base(service, options) {