-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathAqualityServices.java
176 lines (154 loc) · 5.19 KB
/
AqualityServices.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package aquality.selenium.browser;
import aquality.selenium.configuration.IBrowserProfile;
import aquality.selenium.configuration.IConfiguration;
import aquality.selenium.configuration.ITimeoutConfiguration;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.logging.Logger;
import aquality.selenium.core.utilities.IActionRetrier;
import aquality.selenium.core.waitings.IConditionalWait;
import aquality.selenium.elements.interfaces.IElementFactory;
import com.google.inject.Injector;
/**
* Controls browser and Aquality services.
*/
public class AqualityServices extends aquality.selenium.core.applications.AqualityServices<Browser> {
private static final ThreadLocal<AqualityServices> INSTANCE_CONTAINER = ThreadLocal.withInitial(AqualityServices::new);
private IBrowserFactory browserFactory;
private AqualityServices() {
super(AqualityServices::getBrowser, () -> new BrowserModule(AqualityServices::getBrowser));
}
private AqualityServices(BrowserModule module) {
super(AqualityServices::getBrowser, () -> module);
}
private static AqualityServices getInstance() {
return INSTANCE_CONTAINER.get();
}
/**
* Gets instance of browser.
*
* @return Instance of desired browser.
*/
public static Browser getBrowser() {
return getInstance().getApp(injector -> AqualityServices.startBrowser());
}
/**
* Check if browser already started or not.
*
* @return true if browser started and false otherwise.
*/
public static boolean isBrowserStarted() {
return getInstance().isAppStarted();
}
/**
* Resolves required service from DI container.
* Note that the service should be binded in {@link BrowserModule}.
*
* @param type class of required service.
* @param <T> type of required service.
* @return required service.
*/
public static <T> T get(Class<T> type) {
return getServiceProvider().getInstance(type);
}
private static Injector getServiceProvider() {
return getInstance().getInjector();
}
/**
* Sets default(local {@link LocalBrowserFactory} or remote {@link RemoteBrowserFactory}) browser factory.
*/
public static void setDefaultBrowserFactory() {
IBrowserFactory browserFactory = getBrowserProfile().isRemote()
? new RemoteBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), get(ITimeoutConfiguration.class), getLocalizedLogger())
: new LocalBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), getLocalizedLogger());
setBrowserFactory(browserFactory);
}
/**
* Gets factory for browser creation.
*
* @return current instance of browser factory.
*/
public static IBrowserFactory getBrowserFactory() {
if (getInstance().browserFactory == null) {
setDefaultBrowserFactory();
}
return getInstance().browserFactory;
}
/**
* Sets custom browser factory.
*
* @param browserFactory Custom implementation of {@link IBrowserFactory}
*/
public static void setBrowserFactory(IBrowserFactory browserFactory) {
getInstance().browserFactory = browserFactory;
}
private static Browser startBrowser() {
return getBrowserFactory().getBrowser();
}
/**
* Sets instance of browser.
*
* @param browser Instance of desired browser.
*/
public static void setBrowser(Browser browser) {
getInstance().setApp(browser);
}
/**
* Reinitializes the dependency injector with custom {@link BrowserModule}.
*
* @param module {@link BrowserModule} object with custom or overriden services.
*/
public static void initInjector(BrowserModule module) {
if (INSTANCE_CONTAINER.get() != null) {
INSTANCE_CONTAINER.remove();
}
INSTANCE_CONTAINER.set(new AqualityServices(module));
}
/**
* Gets logger.
*
* @return instance of logger.
*/
public static Logger getLogger() {
return get(Logger.class);
}
/**
* Gets logger which logs messages in current language.
*
* @return instance of localized logger.
*/
public static ILocalizedLogger getLocalizedLogger() {
return get(ILocalizedLogger.class);
}
/**
* Provides the utility used to wait for some condition.
*
* @return instance of conditional wait.
*/
public static IConditionalWait getConditionalWait() {
return get(IConditionalWait.class);
}
/**
* Gets factory to create elements.
*
* @return Factory of elements.
*/
public static IElementFactory getElementFactory() {
return get(IElementFactory.class);
}
/**
* Gets current profile of browser settings.
*
* @return current browser profile.
*/
public static IBrowserProfile getBrowserProfile() {
return get(IBrowserProfile.class);
}
/**
* Provides interface to access all described configurations.
*
* @return configuration.
*/
public static IConfiguration getConfiguration() {
return get(IConfiguration.class);
}
}