diff --git a/src/Atata.Configuration.Json.Tests/DriverTests.cs b/src/Atata.Configuration.Json.Tests/DriverTests.cs index 0520aaf..22f02ac 100644 --- a/src/Atata.Configuration.Json.Tests/DriverTests.cs +++ b/src/Atata.Configuration.Json.Tests/DriverTests.cs @@ -27,7 +27,36 @@ public void Driver_Chrome() builder.BuildingContext.DriverFactoryToUse.Create(); } - var capabilities = context.Options.ToCapabilities(); + VerifyChromeOptions(context.Options); + VerifyChromeService(context.Service); + + context.CommandTimeout.Should().Be(TimeSpan.FromMinutes(1)); + } + + [Test] + public void Driver_Chrome_ThruGlobalConfiguration() + { + AtataContext.GlobalConfiguration. + ApplyJsonConfig(@"Configs/Chrome.json"); + + var context = ChromeAtataContextBuilderOverride.Context; + + using (context.UseNullDriver()) + { + AtataContextBuilder builder = AtataContext.Configure(); + + builder.BuildingContext.DriverFactoryToUse.Create(); + } + + VerifyChromeOptions(context.Options); + VerifyChromeService(context.Service); + + context.CommandTimeout.Should().Be(TimeSpan.FromMinutes(1)); + } + + private static void VerifyChromeOptions(ChromeOptions options) + { + var capabilities = options.ToCapabilities(); var optionsCapabilities = (Dictionary)capabilities.GetCapability(ChromeOptions.Capability); capabilities.GetCapability(CapabilityType.LoggingPreferences).ShouldBeEquivalentTo(new Dictionary @@ -47,22 +76,22 @@ public void Driver_Chrome() capabilities.GetCapability("globalcap2").Should().Be(5); capabilities.GetCapability("globalcap3").Should().Be("str"); - context.Options.Proxy.Kind.Should().Be(ProxyKind.Manual); - context.Options.Proxy.HttpProxy.Should().Be("http"); - context.Options.Proxy.FtpProxy.Should().Be("ftp"); + options.Proxy.Kind.Should().Be(ProxyKind.Manual); + options.Proxy.HttpProxy.Should().Be("http"); + options.Proxy.FtpProxy.Should().Be("ftp"); - context.Options.Arguments.Should().Equal("disable-extensions", "start-maximized"); + options.Arguments.Should().Equal("disable-extensions", "start-maximized"); ((List)optionsCapabilities["excludeSwitches"]).Should().Equal("exc-arg"); - context.Options.Extensions.Should().Equal("ZW5jLWV4dDE=", "ZW5jLWV4dDI="); + options.Extensions.Should().Equal("ZW5jLWV4dDE=", "ZW5jLWV4dDI="); ((List)optionsCapabilities["windowTypes"]).Should().Equal("win1", "win2"); - context.Options.PerformanceLoggingPreferences.IsCollectingNetworkEvents.Should().BeFalse(); - context.Options.PerformanceLoggingPreferences.IsCollectingPageEvents.Should().BeFalse(); - context.Options.PerformanceLoggingPreferences.BufferUsageReportingInterval.Should().Be(TimeSpan.FromSeconds(70)); - context.Options.PerformanceLoggingPreferences.TracingCategories.Should().Be("cat1,cat2"); + options.PerformanceLoggingPreferences.IsCollectingNetworkEvents.Should().BeFalse(); + options.PerformanceLoggingPreferences.IsCollectingPageEvents.Should().BeFalse(); + options.PerformanceLoggingPreferences.BufferUsageReportingInterval.Should().Be(TimeSpan.FromSeconds(70)); + options.PerformanceLoggingPreferences.TracingCategories.Should().Be("cat1,cat2"); ((Dictionary)optionsCapabilities["prefs"]).Should().Equal(new Dictionary { @@ -83,14 +112,109 @@ public void Driver_Chrome() ["deviceName"] = "emul" }); - context.Options.LeaveBrowserRunning.Should().BeTrue(); - context.Options.MinidumpPath.Should().Be("mdp"); + options.LeaveBrowserRunning.Should().BeTrue(); + options.MinidumpPath.Should().Be("mdp"); + } + + private void VerifyChromeService(ChromeDriverService service) + { + service.Port.Should().Be(555); + service.HostName.Should().Be("127.0.0.1"); + service.WhitelistedIPAddresses.Should().Be("5.5.5.5,7.7.7.7"); + } - context.Service.Port.Should().Be(555); - context.Service.HostName.Should().Be("127.0.0.1"); - context.Service.WhitelistedIPAddresses.Should().Be("5.5.5.5,7.7.7.7"); + [Test] + public void Driver_Chrome_ThruGlobalConfiguration_VerifyJsonConfig() + { + AtataContext.GlobalConfiguration. + ApplyJsonConfig(@"Configs/Chrome.json"); - context.CommandTimeout.Should().Be(TimeSpan.FromMinutes(1)); + AtataContext.Configure(). + UseChrome(). + WithDriverService(() => ChromeDriverService.CreateDefaultService()). + Build(); + + VerifyChromeJsonConfig(JsonConfig.Current); + VerifyChromeJsonConfig(JsonConfig.Global); + } + + private static void VerifyChromeJsonConfig(JsonConfig config) + { + config.Driver.Options.LoggingPreferences.Should().Equal( + new Dictionary + { + ["browser"] = OpenQA.Selenium.LogLevel.Info, + ["driver"] = OpenQA.Selenium.LogLevel.Warning + }); + + config.Driver.Options.AdditionalCapabilities.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["cap1"] = true, + ["cap2"] = 5, + ["cap3"] = "str" + }); + + config.Driver.Options.GlobalAdditionalCapabilities.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["globalcap1"] = true, + ["globalcap2"] = 5, + ["globalcap3"] = "str" + }); + + config.Driver.Options.Proxy.Kind.Should().BeNull(); + config.Driver.Options.Proxy.HttpProxy.Should().Be("http"); + config.Driver.Options.Proxy.FtpProxy.Should().Be("ftp"); + + config.Driver.Options.Arguments.Should().Equal("disable-extensions", "start-maximized"); + config.Driver.Options.ExcludedArguments.Should().Equal("exc-arg"); + + config.Driver.Options.EncodedExtensions.Should().Equal("ZW5jLWV4dDE=", "ZW5jLWV4dDI="); + + config.Driver.Options.WindowTypes.Should().Equal("win1", "win2"); + + config.Driver.Options.PerformanceLoggingPreferences.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["isCollectingNetworkEvents"] = false, + ["IsCollectingPageEvents"] = false, + ["bufferUsageReportingInterval"] = "00:01:10" + }); + config.Driver.Options.PerformanceLoggingPreferences.TracingCategories.Should().Equal("cat1", "cat2"); + + config.Driver.Options.UserProfilePreferences.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["pref1"] = 7, + ["pref2"] = false, + ["pref3"] = "str" + }); + + config.Driver.Options.LocalStatePreferences.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["pref1"] = 2.7, + ["pref2"] = true, + ["pref3"] = string.Empty + }); + + config.Driver.Options.MobileEmulationDeviceName.Should().Be("emul"); + + config.Driver.Options.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["LeaveBrowserRunning"] = true, + ["minidumpPath"] = "mdp" + }); + + config.Driver.Service.ExtraPropertiesMap.Should().Equal( + new Dictionary + { + ["port"] = 555, + ["hostName"] = "127.0.0.1", + ["whitelistedIPAddresses"] = "5.5.5.5,7.7.7.7" + }); } [Test] @@ -301,5 +425,26 @@ public void Driver_Multiple() builder.BuildingContext.DriverFactories[1].Alias.Should().Be(DriverAliases.Firefox); builder.BuildingContext.DriverFactoryToUse.Alias.Should().Be(DriverAliases.Firefox); } + + [Test] + public void Driver_Multiple_ThruGlobalConfiguration() + { + AtataContext.GlobalConfiguration. + ApplyJsonConfig(@"Configs/MultipleDrivers.json"); + + var driver = JsonConfig.Global.Driver; + + AtataContextBuilder builder = AtataContext.Configure(); + + builder.BuildingContext.DriverFactories.Should().HaveCount(2); + builder.BuildingContext.DriverFactories[0].Alias.Should().Be(DriverAliases.Chrome); + builder.BuildingContext.DriverFactories[1].Alias.Should().Be(DriverAliases.Firefox); + builder.BuildingContext.DriverFactoryToUse.Alias.Should().Be(DriverAliases.Firefox); + + builder.Build(); + AtataContext.Current.Driver.Should().BeOfType(); + JsonConfig.Global.Drivers.Should().HaveCount(2); + JsonConfig.Current.Drivers.Should().HaveCount(2); + } } } diff --git a/src/Atata.Configuration.Json/JsonConfigContractResolver.cs b/src/Atata.Configuration.Json/JsonConfigContractResolver.cs new file mode 100644 index 0000000..9dc9f7d --- /dev/null +++ b/src/Atata.Configuration.Json/JsonConfigContractResolver.cs @@ -0,0 +1,21 @@ +using System.Reflection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Atata.Configuration.Json +{ + internal class JsonConfigContractResolver : DefaultContractResolver + { + public static JsonConfigContractResolver Instance { get; } = new JsonConfigContractResolver(); + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty property = base.CreateProperty(member, memberSerialization); + + if (property.DeclaringType.IsGenericType && property.DeclaringType.GetGenericTypeDefinition() == typeof(JsonConfig<>) && property.PropertyName == nameof(JsonConfig.Driver)) + property.ShouldSerialize = _ => false; + + return property; + } + } +} diff --git a/src/Atata.Configuration.Json/JsonConfigManager`1.cs b/src/Atata.Configuration.Json/JsonConfigManager`1.cs index 17f636e..88235d2 100644 --- a/src/Atata.Configuration.Json/JsonConfigManager`1.cs +++ b/src/Atata.Configuration.Json/JsonConfigManager`1.cs @@ -7,6 +7,12 @@ namespace Atata.Configuration.Json internal static class JsonConfigManager where TConfig : JsonConfig { + private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings + { + ContractResolver = JsonConfigContractResolver.Instance, + NullValueHandling = NullValueHandling.Ignore + }; + internal static void UpdateGlobalValue(string jsonContent, TConfig config) { PropertyInfo globalConfigProperty = GetConfigProperty(nameof(JsonConfig.Global)); @@ -37,7 +43,8 @@ internal static void InitCurrentValue() if (globalValue != null) { - string serializedGlobalValue = JsonConvert.SerializeObject(globalValue); + string serializedGlobalValue = JsonConvert.SerializeObject(globalValue, SerializerSettings); + object clonedGlobalValue = JsonConvert.DeserializeObject(serializedGlobalValue, globalValue.GetType()); currentConfigProperty.SetValue(null, clonedGlobalValue, null); } diff --git a/src/Atata.Configuration.Json/JsonConfig`1.cs b/src/Atata.Configuration.Json/JsonConfig`1.cs index 06192f2..bafb53c 100644 --- a/src/Atata.Configuration.Json/JsonConfig`1.cs +++ b/src/Atata.Configuration.Json/JsonConfig`1.cs @@ -41,8 +41,8 @@ public static TConfig Current public DriverJsonSection Driver { - get { return Drivers?.SingleOrDefault(); } - set { Drivers = new[] { value }; } + get { return Drivers?.FirstOrDefault(); } + set { Drivers = value == null ? null : new[] { value }; } } public LogConsumerJsonSection[] LogConsumers { get; set; } diff --git a/src/Atata.Configuration.Json/JsonSection.cs b/src/Atata.Configuration.Json/JsonSection.cs index 804eae4..9333a9f 100644 --- a/src/Atata.Configuration.Json/JsonSection.cs +++ b/src/Atata.Configuration.Json/JsonSection.cs @@ -14,6 +14,7 @@ public class JsonSection [JsonExtensionData] public Dictionary AdditionalProperties { get; } = new Dictionary(); + [JsonIgnore] public Dictionary ExtraPropertiesMap => AdditionalProperties?.ToDictionary(x => x.Key, x => ConvertJToken(x.Value)); private static object ConvertJToken(JToken token)