Skip to content

Commit

Permalink
#18 Applying of JsonConfig with muliple drivers fails when applied to…
Browse files Browse the repository at this point in the history
… AtataContext.GlobalConfiguration; Add tests: Driver_Chrome_ThruGlobalConfiguration, Driver_Chrome_ThruGlobalConfiguration_VerifyJsonConfig and Driver_Multiple_ThruGlobalConfiguration
  • Loading branch information
YevgeniyShunevych committed Oct 19, 2018
1 parent a0f657c commit c7981fd
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 19 deletions.
177 changes: 161 additions & 16 deletions src/Atata.Configuration.Json.Tests/DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>)capabilities.GetCapability(ChromeOptions.Capability);

capabilities.GetCapability(CapabilityType.LoggingPreferences).ShouldBeEquivalentTo(new Dictionary<string, object>
Expand All @@ -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<string>)optionsCapabilities["excludeSwitches"]).Should().Equal("exc-arg");

context.Options.Extensions.Should().Equal("ZW5jLWV4dDE=", "ZW5jLWV4dDI=");
options.Extensions.Should().Equal("ZW5jLWV4dDE=", "ZW5jLWV4dDI=");

((List<string>)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<string, object>)optionsCapabilities["prefs"]).Should().Equal(new Dictionary<string, object>
{
Expand All @@ -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<string, OpenQA.Selenium.LogLevel>
{
["browser"] = OpenQA.Selenium.LogLevel.Info,
["driver"] = OpenQA.Selenium.LogLevel.Warning
});

config.Driver.Options.AdditionalCapabilities.ExtraPropertiesMap.Should().Equal(
new Dictionary<string, object>
{
["cap1"] = true,
["cap2"] = 5,
["cap3"] = "str"
});

config.Driver.Options.GlobalAdditionalCapabilities.ExtraPropertiesMap.Should().Equal(
new Dictionary<string, object>
{
["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<string, object>
{
["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<string, object>
{
["pref1"] = 7,
["pref2"] = false,
["pref3"] = "str"
});

config.Driver.Options.LocalStatePreferences.ExtraPropertiesMap.Should().Equal(
new Dictionary<string, object>
{
["pref1"] = 2.7,
["pref2"] = true,
["pref3"] = string.Empty
});

config.Driver.Options.MobileEmulationDeviceName.Should().Be("emul");

config.Driver.Options.ExtraPropertiesMap.Should().Equal(
new Dictionary<string, object>
{
["LeaveBrowserRunning"] = true,
["minidumpPath"] = "mdp"
});

config.Driver.Service.ExtraPropertiesMap.Should().Equal(
new Dictionary<string, object>
{
["port"] = 555,
["hostName"] = "127.0.0.1",
["whitelistedIPAddresses"] = "5.5.5.5,7.7.7.7"
});
}

[Test]
Expand Down Expand Up @@ -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<FirefoxDriver>();
JsonConfig.Global.Drivers.Should().HaveCount(2);
JsonConfig.Current.Drivers.Should().HaveCount(2);
}
}
}
21 changes: 21 additions & 0 deletions src/Atata.Configuration.Json/JsonConfigContractResolver.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
9 changes: 8 additions & 1 deletion src/Atata.Configuration.Json/JsonConfigManager`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace Atata.Configuration.Json
internal static class JsonConfigManager<TConfig>
where TConfig : JsonConfig<TConfig>
{
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));
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Atata.Configuration.Json/JsonConfig`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions src/Atata.Configuration.Json/JsonSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class JsonSection
[JsonExtensionData]
public Dictionary<string, JToken> AdditionalProperties { get; } = new Dictionary<string, JToken>();

[JsonIgnore]
public Dictionary<string, object> ExtraPropertiesMap => AdditionalProperties?.ToDictionary(x => x.Key, x => ConvertJToken(x.Value));

private static object ConvertJToken(JToken token)
Expand Down

0 comments on commit c7981fd

Please sign in to comment.