diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs index 2fb47a4..76d57e3 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs @@ -93,12 +93,7 @@ private DriverContext GetDriver(Func driverServiceProvider, Dr var currentBrowserVersionRegex = new Regex(CurrentBrowserVersionPattern, RegexOptions.None, TimeoutConfiguration.Condition); try { - var driverService = driverServiceProvider.Invoke(); - var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout); - var context = new DriverContext(driver) - { - DriverService = driverService - }; + var context = CreateWebDriverInstance(driverServiceProvider, driverOptions, commandTimeout); return context; } catch (TargetInvocationException exception) @@ -107,14 +102,20 @@ private DriverContext GetDriver(Func driverServiceProvider, Dr Logger.Instance.Debug(exception.InnerException.Message, exception); var currentVersion = currentBrowserVersionRegex.Match(exception.InnerException.Message).Groups[1].Value; Environment.SetEnvironmentVariable(DriverVersionVariableName, currentVersion); - var driverService = driverServiceProvider.Invoke(); - var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout); - var context = new DriverContext(driver) - { - DriverService = driverService - }; + var context = CreateWebDriverInstance(driverServiceProvider, driverOptions, commandTimeout); return context; } } + + private DriverContext CreateWebDriverInstance(Func driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver + { + var driverService = driverServiceProvider.Invoke(); + var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout); + var context = new DriverContext(driver) + { + DriverService = driverService + }; + return context; + } } }