diff --git a/src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs b/src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs similarity index 70% rename from src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs rename to src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs index 74a0cc36..23f7d2dd 100644 --- a/src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs +++ b/src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs @@ -1,6 +1,6 @@ namespace TestStack.White.Reporting.Configuration { - public interface ReportingConfiguration + public interface IReportingConfiguration { bool PublishTestReports { get; } } diff --git a/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs b/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs index 88847497..cb6bce9a 100644 --- a/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs +++ b/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs @@ -5,9 +5,9 @@ namespace TestStack.White.Reporting.Configuration { - public class ReportingAppXmlConfiguration : AssemblyConfiguration, ReportingConfiguration + public class ReportingAppXmlConfiguration : AssemblyConfiguration, IReportingConfiguration { - private static ReportingConfiguration instance; + private static IReportingConfiguration instance; private static readonly Dictionary DefaultValues = new Dictionary(); @@ -19,7 +19,7 @@ static ReportingAppXmlConfiguration() private ReportingAppXmlConfiguration() : base("White", "Reporting", DefaultValues, CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(ReportingAppXmlConfiguration))) {} - public static ReportingConfiguration Instance + public static IReportingConfiguration Instance { get { return instance ?? (instance = new ReportingAppXmlConfiguration()); } } diff --git a/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj b/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj index ede5ba94..64010a37 100644 --- a/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj +++ b/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj @@ -73,7 +73,7 @@ - + @@ -113,5 +113,4 @@ --> - - + \ No newline at end of file diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs deleted file mode 100644 index 14e66f1a..00000000 --- a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using TestStack.White.Bricks; -using TestStack.White.Configuration; - -namespace TestStack.White.ScreenObjects.Configuration -{ - public class RepositoryAppXmlConfiguration : AssemblyConfiguration, RepositoryConfiguration - { - public static RepositoryConfiguration instance; - - private static readonly Dictionary DefaultValues = new Dictionary(); - private const string UseHistoryKey = "UseHistory"; - private const string ServiceCallHistoryLocationKey = "ServiceCallHistoryLocation"; - private const string RecordFlowKey = "RecordFlow"; - - static RepositoryAppXmlConfiguration() - { - DefaultValues[RecordFlowKey] = false; - DefaultValues[ServiceCallHistoryLocationKey] = "."; - DefaultValues[UseHistoryKey] = false; - } - - public static RepositoryConfiguration Instance - { - get { return instance ?? (instance = new RepositoryAppXmlConfiguration()); } - } - - private RepositoryAppXmlConfiguration() : base("White", "Repository", DefaultValues, - CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(RepositoryAppXmlConfiguration))) {} - - public virtual bool RecordFlow - { - get { return Convert.ToBoolean(UsedValues[RecordFlowKey]); } - } - - public virtual DirectoryInfo ServiceCallHistoryLocation - { - get { return new DirectoryInfo(UsedValues[ServiceCallHistoryLocationKey]); } - } - - public virtual bool UseHistory - { - get { return Convert.ToBoolean(UsedValues[UseHistoryKey]); } - } - } -} \ No newline at end of file diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs index 28e6f549..cea22cfd 100644 --- a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs +++ b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs @@ -1,12 +1,44 @@ +using System.Collections.Generic; using System.IO; +using TestStack.White.Configuration; namespace TestStack.White.ScreenObjects.Configuration { - //TODO: Power management - public interface RepositoryConfiguration + public class RepositoryConfiguration : ConfigurationBase { - bool RecordFlow { get; } - DirectoryInfo ServiceCallHistoryLocation { get; } - bool UseHistory { get; } + private const string RecordFlowKey = "RecordFlow"; + private const string ServiceCallHistoryLocationKey = "ServiceCallHistoryLocation"; + private const string UseHistoryKey = "UseHistory"; + + public bool RecordFlow + { + get { return GetValueBoolean(RecordFlowKey); } + set { SetValue(RecordFlowKey, value); } + } + + public DirectoryInfo ServiceCallHistoryLocation + { + get { return new DirectoryInfo(GetValue(ServiceCallHistoryLocationKey)); } + set { SetValue(ServiceCallHistoryLocationKey, value); } + } + + public bool UseHistory + { + get { return GetValueBoolean(UseHistoryKey); } + set { SetValue(UseHistoryKey, value); } + } + + public override Dictionary DefaultValues + { + get + { + return new Dictionary + { + {RecordFlowKey, false}, + {ServiceCallHistoryLocationKey, "."}, + {UseHistoryKey, false} + }; + } + } } -} \ No newline at end of file +} diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs new file mode 100644 index 00000000..1408abc1 --- /dev/null +++ b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs @@ -0,0 +1,24 @@ +using TestStack.White.Configuration; + +namespace TestStack.White.ScreenObjects.Configuration +{ + public static class RepositoryConfigurationLocator + { + private static RepositoryConfiguration repositoryConfiguration; + + public static RepositoryConfiguration Get() + { + if (repositoryConfiguration == null) + { + Register(new AppConfigReader("White", "Repository")); + } + return repositoryConfiguration; + } + + public static void Register(ConfigurationReaderBase configurationReader) + { + repositoryConfiguration = new RepositoryConfiguration(); + configurationReader.FillConfigurationFromReader(repositoryConfiguration); + } + } +} diff --git a/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs b/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs index 17a59b0e..871acfbe 100644 --- a/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs +++ b/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs @@ -91,7 +91,7 @@ public virtual void RevertToSnapshot() public static ServiceExecution Create(IWorkEnvironment workEnvironment) { - if (!RepositoryAppXmlConfiguration.Instance.UseHistory) + if (!RepositoryConfigurationLocator.Get().UseHistory) return new NullServiceExecution(); ExecutionHistory executionHistory = ExecutionHistory.Create(); return new ServiceExecution(executionHistory, workEnvironment ?? new NullWorkEnvironment()); diff --git a/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs b/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs index 88f0db16..2efef6ce 100644 --- a/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs +++ b/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs @@ -37,7 +37,7 @@ public virtual T GetService(params object[] objs) where T : Service if (services.TryGetValue(typeof (T), out service)) return (T) service; service = (T) Activator.CreateInstance(typeof(T), objs); - if (RepositoryAppXmlConfiguration.Instance.UseHistory || ReportingAppXmlConfiguration.Instance.PublishTestReports) + if (RepositoryConfigurationLocator.Get().UseHistory || ReportingAppXmlConfiguration.Instance.PublishTestReports) { service = (Service) DynamicProxyGenerator.Instance.CreateProxy(typeof (T), new ServiceInterceptor(service, serviceExecution, sessionReport)); services.Add(typeof (T), service); diff --git a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj index 62fa3b40..3e227a0d 100644 --- a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj +++ b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj @@ -77,12 +77,12 @@ + + - - @@ -161,5 +161,4 @@ --> - - + \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs b/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs deleted file mode 100644 index 0f1ebf1c..00000000 --- a/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TestStack.White.WebBrowser.Config -{ - public interface WebBrowserConfiguration - { - int FirefoxSingleWindowCheckWait { get; } - } -} \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs b/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs new file mode 100644 index 00000000..d05ddc00 --- /dev/null +++ b/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs @@ -0,0 +1,7 @@ +namespace TestStack.White.WebBrowser.Configuration +{ + public interface IWebBrowserConfiguration + { + int FirefoxSingleWindowCheckWait { get; } + } +} \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs similarity index 54% rename from src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs rename to src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs index 590efae5..00aa02a8 100644 --- a/src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs +++ b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs @@ -3,25 +3,20 @@ using TestStack.White.Bricks; using TestStack.White.Configuration; -namespace TestStack.White.WebBrowser.Config +namespace TestStack.White.WebBrowser.Configuration { - public class WebBrowserAppXmlConfiguration : AssemblyConfiguration, WebBrowserConfiguration + public class WebBrowserAppXmlConfiguration : AssemblyConfiguration, IWebBrowserConfiguration { private static readonly Dictionary DefaultValues = new Dictionary(); - private static WebBrowserConfiguration instance; static WebBrowserAppXmlConfiguration() { DefaultValues.Add("FirefoxSingleWindowCheckWait", 2000); } - private WebBrowserAppXmlConfiguration() : base("White", "WebBrowser", DefaultValues, - CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WebBrowserAppXmlConfiguration))){} - - public static WebBrowserConfiguration Instance - { - get { return instance ?? (instance = new WebBrowserAppXmlConfiguration()); } - } + public WebBrowserAppXmlConfiguration() + : base("White", "WebBrowser", DefaultValues, + CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WebBrowserAppXmlConfiguration))) { } public virtual int FirefoxSingleWindowCheckWait { diff --git a/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs b/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs new file mode 100644 index 00000000..4e8747af --- /dev/null +++ b/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs @@ -0,0 +1,23 @@ +using System; + +namespace TestStack.White.WebBrowser.Configuration +{ + public static class WebBrowserConfigurationLocator + { + private static IWebBrowserConfiguration webBrowserConfiguration; + + public static IWebBrowserConfiguration Get() + { + if (webBrowserConfiguration == null) + { + webBrowserConfiguration = new WebBrowserAppXmlConfiguration(); + } + return webBrowserConfiguration; + } + + public static void Register(IWebBrowserConfiguration webBrowserConfiguration) + { + WebBrowserConfigurationLocator.webBrowserConfiguration = webBrowserConfiguration; + } + } +} diff --git a/src/TestStack.White.WebBrowser/Firefox.cs b/src/TestStack.White.WebBrowser/Firefox.cs index 3eed4cdf..9d62765a 100644 --- a/src/TestStack.White.WebBrowser/Firefox.cs +++ b/src/TestStack.White.WebBrowser/Firefox.cs @@ -2,7 +2,7 @@ using System.Diagnostics; using System.Threading; using TestStack.White.UIItems.WindowItems; -using TestStack.White.WebBrowser.Config; +using TestStack.White.WebBrowser.Configuration; namespace TestStack.White.WebBrowser { @@ -26,7 +26,7 @@ public static FirefoxWindow Launch(string url) string commandLine = string.Format("-new-window {0}", url); var processStartInfo = new ProcessStartInfo {FileName = executable, Arguments = commandLine}; Application application = Application.Launch(processStartInfo); - Thread.Sleep(WebBrowserAppXmlConfiguration.Instance.FirefoxSingleWindowCheckWait); + Thread.Sleep(WebBrowserConfigurationLocator.Get().FirefoxSingleWindowCheckWait); int numberOfFirefoxProcessesAfterLaunch = Process.GetProcessesByName(executableName).Length; bool processPerWindow = numberOfFirefoxProcessesAfterLaunch > numberBeforeLaunch; diff --git a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj index 9169945e..2ed62459 100644 --- a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj +++ b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj @@ -83,13 +83,14 @@ + - + - + @@ -128,5 +129,4 @@ --> - - + \ No newline at end of file diff --git a/src/TestStack.White/Configuration/AppConfigReader.cs b/src/TestStack.White/Configuration/AppConfigReader.cs new file mode 100644 index 00000000..a04f670d --- /dev/null +++ b/src/TestStack.White/Configuration/AppConfigReader.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Configuration; + +namespace TestStack.White.Configuration +{ + /// + /// Configuration reader which reads from the app.config file + /// + public class AppConfigReader : ConfigurationReaderBase + { + private NameValueCollection nameValues; + + public AppConfigReader(string sectionGroup, string sectionName) + { + nameValues = (NameValueCollection)ConfigurationManager.GetSection(sectionGroup + "/" + sectionName); + if (nameValues == null) + { + nameValues = new NameValueCollection(); + } + } + + protected override void FillConfigurationFromReaderInternal(ConfigurationBase configuration) + { + // Overwrite the values contained in the section + foreach (var key in nameValues.AllKeys) + { + var value = nameValues.Get(key); + if (value != null) + { + configuration.SetValue(key, value); + } + } + // Overwrite the values from the AppSettings + var allKeys = new List(nameValues.AllKeys); + allKeys.AddRange(configuration.DefaultValues.Keys); + foreach (var key in allKeys) + { + var value = ConfigurationManager.AppSettings[key]; + if (value != null) + { + configuration.SetValue(key, value); + } + } + } + } +} diff --git a/src/TestStack.White/Configuration/ConfigurationBase.cs b/src/TestStack.White/Configuration/ConfigurationBase.cs new file mode 100644 index 00000000..124582a8 --- /dev/null +++ b/src/TestStack.White/Configuration/ConfigurationBase.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using TestStack.White.Utility; + +namespace TestStack.White.Configuration +{ + /// + /// Base class for configurations + /// + public abstract class ConfigurationBase + { + /// + /// Dictionary which holds the current values + /// + protected readonly Dictionary ConfigurationValues = new Dictionary(); + + /// + /// Temporarily override some configuration values + /// + public virtual IDisposable ApplyTemporarySetting(Action changes) + { + var existing = new Dictionary(ConfigurationValues); + changes(this); + + return new DelegateDisposable(() => + { + foreach (var value in existing) + { + SetValue(value.Key, value.Value); + } + }); + } + + /// + /// Set the value for the given key + /// + public void SetValue(string key, object value) + { + ConfigurationValues[key] = value.ToString(); + } + + /// + /// Get the value for the given key + /// + public string GetValue(string key) + { + return ConfigurationValues[key]; + } + + /// + /// Get the value converted to int32 for the given key + /// + public int GetValueInt32(string key) + { + return Convert.ToInt32(GetValue(key)); + } + + /// + /// Get the value converted to boolean for the given key + /// + public bool GetValueBoolean(string key) + { + return Convert.ToBoolean(GetValue(key)); + } + + /// + /// Dictionary with the default values + /// + public abstract Dictionary DefaultValues { get; } + } +} diff --git a/src/TestStack.White/Configuration/ConfigurationReaderBase.cs b/src/TestStack.White/Configuration/ConfigurationReaderBase.cs new file mode 100644 index 00000000..27976423 --- /dev/null +++ b/src/TestStack.White/Configuration/ConfigurationReaderBase.cs @@ -0,0 +1,23 @@ +using System; + +namespace TestStack.White.Configuration +{ + /// + /// Base class for a configuration reader + /// + public abstract class ConfigurationReaderBase + { + public void FillConfigurationFromReader(ConfigurationBase configuration) + { + // Initialize with default Values + foreach (var kvp in configuration.DefaultValues) + { + configuration.SetValue(kvp.Key, kvp.Value); + } + // Custom initialization + FillConfigurationFromReaderInternal(configuration); + } + + protected abstract void FillConfigurationFromReaderInternal(ConfigurationBase configuration); + } +} diff --git a/src/TestStack.White/TestStack.White.csproj b/src/TestStack.White/TestStack.White.csproj index 65b07908..a3cf28dc 100644 --- a/src/TestStack.White/TestStack.White.csproj +++ b/src/TestStack.White/TestStack.White.csproj @@ -91,7 +91,10 @@ + + +