Skip to content
Merged

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

22 changes: 21 additions & 1 deletion Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ public class Browser : IApplication

private readonly IBrowserProfile browserProfile;
private readonly IConditionalWait conditionalWait;
private readonly DriverService driverService;

/// <summary>
/// Instantiate browser.
/// </summary>
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
public Browser(WebDriver webDriver)
/// <param name="driverService">Exposes the service provided by a native WebDriver server executable.</param>
public Browser(WebDriver webDriver, DriverService driverService = null)
{
Driver = webDriver;
this.driverService = driverService;
Network = new NetworkHandling(webDriver);
JavaScriptEngine = new JavaScriptHandling(webDriver);
Logger = AqualityServices.LocalizedLogger;
Expand All @@ -55,6 +58,23 @@ public Browser(WebDriver webDriver)
/// </summary>
/// <value>Instance of Selenium WebDriver for desired web browser.</value>
public WebDriver Driver { get; }

/// <summary>
/// Exposes the service provided by a native WebDriver server executable.
/// </summary>
public DriverService DriverService
{
get
{
if (driverService != null)
{
return driverService;
}

throw new InvalidOperationException("DriverService hasn't been provided during Browser instantiation." +
Environment.NewLine + "Please, check your BrowserFactory if it passes DriverService during the Browser instantiation.");
}
}

/// <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,19 @@ protected BrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserPr
protected ILocalizedLogger LocalizedLogger { get; }

protected abstract WebDriver Driver { get; }
protected virtual 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 = driverCtx != null
? new Browser(driverCtx.Driver, driverCtx.DriverService)
: new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }));

LocalizedLogger.Info("loc.browser.ready", BrowserProfile.BrowserName);
return browser;
}
Expand Down
16 changes: 16 additions & 0 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/DriverContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using OpenQA.Selenium;

namespace Aquality.Selenium.Browsers
{
public class DriverContext
{
public DriverContext(WebDriver driver, DriverService driverService)
{
Driver = driver;
DriverService = driverService;
}

public WebDriver Driver { get; }
public DriverService DriverService { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,72 +32,87 @@ 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 = GetDriverContext<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 = GetDriverContext<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.IExplorer:
driver = GetDriver<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
driverCtx = GetDriverContext<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Edge:
driver = GetDriver<EdgeDriver>(() => EdgeDriverService.CreateDefaultService(),
driverCtx = GetDriverContext<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 = GetDriverContext<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Safari:
driver = GetDriver<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
driverCtx = GetDriverContext<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 GetDriverContext<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 context = CreateWebDriverInstance<T>(driverServiceProvider, driverOptions, commandTimeout);
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 context = CreateWebDriverInstance<T>(driverServiceProvider, driverOptions, commandTimeout);
return context;
}
}

private DriverContext CreateWebDriverInstance<T>(Func<DriverService> 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);
return context;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ 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, null);
return context;
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,12 @@ public void Should_BePossibleTo_GetDownloadDir()
var downloadDir = AqualityServices.Browser.DownloadDirectory;
Assert.That(downloadDir.ToLower().Contains("downloads", StringComparison.InvariantCultureIgnoreCase));
}

[Test]
public void Should_BePossibleTo_GetAccessToDriverContext()
{
var driverProcessId = AqualityServices.Browser.DriverService?.ProcessId;
Assert.That(driverProcessId, Is.Not.Null.And.Not.EqualTo(0), "DriverProcessId should not be null or zero.");
}
}
}