From f4ce188d8c86dc51d3503070c8544e5450d53d15 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 31 Jan 2025 00:44:10 -0500 Subject: [PATCH 1/5] [dotnet] Simplify and modernize `DevToolsDomains.InitializeDomains` --- .../src/webdriver/DevTools/DevToolsDomains.cs | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index 2cc4c6271c818..afa94979342c1 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs @@ -19,7 +19,8 @@ using System; using System.Collections.Generic; -using System.Reflection; + +#nullable enable namespace OpenQA.Selenium.DevTools { @@ -30,17 +31,26 @@ public abstract class DevToolsDomains { // By default, we will look for a supported version within this // number of versions, as that will most likely still work. - private static readonly int DefaultVersionRange = 5; + private const int DefaultVersionRange = 5; // This is the list of known supported DevTools version implementation. // When new versions are implemented for support, new types must be // added to this dictionary. - private static readonly Dictionary SupportedDevToolsVersions = new Dictionary() + private static int[] SupportedProtocolVersions => + [ + 130, + 132, + 131, + 85 + ]; + + private static DevToolsDomains? CreateDevToolsDomain(int protocolVersion, DevToolsSession session) => protocolVersion switch { - { 130, typeof(V130.V130Domains) }, - { 132, typeof(V132.V132Domains) }, - { 131, typeof(V131.V131Domains) }, - { 85, typeof(V85.V85Domains) } + 130 => new V130.V130Domains(session), + 132 => new V132.V132Domains(session), + 131 => new V131.V131Domains(session), + 85 => new V85.V85Domains(session), + _ => null }; /// @@ -74,6 +84,8 @@ public abstract class DevToolsDomains /// The version of the DevTools Protocol to use. /// The for which to initialiize the domains. /// The object containing the version-specific domains. + /// If is negative. + /// If the desired protocol version is not supported. public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session) { return InitializeDomains(protocolVersion, session, DefaultVersionRange); @@ -86,34 +98,24 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes /// The for which to initialiize the domains. /// The range of versions within which to match the provided version number. Defaults to 5 versions. /// The object containing the version-specific domains. + /// If is negative. + /// If the desired protocol version is not in the supported range. public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange) { if (versionRange < 0) { - throw new ArgumentException("Version range must be positive", nameof(versionRange)); - } - - DevToolsDomains domains = null; - Type domainType = MatchDomainsVersion(protocolVersion, versionRange); - ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) }); - if (constructor != null) - { - domains = constructor.Invoke(new object[] { session }) as DevToolsDomains; + throw new ArgumentOutOfRangeException(nameof(versionRange), "Version range must not be negative"); } - return domains; - } - - private static Type MatchDomainsVersion(int desiredVersion, int versionRange) - { // Return fast on an exact match - if (SupportedDevToolsVersions.ContainsKey(desiredVersion)) + DevToolsDomains? domains = CreateDevToolsDomain(protocolVersion, session); + if (domains is not null) { - return SupportedDevToolsVersions[desiredVersion]; + return domains; } // Get the list of supported versions and sort descending - List supportedVersions = new List(SupportedDevToolsVersions.Keys); + List supportedVersions = new List(SupportedProtocolVersions); supportedVersions.Sort((first, second) => second.CompareTo(first)); foreach (int supportedVersion in supportedVersions) @@ -121,13 +123,13 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange) // Match the version with the desired version within the // version range, using "The Price Is Right" style matching // (that is, closest without going over). - if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange) + if (protocolVersion >= supportedVersion && protocolVersion - supportedVersion < versionRange) { - return SupportedDevToolsVersions[supportedVersion]; + return CreateDevToolsDomain(supportedVersion, session)!; } } - throw new WebDriverException($"DevTools version is not in the supported range. Desired version={desiredVersion}, range={versionRange}. Supported versions: {string.Join(", ", supportedVersions)}"); + throw new WebDriverException($"DevTools version is not in the supported range. Desired version={protocolVersion}, range={versionRange}. Supported versions: {string.Join(", ", supportedVersions)}"); } } } From 610d6ecb8249443768734d67d6ff4b7509a5ec49 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 31 Jan 2025 00:46:43 -0500 Subject: [PATCH 2/5] Remove unnecessary changes --- dotnet/src/webdriver/DevTools/DevToolsDomains.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index afa94979342c1..cb78e6bceefc3 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs @@ -84,7 +84,7 @@ public abstract class DevToolsDomains /// The version of the DevTools Protocol to use. /// The for which to initialiize the domains. /// The object containing the version-specific domains. - /// If is negative. + /// If is negative. /// If the desired protocol version is not supported. public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session) { @@ -98,13 +98,13 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes /// The for which to initialiize the domains. /// The range of versions within which to match the provided version number. Defaults to 5 versions. /// The object containing the version-specific domains. - /// If is negative. + /// If is negative. /// If the desired protocol version is not in the supported range. public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange) { if (versionRange < 0) { - throw new ArgumentOutOfRangeException(nameof(versionRange), "Version range must not be negative"); + throw new ArgumentException("Version range must be positive", nameof(versionRange)); } // Return fast on an exact match From fc6e323f143aa2f6e8aa2930a48a206c427b907f Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 31 Jan 2025 00:48:37 -0500 Subject: [PATCH 3/5] Use fallback method --- dotnet/src/webdriver/DevTools/DevToolsDomains.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index cb78e6bceefc3..043632ea05d3b 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs @@ -114,6 +114,11 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes return domains; } + return CreateFallbackDomain(protocolVersion, session, versionRange); + } + + private static DevToolsDomains CreateFallbackDomain(int desiredVersion, DevToolsSession session, int versionRange) + { // Get the list of supported versions and sort descending List supportedVersions = new List(SupportedProtocolVersions); supportedVersions.Sort((first, second) => second.CompareTo(first)); @@ -123,13 +128,13 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes // Match the version with the desired version within the // version range, using "The Price Is Right" style matching // (that is, closest without going over). - if (protocolVersion >= supportedVersion && protocolVersion - supportedVersion < versionRange) + if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange) { return CreateDevToolsDomain(supportedVersion, session)!; } } - throw new WebDriverException($"DevTools version is not in the supported range. Desired version={protocolVersion}, range={versionRange}. Supported versions: {string.Join(", ", supportedVersions)}"); + throw new WebDriverException($"DevTools version is not in the supported range. Desired version={desiredVersion}, range={versionRange}. Supported versions: {string.Join(", ", supportedVersions)}"); } } } From 73f78ee13e0fabd9887a03dc9f0605c3c985a26b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 31 Jan 2025 00:50:01 -0500 Subject: [PATCH 4/5] rename variable --- dotnet/src/webdriver/DevTools/DevToolsDomains.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index 043632ea05d3b..a2503588a8eea 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs @@ -36,7 +36,7 @@ public abstract class DevToolsDomains // This is the list of known supported DevTools version implementation. // When new versions are implemented for support, new types must be // added to this dictionary. - private static int[] SupportedProtocolVersions => + private static int[] SupportedDevToolsVersions => [ 130, 132, @@ -120,7 +120,7 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes private static DevToolsDomains CreateFallbackDomain(int desiredVersion, DevToolsSession session, int versionRange) { // Get the list of supported versions and sort descending - List supportedVersions = new List(SupportedProtocolVersions); + List supportedVersions = new List(SupportedDevToolsVersions); supportedVersions.Sort((first, second) => second.CompareTo(first)); foreach (int supportedVersion in supportedVersions) From 9459214954376c0964e2fc95a14b65545b2796ea Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 31 Jan 2025 00:51:06 -0500 Subject: [PATCH 5/5] fix comment --- dotnet/src/webdriver/DevTools/DevToolsDomains.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index a2503588a8eea..4b8e688e1029b 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs @@ -35,7 +35,7 @@ public abstract class DevToolsDomains // This is the list of known supported DevTools version implementation. // When new versions are implemented for support, new types must be - // added to this dictionary. + // added to this array and to the method below. private static int[] SupportedDevToolsVersions => [ 130,