1- using Aquality . Selenium . Elements ;
2- using Aquality . Selenium . Elements . Interfaces ;
3- using Microsoft . Extensions . DependencyInjection ;
1+ using Microsoft . Extensions . DependencyInjection ;
42using System ;
53using System . Threading ;
6- using Aquality . Selenium . Core . Localization ;
7- using Aquality . Selenium . Core . Logging ;
8- using System . Reflection ;
94using Aquality . Selenium . Core . Applications ;
105using Aquality . Selenium . Configurations ;
11- using CoreElementFactory = Aquality . Selenium . Core . Elements . Interfaces . IElementFactory ;
12- using CoreTimeoutConfiguration = Aquality . Selenium . Core . Configurations . ITimeoutConfiguration ;
13- using ILoggerConfiguration = Aquality . Selenium . Core . Configurations . ILoggerConfiguration ;
146
157namespace Aquality . Selenium . Browsers
168{
@@ -19,6 +11,7 @@ namespace Aquality.Selenium.Browsers
1911 /// </summary>
2012 public class BrowserManager : ApplicationManager < Browser >
2113 {
14+ private static readonly ThreadLocal < BrowserStartup > BrowserStartupContainer = new ThreadLocal < BrowserStartup > ( ( ) => new BrowserStartup ( ) ) ;
2215 private static readonly ThreadLocal < IBrowserFactory > BrowserFactoryContainer = new ThreadLocal < IBrowserFactory > ( ) ;
2316
2417 /// <summary>
@@ -27,27 +20,28 @@ public class BrowserManager : ApplicationManager<Browser>
2720 /// <value>Instance of desired browser.</value>
2821 public static Browser Browser
2922 {
30- get
31- {
32- return GetApplication ( StartBrowserFunction , ( ) => RegisterServices ( services => Browser ) ) ;
33- }
34- set
35- {
36- SetApplication ( value ) ;
37- }
23+ get => GetApplication ( StartBrowserFunction , ConfigureServices ) ;
24+ set => SetApplication ( value ) ;
3825 }
3926
4027 private static Func < IServiceProvider , Browser > StartBrowserFunction => services => BrowserFactory . Browser ;
4128
4229 public static IServiceProvider ServiceProvider
4330 {
44- get
45- {
46- return GetServiceProvider ( services => Browser , ( ) => RegisterServices ( services => Browser ) ) ;
47- }
48- set
31+ get => GetServiceProvider ( services => Browser , ConfigureServices ) ;
32+ set => SetServiceProvider ( value ) ;
33+ }
34+
35+ /// <summary>
36+ /// Method which allow user to override or add custom services.
37+ /// </summary>
38+ /// <param name="startup"><see cref="BrowserStartup"/>> object with custom or overriden services.</param>
39+ public static void SetStartup ( BrowserStartup startup )
40+ {
41+ if ( startup != null )
4942 {
50- SetServiceProvider ( value ) ;
43+ BrowserStartupContainer . Value = startup ;
44+ SetServiceProvider ( ConfigureServices ( ) . BuildServiceProvider ( ) ) ;
5145 }
5246 }
5347
@@ -62,30 +56,12 @@ public static IBrowserFactory BrowserFactory
6256 {
6357 SetDefaultFactory ( ) ;
6458 }
59+
6560 return BrowserFactoryContainer . Value ;
6661 }
67- set
68- {
69- BrowserFactoryContainer . Value = value ;
70- }
62+ set => BrowserFactoryContainer . Value = value ;
7163 }
7264
73- private static IServiceCollection RegisterServices ( Func < IServiceProvider , Browser > browserSupplier )
74- {
75- var services = new ServiceCollection ( ) ;
76- var startup = new Startup ( ) ;
77- var settingsFile = startup . GetSettings ( ) ;
78- startup . ConfigureServices ( services , browserSupplier , settingsFile ) ;
79- services . AddTransient < IElementFactory , ElementFactory > ( ) ;
80- services . AddTransient < CoreElementFactory , ElementFactory > ( ) ;
81- services . AddSingleton < ITimeoutConfiguration > ( serviceProvider => new TimeoutConfiguration ( settingsFile ) ) ;
82- services . AddSingleton < CoreTimeoutConfiguration > ( serviceProvider => new TimeoutConfiguration ( settingsFile ) ) ;
83- services . AddSingleton < IBrowserProfile > ( serviceProvider => new BrowserProfile ( settingsFile ) ) ;
84- services . AddSingleton ( serviceProvider => new LocalizationManager ( serviceProvider . GetRequiredService < ILoggerConfiguration > ( ) , serviceProvider . GetRequiredService < Logger > ( ) , Assembly . GetExecutingAssembly ( ) ) ) ;
85- services . AddTransient ( serviceProvider => BrowserFactory ) ;
86- return services ;
87- }
88-
8965 /// <summary>
9066 /// Sets default factory responsible for browser creation.
9167 /// RemoteBrowserFactory if value set in configuration and LocalBrowserFactory otherwise.
@@ -109,12 +85,17 @@ public static void SetDefaultFactory()
10985 /// <summary>
11086 /// Resolves required service from <see cref="ServiceProvider"/>
11187 /// </summary>
112- /// <typeparam name="T">type of required service</typeparam>
88+ /// <typeparam name="T">type of required service. </typeparam>
11389 /// <exception cref="InvalidOperationException">Thrown if there is no service of the required type.</exception>
11490 /// <returns></returns>
11591 public static T GetRequiredService < T > ( )
11692 {
11793 return ServiceProvider . GetRequiredService < T > ( ) ;
11894 }
95+
96+ private static IServiceCollection ConfigureServices ( )
97+ {
98+ return BrowserStartupContainer . Value . ConfigureServices ( new ServiceCollection ( ) , services => Browser ) ;
99+ }
119100 }
120101}
0 commit comments