From 80f77444eadba7484720ac9bfa134c8e88b2a7e9 Mon Sep 17 00:00:00 2001 From: "dmitry.bogatko" Date: Tue, 17 Sep 2024 23:37:12 +0300 Subject: [PATCH] [#262] moved driver creation to a separate method --- .../Browsers/LocalBrowserFactory.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) 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; + } } }