Skip to content

Commit

Permalink
Use DriverManager to download drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
huaxing-yuan committed Apr 2, 2024
1 parent 91f2645 commit 37dde2e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 113 deletions.
1 change: 1 addition & 0 deletions src/AxaFrance.WebEngine.Web/AxaFrance.WebEngine.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
<PackageReference Include="WebDriverManager" Version="2.17.2" />
</ItemGroup>

<ItemGroup>
Expand Down
132 changes: 21 additions & 111 deletions src/AxaFrance.WebEngine.Web/BrowserFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using WebDriverManager.DriverConfigs.Impl;
using static System.Environment;

[assembly: InternalsVisibleTo("WebRunner")]
Expand Down Expand Up @@ -101,7 +102,7 @@ public static WebDriver GetDriver(Platform platform, BrowserType browserType, IE


#if NET48_OR_GREATER || NET6_0_OR_GREATER
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
#else
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
#endif
Expand Down Expand Up @@ -427,123 +428,32 @@ private static WebDriver GetIEDriver()

private static WebDriver GetEdgeDriver(IEnumerable<string> browserOptions)
{
object path = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe", "", null);
var version = FileVersionInfo.GetVersionInfo(path.ToString()).FileVersion;
DebugLogger.WriteLine($"Edge: {version} installed. Checking WebDriver for the browser.");
var edgeDriver = GetEdgeDriver(version, browserOptions);
return edgeDriver;
}

private static WebDriver GetEdgeDriver(string version, IEnumerable<string> browserOptions)
{
string downloadUrl = $"https://msedgedriver.azureedge.net/{version}/edgedriver_win64.zip";
string file = $"{workingDirectory}\\Edge\\{version}\\Webdriver.zip";
string folder = $"{workingDirectory}\\Edge\\{version}\\Extracted";
DirectoryInfo di = new DirectoryInfo(folder);
using (WebClient c = new WebClient())
new WebDriverManager.DriverManager().SetUpDriver(new EdgeConfig(), "MatchingBrowser");
OpenQA.Selenium.Edge.EdgeOptions options = new OpenQA.Selenium.Edge.EdgeOptions()
{
if (!(di.Exists && di.GetFiles().Any()))
{
DebugLogger.WriteLine($"Downloading driver and install into {folder}.");
System.IO.Directory.CreateDirectory(folder);
c.DownloadFile(downloadUrl, file);
System.IO.Compression.ZipFile.ExtractToDirectory(file, folder);
}
else
{
DebugLogger.WriteLine($"A Webdriver is found for your current version of Browser.");
}
OpenQA.Selenium.Edge.EdgeOptions options = new OpenQA.Selenium.Edge.EdgeOptions()
{
AcceptInsecureCertificates = true,
};
options.AddArguments(browserOptions);
AcceptInsecureCertificates = true,
};
options.AddArguments(browserOptions);

OpenQA.Selenium.Edge.EdgeDriver cd = new OpenQA.Selenium.Edge.EdgeDriver(folder, options);
return cd;
}
OpenQA.Selenium.Edge.EdgeDriver cd = new OpenQA.Selenium.Edge.EdgeDriver(options);
return cd;
}

private static WebDriver GetChromeDriver(IEnumerable<string> browserOptions)
{
object path = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null);
if (path == null)
{
path = Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null);
}
var version = FileVersionInfo.GetVersionInfo(path.ToString()).FileVersion;
DebugLogger.WriteLine($"Detected installed Chrome Version: {version}");
var chromeDriver = GetChromeDriver(version, browserOptions);
return chromeDriver;
}
private static WebDriver GetChromeDriver(string version, IEnumerable<string> browserOptions)
{
string downloadUrl = null;
int major = int.Parse(version.Substring(0, version.IndexOf(".")));
string v = version.Substring(0, version.LastIndexOf("."));
string getVersionUrl;
if (major >= 115)
{
getVersionUrl = $"https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_{v}";
}
else
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig(), "MatchingBrowser");
OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions()
{
getVersionUrl = $"https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{v}";
}

using (WebClient c = new WebClient())
{
string driverVersion = c.DownloadString(getVersionUrl);
if (major >= 115)
{
downloadUrl = $"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/{driverVersion}/win64/chromedriver-win64.zip";
}
else
{
downloadUrl = $"https://chromedriver.storage.googleapis.com/{driverVersion}/chromedriver_win32.zip";
}
string file = $"{workingDirectory}\\ChromeDriver\\{driverVersion}\\Webdriver.zip";
string folder = $"{workingDirectory}\\ChromeDriver\\{driverVersion}\\Extracted";
DirectoryInfo di = new DirectoryInfo(folder);
//fix bug #48: Deadlock if webdriver download fails
//Checks if the folder is not empty, or we need to download and install the webdriver again.
//Same implementation for Edge Driver.
if (!(di.Exists && di.GetFiles().Any()))
{
DebugLogger.WriteLine($"Downloading driver and install into {folder}.");
System.IO.Directory.CreateDirectory(folder);
c.DownloadFile(downloadUrl, file);
System.IO.Compression.ZipFile.ExtractToDirectory(file, folder);
//new version of chromedriver are in a folder, copy them to root folder of Extracted.

if (di.GetDirectories().Any())
{
foreach (var subdir in di.GetDirectories())
{
foreach (var fil in subdir.GetFiles())
{
fil.CopyTo(Path.Combine(folder, fil.Name));
}
}
}
}
else
{
DebugLogger.WriteLine($"A Webdriver is found for your current version of Browser.");
}
OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions()
{
AcceptInsecureCertificates = true,
};
options.AddArgument($"user-data-dir={GetEnvironmentVariable("LOCALAPPDATA")}\\Google\\Chrome\\User Data");
options.AddArgument("dns-prefetch-disable");
options.AddArgument("homepage=about:blank");
options.AddArgument("disable-popup-blocking");
options.AddArgument("no-default-browser-check");
if (browserOptions != null) options.AddArguments(browserOptions);
OpenQA.Selenium.Chrome.ChromeDriver cd = new OpenQA.Selenium.Chrome.ChromeDriver(folder, options);
return cd;
}
AcceptInsecureCertificates = true,
};
options.AddArgument($"user-data-dir={GetEnvironmentVariable("LOCALAPPDATA")}\\Google\\Chrome\\User Data");
options.AddArgument("dns-prefetch-disable");
options.AddArgument("homepage=about:blank");
options.AddArgument("disable-popup-blocking");
options.AddArgument("no-default-browser-check");
if (browserOptions != null) options.AddArguments(browserOptions);
OpenQA.Selenium.Chrome.ChromeDriver cd = new OpenQA.Selenium.Chrome.ChromeDriver(options);
return cd;
}

private static WebDriver GetFirefoxDriver(IEnumerable<string> browserOptions)
Expand Down
Binary file modified src/AxaFrance.WebEngine.Web/geckodriver.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion src/WebEngine.Test/UnitTests/WebTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void Initialize(TestContext context)
{
if (driver == null)
{
driver = BrowserFactory.GetDriver(AxaFrance.WebEngine.Platform.Windows, BrowserType.ChromiumEdge);
driver = BrowserFactory.GetDriver(AxaFrance.WebEngine.Platform.Windows, BrowserType.Chrome);
}

networkIntercepter = driver.Manage().Network;
Expand Down
6 changes: 5 additions & 1 deletion src/WebEngine.Test/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
"additionalCapability1": "value1",
"bstack:options": {
"local": true
},
"other:capability": {
"key": "value",
"key2": 12
}
},
"chromeOptions": [ "incognito" ],
"chromeOptions": [],
"firefoxOptions": [],
"edgeOptions": [ "inprivate", "start-maximized" ],
"safariOptions": []
Expand Down

0 comments on commit 37dde2e

Please sign in to comment.