|
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using OpenQA.Selenium; |
| 5 | + |
| 6 | +namespace CSF.Extensions.WebDriver.Factories |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Default implementation of <see cref="IParsesSingleWebDriverConfigurationSection"/>. |
| 10 | + /// </summary> |
| 11 | + public class WebDriverConfigurationItemParser : IParsesSingleWebDriverConfigurationSection |
| 12 | + { |
| 13 | + readonly IGetsWebDriverAndOptionsTypes typeProvider; |
| 14 | + readonly ILogger<WebDriverConfigurationItemParser> logger; |
| 15 | + |
| 16 | + /// <inheritdoc/> |
| 17 | + public WebDriverCreationOptions GetDriverConfiguration(IConfigurationSection configuration) |
| 18 | + { |
| 19 | + if(configuration is null) throw new ArgumentNullException(nameof(configuration)); |
| 20 | + |
| 21 | + var creationOptions = new WebDriverCreationOptions |
| 22 | + { |
| 23 | + DriverType = configuration.GetValue<string>(nameof(WebDriverCreationOptions.DriverType)), |
| 24 | + OptionsType = configuration.GetValue<string>(nameof(WebDriverCreationOptions.OptionsType)), |
| 25 | + GridUrl = configuration.GetValue<string>(nameof(WebDriverCreationOptions.GridUrl)), |
| 26 | + DriverFactoryType = configuration.GetValue<string>(nameof(WebDriverCreationOptions.DriverFactoryType)), |
| 27 | + }; |
| 28 | + |
| 29 | + if(!TryGetDriverType(creationOptions, configuration, out var driverType)) |
| 30 | + return null; |
| 31 | + |
| 32 | + if(!TryGetOptionsType(creationOptions, configuration, driverType, out var optionsType)) |
| 33 | + return null; |
| 34 | + |
| 35 | + if(!TrySetOptionsCustomizer(creationOptions, configuration, optionsType)) |
| 36 | + return null; |
| 37 | + |
| 38 | + return creationOptions; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Validates and gets the <see cref="Type"/> of the implementation of <see cref="IWebDriver"/> implementation indicated by the configuration. |
| 43 | + /// </summary> |
| 44 | + /// <remarks> |
| 45 | + /// <para> |
| 46 | + /// Note that it is valid for the driver type to be <see langword="null"/> if <see cref="WebDriverCreationOptions.DriverFactoryType"/> is specified. |
| 47 | + /// In that scenario, the driver type is unused, but it still indicates a valid configuration. |
| 48 | + /// </para> |
| 49 | + /// </remarks> |
| 50 | + /// <param name="options">The options, as they have been parsed so far</param> |
| 51 | + /// <param name="configuration">The configuration section</param> |
| 52 | + /// <param name="driverType">If this method returns <see langword="true"/> then this is a <see cref="Type"/> of the web driver, otherwise |
| 53 | + /// this value is undefined and must be ignored.</param> |
| 54 | + /// <returns><see langword="true"/> if the driver type information is valid; <see langword="false"/> if not</returns> |
| 55 | + bool TryGetDriverType(WebDriverCreationOptions options, IConfigurationSection configuration, out Type driverType) |
| 56 | + { |
| 57 | + driverType = null; |
| 58 | + if(options.DriverFactoryType != null) |
| 59 | + return true; |
| 60 | + |
| 61 | + if(options.DriverType is null) |
| 62 | + { |
| 63 | + logger.LogError("{ParamName} is mandatory unless {FactoryTypeKey} is specified; the configuration '{ConfigKey}' will be omitted.", |
| 64 | + nameof(WebDriverCreationOptions.DriverType), |
| 65 | + nameof(WebDriverCreationOptions.DriverFactoryType), |
| 66 | + configuration.Key); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + driverType = typeProvider.GetWebDriverType(options.DriverType); |
| 73 | + return true; |
| 74 | + } |
| 75 | + catch(Exception e) |
| 76 | + { |
| 77 | + logger.LogError(e, |
| 78 | + "No implementation of {WebDriverIface} could be found for the {DriverTypeProp} '{DriverType}'; the driver configuration '{ConfigKey}' will be omitted. " + |
| 79 | + "Reminder: If the driver type is not one which is shipped with Selenium then you must specify its assembly-qualified type name.", |
| 80 | + nameof(IWebDriver), |
| 81 | + nameof(WebDriverCreationOptions.DriverType), |
| 82 | + options.DriverType, |
| 83 | + configuration.Key); |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Validates and gets the <see cref="Type"/> of the implementation of <see cref="DriverOptions"/> implementation indicated by the configuration. |
| 90 | + /// </summary> |
| 91 | + /// <remarks> |
| 92 | + /// <para> |
| 93 | + /// Note that it is valid for the options type to be <see langword="null"/> if <see cref="WebDriverCreationOptions.DriverFactoryType"/> is specified. |
| 94 | + /// In that scenario, the options type is unused, but it still indicates a valid configuration. |
| 95 | + /// </para> |
| 96 | + /// </remarks> |
| 97 | + /// <param name="options">The options, as they have been parsed so far</param> |
| 98 | + /// <param name="configuration">The configuration section</param> |
| 99 | + /// <param name="driverType">The type of the Web Driver, as has already been determined by |
| 100 | + /// <see cref="TryGetDriverType(WebDriverCreationOptions, IConfigurationSection, out Type)"/>.</param> |
| 101 | + /// <param name="optionsType">If this method returns <see langword="true"/> then this is a <see cref="Type"/> of the driver options, otherwise |
| 102 | + /// this value is undefined and must be ignored.</param> |
| 103 | + /// <returns><see langword="true"/> if the driver type information is valid; <see langword="false"/> if not</returns> |
| 104 | + bool TryGetOptionsType(WebDriverCreationOptions options, IConfigurationSection configuration, Type driverType, out Type optionsType) |
| 105 | + { |
| 106 | + optionsType = null; |
| 107 | + if(options.DriverFactoryType != null) |
| 108 | + return true; |
| 109 | + |
| 110 | + try |
| 111 | + { |
| 112 | + optionsType = typeProvider.GetWebDriverOptionsType(driverType, options.OptionsType); |
| 113 | + } |
| 114 | + catch(Exception e) |
| 115 | + { |
| 116 | + logger.LogError(e, |
| 117 | + "No type deriving from {OptionsBase} could be found for the combination of {WebDriverIface} {DriverType} and {OptionsTypeProp} '{OptionsType}'; the configuration '{ConfigKey}' will be omitted. " + |
| 118 | + "See the exception details for more information.", |
| 119 | + nameof(DriverOptions), |
| 120 | + nameof(IWebDriver), |
| 121 | + driverType.Name, |
| 122 | + nameof(WebDriverCreationOptions.OptionsType), |
| 123 | + options.OptionsType, |
| 124 | + configuration.Key); |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + try |
| 129 | + { |
| 130 | + options.OptionsFactory = GetOptions(optionsType, configuration); |
| 131 | + return true; |
| 132 | + } |
| 133 | + catch(Exception e) |
| 134 | + { |
| 135 | + logger.LogError(e, |
| 136 | + "An unexpected error occurred creating or binding to the {OptionsClass} type {OptionsType}; the configuration '{ConfigKey}' will be omitted.", |
| 137 | + nameof(DriverOptions), |
| 138 | + optionsType.FullName, |
| 139 | + configuration.Key); |
| 140 | + return false; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + bool TrySetOptionsCustomizer(WebDriverCreationOptions options, IConfigurationSection configuration, Type optionsType) |
| 145 | + { |
| 146 | + var customizerTypeName = configuration.GetValue<string>("OptionsCustomizerType"); |
| 147 | + try |
| 148 | + { |
| 149 | + options.OptionsCustomizer = GetOptionsCustomizer(optionsType, customizerTypeName); |
| 150 | + return true; |
| 151 | + } |
| 152 | + catch(Exception e) |
| 153 | + { |
| 154 | + logger.LogError(e, |
| 155 | + "An unexpected error occurred binding the {OptionsCustomizer} type {CustomizerType}; the configuration '{ConfigKey}' will be omitted.", |
| 156 | + nameof(WebDriverCreationOptions.OptionsCustomizer), |
| 157 | + customizerTypeName, |
| 158 | + configuration.Key); |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + static Func<DriverOptions> GetOptions(Type optionsType, IConfigurationSection config) |
| 164 | + { |
| 165 | + return () => |
| 166 | + { |
| 167 | + var options = (DriverOptions)Activator.CreateInstance(optionsType); |
| 168 | + config.Bind("Options", options); |
| 169 | + return options; |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + static ICustomizesOptions<DriverOptions> GetOptionsCustomizer(Type optionsType, string customizerTypeName) |
| 174 | + { |
| 175 | + if(string.IsNullOrWhiteSpace(customizerTypeName)) return null; |
| 176 | + var customizerType = Type.GetType(customizerTypeName, true); |
| 177 | + |
| 178 | + if(!typeof(ICustomizesOptions<>).MakeGenericType(optionsType).IsAssignableFrom(customizerType)) |
| 179 | + throw new ArgumentException($"The specified customizer type must implement {nameof(ICustomizesOptions<DriverOptions>)}<{optionsType.Name}>.", nameof(customizerTypeName)); |
| 180 | + if(customizerType.GetConstructor(Type.EmptyTypes) == null) |
| 181 | + throw new ArgumentException($"The specified customizer type must have a public parameterless constructor.", nameof(customizerTypeName)); |
| 182 | + |
| 183 | + return (ICustomizesOptions<DriverOptions>) Activator.CreateInstance(customizerType); |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Initializes a new instance of the <see cref="WebDriverConfigurationItemParser"/> class. |
| 188 | + /// </summary> |
| 189 | + /// <param name="typeProvider">The provider for web driver and options types.</param> |
| 190 | + /// <param name="logger">The logger for this parser.</param> |
| 191 | + public WebDriverConfigurationItemParser(IGetsWebDriverAndOptionsTypes typeProvider, |
| 192 | + ILogger<WebDriverConfigurationItemParser> logger) |
| 193 | + { |
| 194 | + this.typeProvider = typeProvider ?? throw new ArgumentNullException(nameof(typeProvider)); |
| 195 | + this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
0 commit comments