Skip to content

Commit

Permalink
[#262] made Browser constructor with DriverService parameter with nul…
Browse files Browse the repository at this point in the history
…l by default
  • Loading branch information
dmitry.bogatko committed Sep 18, 2024
1 parent 1b645ee commit d09a50d
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Copyright>Copyright 2024 Aquality Automation</Copyright>
<IsPackable>true</IsPackable>
<LangVersion>8</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
9 changes: 5 additions & 4 deletions Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

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

30 changes: 22 additions & 8 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ public class Browser : IApplication

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

/// <summary>
/// Instantiate browser.
/// </summary>
/// <param name="driverContext">Context of the webdriver (for example: DriverService is a part of context)</param>
public Browser(DriverContext driverContext)
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
/// <param name="driverService">Exposes the service provided by a native WebDriver server executable.</param>
public Browser(WebDriver webDriver, DriverService driverService = null)
{
Driver = driverContext.Driver;
DriverContext = driverContext;
Network = new NetworkHandling(driverContext.Driver);
JavaScriptEngine = new JavaScriptHandling(driverContext.Driver);
Driver = webDriver;
this.driverService = driverService;
Network = new NetworkHandling(webDriver);
JavaScriptEngine = new JavaScriptHandling(webDriver);
Logger = AqualityServices.LocalizedLogger;
LocalizationManager = AqualityServices.Get<ILocalizationManager>();
browserProfile = AqualityServices.Get<IBrowserProfile>();
Expand All @@ -58,9 +60,21 @@ public Browser(DriverContext driverContext)
public WebDriver Driver { get; }

/// <summary>
/// Container of WebDriver and other objects which are involved into webdriver instantiation process
/// Exposes the service provided by a native WebDriver server executable.
/// </summary>
public DriverContext DriverContext { get; }
public DriverService DriverService
{
get
{
if (driverService != null)
{
return driverService;
}

throw new InvalidOperationException("DriverService hasn't been provided during Browser instantiation." +
"\nPlease, 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,14 +25,20 @@ protected BrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserPr
protected ILocalizedLogger LocalizedLogger { get; }

protected abstract WebDriver Driver { get; }
protected abstract DriverContext DriverContext { get; }
protected virtual DriverContext DriverContext { get; }

public virtual Browser Browser
{
get
{
var driverCtx = ActionRetrier.DoWithRetry(() => DriverContext, new[] { typeof(WebDriverException), typeof(InvalidOperationException) });
var browser = new Browser(driverCtx);
var driverCtx = ActionRetrier.DoWithRetry(() => DriverContext,
new[] { typeof(WebDriverException), typeof(InvalidOperationException) });

Browser browser;
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#nullable enable
using OpenQA.Selenium;
using OpenQA.Selenium;

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

public WebDriver Driver { get; }
public DriverService? DriverService { get; set; }
public DriverService DriverService { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ private DriverContext CreateWebDriverInstance<T>(Func<DriverService> driverServi
{
var driverService = driverServiceProvider.Invoke();
var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout);
var context = new DriverContext(driver)
{
DriverService = driverService
};
var context = new DriverContext(driver, driverService);
return context;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override DriverContext DriverContext
try
{
var driver = new RemoteWebDriver(BrowserProfile.RemoteConnectionUrl, capabilities, TimeoutConfiguration.Command);
var context = new DriverContext(driver);
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 @@ -262,7 +262,7 @@ public void Should_BePossibleTo_GetDownloadDir()
[Test]
public void Should_BePossibleTo_GetAccessToDriverContext()
{
var driverProcessId = AqualityServices.Browser.DriverContext.DriverService?.ProcessId;
var driverProcessId = AqualityServices.Browser.DriverService?.ProcessId;
Assert.That(driverProcessId, Is.Not.Null.And.Not.EqualTo(0), "DriverProcessId should not be null or zero.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,16 @@ public CustomLocalBrowserFactory() :
{
}

protected override WebDriver Driver => DriverContext.Driver;

protected override DriverContext DriverContext {
protected override WebDriver Driver
{
get
{
var driverSettings = BrowserProfile.DriverSettings;
SetUpDriver(new());
var driverService = ChromeDriverService.CreateDefaultService();
var driver = new ChromeDriver(driverService, (ChromeOptions)driverSettings.DriverOptions);
return new DriverContext(driver)
{
DriverService = driverService
};
return new ChromeDriver((ChromeOptions)driverSettings.DriverOptions);
}
}

private static void SetUpDriver(ChromeConfig driverConfig)
{
var architecture = ArchitectureHelper.GetArchitecture();
Expand Down

0 comments on commit d09a50d

Please sign in to comment.