From 353270167805fba25286bfe07c19b5e5387e5ea3 Mon Sep 17 00:00:00 2001 From: knysh Date: Tue, 19 May 2020 14:55:05 +0300 Subject: [PATCH] Feature/83 add action retrier for browser creation (#186) * #83 add ActionRetrier for browser creation * update core version --- .../Aquality.Selenium.csproj | 2 +- .../Browsers/LocalBrowserFactory.cs | 26 ++++++++++++------- .../Browsers/RemoteBrowserFactory.cs | 6 ++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj index 35e707b4..662f36d1 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj +++ b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj @@ -65,7 +65,7 @@ - + diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs index 036684ec..fc6cb307 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs @@ -1,5 +1,7 @@ using Aquality.Selenium.Configurations; using Aquality.Selenium.Configurations.WebDriverSettings; +using Aquality.Selenium.Core.Utilities; +using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Edge; using OpenQA.Selenium.Firefox; @@ -39,27 +41,27 @@ private Browser CreateBrowser() { case BrowserName.Chrome: SetUpDriver(new ChromeConfig(), driverSettings); - driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), - (ChromeOptions) driverSettings.DriverOptions, commandTimeout); + driver = GetDriver(ChromeDriverService.CreateDefaultService(), + (ChromeOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Firefox: SetUpDriver(new FirefoxConfig(), driverSettings); FirefoxDriverService geckoService = FirefoxDriverService.CreateDefaultService(); geckoService.Host = "::1"; - driver = new FirefoxDriver(geckoService, (FirefoxOptions) driverSettings.DriverOptions, commandTimeout); + driver = GetDriver(geckoService, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.IExplorer: SetUpDriver(new InternetExplorerConfig(), driverSettings); - driver = new InternetExplorerDriver(InternetExplorerDriverService.CreateDefaultService(), - (InternetExplorerOptions) driverSettings.DriverOptions, commandTimeout); + driver = GetDriver(InternetExplorerDriverService.CreateDefaultService(), + (InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Edge: - driver = new EdgeDriver(EdgeDriverService.CreateDefaultService(), - (EdgeOptions) driverSettings.DriverOptions, commandTimeout); + driver = GetDriver(EdgeDriverService.CreateDefaultService(), + (EdgeOptions)driverSettings.DriverOptions, commandTimeout); break; case BrowserName.Safari: - driver = new SafariDriver(SafariDriverService.CreateDefaultService(), - (SafariOptions) driverSettings.DriverOptions, commandTimeout); + driver = GetDriver(SafariDriverService.CreateDefaultService(), + (SafariOptions)driverSettings.DriverOptions, commandTimeout); break; default: throw new ArgumentOutOfRangeException($"Browser {browserName} is not supported."); @@ -68,6 +70,12 @@ private Browser CreateBrowser() return new Browser(driver); } + private RemoteWebDriver GetDriver(DriverService driverService, DriverOptions driverOptions, TimeSpan commandTimeout) where T : RemoteWebDriver + { + return AqualityServices.Get().DoWithRetry(() => + (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout)); + } + private static void SetUpDriver(IDriverConfig driverConfig, IDriverSettings driverSettings) { var architecture = driverSettings.SystemArchitecture.Equals(Architecture.Auto) ? ArchitectureHelper.GetArchitecture() : driverSettings.SystemArchitecture; diff --git a/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs b/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs index d90961cb..4b4025de 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs @@ -1,5 +1,7 @@ using Aquality.Selenium.Configurations; +using Aquality.Selenium.Core.Utilities; using OpenQA.Selenium.Remote; +using System; namespace Aquality.Selenium.Browsers { @@ -19,7 +21,9 @@ public override Browser Browser var browserProfile = AqualityServices.Get(); var capabilities = browserProfile.DriverSettings.DriverOptions.ToCapabilities(); var timeoutConfiguration = AqualityServices.Get(); - var driver = new RemoteWebDriver(browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command); + var driver = AqualityServices.Get().DoWithRetry(() => + (RemoteWebDriver)Activator.CreateInstance(typeof(RemoteWebDriver), + browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command)); return new Browser(driver); } }