Last active
December 9, 2025 18:10
-
-
Save ohadcn/6d1bdcb663480e0817816354ee97bab0 to your computer and use it in GitHub Desktop.
Slenium WebDriverManager for c# - linux adjusments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.Runtime.InteropServices; | |
| using WebDriverManager.DriverConfigs; | |
| using WebDriverManager.Helpers; | |
| using Architecture = WebDriverManager.Helpers.Architecture; | |
| namespace Helpers | |
| { | |
| public class ChromeConfigHelper : IDriverConfig | |
| { | |
| private string _chromeVersion; | |
| public virtual string GetName() => "Chrome"; | |
| public virtual string GetUrl32() => this.GetUrl(Architecture.X32); | |
| public virtual string GetUrl64() => this.GetUrl(Architecture.X64); | |
| private string GetUrl(Architecture architecture) | |
| { | |
| string plat = GetOs() + (architecture == Architecture.X32 ? "32" : "64"); | |
| string url = | |
| "https://storage.googleapis.com/chrome-for-testing-public/" | |
| + GetRawBrowserVersion() + "/" | |
| + plat + "/" | |
| + "chromedriver-" + plat + ".zip"; | |
| Console.WriteLine(url); | |
| return url; | |
| } | |
| public virtual string GetBinaryName() => "chromedriver" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty); | |
| public virtual string GetLatestVersion() | |
| { | |
| return GetRawBrowserVersion(); | |
| } | |
| public string GetMatchingBrowserVersion() | |
| { | |
| return GetRawBrowserVersion(); | |
| } | |
| private static string GetOs() | |
| { | |
| string os = "win"; | |
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | |
| { | |
| os = "linux"; | |
| } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | |
| { | |
| os = "mac-x"; | |
| } | |
| return os; | |
| } | |
| private string GetUrlFromChromeStorage(Architecture architecture) | |
| { | |
| string url = "https://chromedriver.storage.googleapis.com/<version>/chromedriver_" + GetOs() + | |
| (architecture == Architecture.X32 ? "32" : "64") + ".zip"; | |
| return url; | |
| } | |
| string Exec(string cmd) | |
| { | |
| var escapedArgs = cmd.Replace("\"", "\\\""); | |
| var process = new Process | |
| { | |
| StartInfo = new ProcessStartInfo | |
| { | |
| RedirectStandardOutput = true, | |
| UseShellExecute = false, | |
| CreateNoWindow = true, | |
| WindowStyle = ProcessWindowStyle.Hidden, | |
| FileName = "/bin/bash", | |
| Arguments = $"-c \"{escapedArgs}\"" | |
| } | |
| }; | |
| process.Start(); | |
| process.WaitForExit(); | |
| return process.StandardOutput.ReadToEnd(); | |
| } | |
| private string GetRawBrowserVersion() | |
| { | |
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
| return RegistryHelper.GetInstalledBrowserVersionWin("chrome" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty)); | |
| string version = Exec("google-chrome --version"); | |
| version = version.Replace("Google", "").Replace("Chrome", "").Trim(); | |
| return version; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment