Skip to content

Commit

Permalink
Feature/156 add properties to get registered services (#157)
Browse files Browse the repository at this point in the history
+semver: feature
* #156 updated core verion
Renamed Browsermanager to AqualityServices
Added properies: Logger, ConditionalWait and method IsBrowserStarted
Changed public ServiceProvider to private

* #156 changed BrowsermanagerTests to AqualityServicesTests
added tests for getting Logger and ConditionalWait from container

* #156 added localized logger to aquality services and added unit test

* #156 updated core to last version

* #156 changed using of ILocalizedLogger
  • Loading branch information
knysh authored Dec 26, 2019
1 parent f3caa97 commit 8a9e933
Show file tree
Hide file tree
Showing 35 changed files with 239 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aquality.Selenium.Core" Version="0.2.5" />
<PackageReference Include="Aquality.Selenium.Core" Version="0.3.1" />
<PackageReference Include="NLog" Version="4.6.6" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading;
using Aquality.Selenium.Core.Applications;
using Aquality.Selenium.Configurations;
using Aquality.Selenium.Core.Applications;
using Aquality.Selenium.Core.Localization;
using Aquality.Selenium.Core.Logging;
using Aquality.Selenium.Core.Waitings;

namespace Aquality.Selenium.Browsers
{
/// <summary>
/// Controls browser instance creation.
/// </summary>
public class BrowserManager : ApplicationManager<Browser>
public class AqualityServices : AqualityServices<Browser>
{
private static readonly ThreadLocal<BrowserStartup> BrowserStartupContainer = new ThreadLocal<BrowserStartup>(() => new BrowserStartup());
private static readonly ThreadLocal<IBrowserFactory> BrowserFactoryContainer = new ThreadLocal<IBrowserFactory>();

/// <summary>
/// Check if browser already started.
/// </summary>
/// <value>true if browser started and false otherwise.</value>
public static bool IsBrowserStarted => IsApplicationStarted();

/// <summary>
/// Gets registered instance of logger
/// </summary>
public static Logger Logger => Get<Logger>();

/// <summary>
/// Gets registered instance of localized logger
/// </summary>
public static ILocalizedLogger LocalizedLogger => Get<ILocalizedLogger>();

/// <summary>
/// Gets ConditionalWait object
/// </summary>
public static ConditionalWait ConditionalWait => Get<ConditionalWait>();

/// <summary>
/// Gets and sets thread-safe instance of browser.
/// </summary>
Expand All @@ -26,12 +50,6 @@ public static Browser Browser

private static Func<IServiceProvider, Browser> StartBrowserFunction => services => BrowserFactory.Browser;

public static IServiceProvider ServiceProvider
{
get => GetServiceProvider(services => Browser, ConfigureServices);
set => SetServiceProvider(value);
}

/// <summary>
/// Method which allow user to override or add custom services.
/// </summary>
Expand Down Expand Up @@ -68,15 +86,15 @@ public static IBrowserFactory BrowserFactory
/// </summary>
public static void SetDefaultFactory()
{
var appProfile = GetRequiredService<IBrowserProfile>();
var appProfile = Get<IBrowserProfile>();
IBrowserFactory applicationFactory;
if (appProfile.IsRemote)
{
applicationFactory = new RemoteBrowserFactory(ServiceProvider);
applicationFactory = new RemoteBrowserFactory();
}
else
{
applicationFactory = new LocalBrowserFactory(ServiceProvider);
applicationFactory = new LocalBrowserFactory();
}

BrowserFactory = applicationFactory;
Expand All @@ -88,11 +106,13 @@ public static void SetDefaultFactory()
/// <typeparam name="T">type of required service.</typeparam>
/// <exception cref="InvalidOperationException">Thrown if there is no service of the required type.</exception>
/// <returns></returns>
public static T GetRequiredService<T>()
public static T Get<T>()
{
return ServiceProvider.GetRequiredService<T>();
}

private static IServiceProvider ServiceProvider => GetServiceProvider(services => Browser, ConfigureServices);

private static IServiceCollection ConfigureServices()
{
return BrowserStartupContainer.Value.ConfigureServices(new ServiceCollection(), services => Browser);
Expand Down
13 changes: 6 additions & 7 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Drawing;
using System.Reflection;
using System;
using Microsoft.Extensions.DependencyInjection;
using Aquality.Selenium.Core.Waitings;

namespace Aquality.Selenium.Browsers
Expand All @@ -26,14 +25,14 @@ public class Browser : IApplication
/// Instantiate browser.
/// </summary>
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
public Browser(RemoteWebDriver webDriver, IServiceProvider serviceProvider)
public Browser(RemoteWebDriver webDriver)
{
Driver = webDriver;
Logger = serviceProvider.GetRequiredService<ILocalizedLogger>();
LocalizationManager = serviceProvider.GetRequiredService<ILocalizationManager>();
browserProfile = serviceProvider.GetRequiredService<IBrowserProfile>();
conditionalWait = serviceProvider.GetRequiredService<ConditionalWait>();
var timeoutConfiguration = serviceProvider.GetRequiredService<ITimeoutConfiguration>();
Logger = AqualityServices.LocalizedLogger;
LocalizationManager = AqualityServices.Get<ILocalizationManager>();
browserProfile = AqualityServices.Get<IBrowserProfile>();
conditionalWait = AqualityServices.ConditionalWait;
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
SetImplicitWaitTimeout(timeoutConfiguration.Implicit);
SetPageLoadTimeout(timeoutConfiguration.PageLoad);
SetScriptTimeout(timeoutConfiguration.Script);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;

namespace Aquality.Selenium.Browsers
namespace Aquality.Selenium.Browsers
{
/// <summary>
/// Abstract representation of <see cref="IBrowserFactory"/>.
/// </summary>
public abstract class BrowserFactory : IBrowserFactory
{
protected BrowserFactory(IServiceProvider serviceProvider)
protected BrowserFactory()
{
ServiceProvider = serviceProvider;
}

protected IServiceProvider ServiceProvider { get; }

public abstract Browser Browser { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal BrowserNavigation(RemoteWebDriver driver)
this.driver = driver;
}

private ILocalizedLogger Logger => BrowserManager.GetRequiredService<ILocalizedLogger>();
private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;

/// <summary>
/// Navigates back.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override IServiceCollection ConfigureServices(IServiceCollection services
services.AddSingleton<CoreTimeoutConfiguration>(serviceProvider => new TimeoutConfiguration(settings));
services.AddSingleton<IBrowserProfile>(serviceProvider => new BrowserProfile(settings));
services.AddSingleton<ILocalizationManager>(serviceProvider => new LocalizationManager(serviceProvider.GetRequiredService<ILoggerConfiguration>(), serviceProvider.GetRequiredService<Logger>(), Assembly.GetExecutingAssembly()));
services.AddTransient(serviceProvider => BrowserManager.BrowserFactory);
services.AddTransient(serviceProvider => AqualityServices.BrowserFactory);
return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Aquality.Selenium.Configurations;
using Aquality.Selenium.Configurations.WebDriverSettings;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
Expand All @@ -23,15 +22,15 @@ public class LocalBrowserFactory : BrowserFactory
{
private static readonly object WebDriverDownloadingLock = new object();

public LocalBrowserFactory(IServiceProvider serviceProvider) : base(serviceProvider)
public LocalBrowserFactory() : base()
{
}

public override Browser Browser => CreateBrowser();

private Browser CreateBrowser()
{
var browserProfile = ServiceProvider.GetRequiredService<IBrowserProfile>();
var browserProfile = AqualityServices.Get<IBrowserProfile>();
var browserName = browserProfile.BrowserName;
var driverSettings = browserProfile.DriverSettings;
RemoteWebDriver driver;
Expand All @@ -58,7 +57,7 @@ private Browser CreateBrowser()
default:
throw new ArgumentOutOfRangeException($"Browser {browserName} is not supported.");
}
return new Browser(driver, ServiceProvider);
return new Browser(driver);
}

private static void SetUpDriver(IDriverConfig driverConfig, IDriverSettings driverSettings)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Aquality.Selenium.Configurations;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.Remote;
using System;

namespace Aquality.Selenium.Browsers
{
Expand All @@ -10,19 +8,19 @@ namespace Aquality.Selenium.Browsers
/// </summary>
public class RemoteBrowserFactory : BrowserFactory
{
public RemoteBrowserFactory(IServiceProvider serviceProvider) : base(serviceProvider)
public RemoteBrowserFactory() : base()
{
}

public override Browser Browser
{
get
{
var browserProfile = ServiceProvider.GetRequiredService<IBrowserProfile>();
var browserProfile = AqualityServices.Get<IBrowserProfile>();
var capabilities = browserProfile.DriverSettings.DriverOptions.ToCapabilities();
var timeoutConfiguration = ServiceProvider.GetRequiredService<ITimeoutConfiguration>();
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
var driver = new RemoteWebDriver(browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command);
return new Browser(driver, ServiceProvider);
return new Browser(driver);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public JsActions(IElement element, string elementType, ILocalizedLogger logger,
Logger = logger;
}

private Browser Browser => BrowserManager.Browser;
private Browser Browser => AqualityServices.Browser;

protected ILocalizedLogger Logger { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MouseActions(IElement element, string elementType, ILocalizedLogger logge
this.elementActionsRetrier = elementActionsRetrier;
}

private JsActions JsActions => new JsActions(element, elementType, logger, BrowserManager.GetRequiredService<IBrowserProfile>());
private JsActions JsActions => new JsActions(element, elementType, logger, AqualityServices.Get<IBrowserProfile>());

/// <summary>
/// Performs click on element.
Expand Down Expand Up @@ -65,12 +65,12 @@ public void MoveToElement()
{
LogElementAction("loc.moving");
JsActions.ScrollIntoView();
elementActionsRetrier.DoWithRetry(() => PerformAction(element => MoveToElement(element)));
elementActionsRetrier.DoWithRetry(() => PerformAction(MoveToElement));
}

private SeleniumActions MoveToElement(IWebElement element)
{
return new SeleniumActions(BrowserManager.Browser.Driver).MoveToElement(element);
return new SeleniumActions(AqualityServices.Browser.Driver).MoveToElement(element);
}

private void PerformAction(Func<RemoteWebElement, SeleniumActions> action)
Expand Down
16 changes: 8 additions & 8 deletions Aquality.Selenium/src/Aquality.Selenium/Elements/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ protected Element(By locator, string name, ElementState state) : base(locator, n

public override CoreElementStateProvider State => new ElementStateProvider(Locator, ConditionalWait, Finder);

protected IBrowserProfile BrowserProfile => BrowserManager.GetRequiredService<IBrowserProfile>();
protected IBrowserProfile BrowserProfile => AqualityServices.Get<IBrowserProfile>();

public JsActions JsActions => new JsActions(this, ElementType, LocalizedLogger, BrowserProfile);

public MouseActions MouseActions => new MouseActions(this, ElementType, LocalizedLogger, ActionRetrier);

private Browser Browser => (Browser)Application;

protected override IApplication Application => BrowserManager.Browser;
protected override IApplication Application => AqualityServices.Browser;

protected override ElementActionRetrier ActionRetrier => BrowserManager.GetRequiredService<ElementActionRetrier>();
protected override ElementActionRetrier ActionRetrier => AqualityServices.Get<ElementActionRetrier>();

protected override CoreElementFactory Factory => CustomFactory;

protected virtual IElementFactory CustomFactory => BrowserManager.GetRequiredService<IElementFactory>();
protected virtual IElementFactory CustomFactory => AqualityServices.Get<IElementFactory>();

protected override CoreElementFinder Finder => BrowserManager.GetRequiredService<CoreElementFinder>();
protected override CoreElementFinder Finder => AqualityServices.Get<CoreElementFinder>();

protected override ILocalizedLogger LocalizedLogger => BrowserManager.GetRequiredService<ILocalizedLogger>();
protected override ILocalizedLogger LocalizedLogger => AqualityServices.LocalizedLogger;

protected ILocalizationManager LocalizationManager => BrowserManager.GetRequiredService<ILocalizationManager>();
protected ILocalizationManager LocalizationManager => AqualityServices.Get<ILocalizationManager>();

protected override ConditionalWait ConditionalWait => BrowserManager.GetRequiredService<ConditionalWait>();
protected override ConditionalWait ConditionalWait => AqualityServices.ConditionalWait;

public void ClickAndWait()
{
Expand Down
4 changes: 2 additions & 2 deletions Aquality.Selenium/src/Aquality.Selenium/Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ protected Form(By locator, string name)
/// Instance of logger <see cref="Logging.Logger">
/// </summary>
/// <value>Logger instance.</value>
protected ILocalizedLogger Logger => BrowserManager.GetRequiredService<ILocalizedLogger>();
protected ILocalizedLogger Logger => AqualityServices.LocalizedLogger;

/// <summary>
/// Element factory <see cref="IElementFactory">
/// </summary>
/// <value>Element factory.</value>
protected IElementFactory ElementFactory => BrowserManager.GetRequiredService<IElementFactory>();
protected IElementFactory ElementFactory => AqualityServices.Get<IElementFactory>();

/// <summary>
/// Return form state for form locator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Core.Elements;
using Aquality.Selenium.Core.Waitings;
using Aquality.Selenium.Elements;
using Aquality.Selenium.Tests.Integration.TestApp;
using Aquality.Selenium.Tests.Integration.TestApp.AutomationPractice.Forms;
Expand Down Expand Up @@ -48,7 +47,7 @@ public void Should_BePossibleTo_HighlightElement()
[Test]
public void Should_BePossibleTo_HoverMouse()
{
BrowserManager.Browser.GoTo(Constants.UrlAutomationPractice);
AqualityServices.Browser.GoTo(Constants.UrlAutomationPractice);
var productList = new ProductListForm();
productList.NavigateToLastProduct();

Expand All @@ -61,7 +60,7 @@ public void Should_BePossibleTo_HoverMouse()
[Test]
public void Should_BePossibleTo_SetFocus()
{
BrowserManager.Browser.GoTo(Constants.UrlAutomationPractice);
AqualityServices.Browser.GoTo(Constants.UrlAutomationPractice);
var productList = new ProductListForm();
productList.NavigateToLastProduct();

Expand Down Expand Up @@ -134,7 +133,7 @@ public void Should_BePossibleTo_ScrollIntoView()
infiniteScrollForm.WaitForPageToLoad();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
Assert.DoesNotThrow(
() => BrowserManager.GetRequiredService<ConditionalWait>().WaitForTrue(() =>
() => AqualityServices.ConditionalWait.WaitForTrue(() =>
{
infiniteScrollForm.LastExampleLabel.JsActions.ScrollIntoView();
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
Expand All @@ -148,7 +147,7 @@ public void Should_BePossibleTo_ScrollBy()
var homeDemoSiteForm = new HomeDemoSiteForm();
homeDemoSiteForm.Open();
homeDemoSiteForm.FirstScrollableExample.JsActions.ScrollBy(point.X, point.Y);
var currentCoordinates = BrowserManager.Browser
var currentCoordinates = AqualityServices.Browser
.ExecuteScriptFromFile<IList<object>>("Resources.GetScrollCoordinates.js",
homeDemoSiteForm.FirstScrollableExample.GetElement()).Select(item => int.Parse(item.ToString()))
.ToList();
Expand All @@ -164,8 +163,8 @@ public void Should_BePossibleTo_ScrollToTheCenter()
welcomeForm.Open();
welcomeForm.GetExampleLink(AvailableExample.Dropdown).JsActions.ScrollToTheCenter();

var windowSize = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetWindowSize.js").ToString();
var currentY = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetElementYCoordinate.js",
var windowSize = AqualityServices.Browser.ExecuteScriptFromFile<object>("Resources.GetWindowSize.js").ToString();
var currentY = AqualityServices.Browser.ExecuteScriptFromFile<object>("Resources.GetElementYCoordinate.js",
welcomeForm.GetExampleLink(AvailableExample.Dropdown).GetElement()).ToString();
var coordinateRelatingWindowCenter = double.Parse(windowSize) / 2 - double.Parse(currentY);
Assert.LessOrEqual(Math.Abs(coordinateRelatingWindowCenter), accuracy, "Upper bound of element should be in the center of the page");
Expand All @@ -179,7 +178,7 @@ public void Should_BePossibleTo_ScrollToTheCenter_CheckUI()
infiniteScrollForm.WaitForPageToLoad();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
Assert.DoesNotThrow(
() => BrowserManager.GetRequiredService<ConditionalWait>().WaitForTrue(() =>
() => AqualityServices.ConditionalWait.WaitForTrue(() =>
{
infiniteScrollForm.Footer.JsActions.ScrollToTheCenter();
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
Expand Down
Loading

0 comments on commit 8a9e933

Please sign in to comment.