@@ -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