1
1
using Aquality . Selenium . Configurations ;
2
- using Aquality . Selenium . Localization ;
3
- using Aquality . Selenium . Logging ;
4
- using Aquality . Selenium . Waitings ;
2
+ using Aquality . Selenium . Core . Applications ;
3
+ using Aquality . Selenium . Core . Localization ;
5
4
using OpenQA . Selenium ;
6
5
using OpenQA . Selenium . Remote ;
7
6
using OpenQA . Selenium . Support . Extensions ;
8
- using System ;
9
7
using System . Drawing ;
10
8
using System . Reflection ;
9
+ using System ;
10
+ using Microsoft . Extensions . DependencyInjection ;
11
+ using Aquality . Selenium . Core . Waitings ;
11
12
12
13
namespace Aquality . Selenium . Browsers
13
14
{
14
15
/// <summary>
15
16
/// Provides functionality to work with browser via Selenium WebDriver.
16
17
/// </summary>
17
- public class Browser
18
+ public class Browser : IApplication
18
19
{
19
- private readonly IConfiguration configuration ;
20
20
private TimeSpan implicitWaitTimeout ;
21
21
private TimeSpan pageLoadTimeout ;
22
+ private readonly IBrowserProfile browserProfile ;
23
+ private readonly ConditionalWait conditionalWait ;
22
24
23
25
/// <summary>
24
26
/// Instantiate browser.
25
27
/// </summary>
26
28
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
27
- /// <param name="configuration">Configuration.</param>
28
- public Browser ( RemoteWebDriver webDriver , IConfiguration configuration )
29
+ public Browser ( RemoteWebDriver webDriver , IServiceProvider serviceProvider )
29
30
{
30
- this . configuration = configuration ;
31
31
Driver = webDriver ;
32
- BrowserName = configuration . BrowserProfile . BrowserName ;
33
- SetImplicitWaitTimeout ( configuration . TimeoutConfiguration . Implicit ) ;
34
- SetPageLoadTimeout ( configuration . TimeoutConfiguration . PageLoad ) ;
35
- SetScriptTimeout ( configuration . TimeoutConfiguration . Script ) ;
32
+ Logger = serviceProvider . GetRequiredService < LocalizationLogger > ( ) ;
33
+ LocalizationManager = serviceProvider . GetRequiredService < LocalizationManager > ( ) ;
34
+ browserProfile = serviceProvider . GetRequiredService < IBrowserProfile > ( ) ;
35
+ conditionalWait = serviceProvider . GetRequiredService < ConditionalWait > ( ) ;
36
+ var timeoutConfiguration = serviceProvider . GetRequiredService < ITimeoutConfiguration > ( ) ;
37
+ SetImplicitWaitTimeout ( timeoutConfiguration . Implicit ) ;
38
+ SetPageLoadTimeout ( timeoutConfiguration . PageLoad ) ;
39
+ SetScriptTimeout ( timeoutConfiguration . Script ) ;
36
40
}
37
41
38
- private Logger Logger => Logger . Instance ;
42
+ private LocalizationLogger Logger { get ; }
43
+
44
+ private LocalizationManager LocalizationManager { get ; }
39
45
40
46
/// <summary>
41
47
/// Gets instance of Selenium WebDriver.
@@ -47,7 +53,7 @@ public Browser(RemoteWebDriver webDriver, IConfiguration configuration)
47
53
/// Gets name of desired browser from configuration.
48
54
/// </summary>
49
55
/// <value>Name of browser.</value>
50
- public BrowserName BrowserName { get ; }
56
+ public BrowserName BrowserName => browserProfile . BrowserName ;
51
57
52
58
/// <summary>
53
59
/// Sets Selenium WebDriver ImplicitWait timeout.
@@ -71,7 +77,7 @@ public void SetImplicitWaitTimeout(TimeSpan timeout)
71
77
/// <param name="timeout">Desired page load timeout.</param>
72
78
public void SetPageLoadTimeout ( TimeSpan timeout )
73
79
{
74
- if ( ! configuration . BrowserProfile . BrowserName . Equals ( BrowserName . Safari ) )
80
+ if ( ! BrowserName . Equals ( BrowserName . Safari ) )
75
81
{
76
82
Driver . Manage ( ) . Timeouts ( ) . PageLoad = timeout ;
77
83
pageLoadTimeout = timeout ;
@@ -91,7 +97,7 @@ public void SetScriptTimeout(TimeSpan timeout)
91
97
/// <summary>
92
98
/// Gets browser configured download directory.
93
99
/// </summary>
94
- public string DownloadDirectory => configuration . BrowserProfile . DriverSettings . DownloadDir ;
100
+ public string DownloadDirectory => browserProfile . DriverSettings . DownloadDir ;
95
101
96
102
/// <summary>
97
103
/// Gets URL of currently opened page in web browser.
@@ -101,7 +107,7 @@ public string CurrentUrl
101
107
{
102
108
get
103
109
{
104
- Logger . InfoLoc ( "loc.browser.getUrl" ) ;
110
+ Logger . Info ( "loc.browser.getUrl" ) ;
105
111
return Driver . Url ;
106
112
}
107
113
}
@@ -111,7 +117,7 @@ public string CurrentUrl
111
117
/// </summary>
112
118
public void Quit ( )
113
119
{
114
- Logger . InfoLoc ( "loc.browser.driver.quit" ) ;
120
+ Logger . Info ( "loc.browser.driver.quit" ) ;
115
121
Driver ? . Quit ( ) ;
116
122
}
117
123
@@ -189,7 +195,7 @@ public void HandleAlert(AlertAction alertAction, string text = null)
189
195
}
190
196
catch ( NoAlertPresentException ex )
191
197
{
192
- Logger . FatalLoc ( "loc.browser.alert.fail" , ex ) ;
198
+ Logger . Fatal ( "loc.browser.alert.fail" , ex ) ;
193
199
throw ;
194
200
}
195
201
}
@@ -199,7 +205,7 @@ public void HandleAlert(AlertAction alertAction, string text = null)
199
205
/// </summary>
200
206
public void Maximize ( )
201
207
{
202
- Logger . InfoLoc ( "loc.browser.maximize" ) ;
208
+ Logger . Info ( "loc.browser.maximize" ) ;
203
209
Driver . Manage ( ) . Window . Maximize ( ) ;
204
210
}
205
211
@@ -209,8 +215,8 @@ public void Maximize()
209
215
/// <exception cref="TimeoutException">Throws when timeout exceeded and page is not loaded.</exception>
210
216
public void WaitForPageToLoad ( )
211
217
{
212
- var errorMessage = LocalizationManager . Instance . GetLocalizedMessage ( "loc.browser.page.timeout" ) ;
213
- ConditionalWait . WaitForTrue ( ( ) => ExecuteScript < bool > ( JavaScript . IsPageLoaded ) , pageLoadTimeout , message : errorMessage ) ;
218
+ var errorMessage = LocalizationManager . GetLocalizedMessage ( "loc.browser.page.timeout" ) ;
219
+ conditionalWait . WaitForTrue ( ( ) => ExecuteScript < bool > ( JavaScript . IsPageLoaded ) , pageLoadTimeout , message : errorMessage ) ;
214
220
}
215
221
216
222
/// <summary>
@@ -242,6 +248,18 @@ public void ExecuteScriptFromFile(string embeddedResourcePath, params object[] a
242
248
ExecuteScript ( embeddedResourcePath . GetScript ( Assembly . GetCallingAssembly ( ) ) , arguments ) ;
243
249
}
244
250
251
+ /// <summary>
252
+ /// Executes JS script from embedded resource file (*.js) and gets result value.
253
+ /// </summary>
254
+ /// <param name="embeddedResourcePath">Embedded resource path.</param>
255
+ /// <param name="arguments">Script arguments.</param>
256
+ /// <typeparam name="T">Type of return value.</typeparam>
257
+ /// <returns>Script execution result.</returns>
258
+ public T ExecuteScriptFromFile < T > ( string embeddedResourcePath , params object [ ] arguments )
259
+ {
260
+ return ExecuteScript < T > ( embeddedResourcePath . GetScript ( Assembly . GetCallingAssembly ( ) ) , arguments ) ;
261
+ }
262
+
245
263
/// <summary>
246
264
/// Executes predefined JS script.
247
265
/// </summary>
@@ -262,18 +280,6 @@ public void ExecuteScript(string script, params object[] arguments)
262
280
Driver . ExecuteJavaScript ( script , arguments ) ;
263
281
}
264
282
265
- /// <summary>
266
- /// Executes JS script from embedded resource file (*.js) and gets result value.
267
- /// </summary>
268
- /// <param name="embeddedResourcePath">Embedded resource path.</param>
269
- /// <param name="arguments">Script arguments.</param>
270
- /// <typeparam name="T">Type of return value.</typeparam>
271
- /// <returns>Script execution result.</returns>
272
- public T ExecuteScriptFromFile < T > ( string embeddedResourcePath , params object [ ] arguments )
273
- {
274
- return ExecuteScript < T > ( embeddedResourcePath . GetScript ( Assembly . GetCallingAssembly ( ) ) , arguments ) ;
275
- }
276
-
277
283
/// <summary>
278
284
/// Executes predefined JS script and gets result value.
279
285
/// </summary>
0 commit comments