Skip to content

Commit 8a9e933

Browse files
authored
Feature/156 add properties to get registered services (#157)
+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
1 parent f3caa97 commit 8a9e933

35 files changed

+239
-198
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
</ItemGroup>
6060

6161
<ItemGroup>
62-
<PackageReference Include="Aquality.Selenium.Core" Version="0.2.5" />
62+
<PackageReference Include="Aquality.Selenium.Core" Version="0.3.1" />
6363
<PackageReference Include="NLog" Version="4.6.6" />
6464
<PackageReference Include="Selenium.Support" Version="3.141.0" />
6565
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserManager.cs renamed to Aquality.Selenium/src/Aquality.Selenium/Browsers/AqualityServices.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using System;
33
using System.Threading;
4-
using Aquality.Selenium.Core.Applications;
54
using Aquality.Selenium.Configurations;
5+
using Aquality.Selenium.Core.Applications;
6+
using Aquality.Selenium.Core.Localization;
7+
using Aquality.Selenium.Core.Logging;
8+
using Aquality.Selenium.Core.Waitings;
69

710
namespace Aquality.Selenium.Browsers
811
{
912
/// <summary>
1013
/// Controls browser instance creation.
1114
/// </summary>
12-
public class BrowserManager : ApplicationManager<Browser>
15+
public class AqualityServices : AqualityServices<Browser>
1316
{
1417
private static readonly ThreadLocal<BrowserStartup> BrowserStartupContainer = new ThreadLocal<BrowserStartup>(() => new BrowserStartup());
1518
private static readonly ThreadLocal<IBrowserFactory> BrowserFactoryContainer = new ThreadLocal<IBrowserFactory>();
1619

20+
/// <summary>
21+
/// Check if browser already started.
22+
/// </summary>
23+
/// <value>true if browser started and false otherwise.</value>
24+
public static bool IsBrowserStarted => IsApplicationStarted();
25+
26+
/// <summary>
27+
/// Gets registered instance of logger
28+
/// </summary>
29+
public static Logger Logger => Get<Logger>();
30+
31+
/// <summary>
32+
/// Gets registered instance of localized logger
33+
/// </summary>
34+
public static ILocalizedLogger LocalizedLogger => Get<ILocalizedLogger>();
35+
36+
/// <summary>
37+
/// Gets ConditionalWait object
38+
/// </summary>
39+
public static ConditionalWait ConditionalWait => Get<ConditionalWait>();
40+
1741
/// <summary>
1842
/// Gets and sets thread-safe instance of browser.
1943
/// </summary>
@@ -26,12 +50,6 @@ public static Browser Browser
2650

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

29-
public static IServiceProvider ServiceProvider
30-
{
31-
get => GetServiceProvider(services => Browser, ConfigureServices);
32-
set => SetServiceProvider(value);
33-
}
34-
3553
/// <summary>
3654
/// Method which allow user to override or add custom services.
3755
/// </summary>
@@ -68,15 +86,15 @@ public static IBrowserFactory BrowserFactory
6886
/// </summary>
6987
public static void SetDefaultFactory()
7088
{
71-
var appProfile = GetRequiredService<IBrowserProfile>();
89+
var appProfile = Get<IBrowserProfile>();
7290
IBrowserFactory applicationFactory;
7391
if (appProfile.IsRemote)
7492
{
75-
applicationFactory = new RemoteBrowserFactory(ServiceProvider);
93+
applicationFactory = new RemoteBrowserFactory();
7694
}
7795
else
7896
{
79-
applicationFactory = new LocalBrowserFactory(ServiceProvider);
97+
applicationFactory = new LocalBrowserFactory();
8098
}
8199

82100
BrowserFactory = applicationFactory;
@@ -88,11 +106,13 @@ public static void SetDefaultFactory()
88106
/// <typeparam name="T">type of required service.</typeparam>
89107
/// <exception cref="InvalidOperationException">Thrown if there is no service of the required type.</exception>
90108
/// <returns></returns>
91-
public static T GetRequiredService<T>()
109+
public static T Get<T>()
92110
{
93111
return ServiceProvider.GetRequiredService<T>();
94112
}
95113

114+
private static IServiceProvider ServiceProvider => GetServiceProvider(services => Browser, ConfigureServices);
115+
96116
private static IServiceCollection ConfigureServices()
97117
{
98118
return BrowserStartupContainer.Value.ConfigureServices(new ServiceCollection(), services => Browser);

Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Drawing;
88
using System.Reflection;
99
using System;
10-
using Microsoft.Extensions.DependencyInjection;
1110
using Aquality.Selenium.Core.Waitings;
1211

1312
namespace Aquality.Selenium.Browsers
@@ -26,14 +25,14 @@ public class Browser : IApplication
2625
/// Instantiate browser.
2726
/// </summary>
2827
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
29-
public Browser(RemoteWebDriver webDriver, IServiceProvider serviceProvider)
28+
public Browser(RemoteWebDriver webDriver)
3029
{
3130
Driver = webDriver;
32-
Logger = serviceProvider.GetRequiredService<ILocalizedLogger>();
33-
LocalizationManager = serviceProvider.GetRequiredService<ILocalizationManager>();
34-
browserProfile = serviceProvider.GetRequiredService<IBrowserProfile>();
35-
conditionalWait = serviceProvider.GetRequiredService<ConditionalWait>();
36-
var timeoutConfiguration = serviceProvider.GetRequiredService<ITimeoutConfiguration>();
31+
Logger = AqualityServices.LocalizedLogger;
32+
LocalizationManager = AqualityServices.Get<ILocalizationManager>();
33+
browserProfile = AqualityServices.Get<IBrowserProfile>();
34+
conditionalWait = AqualityServices.ConditionalWait;
35+
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
3736
SetImplicitWaitTimeout(timeoutConfiguration.Implicit);
3837
SetPageLoadTimeout(timeoutConfiguration.PageLoad);
3938
SetScriptTimeout(timeoutConfiguration.Script);
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
using System;
2-
3-
namespace Aquality.Selenium.Browsers
1+
namespace Aquality.Selenium.Browsers
42
{
53
/// <summary>
64
/// Abstract representation of <see cref="IBrowserFactory"/>.
75
/// </summary>
86
public abstract class BrowserFactory : IBrowserFactory
97
{
10-
protected BrowserFactory(IServiceProvider serviceProvider)
8+
protected BrowserFactory()
119
{
12-
ServiceProvider = serviceProvider;
1310
}
1411

15-
protected IServiceProvider ServiceProvider { get; }
16-
1712
public abstract Browser Browser { get; }
1813
}
1914
}

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserNavigation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal BrowserNavigation(RemoteWebDriver driver)
1717
this.driver = driver;
1818
}
1919

20-
private ILocalizedLogger Logger => BrowserManager.GetRequiredService<ILocalizedLogger>();
20+
private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;
2121

2222
/// <summary>
2323
/// Navigates back.

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override IServiceCollection ConfigureServices(IServiceCollection services
2929
services.AddSingleton<CoreTimeoutConfiguration>(serviceProvider => new TimeoutConfiguration(settings));
3030
services.AddSingleton<IBrowserProfile>(serviceProvider => new BrowserProfile(settings));
3131
services.AddSingleton<ILocalizationManager>(serviceProvider => new LocalizationManager(serviceProvider.GetRequiredService<ILoggerConfiguration>(), serviceProvider.GetRequiredService<Logger>(), Assembly.GetExecutingAssembly()));
32-
services.AddTransient(serviceProvider => BrowserManager.BrowserFactory);
32+
services.AddTransient(serviceProvider => AqualityServices.BrowserFactory);
3333
return services;
3434
}
3535
}

Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Aquality.Selenium.Configurations;
22
using Aquality.Selenium.Configurations.WebDriverSettings;
3-
using Microsoft.Extensions.DependencyInjection;
43
using OpenQA.Selenium.Chrome;
54
using OpenQA.Selenium.Edge;
65
using OpenQA.Selenium.Firefox;
@@ -23,15 +22,15 @@ public class LocalBrowserFactory : BrowserFactory
2322
{
2423
private static readonly object WebDriverDownloadingLock = new object();
2524

26-
public LocalBrowserFactory(IServiceProvider serviceProvider) : base(serviceProvider)
25+
public LocalBrowserFactory() : base()
2726
{
2827
}
2928

3029
public override Browser Browser => CreateBrowser();
3130

3231
private Browser CreateBrowser()
3332
{
34-
var browserProfile = ServiceProvider.GetRequiredService<IBrowserProfile>();
33+
var browserProfile = AqualityServices.Get<IBrowserProfile>();
3534
var browserName = browserProfile.BrowserName;
3635
var driverSettings = browserProfile.DriverSettings;
3736
RemoteWebDriver driver;
@@ -58,7 +57,7 @@ private Browser CreateBrowser()
5857
default:
5958
throw new ArgumentOutOfRangeException($"Browser {browserName} is not supported.");
6059
}
61-
return new Browser(driver, ServiceProvider);
60+
return new Browser(driver);
6261
}
6362

6463
private static void SetUpDriver(IDriverConfig driverConfig, IDriverSettings driverSettings)

Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Aquality.Selenium.Configurations;
2-
using Microsoft.Extensions.DependencyInjection;
32
using OpenQA.Selenium.Remote;
4-
using System;
53

64
namespace Aquality.Selenium.Browsers
75
{
@@ -10,19 +8,19 @@ namespace Aquality.Selenium.Browsers
108
/// </summary>
119
public class RemoteBrowserFactory : BrowserFactory
1210
{
13-
public RemoteBrowserFactory(IServiceProvider serviceProvider) : base(serviceProvider)
11+
public RemoteBrowserFactory() : base()
1412
{
1513
}
1614

1715
public override Browser Browser
1816
{
1917
get
2018
{
21-
var browserProfile = ServiceProvider.GetRequiredService<IBrowserProfile>();
19+
var browserProfile = AqualityServices.Get<IBrowserProfile>();
2220
var capabilities = browserProfile.DriverSettings.DriverOptions.ToCapabilities();
23-
var timeoutConfiguration = ServiceProvider.GetRequiredService<ITimeoutConfiguration>();
21+
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
2422
var driver = new RemoteWebDriver(browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command);
25-
return new Browser(driver, ServiceProvider);
23+
return new Browser(driver);
2624
}
2725
}
2826
}

Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public JsActions(IElement element, string elementType, ILocalizedLogger logger,
2727
Logger = logger;
2828
}
2929

30-
private Browser Browser => BrowserManager.Browser;
30+
private Browser Browser => AqualityServices.Browser;
3131

3232
protected ILocalizedLogger Logger { get; }
3333

Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/MouseActions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MouseActions(IElement element, string elementType, ILocalizedLogger logge
2828
this.elementActionsRetrier = elementActionsRetrier;
2929
}
3030

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

3333
/// <summary>
3434
/// Performs click on element.
@@ -65,12 +65,12 @@ public void MoveToElement()
6565
{
6666
LogElementAction("loc.moving");
6767
JsActions.ScrollIntoView();
68-
elementActionsRetrier.DoWithRetry(() => PerformAction(element => MoveToElement(element)));
68+
elementActionsRetrier.DoWithRetry(() => PerformAction(MoveToElement));
6969
}
7070

7171
private SeleniumActions MoveToElement(IWebElement element)
7272
{
73-
return new SeleniumActions(BrowserManager.Browser.Driver).MoveToElement(element);
73+
return new SeleniumActions(AqualityServices.Browser.Driver).MoveToElement(element);
7474
}
7575

7676
private void PerformAction(Func<RemoteWebElement, SeleniumActions> action)

0 commit comments

Comments
 (0)