Skip to content

Commit c789b68

Browse files
committed
Remove and simplify creation of default driver service
1 parent b3ea344 commit c789b68

File tree

1 file changed

+6
-42
lines changed

1 file changed

+6
-42
lines changed

dotnet/test/common/Environment/DriverFactory.cs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
7777
var chromeOptions = (ChromeOptions)options;
7878
chromeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
7979

80-
service = CreateService<ChromeDriverService>();
80+
service = ChromeDriverService.CreateDefaultService();
8181
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
8282
{
8383
((ChromeOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -95,7 +95,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
9595
var edgeOptions = (EdgeOptions)options;
9696
edgeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
9797

98-
service = CreateService<EdgeDriverService>();
98+
service = EdgeDriverService.CreateDefaultService();
9999
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
100100
{
101101
((EdgeOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -109,7 +109,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
109109
{
110110
browser = Browser.IE;
111111
options = GetDriverOptions<InternetExplorerOptions>(driverType, driverOptions);
112-
service = CreateService<InternetExplorerDriverService>();
112+
service = InternetExplorerDriverService.CreateDefaultService();
113113
if (enableLogging)
114114
{
115115
((InternetExplorerDriverService)service).LoggingLevel = InternetExplorerDriverLogLevel.Trace;
@@ -119,7 +119,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
119119
{
120120
browser = Browser.Firefox;
121121
options = GetDriverOptions<FirefoxOptions>(driverType, driverOptions);
122-
service = CreateService<FirefoxDriverService>();
122+
service = FirefoxDriverService.CreateDefaultService();
123123
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
124124
{
125125
((FirefoxOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -133,7 +133,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
133133
{
134134
browser = Browser.Safari;
135135
options = GetDriverOptions<SafariOptions>(driverType, driverOptions);
136-
service = CreateService<SafariDriverService>();
136+
service = SafariDriverService.CreateDefaultService();
137137
}
138138

139139
if (!String.IsNullOrEmpty(this.driverPath) && service != null)
@@ -155,7 +155,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
155155
}
156156
}
157157

158-
driver = (IWebDriver)Activator.CreateInstance(driverType, service, true);
158+
driver = (IWebDriver)Activator.CreateInstance(driverType, service);
159159
return driver;
160160
}
161161

@@ -192,41 +192,5 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options)
192192

193193
return options;
194194
}
195-
196-
private T MergeOptions<T>(object baseOptions, DriverOptions overriddenOptions) where T : DriverOptions, new()
197-
{
198-
// If the driver type has a static DefaultOptions property,
199-
// get the value of that property, which should be a valid
200-
// options of the generic type (T). Otherwise, create a new
201-
// instance of the browser-specific options class.
202-
T mergedOptions = new T();
203-
if (baseOptions != null && baseOptions is T)
204-
{
205-
mergedOptions = (T)baseOptions;
206-
}
207-
208-
if (overriddenOptions != null)
209-
{
210-
mergedOptions.PageLoadStrategy = overriddenOptions.PageLoadStrategy;
211-
mergedOptions.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior;
212-
mergedOptions.Proxy = overriddenOptions.Proxy;
213-
}
214-
215-
return mergedOptions;
216-
}
217-
218-
private T CreateService<T>() where T : DriverService
219-
{
220-
T service = default(T);
221-
Type serviceType = typeof(T);
222-
223-
MethodInfo createDefaultServiceMethod = serviceType.GetMethod("CreateDefaultService", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
224-
if (createDefaultServiceMethod != null && createDefaultServiceMethod.ReturnType == serviceType)
225-
{
226-
service = (T)createDefaultServiceMethod.Invoke(null, new object[] { });
227-
}
228-
229-
return service;
230-
}
231195
}
232196
}

0 commit comments

Comments
 (0)