diff --git a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj index 146099d..2f1ede8 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj +++ b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj @@ -16,6 +16,7 @@ LICENSE Copyright 2024 Aquality Automation true + 8 diff --git a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml index a60beb6..2bc6bd2 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml +++ b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml @@ -66,11 +66,12 @@ Provides functionality to work with browser via Selenium WebDriver. - + Instantiate browser. Instance of Selenium WebDriver for desired web browser. + Context of the webdriver (for example: DriverService is a part of context) diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs index 02fa0af..3f5d46c 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs @@ -29,11 +29,13 @@ public class Browser : IApplication /// Instantiate browser. /// /// Instance of Selenium WebDriver for desired web browser. - public Browser(WebDriver webDriver) + /// Context of the webdriver (for example: DriverService is a part of context) + public Browser(DriverContext driverContext) { - Driver = webDriver; - Network = new NetworkHandling(webDriver); - JavaScriptEngine = new JavaScriptHandling(webDriver); + Driver = driverContext.Driver; + DriverContext = driverContext; + Network = new NetworkHandling(driverContext.Driver); + JavaScriptEngine = new JavaScriptHandling(driverContext.Driver); Logger = AqualityServices.LocalizedLogger; LocalizationManager = AqualityServices.Get(); browserProfile = AqualityServices.Get(); @@ -53,6 +55,7 @@ public Browser(WebDriver webDriver) /// /// Instance of Selenium WebDriver for desired web browser. public WebDriver Driver { get; } + public DriverContext DriverContext { get; } /// /// Provides Network Handling functionality diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs index c18823e..553229b 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs @@ -25,12 +25,14 @@ protected BrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserPr protected ILocalizedLogger LocalizedLogger { get; } protected abstract WebDriver Driver { get; } + protected abstract DriverContext DriverContext { get; } public virtual Browser Browser { get { - var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) })); + var driverCtx = ActionRetrier.DoWithRetry(() => DriverContext, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }); + var browser = new Browser(driverCtx); LocalizedLogger.Info("loc.browser.ready", BrowserProfile.BrowserName); return browser; } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/DriverContext.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/DriverContext.cs new file mode 100644 index 0000000..728a909 --- /dev/null +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/DriverContext.cs @@ -0,0 +1,11 @@ +#nullable enable +using OpenQA.Selenium; + +namespace Aquality.Selenium.Browsers +{ + public class DriverContext + { + public WebDriver Driver { get; set; } + public DriverService? DriverService { get; set; } + } +} \ No newline at end of file diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs index 797bf37..2fec3e0 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs @@ -32,63 +32,76 @@ public LocalBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browser { } - protected override WebDriver Driver + protected override WebDriver Driver => DriverContext.Driver; + + protected override DriverContext DriverContext { get { var commandTimeout = TimeoutConfiguration.Command; var browserName = BrowserProfile.BrowserName; var driverSettings = BrowserProfile.DriverSettings; - WebDriver driver; + DriverContext driverCtx; switch (browserName) { case BrowserName.Chrome: case BrowserName.Yandex: - driver = GetDriver(() => ChromeDriverService.CreateDefaultService(), + driverCtx = GetDriver(() => ChromeDriverService.CreateDefaultService(), (ChromeOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Firefox: Func geckoServiceProvider = () => { var geckoService = FirefoxDriverService.CreateDefaultService(); - geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService.Host; + geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled + ? HostAddressDefault + : geckoService.Host; return geckoService; }; - driver = GetDriver(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout); + driverCtx = GetDriver(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.IExplorer: - driver = GetDriver(() => InternetExplorerDriverService.CreateDefaultService(), + driverCtx = GetDriver(() => InternetExplorerDriverService.CreateDefaultService(), (InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Edge: - driver = GetDriver(() => EdgeDriverService.CreateDefaultService(), + driverCtx = GetDriver(() => EdgeDriverService.CreateDefaultService(), (EdgeOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Opera: var config = new OperaConfig(); - var operaSettings = (OperaSettings) driverSettings; + var operaSettings = (OperaSettings)driverSettings; var driverPath = new DriverManager().SetUpDriver(config, operaSettings.WebDriverVersion, operaSettings.SystemArchitecture); - driver = GetDriver(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()), + driverCtx = GetDriver( + () => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()), (ChromeOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Safari: - driver = GetDriver(() => SafariDriverService.CreateDefaultService(), + driverCtx = GetDriver(() => SafariDriverService.CreateDefaultService(), (SafariOptions)driverSettings.DriverOptions, commandTimeout); break; default: throw new NotSupportedException($"Browser [{browserName}] is not supported."); } - return driver; + + return driverCtx; } } - private WebDriver GetDriver(Func driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver + private DriverContext GetDriver(Func driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver { var currentBrowserVersionRegex = new Regex(CurrentBrowserVersionPattern, RegexOptions.None, TimeoutConfiguration.Condition); try { - return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout); + var driverService = driverServiceProvider.Invoke(); + var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout); + var context = new DriverContext + { + Driver = driver, + DriverService = driverService + }; + return context; } catch (TargetInvocationException exception) when (exception.InnerException != null && currentBrowserVersionRegex.IsMatch(exception.InnerException.Message)) @@ -96,7 +109,14 @@ private WebDriver GetDriver(Func driverServiceProvider, Driver Logger.Instance.Debug(exception.InnerException.Message, exception); var currentVersion = currentBrowserVersionRegex.Match(exception.InnerException.Message).Groups[1].Value; Environment.SetEnvironmentVariable(DriverVersionVariableName, currentVersion); - return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout); + var driverService = driverServiceProvider.Invoke(); + var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout); + var context = new DriverContext + { + Driver = driver, + DriverService = driverService + }; + return context; } } } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs index 1ffd071..a127951 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs @@ -17,7 +17,9 @@ public RemoteBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browse { } - protected override WebDriver Driver + protected override WebDriver Driver => DriverContext.Driver; + + protected override DriverContext DriverContext { get { @@ -25,7 +27,13 @@ protected override WebDriver Driver var capabilities = BrowserProfile.DriverSettings.DriverOptions.ToCapabilities(); try { - return new RemoteWebDriver(BrowserProfile.RemoteConnectionUrl, capabilities, TimeoutConfiguration.Command); + var driver = new RemoteWebDriver(BrowserProfile.RemoteConnectionUrl, capabilities, TimeoutConfiguration.Command); + var context = new DriverContext + { + Driver = driver, + DriverService = null + }; + return context; } catch (Exception e) { diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs index d96341d..4072eca 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs @@ -52,16 +52,23 @@ public CustomLocalBrowserFactory() : { } - protected override WebDriver Driver - { + protected override WebDriver Driver => DriverContext.Driver; + + protected override DriverContext DriverContext { get { var driverSettings = BrowserProfile.DriverSettings; SetUpDriver(new()); - return new ChromeDriver((ChromeOptions)driverSettings.DriverOptions); + var driverService = ChromeDriverService.CreateDefaultService(); + var driver = new ChromeDriver(driverService, (ChromeOptions)driverSettings.DriverOptions); + return new DriverContext + { + Driver = driver, + DriverService = driverService + }; } } - + private static void SetUpDriver(ChromeConfig driverConfig) { var architecture = ArchitectureHelper.GetArchitecture();