Skip to content

Commit

Permalink
[#262] initial implementation of DriverContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry.bogatko committed Sep 17, 2024
1 parent 9d0641f commit 2717222
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Copyright>Copyright 2024 Aquality Automation</Copyright>
<IsPackable>true</IsPackable>
<LangVersion>8</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class Browser : IApplication
/// Instantiate browser.
/// </summary>
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
public Browser(WebDriver webDriver)
/// <param name="driverContext">Context of the webdriver (for example: DriverService is a part of context)</param>
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<ILocalizationManager>();
browserProfile = AqualityServices.Get<IBrowserProfile>();
Expand All @@ -53,6 +55,7 @@ public Browser(WebDriver webDriver)
/// </summary>
/// <value>Instance of Selenium WebDriver for desired web browser.</value>
public WebDriver Driver { get; }
public DriverContext DriverContext { get; }

/// <summary>
/// Provides Network Handling functionality <see cref="NetworkHandling"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/DriverContext.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,71 +32,91 @@ 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<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(),
driverCtx = GetDriver<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(),
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Firefox:
Func<DriverService> 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<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
driverCtx = GetDriver<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.IExplorer:
driver = GetDriver<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
driverCtx = GetDriver<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Edge:
driver = GetDriver<EdgeDriver>(() => EdgeDriverService.CreateDefaultService(),
driverCtx = GetDriver<EdgeDriver>(() => 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<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
driverCtx = GetDriver<ChromeDriver>(
() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Safari:
driver = GetDriver<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
driverCtx = GetDriver<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
(SafariOptions)driverSettings.DriverOptions, commandTimeout);
break;
default:
throw new NotSupportedException($"Browser [{browserName}] is not supported.");
}
return driver;

return driverCtx;
}
}

private WebDriver GetDriver<T>(Func<DriverService> driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
private DriverContext GetDriver<T>(Func<DriverService> 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))
{
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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ public RemoteBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browse
{
}

protected override WebDriver Driver
protected override WebDriver Driver => DriverContext.Driver;

protected override DriverContext DriverContext
{
get
{
LocalizedLogger.Info("loc.browser.grid");
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2717222

Please sign in to comment.