Skip to content

Commit

Permalink
extract i localization manager (#27)
Browse files Browse the repository at this point in the history
* exstracted interfaces for LocalizationManager and LocalizationManager

* updated LocalizationManagerTests

* #23 renamed LocalizationLogger to LocalizedLogger

* changed AddTransient to AddSingleton
  • Loading branch information
knysh authored Oct 16, 2019
1 parent 83dcd68 commit cc45095
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void ConfigureServices(IServiceCollection services, Func<IServiceProvider
services.AddTransient<ConditionalWait>();
services.AddSingleton<ILoggerConfiguration>(new LoggerConfiguration(settingsFile));
services.AddSingleton(Logger.Instance);
services.AddSingleton<LocalizationManager>();
services.AddSingleton<LocalizationLogger>();
services.AddSingleton<ILocalizationManager, LocalizationManager>();
services.AddSingleton<ILocalizedLogger, LocalizedLogger>();
services.AddSingleton<IRetryConfiguration>(new RetryConfiguration(settingsFile));
services.AddSingleton<ElementActionRetrier>();

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected Element(By locator, string name, ElementState state)

protected abstract IElementFinder Finder { get; }

protected abstract LocalizationLogger LocalizationLogger { get; }
protected abstract ILocalizedLogger LocalizedLogger { get; }

protected virtual Logger Logger => Logger.Instance;

Expand Down Expand Up @@ -103,7 +103,7 @@ protected virtual T DoWithRetry<T>(Func<T> function)

protected virtual void LogElementAction(string messageKey, params object[] args)
{
LocalizationLogger.InfoElementAction(ElementType, Name, messageKey, args);
LocalizedLogger.InfoElementAction(ElementType, Name, messageKey, args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ElementFactory : IElementFactory
{
private const string ByXpathIdentifier = "By.XPath";

public ElementFactory(ConditionalWait conditionalWait, IElementFinder elementFinder, LocalizationManager localizationManager)
public ElementFactory(ConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager)
{
ConditionalWait = conditionalWait;
ElementFinder = elementFinder;
Expand All @@ -28,7 +28,7 @@ public ElementFactory(ConditionalWait conditionalWait, IElementFinder elementFin

protected IElementFinder ElementFinder { get; }

protected LocalizationManager LocalizationManager { get; }
protected ILocalizationManager LocalizationManager { get; }

/// <summary>
/// Gets map between elements interfaces and their implementations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ namespace Aquality.Selenium.Core.Elements
/// </summary>
public class ElementFinder : IElementFinder
{
public ElementFinder(Logger logger, LocalizationLogger localizationLogger, LocalizationManager localizationManager, ConditionalWait conditionalWait)
public ElementFinder(Logger logger, ILocalizedLogger localizedLogger, ILocalizationManager localizationManager, ConditionalWait conditionalWait)
{
Logger = logger;
LocalizationLogger = localizationLogger;
LocalizedLogger = localizedLogger;
LocalizationManager = localizationManager;
ConditionalWait = conditionalWait;
}

private Logger Logger { get; }

private LocalizationLogger LocalizationLogger { get; }
private ILocalizedLogger LocalizedLogger { get; }

private LocalizationManager LocalizationManager { get; }
private ILocalizationManager LocalizationManager { get; }

private ConditionalWait ConditionalWait { get; }

Expand Down Expand Up @@ -103,7 +103,7 @@ private void HandleTimeoutException(WebDriverTimeoutException ex, DesiredState d
}
else
{
LocalizationLogger.Debug("loc.elements.were.found.but.not.in.state", null, locator.ToString(), desiredState.StateName);
LocalizedLogger.Debug("loc.elements.were.found.but.not.in.state", null, locator.ToString(), desiredState.StateName);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Aquality.Selenium.Core.Localization
{
/// <summary>
/// This interface is using for translation messages to different languages
/// </summary>
public interface ILocalizationManager
{
/// <summary>
/// Get localized message from resources by its key.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
/// <returns>Localized message.</returns>
string GetLocalizedMessage(string messageKey, params object[] args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace Aquality.Selenium.Core.Localization
{
/// <summary>
/// Log messages to different languages
/// </summary>
public interface ILocalizedLogger
{
/// <summary>
/// Logs localized message for action with INFO level which is applied for element, for example, click, send keys etc.
/// </summary>
/// <param name="elementType">Type of the element.</param>
/// <param name="elementName">Name of the element.</param>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void InfoElementAction(string elementType, string elementName, string messageKey, params object[] args);

/// <summary>
/// Logs localized message with INFO level.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void Info(string messageKey, params object[] args);

/// <summary>
/// Logs localized message with DEBUG level.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="exception">Exception, gets null value by default.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void Debug(string messageKey, Exception exception = null, params object[] args);

/// <summary>
/// Logs localized message with WARN level.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void Warn(string messageKey, params object[] args);

/// <summary>
/// Logs localized message with ERROR level.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void Error(string messageKey, params object[] args);

/// <summary>
/// Logs localized message with FATAL level.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="exception">Exception, gets null value by default.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
void Fatal(string messageKey, Exception exception = null, params object[] args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

namespace Aquality.Selenium.Core.Localization
{
/// <summary>
/// This class is using for translation messages to different languages
/// </summary>
public class LocalizationManager
public class LocalizationManager : ILocalizationManager
{
private const string LangResource = "Resources.Localization.{0}.json";
private readonly JsonSettingsFile localizationFile;
private readonly ISettingsFile localizationFile;
private readonly Logger logger;

public LocalizationManager(ILoggerConfiguration loggerConfiguration, Logger logger, Assembly assembly = null)
Expand All @@ -21,12 +18,6 @@ public LocalizationManager(ILoggerConfiguration loggerConfiguration, Logger logg
this.logger = logger;
}

/// <summary>
/// Get localized message from resources by its key.
/// </summary>
/// <param name="messageKey">Key in resource file.</param>
/// <param name="args">Arguments, which will be provided to template of localized message.</param>
/// <returns>Localized message.</returns>
public string GetLocalizedMessage(string messageKey, params object[] args)
{
var jsonKey = $"$['{messageKey}']";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@

namespace Aquality.Selenium.Core.Localization
{
/// <summary>
/// Log messages to different languages
/// </summary>
public class LocalizationLogger
public class LocalizedLogger : ILocalizedLogger
{
private readonly LocalizationManager localizationManager;
private readonly ILocalizationManager localizationManager;
private readonly Logger logger;

public LocalizationLogger(LocalizationManager localizationManager, Logger logger)
public LocalizedLogger(ILocalizationManager localizationManager, Logger logger)
{
this.localizationManager = localizationManager;
this.logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class ApplicationNotStartedTests : TestWithBrowser
typeof(ElementActionRetrier),
typeof(ConditionalWait),
typeof(Logger),
typeof(LocalizationManager),
typeof(LocalizationLogger),
typeof(ILocalizationManager),
typeof(ILocalizedLogger),
typeof(IElementFinder),
typeof(IElementFactory),
typeof(ITimeoutConfiguration),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ protected WindowElement(By locator, string name) : base(locator, name, ElementSt

protected override IElementFinder Finder => ApplicationManager.ServiceProvider.GetRequiredService<IElementFinder>();

protected override LocalizationLogger LocalizationLogger => ApplicationManager.ServiceProvider.GetRequiredService<LocalizationLogger>();
protected override ILocalizedLogger LocalizedLogger => ApplicationManager.ServiceProvider.GetRequiredService<ILocalizedLogger>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ public sealed class LocalizationManagerTests : TestWithoutApplication
"loc.elements.found.but.should.not"
};

[Test]
public void Should_BePossibleTo_UseLocalizationManager_ForClicking_CustomConfig()
{
Environment.SetEnvironmentVariable("profile", "custom");
SetUp();
Environment.SetEnvironmentVariable("profile", string.Empty);
Assert.AreEqual("Націскаем", ServiceProvider.GetService<ILocalizationManager>().GetLocalizedMessage("loc.clicking"));
}

[Test]
public void Should_BePossibleTo_UseLocalizationManager_ForClicking()
{
Assert.AreEqual("Clicking", ServiceProvider.GetService<LocalizationManager>().GetLocalizedMessage("loc.clicking"));
Assert.AreEqual("Clicking", ServiceProvider.GetService<ILocalizationManager>().GetLocalizedMessage("loc.clicking"));
}

[Test]
public void Should_BePossibleTo_UseLocalizationManager_ForUnknownKey()
{
var unknownKey = "loc.unknown.fake.key";
Assert.AreEqual(unknownKey, ServiceProvider.GetService<LocalizationManager>().GetLocalizedMessage(unknownKey));
Assert.AreEqual(unknownKey, ServiceProvider.GetService<ILocalizationManager>().GetLocalizedMessage(unknownKey));
}

[Test]
Expand Down

0 comments on commit cc45095

Please sign in to comment.