-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebDriverFactory.cs
150 lines (104 loc) · 5.76 KB
/
WebDriverFactory.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
using Gsemac.IO.Logging;
using Gsemac.Net.Http;
using Gsemac.Net.WebBrowsers;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
namespace Gsemac.Net.WebDrivers {
public sealed class WebDriverFactory :
IWebDriverFactory {
// Public members
public event DownloadFileProgressChangedEventHandler DownloadFileProgressChanged;
public event DownloadFileCompletedEventHandler DownloadFileCompleted;
public static WebDriverFactory Default => new WebDriverFactory();
public WebDriverFactory() :
this(WebDriverOptions.Default) {
}
public WebDriverFactory(ILogger logger) :
this(WebDriverOptions.Default, logger) {
}
public WebDriverFactory(IWebDriverOptions webDriverOptions) :
this(webDriverOptions, WebDriverFactoryOptions.Default) {
}
public WebDriverFactory(IWebDriverOptions webDriverOptions, ILogger logger) :
this(webDriverOptions, WebDriverFactoryOptions.Default, logger) {
}
public WebDriverFactory(IWebDriverFactoryOptions webDriverFactoryOptions) :
this(WebDriverOptions.Default, webDriverFactoryOptions) {
}
public WebDriverFactory(IWebDriverFactoryOptions webDriverFactoryOptions, ILogger logger) :
this(WebDriverOptions.Default, webDriverFactoryOptions, logger) {
}
public WebDriverFactory(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
this(HttpWebRequestFactory.Default, webDriverOptions, webDriverFactoryOptions) {
}
public WebDriverFactory(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions, ILogger logger) :
this(HttpWebRequestFactory.Default, webDriverOptions, webDriverFactoryOptions, logger) {
}
public WebDriverFactory(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
this(webRequestFactory, webDriverOptions, webDriverFactoryOptions, Logger.Null) {
}
public WebDriverFactory(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(WebDriverFactory));
}
public IWebDriver Create() {
return Create(webDriverFactoryOptions.WebBrowser ?? WebBrowserInfoFactory.Default.GetDefaultWebBrowser());
}
public IWebDriver Create(IWebBrowserInfo webBrowserInfo) {
if (isDisposed)
throw new ObjectDisposedException(nameof(WebDriverFactory));
if (webBrowserInfo is null)
throw new ArgumentNullException(nameof(webBrowserInfo));
return GetOrCreateFactory(webBrowserInfo).Create(webBrowserInfo);
}
public void Dispose() {
if (!isDisposed) {
isDisposed = true;
foreach (IWebDriverFactory factory in factoryDict.Values)
factory.Dispose();
}
}
// Private members
private readonly IHttpWebRequestFactory webRequestFactory;
private readonly IWebDriverOptions webDriverOptions;
private readonly IWebDriverFactoryOptions webDriverFactoryOptions;
private readonly ILogger logger;
private readonly IDictionary<WebBrowserId, IWebDriverFactory> factoryDict = new Dictionary<WebBrowserId, IWebDriverFactory>();
private bool isDisposed = false;
private IWebDriverFactory GetOrCreateFactory(IWebBrowserInfo webBrowserInfo) {
IWebDriverFactory factory = null;
lock (factoryDict) {
if (!factoryDict.TryGetValue(webBrowserInfo.Id, out factory)) {
switch (webBrowserInfo.Id) {
case WebBrowserId.GoogleChrome:
factoryDict[webBrowserInfo.Id] = new ChromeWebDriverFactory(webRequestFactory, webDriverOptions, webDriverFactoryOptions, logger);
break;
case WebBrowserId.MicrosoftEdge:
factoryDict[webBrowserInfo.Id] = new EdgeWebDriverFactory(webRequestFactory, webDriverOptions, webDriverFactoryOptions, logger);
break;
case WebBrowserId.Firefox:
factoryDict[webBrowserInfo.Id] = new FirefoxWebDriverFactory(webRequestFactory, webDriverOptions, webDriverFactoryOptions, logger);
break;
default:
throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowserWithBrowserName, webBrowserInfo.Name), nameof(webBrowserInfo));
}
factory = factoryDict[webBrowserInfo.Id];
factory.DownloadFileCompleted += DownloadFileCompleted;
factory.DownloadFileProgressChanged += DownloadFileProgressChanged;
}
}
return factory;
}
}
}