|
| 1 | +using System.Collections.ObjectModel; |
| 2 | + |
| 3 | +namespace DotPilot.UITests; |
| 4 | + |
| 5 | +internal static class BrowserAutomationBootstrap |
| 6 | +{ |
| 7 | + private const string BrowserDriverEnvironmentVariableName = "UNO_UITEST_DRIVER_PATH"; |
| 8 | + private const string BrowserBinaryEnvironmentVariableName = "UNO_UITEST_CHROME_BINARY_PATH"; |
| 9 | + private const string BrowserPathEnvironmentVariableName = "UNO_UITEST_BROWSER_PATH"; |
| 10 | + private const string ChromeDriverExecutableName = "chromedriver"; |
| 11 | + private const string ChromeDriverExecutableNameWindows = "chromedriver.exe"; |
| 12 | + private const string ChromeExecutableNameLinux = "google-chrome"; |
| 13 | + private const string ChromeStableExecutableNameLinux = "google-chrome-stable"; |
| 14 | + private const string ChromiumExecutableNameLinux = "chromium"; |
| 15 | + private const string ChromiumBrowserExecutableNameLinux = "chromium-browser"; |
| 16 | + private const string WindowsChromeRelativePath = @"Google\Chrome\Application\chrome.exe"; |
| 17 | + private const string WindowsChromeLocalAppDataRelativePath = @"Google\Chrome\Application\chrome.exe"; |
| 18 | + private const string MacChromeBinaryPath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; |
| 19 | + private const string MacChromeForTestingBinaryPath = |
| 20 | + "/Applications/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing"; |
| 21 | + private const string BrowserBinaryNotFoundMessage = |
| 22 | + "Unable to locate a Chrome browser binary for DotPilot UI smoke tests. " + |
| 23 | + "Set UNO_UITEST_CHROME_BINARY_PATH or UNO_UITEST_BROWSER_PATH explicitly."; |
| 24 | + private const string SearchedLocationsLabel = "Searched locations:"; |
| 25 | + private static readonly ReadOnlyCollection<string> DefaultBrowserBinaryCandidates = |
| 26 | + CreateDefaultBrowserBinaryCandidates(); |
| 27 | + |
| 28 | + public static BrowserAutomationSettings Resolve() |
| 29 | + { |
| 30 | + return Resolve(CreateEnvironmentSnapshot(), DefaultBrowserBinaryCandidates); |
| 31 | + } |
| 32 | + |
| 33 | + internal static BrowserAutomationSettings Resolve( |
| 34 | + IReadOnlyDictionary<string, string?> environment, |
| 35 | + IReadOnlyList<string> browserBinaryCandidates) |
| 36 | + { |
| 37 | + var driverPath = NormalizeBrowserDriverPath(environment); |
| 38 | + var browserBinaryPath = ResolveBrowserBinaryPath(environment, browserBinaryCandidates); |
| 39 | + SetEnvironmentVariableIfMissing(BrowserBinaryEnvironmentVariableName, browserBinaryPath, environment); |
| 40 | + SetEnvironmentVariableIfMissing(BrowserPathEnvironmentVariableName, browserBinaryPath, environment); |
| 41 | + |
| 42 | + return new BrowserAutomationSettings(driverPath, browserBinaryPath); |
| 43 | + } |
| 44 | + |
| 45 | + private static string? NormalizeBrowserDriverPath(IReadOnlyDictionary<string, string?> environment) |
| 46 | + { |
| 47 | + if (!environment.TryGetValue(BrowserDriverEnvironmentVariableName, out var configuredPath) || |
| 48 | + string.IsNullOrWhiteSpace(configuredPath)) |
| 49 | + { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + if (File.Exists(configuredPath)) |
| 54 | + { |
| 55 | + var directory = Path.GetDirectoryName(configuredPath); |
| 56 | + if (!string.IsNullOrWhiteSpace(directory)) |
| 57 | + { |
| 58 | + Environment.SetEnvironmentVariable(BrowserDriverEnvironmentVariableName, directory); |
| 59 | + return directory; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (Directory.Exists(configuredPath)) |
| 64 | + { |
| 65 | + var driverPath = Path.Combine(configuredPath, GetChromeDriverExecutableFileName()); |
| 66 | + if (File.Exists(driverPath)) |
| 67 | + { |
| 68 | + return configuredPath; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + private static string ResolveBrowserBinaryPath( |
| 76 | + IReadOnlyDictionary<string, string?> environment, |
| 77 | + IReadOnlyList<string> browserBinaryCandidates) |
| 78 | + { |
| 79 | + foreach (var environmentVariableName in GetBrowserBinaryEnvironmentVariableNames()) |
| 80 | + { |
| 81 | + if (environment.TryGetValue(environmentVariableName, out var configuredPath) && |
| 82 | + !string.IsNullOrWhiteSpace(configuredPath) && |
| 83 | + File.Exists(configuredPath)) |
| 84 | + { |
| 85 | + return configuredPath; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + foreach (var candidatePath in browserBinaryCandidates) |
| 90 | + { |
| 91 | + if (File.Exists(candidatePath)) |
| 92 | + { |
| 93 | + return candidatePath; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + var searchedLocations = browserBinaryCandidates.Count == 0 |
| 98 | + ? " none" |
| 99 | + : $"{Environment.NewLine}- {string.Join($"{Environment.NewLine}- ", browserBinaryCandidates)}"; |
| 100 | + |
| 101 | + throw new InvalidOperationException($"{BrowserBinaryNotFoundMessage}{Environment.NewLine}{SearchedLocationsLabel}{searchedLocations}"); |
| 102 | + } |
| 103 | + |
| 104 | + private static Dictionary<string, string?> CreateEnvironmentSnapshot() |
| 105 | + { |
| 106 | + return new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase) |
| 107 | + { |
| 108 | + [BrowserDriverEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserDriverEnvironmentVariableName), |
| 109 | + [BrowserBinaryEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserBinaryEnvironmentVariableName), |
| 110 | + [BrowserPathEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserPathEnvironmentVariableName), |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + private static void SetEnvironmentVariableIfMissing( |
| 115 | + string environmentVariableName, |
| 116 | + string value, |
| 117 | + IReadOnlyDictionary<string, string?> environment) |
| 118 | + { |
| 119 | + if (environment.TryGetValue(environmentVariableName, out var configuredValue) && |
| 120 | + !string.IsNullOrWhiteSpace(configuredValue)) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + Environment.SetEnvironmentVariable(environmentVariableName, value); |
| 126 | + } |
| 127 | + |
| 128 | + private static ReadOnlyCollection<string> CreateDefaultBrowserBinaryCandidates() |
| 129 | + { |
| 130 | + var candidates = new List<string>(); |
| 131 | + |
| 132 | + if (OperatingSystem.IsMacOS()) |
| 133 | + { |
| 134 | + candidates.Add(MacChromeBinaryPath); |
| 135 | + candidates.Add(MacChromeForTestingBinaryPath); |
| 136 | + return candidates.AsReadOnly(); |
| 137 | + } |
| 138 | + |
| 139 | + if (OperatingSystem.IsLinux()) |
| 140 | + { |
| 141 | + candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromeExecutableNameLinux)); |
| 142 | + candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromeStableExecutableNameLinux)); |
| 143 | + candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromiumExecutableNameLinux)); |
| 144 | + candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromiumBrowserExecutableNameLinux)); |
| 145 | + return candidates.AsReadOnly(); |
| 146 | + } |
| 147 | + |
| 148 | + if (OperatingSystem.IsWindows()) |
| 149 | + { |
| 150 | + AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.ProgramFiles, WindowsChromeRelativePath); |
| 151 | + AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.ProgramFilesX86, WindowsChromeRelativePath); |
| 152 | + AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.LocalApplicationData, WindowsChromeLocalAppDataRelativePath); |
| 153 | + } |
| 154 | + |
| 155 | + return candidates.AsReadOnly(); |
| 156 | + } |
| 157 | + |
| 158 | + private static void AddWindowsBrowserCandidate( |
| 159 | + List<string> candidates, |
| 160 | + Environment.SpecialFolder specialFolder, |
| 161 | + string relativePath) |
| 162 | + { |
| 163 | + var rootPath = Environment.GetFolderPath(specialFolder); |
| 164 | + if (string.IsNullOrWhiteSpace(rootPath)) |
| 165 | + { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + candidates.Add(Path.Combine(rootPath, relativePath)); |
| 170 | + } |
| 171 | + |
| 172 | + private static string GetChromeDriverExecutableFileName() |
| 173 | + { |
| 174 | + return OperatingSystem.IsWindows() |
| 175 | + ? ChromeDriverExecutableNameWindows |
| 176 | + : ChromeDriverExecutableName; |
| 177 | + } |
| 178 | + |
| 179 | + private static IEnumerable<string> GetBrowserBinaryEnvironmentVariableNames() |
| 180 | + { |
| 181 | + yield return BrowserBinaryEnvironmentVariableName; |
| 182 | + yield return BrowserPathEnvironmentVariableName; |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +internal sealed record BrowserAutomationSettings(string? DriverPath, string BrowserBinaryPath); |
0 commit comments