Skip to content

Commit

Permalink
Feature/34 add properties for aquality services (#35)
Browse files Browse the repository at this point in the history
* updated core version
* renamed ApplicationManager to AqualityServices
* Added properties to simple get some services
  • Loading branch information
knysh authored and mialeska committed Dec 20, 2019
1 parent 13ff1a0 commit 29c0559
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Aquality.Selenium.Core.Configurations;
using Aquality.Selenium.Core.Localization;
using Aquality.WinAppDriver.Actions;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;

Expand All @@ -25,15 +24,14 @@ public class Application : IWindowsApplication
/// </summary>
/// <param name="createApplicationSession">Function to create an instance of WinAppDriver for current application</param>
/// <param name="createRootSession">Function to create an instance of WinAppDriver for desktop session</param>
/// <param name="serviceProvider">Service provider to resolve all dependencies from DI container</param>
public Application(Func<WindowsDriver<WindowsElement>> createApplicationSession, Func<WindowsDriver<WindowsElement>> createRootSession, IServiceProvider serviceProvider)
public Application(Func<WindowsDriver<WindowsElement>> createApplicationSession, Func<WindowsDriver<WindowsElement>> createRootSession)
{
this.createApplicationSession = createApplicationSession;
this.createDesktopSession = createRootSession;
Logger = serviceProvider.GetRequiredService<ILocalizedLogger>();
KeyboardActions = serviceProvider.GetRequiredService<IKeyboardActions>();
MouseActions = serviceProvider.GetRequiredService<IMouseActions>();
var timeoutConfiguration = serviceProvider.GetRequiredService<ITimeoutConfiguration>();
Logger = AqualityServices.LocalizedLogger;
KeyboardActions = AqualityServices.KeyboardActions;
MouseActions = AqualityServices.MouseActions;
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
implicitWait = timeoutConfiguration.Implicit;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Aquality.Selenium.Core.Configurations;
using Aquality.Selenium.Core.Localization;
using Aquality.WinAppDriver.Configurations;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;
Expand All @@ -14,21 +13,19 @@ public abstract class ApplicationFactory : IApplicationFactory

protected ILocalizedLogger LocalizedLogger { get; }
protected IDriverSettings DriverSettings { get; }
protected IServiceProvider ServiceProvider { get; }

protected ApplicationFactory(IServiceProvider serviceProvider)
protected ApplicationFactory()
{
LocalizedLogger = serviceProvider.GetRequiredService<ILocalizedLogger>();
DriverSettings = serviceProvider.GetRequiredService<IDriverSettings>();
timeoutConfiguration = serviceProvider.GetRequiredService<ITimeoutConfiguration>();
ServiceProvider = serviceProvider;
LocalizedLogger = AqualityServices.LocalizedLogger;
DriverSettings = AqualityServices.Get<IDriverSettings>();
timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
}

public abstract Application Application { get; }

protected virtual Application GetApplication(Uri driverServerUri)
{
return new Application(() => GetApplicationSession(driverServerUri), () => GetRootSession(driverServerUri), ServiceProvider);
return new Application(() => GetApplicationSession(driverServerUri), () => GetRootSession(driverServerUri));
}

protected virtual WindowsDriver<WindowsElement> GetApplicationSession(Uri driverServerUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public override IServiceCollection ConfigureServices(IServiceCollection services
services.AddSingleton<IDriverSettings>(serviceProvider => new DriverSettings(settings));
services.AddSingleton<IApplicationProfile>(serviceProvider => new ApplicationProfile(settings, serviceProvider.GetRequiredService<IDriverSettings>()));
services.AddSingleton<ILocalizationManager>(serviceProvider => new LocalizationManager(serviceProvider.GetRequiredService<ILoggerConfiguration>(), serviceProvider.GetRequiredService<Logger>(), Assembly.GetExecutingAssembly()));
services.AddSingleton<IKeyboardActions>(serviceProvider => new KeyboardActions(serviceProvider.GetRequiredService<ILocalizedLogger>(), () => ApplicationManager.Application.Driver));
services.AddSingleton<IMouseActions>(serviceProvider => new MouseActions(serviceProvider.GetRequiredService<ILocalizedLogger>(), () => ApplicationManager.Application.Driver));
services.AddTransient(serviceProvider => ApplicationManager.ApplicationFactory);
services.AddSingleton<IKeyboardActions>(serviceProvider => new KeyboardActions(serviceProvider.GetRequiredService<ILocalizedLogger>(), () => AqualityServices.Application.Driver));
services.AddSingleton<IMouseActions>(serviceProvider => new MouseActions(serviceProvider.GetRequiredService<ILocalizedLogger>(), () => AqualityServices.Application.Driver));
services.AddTransient(serviceProvider => AqualityServices.ApplicationFactory);
services.AddTransient<IProcessManager, ProcessManager>();
services.AddTransient<IWinAppDriverLauncher, WinAppDriverLauncher>();
return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
using Aquality.Selenium.Core.Configurations;
using System;
using System;
using Microsoft.Extensions.DependencyInjection;
using Aquality.Selenium.Core.Localization;
using CoreElementFactory = Aquality.Selenium.Core.Elements.Interfaces.IElementFactory;
using Aquality.WinAppDriver.Elements.Interfaces;
using Aquality.WinAppDriver.Elements;
using Aquality.Selenium.Core.Applications;
using System.Threading;
using Aquality.WinAppDriver.Configurations;
using OpenQA.Selenium.Appium.Service;
using Aquality.Selenium.Core.Logging;
using System.Reflection;
using Aquality.WinAppDriver.Actions;
using Aquality.WinAppDriver.Utilities;
using OpenQA.Selenium.Appium.Windows;
using Aquality.Selenium.Core.Waitings;
using Aquality.WinAppDriver.Utilities;
using Aquality.WinAppDriver.Actions;

namespace Aquality.WinAppDriver.Applications
{
/// <summary>
/// Controls application and Aquality services
/// </summary>
public class ApplicationManager : ApplicationManager<Application>
public class AqualityServices : AqualityServices<Application>
{
private static readonly ThreadLocal<ApplicationStartup> ApplicationStartupContainer = new ThreadLocal<ApplicationStartup>(() => new ApplicationStartup());
private static readonly ThreadLocal<IApplicationFactory> ApplicationFactoryContainer = new ThreadLocal<IApplicationFactory>();
private static readonly ThreadLocal<AppiumLocalService> AppiumLocalServiceContainer = new ThreadLocal<AppiumLocalService>(AppiumLocalService.BuildDefaultService);

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

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

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

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

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

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

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

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

/// <summary>
/// Stops appium local service.
/// </summary>
Expand All @@ -34,7 +71,7 @@ public static bool TryToStopAppiumLocalService()
{
if(AppiumLocalServiceContainer.IsValueCreated && AppiumLocalServiceContainer.Value.IsRunning)
{
GetRequiredService<ILocalizedLogger>().Info("loc.application.driver.service.local.stop");
Get<ILocalizedLogger>().Info("loc.application.driver.service.local.stop");
AppiumLocalServiceContainer.Value.Dispose();
return true;
}
Expand All @@ -49,16 +86,7 @@ public static Application Application
{
get => GetApplication(StartApplicationFunction, ConfigureServices);
set => SetApplication(value);
}

/// <summary>
/// Provides access to Aquality services, registered in DI container.
/// </summary>
public static IServiceProvider ServiceProvider
{
get => GetServiceProvider(services => Application, ConfigureServices);
set => SetServiceProvider(value);
}
}

/// <summary>
/// Method which allow user to override or add custom services.
Expand Down Expand Up @@ -95,7 +123,7 @@ public static IApplicationFactory ApplicationFactory
/// <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>();
}
Expand All @@ -106,15 +134,15 @@ public static T GetRequiredService<T>()
/// </summary>
public static void SetDefaultFactory()
{
var appProfile = GetRequiredService<IApplicationProfile>();
var appProfile = Get<IApplicationProfile>();
IApplicationFactory applicationFactory;
if (appProfile.IsRemote)
{
applicationFactory = new RemoteApplicationFactory(appProfile.RemoteConnectionUrl, ServiceProvider);
applicationFactory = new RemoteApplicationFactory(appProfile.RemoteConnectionUrl);
}
else
{
applicationFactory = new LocalApplicationFactory(AppiumLocalServiceContainer.Value, ServiceProvider);
applicationFactory = new LocalApplicationFactory(AppiumLocalServiceContainer.Value);
}

ApplicationFactory = applicationFactory;
Expand All @@ -126,11 +154,13 @@ public static void SetDefaultFactory()
/// <param name="getWindowHandleFunction">Function to get window handle via RootSession of Application</param>
public static void SetWindowHandleApplicationFactory(Func<WindowsDriver<WindowsElement>, string> getWindowHandleFunction)
{
var appProfile = GetRequiredService<IApplicationProfile>();
var appProfile = Get<IApplicationProfile>();
var serviceUri = appProfile.IsRemote ? appProfile.RemoteConnectionUrl : AppiumLocalServiceContainer.Value.ServiceUrl;
ApplicationFactory = new WindowHandleApplicationFactory(serviceUri, ServiceProvider, getWindowHandleFunction);
ApplicationFactory = new WindowHandleApplicationFactory(serviceUri, getWindowHandleFunction);
}

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

private static Func<IServiceProvider, Application> StartApplicationFunction
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public class LocalApplicationFactory : ApplicationFactory
{
private readonly AppiumLocalService driverService;

public LocalApplicationFactory(AppiumLocalService driverService, IServiceProvider serviceProvider)
: base(serviceProvider)
public LocalApplicationFactory(AppiumLocalService driverService) : base()
{
this.driverService = driverService;
}
Expand All @@ -20,7 +19,7 @@ public override Application Application
driverService.Start();
var serviceUrl = driverService.ServiceUrl;
LocalizedLogger.Info("loc.application.driver.service.local.start", serviceUrl);
return new Application(() => GetApplicationSession(serviceUrl), () => GetRootSession(serviceUrl), ServiceProvider);
return new Application(() => GetApplicationSession(serviceUrl), () => GetRootSession(serviceUrl));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class RemoteApplicationFactory : ApplicationFactory
{
private readonly Uri driverServerUri;

public RemoteApplicationFactory(Uri driverServerUri, IServiceProvider serviceProvider)
: base(serviceProvider)
public RemoteApplicationFactory(Uri driverServerUri) : base()
{
this.driverServerUri = driverServerUri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Aquality.WinAppDriver.Configurations;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
Expand All @@ -15,12 +14,12 @@ public class WindowHandleApplicationFactory : ApplicationFactory
private readonly Func<WindowsDriver<WindowsElement>, string> getWindowHandleFunction;
private readonly bool isRemote;

public WindowHandleApplicationFactory(Uri driverServerUri, IServiceProvider serviceProvider, Func<WindowsDriver<WindowsElement>, string> getWindowHandleFunction)
: base(serviceProvider)
public WindowHandleApplicationFactory(Uri driverServerUri, Func<WindowsDriver<WindowsElement>, string> getWindowHandleFunction)
: base()
{
this.driverServerUri = driverServerUri;
this.getWindowHandleFunction = getWindowHandleFunction;
isRemote = serviceProvider.GetRequiredService<IApplicationProfile>().IsRemote;
isRemote = AqualityServices.Get<IApplicationProfile>().IsRemote;
}

public override Application Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.0.0" />
<PackageReference Include="Aquality.Selenium.Core" Version="0.2.5" />
<PackageReference Include="Appium.WebDriver" Version="4.1.1" />
<PackageReference Include="Aquality.Selenium.Core" Version="0.3.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit 29c0559

Please sign in to comment.