diff --git a/dotnet/src/webdriver/DevTools/DevToolsDomains.cs b/dotnet/src/webdriver/DevTools/DevToolsDomains.cs index 2cc4c6271c818..4b8e688e1029b 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<int, Type> SupportedDevToolsVersions = new Dictionary<int, Type>() + // added to this array and to the method below. + private static int[] SupportedDevToolsVersions => + [ + 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 }; /// <summary> @@ -74,6 +84,8 @@ public abstract class DevToolsDomains /// <param name="protocolVersion">The version of the DevTools Protocol to use.</param> /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param> /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns> + /// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception> + /// <exception cref="WebDriverException">If the desired protocol version is not supported.</exception> public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session) { return InitializeDomains(protocolVersion, session, DefaultVersionRange); @@ -86,6 +98,8 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param> /// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param> /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns> + /// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception> + /// <exception cref="WebDriverException">If the desired protocol version is not in the supported range.</exception> public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange) { if (versionRange < 0) @@ -93,27 +107,20 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes 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) + // Return fast on an exact match + DevToolsDomains? domains = CreateDevToolsDomain(protocolVersion, session); + if (domains is not null) { - domains = constructor.Invoke(new object[] { session }) as DevToolsDomains; + return domains; } - return domains; + return CreateFallbackDomain(protocolVersion, session, versionRange); } - private static Type MatchDomainsVersion(int desiredVersion, int versionRange) + private static DevToolsDomains CreateFallbackDomain(int desiredVersion, DevToolsSession session, int versionRange) { - // Return fast on an exact match - if (SupportedDevToolsVersions.ContainsKey(desiredVersion)) - { - return SupportedDevToolsVersions[desiredVersion]; - } - // Get the list of supported versions and sort descending - List<int> supportedVersions = new List<int>(SupportedDevToolsVersions.Keys); + List<int> supportedVersions = new List<int>(SupportedDevToolsVersions); supportedVersions.Sort((first, second) => second.CompareTo(first)); foreach (int supportedVersion in supportedVersions) @@ -123,7 +130,7 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange) // (that is, closest without going over). if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange) { - return SupportedDevToolsVersions[supportedVersion]; + return CreateDevToolsDomain(supportedVersion, session)!; } }