Skip to content

Commit

Permalink
Feature/83 add action retrier for browser creation (#186)
Browse files Browse the repository at this point in the history
* #83 add ActionRetrier for browser creation

* update core version
  • Loading branch information
knysh authored May 19, 2020
1 parent 5d6814f commit 3532701
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aquality.Selenium.Core" Version="1.0.0" />
<PackageReference Include="Aquality.Selenium.Core" Version="1.0.1" />
<PackageReference Include="NLog" Version="4.7.0" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ChromeDriver>(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<FirefoxDriver>(geckoService, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.IExplorer:
SetUpDriver(new InternetExplorerConfig(), driverSettings);
driver = new InternetExplorerDriver(InternetExplorerDriverService.CreateDefaultService(),
(InternetExplorerOptions) driverSettings.DriverOptions, commandTimeout);
driver = GetDriver<InternetExplorerDriver>(InternetExplorerDriverService.CreateDefaultService(),
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Edge:
driver = new EdgeDriver(EdgeDriverService.CreateDefaultService(),
(EdgeOptions) driverSettings.DriverOptions, commandTimeout);
driver = GetDriver<EdgeDriver>(EdgeDriverService.CreateDefaultService(),
(EdgeOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Safari:
driver = new SafariDriver(SafariDriverService.CreateDefaultService(),
(SafariOptions) driverSettings.DriverOptions, commandTimeout);
driver = GetDriver<SafariDriver>(SafariDriverService.CreateDefaultService(),
(SafariOptions)driverSettings.DriverOptions, commandTimeout);
break;
default:
throw new ArgumentOutOfRangeException($"Browser {browserName} is not supported.");
Expand All @@ -68,6 +70,12 @@ private Browser CreateBrowser()
return new Browser(driver);
}

private RemoteWebDriver GetDriver<T>(DriverService driverService, DriverOptions driverOptions, TimeSpan commandTimeout) where T : RemoteWebDriver
{
return AqualityServices.Get<IActionRetrier>().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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Aquality.Selenium.Configurations;
using Aquality.Selenium.Core.Utilities;
using OpenQA.Selenium.Remote;
using System;

namespace Aquality.Selenium.Browsers
{
Expand All @@ -19,7 +21,9 @@ public override Browser Browser
var browserProfile = AqualityServices.Get<IBrowserProfile>();
var capabilities = browserProfile.DriverSettings.DriverOptions.ToCapabilities();
var timeoutConfiguration = AqualityServices.Get<ITimeoutConfiguration>();
var driver = new RemoteWebDriver(browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command);
var driver = AqualityServices.Get<IActionRetrier>().DoWithRetry(() =>
(RemoteWebDriver)Activator.CreateInstance(typeof(RemoteWebDriver),
browserProfile.RemoteConnectionUrl, capabilities, timeoutConfiguration.Command));
return new Browser(driver);
}
}
Expand Down

0 comments on commit 3532701

Please sign in to comment.