1
- using Aquality . Selenium . Elements ;
2
- using Aquality . Selenium . Elements . Interfaces ;
3
- using Microsoft . Extensions . DependencyInjection ;
1
+ using Microsoft . Extensions . DependencyInjection ;
4
2
using System ;
5
3
using System . Threading ;
6
- using Aquality . Selenium . Core . Localization ;
7
- using Aquality . Selenium . Core . Logging ;
8
- using System . Reflection ;
9
4
using Aquality . Selenium . Core . Applications ;
10
5
using 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 ;
14
6
15
7
namespace Aquality . Selenium . Browsers
16
8
{
@@ -19,6 +11,7 @@ namespace Aquality.Selenium.Browsers
19
11
/// </summary>
20
12
public class BrowserManager : ApplicationManager < Browser >
21
13
{
14
+ private static readonly ThreadLocal < BrowserStartup > BrowserStartupContainer = new ThreadLocal < BrowserStartup > ( ( ) => new BrowserStartup ( ) ) ;
22
15
private static readonly ThreadLocal < IBrowserFactory > BrowserFactoryContainer = new ThreadLocal < IBrowserFactory > ( ) ;
23
16
24
17
/// <summary>
@@ -27,27 +20,28 @@ public class BrowserManager : ApplicationManager<Browser>
27
20
/// <value>Instance of desired browser.</value>
28
21
public static Browser Browser
29
22
{
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 ) ;
38
25
}
39
26
40
27
private static Func < IServiceProvider , Browser > StartBrowserFunction => services => BrowserFactory . Browser ;
41
28
42
29
public static IServiceProvider ServiceProvider
43
30
{
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 )
49
42
{
50
- SetServiceProvider ( value ) ;
43
+ BrowserStartupContainer . Value = startup ;
44
+ SetServiceProvider ( ConfigureServices ( ) . BuildServiceProvider ( ) ) ;
51
45
}
52
46
}
53
47
@@ -62,30 +56,12 @@ public static IBrowserFactory BrowserFactory
62
56
{
63
57
SetDefaultFactory ( ) ;
64
58
}
59
+
65
60
return BrowserFactoryContainer . Value ;
66
61
}
67
- set
68
- {
69
- BrowserFactoryContainer . Value = value ;
70
- }
62
+ set => BrowserFactoryContainer . Value = value ;
71
63
}
72
64
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
-
89
65
/// <summary>
90
66
/// Sets default factory responsible for browser creation.
91
67
/// RemoteBrowserFactory if value set in configuration and LocalBrowserFactory otherwise.
@@ -109,12 +85,17 @@ public static void SetDefaultFactory()
109
85
/// <summary>
110
86
/// Resolves required service from <see cref="ServiceProvider"/>
111
87
/// </summary>
112
- /// <typeparam name="T">type of required service</typeparam>
88
+ /// <typeparam name="T">type of required service. </typeparam>
113
89
/// <exception cref="InvalidOperationException">Thrown if there is no service of the required type.</exception>
114
90
/// <returns></returns>
115
91
public static T GetRequiredService < T > ( )
116
92
{
117
93
return ServiceProvider . GetRequiredService < T > ( ) ;
118
94
}
95
+
96
+ private static IServiceCollection ConfigureServices ( )
97
+ {
98
+ return BrowserStartupContainer . Value . ConfigureServices ( new ServiceCollection ( ) , services => Browser ) ;
99
+ }
119
100
}
120
101
}
0 commit comments