Skip to content

Commit c321a46

Browse files
committed
[dotnet] Simplify testing driver factory
1 parent 9b0ccf1 commit c321a46

11 files changed

+89
-119
lines changed

dotnet/test/common/CustomDriverConfigs/DefaultSafariDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public DefaultSafariDriver(SafariOptions options)
3030
{
3131
}
3232

33+
public DefaultSafariDriver(SafariDriverService service)
34+
: base(service)
35+
{
36+
}
37+
3338
public DefaultSafariDriver(SafariDriverService service, SafariOptions options)
3439
: base(service, options)
3540
{

dotnet/test/common/CustomDriverConfigs/DevChannelChromeDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public DevChannelChromeDriver(ChromeOptions options)
3232
{
3333
}
3434

35+
public DevChannelChromeDriver(ChromeDriverService service)
36+
: base(service)
37+
{
38+
}
39+
3540
public DevChannelChromeDriver(ChromeDriverService service, ChromeOptions options)
3641
: base(service, options)
3742
{

dotnet/test/common/CustomDriverConfigs/DevChannelEdgeDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public DevChannelEdgeDriver(EdgeOptions options)
3232
{
3333
}
3434

35+
public DevChannelEdgeDriver(EdgeDriverService service)
36+
: base(service)
37+
{
38+
}
39+
3540
public DevChannelEdgeDriver(EdgeDriverService service, EdgeOptions options)
3641
: base(service, options)
3742
{

dotnet/test/common/CustomDriverConfigs/EdgeInternetExplorerModeDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public EdgeInternetExplorerModeDriver(InternetExplorerOptions options)
3535
{
3636
}
3737

38+
public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service)
39+
: base(service)
40+
{
41+
}
42+
3843
public EdgeInternetExplorerModeDriver(InternetExplorerDriverService service, InternetExplorerOptions options)
3944
: base(service, options)
4045
{

dotnet/test/common/CustomDriverConfigs/NightlyChannelFirefoxDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public NightlyChannelFirefoxDriver(FirefoxOptions options)
3535
{
3636
}
3737

38+
public NightlyChannelFirefoxDriver(FirefoxDriverService service)
39+
: base(service)
40+
{
41+
}
42+
3843
public NightlyChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options)
3944
: base(service, options)
4045
{

dotnet/test/common/CustomDriverConfigs/SafariTechnologyPreviewDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public SafariTechnologyPreviewDriver(SafariOptions options)
3535
{
3636
}
3737

38+
public SafariTechnologyPreviewDriver(SafariDriverService service)
39+
: base(service)
40+
{
41+
}
42+
3843
public SafariTechnologyPreviewDriver(SafariDriverService service, SafariOptions options)
3944
: base(service, options)
4045
{

dotnet/test/common/CustomDriverConfigs/StableChannelChromeDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public StableChannelChromeDriver(ChromeOptions options)
3232
{
3333
}
3434

35+
public StableChannelChromeDriver(ChromeDriverService service)
36+
: base(service)
37+
{
38+
}
39+
3540
public StableChannelChromeDriver(ChromeDriverService service, ChromeOptions options)
3641
: base(service, options)
3742
{

dotnet/test/common/CustomDriverConfigs/StableChannelEdgeDriver.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ public StableChannelEdgeDriver(EdgeOptions options)
3333
{
3434
}
3535

36+
public StableChannelEdgeDriver(EdgeDriverService service)
37+
: base(service)
38+
{
39+
}
40+
3641
public StableChannelEdgeDriver(EdgeDriverService service, EdgeOptions options)
3742
: base(service, options)
3843
{
3944
}
45+
4046
public static EdgeOptions DefaultOptions
4147
{
4248
get { return new EdgeOptions(); }

dotnet/test/common/CustomDriverConfigs/StableChannelFirefoxDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public StableChannelFirefoxDriver(FirefoxOptions options)
3535
{
3636
}
3737

38+
public StableChannelFirefoxDriver(FirefoxDriverService service)
39+
: base(service)
40+
{
41+
}
42+
3843
public StableChannelFirefoxDriver(FirefoxDriverService service, FirefoxOptions options)
3944
: base(service, options)
4045
{

dotnet/test/common/Environment/DriverFactory.cs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ namespace OpenQA.Selenium.Environment
3232
{
3333
public class DriverFactory
3434
{
35-
string driverPath;
36-
string browserBinaryLocation;
37-
private Dictionary<Browser, Type> serviceTypes = new Dictionary<Browser, Type>();
38-
private Dictionary<Browser, Type> optionsTypes = new Dictionary<Browser, Type>();
35+
private readonly string driverPath;
36+
private readonly string browserBinaryLocation;
37+
private readonly Dictionary<Browser, Type> serviceTypes = new Dictionary<Browser, Type>();
38+
private readonly Dictionary<Browser, Type> optionsTypes = new Dictionary<Browser, Type>();
3939

4040
public DriverFactory(string driverPath, string browserBinaryLocation)
4141
{
@@ -71,17 +71,15 @@ public IWebDriver CreateDriver(Type driverType, bool logging = false)
7171
return CreateDriverWithOptions(driverType, null, logging);
7272
}
7373

74-
public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions, bool logging = false)
74+
public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions, bool enableLogging = false)
7575
{
7676
Console.WriteLine($"Creating new driver of {driverType} type...");
7777

7878
Browser browser = Browser.All;
7979
DriverService service = null;
8080
DriverOptions options = null;
81-
bool enableLogging = logging;
8281

83-
List<Type> constructorArgTypeList = new List<Type>();
84-
IWebDriver driver = null;
82+
IWebDriver driver;
8583
if (typeof(ChromeDriver).IsAssignableFrom(driverType))
8684
{
8785
browser = Browser.Chrome;
@@ -149,7 +147,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
149147
service = CreateService<SafariDriverService>();
150148
}
151149

152-
if (!String.IsNullOrEmpty(this.driverPath) && service != null)
150+
if (!string.IsNullOrEmpty(this.driverPath) && service != null)
153151
{
154152
service.DriverServicePath = Path.GetDirectoryName(this.driverPath);
155153
service.DriverServiceExecutableName = Path.GetFileName(this.driverPath);
@@ -159,12 +157,10 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
159157

160158
if (browser != Browser.All)
161159
{
162-
constructorArgTypeList.Add(this.serviceTypes[browser]);
163-
constructorArgTypeList.Add(this.optionsTypes[browser]);
164-
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
160+
ConstructorInfo ctorInfo = driverType.GetConstructor([this.serviceTypes[browser], this.optionsTypes[browser]]);
165161
if (ctorInfo != null)
166162
{
167-
return (IWebDriver)ctorInfo.Invoke(new object[] { service, options });
163+
return (IWebDriver)ctorInfo.Invoke([service, options]);
168164
}
169165
}
170166

@@ -176,20 +172,23 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options)
176172
{
177173
if (this.DriverStarting != null)
178174
{
179-
DriverStartingEventArgs args = new DriverStartingEventArgs(service, options);
180-
this.DriverStarting(this, args);
175+
this.DriverStarting(this, new DriverStartingEventArgs(service, options));
181176
}
182177
}
183178

184-
private T GetDriverOptions<T>(Type driverType, DriverOptions overriddenOptions) where T : DriverOptions, new()
179+
private TOptions GetDriverOptions<TOptions>(Type driverType, DriverOptions overriddenOptions)
180+
where TOptions : DriverOptions, new()
185181
{
186-
T options = new T();
187-
Type optionsType = typeof(T);
182+
TOptions options;
188183

189184
PropertyInfo defaultOptionsProperty = driverType.GetProperty("DefaultOptions", BindingFlags.Public | BindingFlags.Static);
190-
if (defaultOptionsProperty != null && defaultOptionsProperty.PropertyType == optionsType)
185+
if (defaultOptionsProperty != null && defaultOptionsProperty.PropertyType == typeof(TOptions))
191186
{
192-
options = (T)defaultOptionsProperty.GetValue(null, null);
187+
options = (TOptions)defaultOptionsProperty.GetValue(null, null);
188+
}
189+
else
190+
{
191+
options = new TOptions();
193192
}
194193

195194
if (overriddenOptions != null)
@@ -208,41 +207,16 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options)
208207
return options;
209208
}
210209

211-
212-
private T MergeOptions<T>(object baseOptions, DriverOptions overriddenOptions) where T : DriverOptions, new()
213-
{
214-
// If the driver type has a static DefaultOptions property,
215-
// get the value of that property, which should be a valid
216-
// options of the generic type (T). Otherwise, create a new
217-
// instance of the browser-specific options class.
218-
T mergedOptions = new T();
219-
if (baseOptions != null && baseOptions is T)
220-
{
221-
mergedOptions = (T)baseOptions;
222-
}
223-
224-
if (overriddenOptions != null)
225-
{
226-
mergedOptions.PageLoadStrategy = overriddenOptions.PageLoadStrategy;
227-
mergedOptions.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior;
228-
mergedOptions.Proxy = overriddenOptions.Proxy;
229-
}
230-
231-
return mergedOptions;
232-
}
233-
234-
private T CreateService<T>() where T : DriverService
210+
private TService CreateService<TService>()
211+
where TService : DriverService
235212
{
236-
T service = default(T);
237-
Type serviceType = typeof(T);
238-
239-
MethodInfo createDefaultServiceMethod = serviceType.GetMethod("CreateDefaultService", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
240-
if (createDefaultServiceMethod != null && createDefaultServiceMethod.ReturnType == serviceType)
213+
MethodInfo createDefaultServiceMethod = typeof(TService).GetMethod("CreateDefaultService", BindingFlags.Public | BindingFlags.Static, null, [], null);
214+
if (createDefaultServiceMethod != null && createDefaultServiceMethod.ReturnType == typeof(TService))
241215
{
242-
service = (T)createDefaultServiceMethod.Invoke(null, new object[] { });
216+
return (TService)createDefaultServiceMethod.Invoke(null, []);
243217
}
244218

245-
return service;
219+
return default;
246220
}
247221
}
248222
}

0 commit comments

Comments
 (0)