-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebDriverFactoryBase.cs
215 lines (140 loc) · 7.73 KB
/
WebDriverFactoryBase.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using Gsemac.IO.Logging;
using Gsemac.Net.Http;
using Gsemac.Net.WebBrowsers;
using OpenQA.Selenium;
using System;
using System.IO;
using System.Threading;
namespace Gsemac.Net.WebDrivers {
public abstract class WebDriverFactoryBase :
IWebDriverFactory {
// Public members
public event DownloadFileProgressChangedEventHandler DownloadFileProgressChanged;
public event DownloadFileCompletedEventHandler DownloadFileCompleted;
public IWebDriver Create() {
WebBrowserId webBrowserId = webDriverFactoryOptions.WebBrowserId;
IWebBrowserInfo webBrowserInfo = webDriverFactoryOptions.WebBrowser ??
(webBrowserId != WebBrowserId.Unknown ? WebBrowserInfoFactory.Default.GetWebBrowserInfo(webBrowserId) : WebBrowserInfoFactory.Default.GetDefaultWebBrowser());
return Create(webBrowserInfo);
}
public IWebDriver Create(IWebBrowserInfo webBrowserInfo) {
if (webBrowserInfo is null)
throw new ArgumentNullException(nameof(webBrowserInfo));
WebBrowserId webBrowserId = webDriverFactoryOptions.WebBrowserId;
if (webBrowserId != WebBrowserId.Unknown && webBrowserInfo.Id != webBrowserId)
throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowserWithBrowserName, webBrowserInfo.Name), nameof(webBrowserInfo));
// Get the web driver executable path.
logger.Info($"Creating web driver for {webBrowserInfo}");
string webDriverExecutablePath = Path.GetFullPath(GetDriverExecutablePathInternal(webBrowserInfo));
// Create the driver.
return GetWebDriver(webBrowserInfo, new WebDriverOptions(webDriverOptions) {
WebDriverExecutablePath = webDriverExecutablePath,
});
}
public void Dispose() {
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
// Protected members
protected WebDriverFactoryBase() :
this(WebDriverOptions.Default) {
}
protected WebDriverFactoryBase(ILogger logger) :
this(WebDriverOptions.Default, logger) {
}
protected WebDriverFactoryBase(IWebDriverOptions webDriverOptions) :
this(webDriverOptions, WebDriverFactoryOptions.Default) {
}
protected WebDriverFactoryBase(IWebDriverOptions webDriverOptions, ILogger logger) :
this(webDriverOptions, WebDriverFactoryOptions.Default, logger) {
}
protected WebDriverFactoryBase(IWebDriverFactoryOptions webDriverFactoryOptions) :
this(WebDriverOptions.Default, webDriverFactoryOptions) {
}
protected WebDriverFactoryBase(IWebDriverFactoryOptions webDriverFactoryOptions, ILogger logger) :
this(WebDriverOptions.Default, webDriverFactoryOptions, logger) {
}
protected WebDriverFactoryBase(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
this(HttpWebRequestFactory.Default, webDriverOptions, webDriverFactoryOptions) {
}
protected WebDriverFactoryBase(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions, ILogger logger) :
this(HttpWebRequestFactory.Default, webDriverOptions, webDriverFactoryOptions, logger) {
}
protected WebDriverFactoryBase(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
this(webRequestFactory, webDriverOptions, webDriverFactoryOptions, Logger.Null) {
}
protected WebDriverFactoryBase(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions, ILogger logger) {
if (webDriverOptions is null)
throw new ArgumentNullException(nameof(webDriverOptions));
if (webDriverFactoryOptions is null)
throw new ArgumentNullException(nameof(webDriverFactoryOptions));
if (webRequestFactory is null)
throw new ArgumentNullException(nameof(webRequestFactory));
if (logger is null)
throw new ArgumentNullException(nameof(logger));
this.webRequestFactory = webRequestFactory;
this.webDriverOptions = webDriverOptions;
this.webDriverFactoryOptions = webDriverFactoryOptions;
this.logger = new NamedLogger(logger, nameof(WebDriverFactoryBase));
}
protected abstract IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions);
protected abstract string GetWebDriverExecutablePath();
protected virtual IWebDriverUpdater GetUpdater() {
return null;
}
protected virtual void Dispose(bool disposing) {
if (disposing && !isDisposed) {
updaterCancellationTokenSource.Cancel();
updaterCancellationTokenSource.Dispose();
if (webDriverFactoryOptions.KillWebDriverProcessesOnDispose) {
logger.Info($"Killing web driver processes");
WebDriverUtilities.KillWebDriverProcesses(GetDriverExecutablePathInternal(null));
}
isDisposed = true;
}
}
protected void OnDownloadFileProgressChanged(object sender, DownloadFileProgressChangedEventArgs e) {
DownloadFileProgressChanged?.Invoke(sender, e);
}
protected void OnDownloadFileCompleted(object sender, DownloadFileCompletedEventArgs e) {
DownloadFileCompleted?.Invoke(sender, e);
}
// Private members
private readonly IHttpWebRequestFactory webRequestFactory;
private readonly IWebDriverOptions webDriverOptions;
private readonly IWebDriverFactoryOptions webDriverFactoryOptions;
private readonly ILogger logger;
private readonly CancellationTokenSource updaterCancellationTokenSource = new CancellationTokenSource();
private bool isDisposed = false;
private string GetDriverExecutablePathInternal(IWebBrowserInfo webBrowserInfo) {
if (!string.IsNullOrWhiteSpace(webDriverOptions.WebDriverExecutablePath))
return webDriverOptions.WebDriverExecutablePath;
if (webBrowserInfo is object && webDriverFactoryOptions.AutoUpdateEnabled) {
IWebDriverUpdater updater = GetUpdaterInternal();
if (updater is object) {
try {
IWebDriverInfo webDriverInfo = updater.Update(webBrowserInfo, updaterCancellationTokenSource.Token);
if (!string.IsNullOrWhiteSpace(webDriverInfo?.ExecutablePath))
return webDriverInfo.ExecutablePath;
}
catch (Exception ex) {
logger.Error(ex.ToString());
if (!webDriverFactoryOptions.IgnoreUpdateErrors)
throw ex;
}
}
}
if (string.IsNullOrWhiteSpace(webDriverFactoryOptions.WebDriverDirectoryPath))
return GetWebDriverExecutablePath();
return Path.Combine(webDriverFactoryOptions.WebDriverDirectoryPath, GetWebDriverExecutablePath());
}
private IWebDriverUpdater GetUpdaterInternal() {
IWebDriverUpdater updater = GetUpdater();
if (updater is object) {
updater.DownloadFileProgressChanged += OnDownloadFileProgressChanged;
updater.DownloadFileCompleted += OnDownloadFileCompleted;
}
return updater;
}
}
}