Skip to content

Commit b388421

Browse files
authored
Replace WebDriverManager usage with Selenium Manager (#245) +semver: feature
- Use build-in Selenium Manager in most cases, except of the Opera browser - Retry driver creation if exception contains the driver version, with setting it to Selenium manager - Remove webDriverVersion and systemArchitecture from settings - Update Aquality.Selenium.Core to v.3.0.5, so that Selenium is updated to v.4.16.2 - Update year for license and package closes #229
1 parent 228f374 commit b388421

File tree

13 files changed

+46
-91
lines changed

13 files changed

+46
-91
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<RepositoryType>git</RepositoryType>
1515
<PackageTags>selenium webdriver browser automation</PackageTags>
1616
<PackageLicenseFile>LICENSE</PackageLicenseFile>
17-
<Copyright>Copyright 2023 Aquality Automation</Copyright>
17+
<Copyright>Copyright 2024 Aquality Automation</Copyright>
1818
<IsPackable>true</IsPackable>
1919
</PropertyGroup>
2020

@@ -76,7 +76,7 @@
7676
</ItemGroup>
7777

7878
<ItemGroup>
79-
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.4" />
79+
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.5" />
8080
<PackageReference Include="WebDriverManager" Version="2.17.1" />
8181
</ItemGroup>
8282

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

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
using System;
1111
using System.IO;
1212
using WebDriverManager;
13-
using WebDriverManager.DriverConfigs;
1413
using WebDriverManager.DriverConfigs.Impl;
15-
using WebDriverManager.Helpers;
1614
using Aquality.Selenium.Core.Localization;
15+
using System.Reflection;
16+
using System.Text.RegularExpressions;
17+
using Aquality.Selenium.Core.Logging;
1718

1819
namespace Aquality.Selenium.Browsers
1920
{
@@ -22,8 +23,9 @@ namespace Aquality.Selenium.Browsers
2223
/// </summary>
2324
public class LocalBrowserFactory : BrowserFactory
2425
{
25-
private static readonly object WebDriverDownloadingLock = new object();
2626
private const string HostAddressDefault = "::1";
27+
private const string DriverVersionVariableName = "SE_DRIVER_VERSION";
28+
private const string CurrentBrowserVersionPattern = "Current browser version is ([\\d,\\.]+)";
2729

2830
public LocalBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile, ITimeoutConfiguration timeoutConfiguration, ILocalizedLogger localizedLogger)
2931
: base(actionRetrier, browserProfile, timeoutConfiguration, localizedLogger)
@@ -42,34 +44,36 @@ protected override WebDriver Driver
4244
{
4345
case BrowserName.Chrome:
4446
case BrowserName.Yandex:
45-
SetUpDriver(new ChromeConfig(), driverSettings);
46-
driver = GetDriver<ChromeDriver>(ChromeDriverService.CreateDefaultService(),
47+
driver = GetDriver<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(),
4748
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
4849
break;
4950
case BrowserName.Firefox:
50-
SetUpDriver(new FirefoxConfig(), driverSettings);
51-
var geckoService = FirefoxDriverService.CreateDefaultService();
52-
geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService.Host;
53-
driver = GetDriver<FirefoxDriver>(geckoService, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
51+
Func<DriverService> geckoServiceProvider = () =>
52+
{
53+
var geckoService = FirefoxDriverService.CreateDefaultService();
54+
geckoService.Host = ((FirefoxSettings)driverSettings).IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService.Host;
55+
return geckoService;
56+
};
57+
58+
driver = GetDriver<FirefoxDriver>(geckoServiceProvider, (FirefoxOptions)driverSettings.DriverOptions, commandTimeout);
5459
break;
5560
case BrowserName.IExplorer:
56-
SetUpDriver(new InternetExplorerConfig(), driverSettings);
57-
driver = GetDriver<InternetExplorerDriver>(InternetExplorerDriverService.CreateDefaultService(),
61+
driver = GetDriver<InternetExplorerDriver>(() => InternetExplorerDriverService.CreateDefaultService(),
5862
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
5963
break;
6064
case BrowserName.Edge:
61-
SetUpDriver(new EdgeConfig(), driverSettings);
62-
driver = GetDriver<EdgeDriver>(EdgeDriverService.CreateDefaultService(),
65+
driver = GetDriver<EdgeDriver>(() => EdgeDriverService.CreateDefaultService(),
6366
(EdgeOptions)driverSettings.DriverOptions, commandTimeout);
6467
break;
6568
case BrowserName.Opera:
6669
var config = new OperaConfig();
67-
var driverPath = SetUpDriver(config, driverSettings);
68-
driver = GetDriver<ChromeDriver>(ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
70+
var operaSettings = (OperaSettings) driverSettings;
71+
var driverPath = new DriverManager().SetUpDriver(config, operaSettings.WebDriverVersion, operaSettings.SystemArchitecture);
72+
driver = GetDriver<ChromeDriver>(() => ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), config.GetBinaryName()),
6973
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
7074
break;
7175
case BrowserName.Safari:
72-
driver = GetDriver<SafariDriver>(SafariDriverService.CreateDefaultService(),
76+
driver = GetDriver<SafariDriver>(() => SafariDriverService.CreateDefaultService(),
7377
(SafariOptions)driverSettings.DriverOptions, commandTimeout);
7478
break;
7579
default:
@@ -79,26 +83,21 @@ protected override WebDriver Driver
7983
}
8084
}
8185

82-
private WebDriver GetDriver<T>(DriverService driverService, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
83-
{
84-
return (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout);
85-
}
86-
87-
private static string SetUpDriver(IDriverConfig driverConfig, IDriverSettings driverSettings)
86+
private WebDriver GetDriver<T>(Func<DriverService> driverServiceProvider, DriverOptions driverOptions, TimeSpan commandTimeout) where T : WebDriver
8887
{
89-
var architecture = driverSettings.SystemArchitecture.Equals(Architecture.Auto) ? ArchitectureHelper.GetArchitecture() : driverSettings.SystemArchitecture;
90-
var version = driverSettings.WebDriverVersion.Equals(VersionResolveStrategy.Latest) ? driverConfig.GetLatestVersion() : driverSettings.WebDriverVersion;
91-
version = version.Equals(VersionResolveStrategy.MatchingBrowser) ? driverConfig.GetMatchingBrowserVersion() : version;
92-
var url = UrlHelper.BuildUrl(architecture.Equals(Architecture.X32) ? driverConfig.GetUrl32() : driverConfig.GetUrl64(), version);
93-
var binaryPath = FileHelper.GetBinDestination(driverConfig.GetName(), version, architecture, driverConfig.GetBinaryName());
94-
if (!File.Exists(binaryPath) || !Environment.GetEnvironmentVariable("PATH").Contains(binaryPath))
88+
var currentBrowserVersionRegex = new Regex(CurrentBrowserVersionPattern, RegexOptions.None, TimeoutConfiguration.Condition);
89+
try
9590
{
96-
lock (WebDriverDownloadingLock)
97-
{
98-
return new DriverManager().SetUpDriver(url, binaryPath);
99-
}
91+
return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout);
92+
}
93+
catch (TargetInvocationException exception)
94+
when (exception.InnerException != null && currentBrowserVersionRegex.IsMatch(exception.InnerException.Message))
95+
{
96+
Logger.Instance.Debug(exception.InnerException.Message, exception);
97+
var currentVersion = currentBrowserVersionRegex.Match(exception.InnerException.Message).Groups[1].Value;
98+
Environment.SetEnvironmentVariable(DriverVersionVariableName, currentVersion);
99+
return (T)Activator.CreateInstance(typeof(T), driverServiceProvider.Invoke(), driverOptions, commandTimeout);
100100
}
101-
return binaryPath;
102101
}
103102
}
104103
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Linq;
9-
using WebDriverManager.Helpers;
109

1110
namespace Aquality.Selenium.Configurations.WebDriverSettings
1211
{
@@ -32,10 +31,6 @@ protected DriverSettings(ISettingsFile settingsFile)
3231

3332
protected ISettingsFile SettingsFile { get; }
3433

35-
public virtual string WebDriverVersion => SettingsFile.GetValueOrDefault($"{DriverSettingsPath}.webDriverVersion", "Latest");
36-
37-
public Architecture SystemArchitecture => SettingsFile.GetValueOrDefault($"{DriverSettingsPath}.systemArchitecture", Architecture.Auto).ToEnum<Architecture>();
38-
3934
public abstract DriverOptions DriverOptions { get; }
4035

4136
public PageLoadStrategy PageLoadStrategy => SettingsFile.GetValueOrDefault($"{DriverSettingsPath}.pageLoadStrategy", PageLoadStrategy.Normal).ToEnum<PageLoadStrategy>();

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/IDriverSettings.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using OpenQA.Selenium;
2-
using WebDriverManager.Helpers;
32

43
namespace Aquality.Selenium.Configurations.WebDriverSettings
54
{
@@ -8,16 +7,6 @@ namespace Aquality.Selenium.Configurations.WebDriverSettings
87
/// </summary>
98
public interface IDriverSettings
109
{
11-
/// <summary>
12-
/// Gets version of web driver for WebDriverManager.
13-
/// </summary>
14-
string WebDriverVersion { get; }
15-
16-
/// <summary>
17-
/// Gets target system architecture for WebDriverManager.
18-
/// </summary>
19-
Architecture SystemArchitecture { get; }
20-
2110
/// <summary>
2211
/// Gets desired options for web driver.
2312
/// </summary>

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/OperaSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.IO;
1010
using System.Reflection;
11+
using WebDriverManager.Helpers;
1112

1213
namespace Aquality.Selenium.Configurations.WebDriverSettings
1314
{
@@ -17,6 +18,7 @@ namespace Aquality.Selenium.Configurations.WebDriverSettings
1718
public class OperaSettings : ChromeSettings
1819
{
1920
private const string DefaultBinaryLocation = "%USERPROFILE%\\AppData\\Local\\Programs\\Opera\\launcher.exe";
21+
2022
/// <summary>
2123
/// Instantiates class using JSON file with general settings.
2224
/// </summary>
@@ -25,6 +27,10 @@ public OperaSettings(ISettingsFile settingsFile) : base(settingsFile)
2527
{
2628
}
2729

30+
public virtual string WebDriverVersion => SettingsFile.GetValueOrDefault($"{DriverSettingsPath}.webDriverVersion", "Latest");
31+
32+
public Architecture SystemArchitecture => SettingsFile.GetValueOrDefault($"{DriverSettingsPath}.systemArchitecture", Architecture.Auto).ToEnum<Architecture>();
33+
2834
public virtual string BinaryLocation
2935
{
3036
get

Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
"driverSettings": {
88
"chrome": {
9-
"webDriverVersion": "MatchingBrowser",
109
"capabilities": {
1110
"selenoid:options":
1211
{
@@ -30,7 +29,6 @@
3029
"pageLoadStrategy": "Normal"
3130
},
3231
"firefox": {
33-
"webDriverVersion": "Latest",
3432
"isGeckoServiceHostDefaultEnabled": false,
3533
"capabilities": {
3634
"enableVNC": true,
@@ -54,8 +52,6 @@
5452
"startArguments": []
5553
},
5654
"iexplorer": {
57-
"webDriverVersion": "Latest",
58-
"systemArchitecture": "X32",
5955
"capabilities": {
6056
"ignoreProtectedModeSettings": true,
6157
"unhandledPromptBehavior": "default"
@@ -104,7 +100,6 @@
104100
"startArguments": ["--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage"]
105101
},
106102
"yandex": {
107-
"webDriverVersion": "102.0.5005.61",
108103
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe",
109104
"capabilities": {
110105
"enableVNC": true,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public void Should_BePossibleTo_SetAndClearDeviceMetricsOverride_WithVersionSpec
5858
{
5959
void setAction(long width, long height, bool isMobile, double scaleFactor)
6060
{
61-
var parameters = new OpenQA.Selenium.DevTools.V119.Emulation.SetDeviceMetricsOverrideCommandSettings
61+
var parameters = new OpenQA.Selenium.DevTools.V120.Emulation.SetDeviceMetricsOverrideCommandSettings
6262
{
63-
DisplayFeature = new OpenQA.Selenium.DevTools.V119.Emulation.DisplayFeature
63+
DisplayFeature = new OpenQA.Selenium.DevTools.V120.Emulation.DisplayFeature
6464
{
65-
Orientation = OpenQA.Selenium.DevTools.V119.Emulation.DisplayFeatureOrientationValues.Horizontal
65+
Orientation = OpenQA.Selenium.DevTools.V120.Emulation.DisplayFeatureOrientationValues.Horizontal
6666
},
6767
Width = width,
6868
Height = height,

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ protected override WebDriver Driver
6666

6767
private static void SetUpDriver(IDriverConfig driverConfig, IDriverSettings driverSettings)
6868
{
69-
var architecture = driverSettings.SystemArchitecture.Equals(Architecture.Auto) ? ArchitectureHelper.GetArchitecture() : driverSettings.SystemArchitecture;
70-
var version = driverSettings.WebDriverVersion.Equals("Latest") ? driverConfig.GetLatestVersion() : driverSettings.WebDriverVersion;
71-
version = version.Equals(VersionResolveStrategy.MatchingBrowser) ? driverConfig.GetMatchingBrowserVersion() : version;
69+
var architecture = ArchitectureHelper.GetArchitecture();
70+
var version = driverConfig.GetMatchingBrowserVersion();
7271
var url = UrlHelper.BuildUrl(architecture.Equals(Architecture.X32) ? driverConfig.GetUrl32() : driverConfig.GetUrl64(), version);
7372
var binaryPath = FileHelper.GetBinDestination(driverConfig.GetName(), version, architecture, driverConfig.GetBinaryName());
7473
if (!File.Exists(binaryPath) || !Environment.GetEnvironmentVariable("PATH").Contains(binaryPath))

Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
"driverSettings": {
88
"chrome": {
9-
"webDriverVersion": "MatchingBrowser",
109
"capabilities": {
1110
"selenoid:options":
1211
{
@@ -30,8 +29,6 @@
3029
"pageLoadStrategy": "Normal"
3130
},
3231
"firefox": {
33-
"webDriverVersion": "0.24.0",
34-
"systemArchitecture": "X64",
3532
"capabilities": {
3633
"enableVNC": true
3734
},
@@ -53,16 +50,13 @@
5350
"startArguments": []
5451
},
5552
"iexplorer": {
56-
"webDriverVersion": "Latest",
57-
"systemArchitecture": "X32",
5853
"capabilities": {
5954
"ignoreProtectedModeSettings": true
6055
},
6156
"options": {},
6257
"startArguments": []
6358
},
6459
"edge": {
65-
"webDriverVersion": "Latest",
6660
"capabilities": {
6761
"enableVNC": true
6862
},

0 commit comments

Comments
 (0)