Skip to content

Commit 824634f

Browse files
DmitryBogatkodmitry.bogatko
andauthored
[Feature] [#262] add drivercontext +semver: feature (#263)
* [#262] made Browser constructor with DriverService parameter with null by default --------- Co-authored-by: dmitry.bogatko <dmitry.bogatko@capsoft.de>
1 parent f9669bb commit 824634f

File tree

7 files changed

+94
-19
lines changed

7 files changed

+94
-19
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ public class Browser : IApplication
2626

2727
private readonly IBrowserProfile browserProfile;
2828
private readonly IConditionalWait conditionalWait;
29+
private readonly DriverService driverService;
2930

3031
/// <summary>
3132
/// Instantiate browser.
3233
/// </summary>
3334
/// <param name="webDriver">Instance of Selenium WebDriver for desired web browser.</param>
34-
public Browser(WebDriver webDriver)
35+
/// <param name="driverService">Exposes the service provided by a native WebDriver server executable.</param>
36+
public Browser(WebDriver webDriver, DriverService driverService = null)
3537
{
3638
Driver = webDriver;
39+
this.driverService = driverService;
3740
Network = new NetworkHandling(webDriver);
3841
JavaScriptEngine = new JavaScriptHandling(webDriver);
3942
Logger = AqualityServices.LocalizedLogger;
@@ -55,6 +58,23 @@ public Browser(WebDriver webDriver)
5558
/// </summary>
5659
/// <value>Instance of Selenium WebDriver for desired web browser.</value>
5760
public WebDriver Driver { get; }
61+
62+
/// <summary>
63+
/// Exposes the service provided by a native WebDriver server executable.
64+
/// </summary>
65+
public DriverService DriverService
66+
{
67+
get
68+
{
69+
if (driverService != null)
70+
{
71+
return driverService;
72+
}
73+
74+
throw new InvalidOperationException("DriverService hasn't been provided during Browser instantiation." +
75+
Environment.NewLine + "Please, check your BrowserFactory if it passes DriverService during the Browser instantiation.");
76+
}
77+
}
5878

5979
/// <summary>
6080
/// Provides Network Handling functionality <see cref="NetworkHandling"/>

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ protected BrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserPr
2525
protected ILocalizedLogger LocalizedLogger { get; }
2626

2727
protected abstract WebDriver Driver { get; }
28+
protected virtual DriverContext DriverContext { get; }
2829

2930
public virtual Browser Browser
3031
{
3132
get
3233
{
33-
var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }));
34+
var driverCtx = ActionRetrier.DoWithRetry(() => DriverContext,
35+
new[] { typeof(WebDriverException), typeof(InvalidOperationException) });
36+
37+
var browser = driverCtx != null
38+
? new Browser(driverCtx.Driver, driverCtx.DriverService)
39+
: new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }));
40+
3441
LocalizedLogger.Info("loc.browser.ready", BrowserProfile.BrowserName);
3542
return browser;
3643
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using OpenQA.Selenium;
2+
3+
namespace Aquality.Selenium.Browsers
4+
{
5+
public class DriverContext
6+
{
7+
public DriverContext(WebDriver driver, DriverService driverService)
8+
{
9+
Driver = driver;
10+
DriverService = driverService;
11+
}
12+
13+
public WebDriver Driver { get; }
14+
public DriverService DriverService { get; }
15+
}
16+
}

Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,87 @@ public LocalBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browser
3232
{
3333
}
3434

35-
protected override WebDriver Driver
35+
protected override WebDriver Driver => DriverContext.Driver;
36+
37+
protected override DriverContext DriverContext
3638
{
3739
get
3840
{
3941
var commandTimeout = TimeoutConfiguration.Command;
4042
var browserName = BrowserProfile.BrowserName;
4143
var driverSettings = BrowserProfile.DriverSettings;
42-
WebDriver driver;
44+
DriverContext driverCtx;
4345
switch (browserName)
4446
{
4547
case BrowserName.Chrome:
4648
case BrowserName.Yandex:
47-
driver = GetDriver<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(),
49+
driverCtx = GetDriverContext<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(),
4850
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
4951
break;
5052
case BrowserName.Firefox:
5153
Func<DriverService> geckoServiceProvider = () =>
5254
{
5355
var geckoService = FirefoxDriverService.CreateDefaultService();
54-
geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService.Host;
56+
geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled
57+
? HostAddressDefault
58+
: geckoService.Host;
5559
return geckoService;
5660
};
5761

58-
driver = GetDriver<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
62+
driverCtx = GetDriverContext<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
5963
break;
6064
case BrowserName.IExplorer:
61-
driver = GetDriver<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
65+
driverCtx = GetDriverContext<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
6266
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
6367
break;
6468
case BrowserName.Edge:
65-
driver = GetDriver<EdgeDriver>(() => EdgeDriverService.CreateDefaultService(),
69+
driverCtx = GetDriverContext<EdgeDriver>(() => EdgeDriverService.CreateDefaultService(),
6670
(EdgeOptions)driverSettings.DriverOptions, commandTimeout);
6771
break;
6872
case BrowserName.Opera:
6973
var config = new OperaConfig();
70-
var operaSettings = (OperaSettings) driverSettings;
74+
var operaSettings = (OperaSettings)driverSettings;
7175
var driverPath = new DriverManager().SetUpDriver(config, operaSettings.WebDriverVersion, operaSettings.SystemArchitecture);
72-
driver = GetDriver<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
76+
driverCtx = GetDriverContext<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
7377
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
7478
break;
7579
case BrowserName.Safari:
76-
driver = GetDriver<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
80+
driverCtx = GetDriverContext<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
7781
(SafariOptions)driverSettings.DriverOptions, commandTimeout);
7882
break;
7983
default:
8084
throw new NotSupportedException($"Browser [{browserName}] is not supported.");
8185
}
82-
return driver;
86+
87+
return driverCtx;
8388
}
8489
}
8590

86-
private WebDriver GetDriver<T>(Func<DriverService> driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
91+
private DriverContext GetDriverContext<T>(Func<DriverService> driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
8792
{
8893
var currentBrowserVersionRegex = new Regex(CurrentBrowserVersionPattern, RegexOptions.None, TimeoutConfiguration.Condition);
8994
try
9095
{
91-
return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout);
96+
var context = CreateWebDriverInstance<T>(driverServiceProvider, driverOptions, commandTimeout);
97+
return context;
9298
}
9399
catch (TargetInvocationException exception)
94100
when (exception.InnerException != null && currentBrowserVersionRegex.IsMatch(exception.InnerException.Message))
95101
{
96102
Logger.Instance.Debug(exception.InnerException.Message, exception);
97103
var currentVersion = currentBrowserVersionRegex.Match(exception.InnerException.Message).Groups[1].Value;
98104
Environment.SetEnvironmentVariable(DriverVersionVariableName, currentVersion);
99-
return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout);
105+
var context = CreateWebDriverInstance<T>(driverServiceProvider, driverOptions, commandTimeout);
106+
return context;
100107
}
101108
}
109+
110+
private static DriverContext CreateWebDriverInstance<T>(Func<DriverService> driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
111+
{
112+
var driverService = driverServiceProvider.Invoke();
113+
var driver = (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout);
114+
var context = new DriverContext(driver, driverService);
115+
return context;
116+
}
102117
}
103118
}

Aquality.Selenium/src/Aquality.Selenium/Browsers/RemoteBrowserFactory.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ public RemoteBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browse
1717
{
1818
}
1919

20-
protected override WebDriver Driver
20+
protected override WebDriver Driver => DriverContext.Driver;
21+
22+
protected override DriverContext DriverContext
2123
{
2224
get
2325
{
2426
LocalizedLogger.Info("loc.browser.grid");
2527
var capabilities = BrowserProfile.DriverSettings.DriverOptions.ToCapabilities();
2628
try
2729
{
28-
return new RemoteWebDriver(BrowserProfile.RemoteConnectionUrl, capabilities, TimeoutConfiguration.Command);
30+
var driver = new RemoteWebDriver(BrowserProfile.RemoteConnectionUrl, capabilities, TimeoutConfiguration.Command);
31+
var context = new DriverContext(driver, null);
32+
return context;
2933
}
3034
catch (Exception e)
3135
{

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/BrowserTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,12 @@ public void Should_BePossibleTo_GetDownloadDir()
258258
var downloadDir = AqualityServices.Browser.DownloadDirectory;
259259
Assert.That(downloadDir.ToLower().Contains("downloads", StringComparison.InvariantCultureIgnoreCase));
260260
}
261+
262+
[Test]
263+
public void Should_BePossibleTo_GetAccessToDriverContext()
264+
{
265+
var driverProcessId = AqualityServices.Browser.DriverService?.ProcessId;
266+
Assert.That(driverProcessId, Is.Not.Null.And.Not.EqualTo(0), "DriverProcessId should not be null or zero.");
267+
}
261268
}
262269
}

0 commit comments

Comments
 (0)