-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserAutomationBootstrap.cs
More file actions
514 lines (444 loc) · 21.5 KB
/
BrowserAutomationBootstrap.cs
File metadata and controls
514 lines (444 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace DotPilot.UITests;
internal static partial class BrowserAutomationBootstrap
{
private const string BrowserDriverEnvironmentVariableName = "UNO_UITEST_DRIVER_PATH";
private const string BrowserBinaryEnvironmentVariableName = "UNO_UITEST_CHROME_BINARY_PATH";
private const string BrowserPathEnvironmentVariableName = "UNO_UITEST_BROWSER_PATH";
private const string ChromeDriverExecutableName = "chromedriver";
private const string ChromeDriverExecutableNameWindows = "chromedriver.exe";
private const string ChromeExecutableNameLinux = "google-chrome";
private const string ChromeStableExecutableNameLinux = "google-chrome-stable";
private const string ChromiumExecutableNameLinux = "chromium";
private const string ChromiumBrowserExecutableNameLinux = "chromium-browser";
private const string WindowsChromeRelativePath = @"Google\Chrome\Application\chrome.exe";
private const string WindowsChromeLocalAppDataRelativePath = @"Google\Chrome\Application\chrome.exe";
private const string MacChromeBinaryPath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
private const string MacChromeForTestingBinaryPath =
"/Applications/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing";
private const string BrowserVersionArgument = "--version";
private const string BrowserVersionPattern = @"(\d+\.\d+\.\d+\.\d+)";
private const string BrowserVersionProbeTimeoutMessage =
"Timed out while probing the installed Chrome version for DotPilot UI smoke tests.";
private const string BrowserBinaryNotFoundMessage =
"Unable to locate a Chrome browser binary for DotPilot UI smoke tests. " +
"Set UNO_UITEST_CHROME_BINARY_PATH or UNO_UITEST_BROWSER_PATH explicitly.";
private const string DriverPlatformNotSupportedMessage =
"DotPilot UI smoke tests do not have an automatic ChromeDriver mapping for the current operating system and architecture.";
private const string BrowserVersionNotFoundMessage =
"Unable to determine the installed Chrome version for DotPilot UI smoke tests.";
private const string DriverVersionNotFoundMessage =
"Unable to determine a matching ChromeDriver version for the installed Chrome build.";
private const string DriverDownloadFailedMessage =
"Failed to download the ChromeDriver archive required for DotPilot UI smoke tests.";
private const string DriverExecutableNotFoundMessage =
"ChromeDriver bootstrap completed without producing the expected executable.";
private const string DriverCacheDirectoryName = "dotpilot-uitest-drivers";
private const string ChromeDriverBundleNamePrefix = "chromedriver-";
private const string DriverVersionCacheFileNameSuffix = ".driver-version";
private const string LatestPatchVersionsUrl =
"https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json";
private const string ChromeForTestingDownloadBaseUrl =
"https://storage.googleapis.com/chrome-for-testing-public";
private const string BuildsPropertyName = "builds";
private const string VersionPropertyName = "version";
private const string SearchedLocationsLabel = "Searched locations:";
private static readonly ReadOnlyCollection<string> DefaultBrowserBinaryCandidates =
CreateDefaultBrowserBinaryCandidates();
private static readonly HttpClient HttpClient = new()
{
Timeout = TimeSpan.FromMinutes(2),
};
private static readonly TimeSpan BrowserVersionProbeTimeout = TimeSpan.FromSeconds(10);
public static BrowserAutomationSettings Resolve()
{
return Resolve(CreateEnvironmentSnapshot(), DefaultBrowserBinaryCandidates, applyEnvironmentVariables: true);
}
internal static BrowserAutomationSettings Resolve(
IReadOnlyDictionary<string, string?> environment,
IReadOnlyList<string> browserBinaryCandidates,
bool applyEnvironmentVariables = false)
{
HarnessLog.Write("Resolving browser automation settings.");
var browserBinaryPath = ResolveBrowserBinaryPath(environment, browserBinaryCandidates);
var driverPath = ResolveBrowserDriverPath(environment, browserBinaryPath);
if (applyEnvironmentVariables)
{
SetEnvironmentVariableIfMissing(BrowserBinaryEnvironmentVariableName, browserBinaryPath, environment);
SetEnvironmentVariableIfMissing(BrowserPathEnvironmentVariableName, browserBinaryPath, environment);
SetEnvironmentVariableIfMissing(BrowserDriverEnvironmentVariableName, driverPath, environment);
}
HarnessLog.Write($"Resolved browser binary path '{browserBinaryPath}'.");
HarnessLog.Write($"Resolved browser driver directory '{driverPath}'.");
return new BrowserAutomationSettings(driverPath, browserBinaryPath);
}
private static string ResolveBrowserDriverPath(
IReadOnlyDictionary<string, string?> environment,
string browserBinaryPath)
{
var configuredDriverPath = NormalizeBrowserDriverPath(environment);
if (!string.IsNullOrWhiteSpace(configuredDriverPath))
{
return configuredDriverPath;
}
var browserVersion = ResolveBrowserVersion(browserBinaryPath);
var browserBuild = BuildChromeVersionKey(browserVersion);
var driverPlatform = ResolveChromeDriverPlatform();
var cacheRootPath = GetDriverCacheRootPath();
HarnessLog.Write($"Browser version '{browserVersion}' resolved for '{browserBinaryPath}'.");
var cachedDriverPath = ResolveCachedChromeDriverDirectory(cacheRootPath, browserBuild, driverPlatform);
if (!string.IsNullOrWhiteSpace(cachedDriverPath))
{
var cachedDriverExecutablePath = Path.Combine(cachedDriverPath, GetChromeDriverExecutableFileName());
EnsureDriverExecutablePermissions(cachedDriverExecutablePath);
HarnessLog.Write($"Reusing cached ChromeDriver at '{cachedDriverExecutablePath}'.");
return cachedDriverPath;
}
return EnsureChromeDriverDownloaded(browserBuild, driverPlatform, cacheRootPath);
}
private static string? NormalizeBrowserDriverPath(IReadOnlyDictionary<string, string?> environment)
{
if (!environment.TryGetValue(BrowserDriverEnvironmentVariableName, out var configuredPath) ||
string.IsNullOrWhiteSpace(configuredPath))
{
return null;
}
if (File.Exists(configuredPath))
{
var directory = Path.GetDirectoryName(configuredPath);
if (!string.IsNullOrWhiteSpace(directory))
{
return directory;
}
}
if (Directory.Exists(configuredPath))
{
var driverPath = Path.Combine(configuredPath, GetChromeDriverExecutableFileName());
if (File.Exists(driverPath))
{
return configuredPath;
}
}
return null;
}
private static string EnsureChromeDriverDownloaded(
string browserBuild,
string driverPlatform,
string cacheRootPath)
{
var driverVersion = ResolveChromeDriverVersion(browserBuild);
var driverVersionRootPath = Path.Combine(cacheRootPath, driverVersion);
var driverDirectory = Path.Combine(driverVersionRootPath, $"{ChromeDriverBundleNamePrefix}{driverPlatform}");
var driverExecutablePath = Path.Combine(driverDirectory, GetChromeDriverExecutableFileName());
HarnessLog.Write($"Matching ChromeDriver version '{driverVersion}' on platform '{driverPlatform}'.");
if (File.Exists(driverExecutablePath))
{
EnsureDriverExecutablePermissions(driverExecutablePath);
PersistDriverVersionMapping(cacheRootPath, browserBuild, driverPlatform, driverVersion);
HarnessLog.Write($"Reusing cached ChromeDriver at '{driverExecutablePath}'.");
return driverDirectory;
}
Directory.CreateDirectory(driverVersionRootPath);
HarnessLog.Write($"Downloading ChromeDriver to '{driverVersionRootPath}'.");
DownloadChromeDriverArchive(driverVersion, driverPlatform, driverVersionRootPath);
EnsureDriverExecutablePermissions(driverExecutablePath);
if (!File.Exists(driverExecutablePath))
{
throw new InvalidOperationException($"{DriverExecutableNotFoundMessage} Expected path: {driverExecutablePath}");
}
PersistDriverVersionMapping(cacheRootPath, browserBuild, driverPlatform, driverVersion);
return driverDirectory;
}
internal static string? ResolveCachedChromeDriverDirectory(string cacheRootPath, string browserVersion)
{
var browserBuild = BuildChromeVersionKey(browserVersion);
var driverPlatform = ResolveChromeDriverPlatform();
return ResolveCachedChromeDriverDirectory(cacheRootPath, browserBuild, driverPlatform);
}
internal static string? ResolveCachedChromeDriverDirectory(string cacheRootPath, string browserBuild, string driverPlatform)
{
var driverVersionMappingPath = GetDriverVersionMappingPath(cacheRootPath, browserBuild, driverPlatform);
if (!File.Exists(driverVersionMappingPath))
{
return null;
}
var driverVersion = File.ReadAllText(driverVersionMappingPath).Trim();
if (string.IsNullOrWhiteSpace(driverVersion))
{
return null;
}
var driverDirectory = Path.Combine(cacheRootPath, driverVersion, $"{ChromeDriverBundleNamePrefix}{driverPlatform}");
var driverExecutablePath = Path.Combine(driverDirectory, GetChromeDriverExecutableFileName());
return File.Exists(driverExecutablePath) ? driverDirectory : null;
}
internal static void PersistDriverVersionMapping(
string cacheRootPath,
string browserBuild,
string driverPlatform,
string driverVersion)
{
Directory.CreateDirectory(cacheRootPath);
File.WriteAllText(GetDriverVersionMappingPath(cacheRootPath, browserBuild, driverPlatform), driverVersion);
}
private static void DownloadChromeDriverArchive(string driverVersion, string driverPlatform, string cacheRootPath)
{
var archiveName = $"{ChromeDriverBundleNamePrefix}{driverPlatform}.zip";
var archivePath = Path.Combine(cacheRootPath, archiveName);
var driverDirectory = Path.Combine(cacheRootPath, $"{ChromeDriverBundleNamePrefix}{driverPlatform}");
if (Directory.Exists(driverDirectory))
{
Directory.Delete(driverDirectory, recursive: true);
}
var downloadUrl = BuildChromeDriverDownloadUrl(driverVersion, driverPlatform, archiveName);
HarnessLog.Write($"Fetching ChromeDriver archive '{downloadUrl}'.");
var archiveBytes = GetResponseBytes(downloadUrl, DriverDownloadFailedMessage);
File.WriteAllBytes(archivePath, archiveBytes);
ZipFile.ExtractToDirectory(archivePath, cacheRootPath, overwriteFiles: true);
HarnessLog.Write($"Extracted ChromeDriver archive to '{driverDirectory}'.");
}
private static byte[] GetResponseBytes(string requestUri, string failureMessage)
{
try
{
return HttpClient.GetByteArrayAsync(requestUri).GetAwaiter().GetResult();
}
catch (Exception exception)
{
throw new InvalidOperationException($"{failureMessage} Source: {requestUri}", exception);
}
}
private static string ResolveBrowserVersion(string browserBinaryPath)
{
var processStartInfo = new ProcessStartInfo
{
FileName = browserBinaryPath,
Arguments = BrowserVersionArgument,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
var output = RunProcessAndCaptureOutput(
processStartInfo,
BrowserVersionProbeTimeout,
BrowserVersionProbeTimeoutMessage);
var match = BrowserVersionRegex().Match(output);
if (!match.Success)
{
throw new InvalidOperationException($"{BrowserVersionNotFoundMessage} Output: {output.Trim()}");
}
return match.Groups[1].Value;
}
private static string ResolveChromeDriverVersion(string browserBuild)
{
var response = GetResponseBytes(LatestPatchVersionsUrl, DriverVersionNotFoundMessage);
using var document = JsonDocument.Parse(response);
if (!document.RootElement.TryGetProperty(BuildsPropertyName, out var buildsElement) ||
!buildsElement.TryGetProperty(browserBuild, out var buildElement) ||
!buildElement.TryGetProperty(VersionPropertyName, out var versionElement))
{
throw new InvalidOperationException($"{DriverVersionNotFoundMessage} Browser build: {browserBuild}");
}
return versionElement.GetString()
?? throw new InvalidOperationException($"{DriverVersionNotFoundMessage} Browser build: {browserBuild}");
}
private static string BuildChromeVersionKey(string browserVersion)
{
var segments = browserVersion.Split('.');
if (segments.Length < 3)
{
throw new InvalidOperationException($"{BrowserVersionNotFoundMessage} Parsed version: {browserVersion}");
}
return string.Join('.', segments.Take(3));
}
internal static string RunProcessAndCaptureOutput(
ProcessStartInfo startInfo,
TimeSpan timeout,
string timeoutMessage)
{
return RunProcessAndCaptureOutputAsync(startInfo, timeout, timeoutMessage).GetAwaiter().GetResult();
}
private static async Task<string> RunProcessAndCaptureOutputAsync(
ProcessStartInfo startInfo,
TimeSpan timeout,
string timeoutMessage)
{
using var process = Process.Start(startInfo)
?? throw new InvalidOperationException(timeoutMessage);
var standardOutputTask = process.StandardOutput.ReadToEndAsync();
var standardErrorTask = process.StandardError.ReadToEndAsync();
var completionTask = Task.WhenAll(standardOutputTask, standardErrorTask, process.WaitForExitAsync());
var completedTask = await Task
.WhenAny(completionTask, Task.Delay(timeout))
.ConfigureAwait(false);
if (completedTask != completionTask)
{
try
{
process.Kill(entireProcessTree: true);
}
catch
{
// Best-effort cleanup only.
}
throw new TimeoutException(timeoutMessage);
}
await completionTask.ConfigureAwait(false);
return $"{await standardOutputTask.ConfigureAwait(false)}{Environment.NewLine}{await standardErrorTask.ConfigureAwait(false)}";
}
private static string ResolveChromeDriverPlatform()
{
if (OperatingSystem.IsMacOS())
{
return RuntimeInformation.ProcessArchitecture == Architecture.Arm64
? "mac-arm64"
: "mac-x64";
}
if (OperatingSystem.IsLinux() && RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
return "linux64";
}
if (OperatingSystem.IsWindows())
{
return RuntimeInformation.ProcessArchitecture == Architecture.X86
? "win32"
: "win64";
}
throw new PlatformNotSupportedException(DriverPlatformNotSupportedMessage);
}
private static string BuildChromeDriverDownloadUrl(
string driverVersion,
string driverPlatform,
string archiveName)
{
return $"{ChromeForTestingDownloadBaseUrl}/{driverVersion}/{driverPlatform}/{archiveName}";
}
private static void EnsureDriverExecutablePermissions(string driverExecutablePath)
{
if (!File.Exists(driverExecutablePath) || OperatingSystem.IsWindows())
{
return;
}
File.SetUnixFileMode(
driverExecutablePath,
UnixFileMode.UserRead |
UnixFileMode.UserWrite |
UnixFileMode.UserExecute |
UnixFileMode.GroupRead |
UnixFileMode.GroupExecute |
UnixFileMode.OtherRead |
UnixFileMode.OtherExecute);
}
private static string ResolveBrowserBinaryPath(
IReadOnlyDictionary<string, string?> environment,
IReadOnlyList<string> browserBinaryCandidates)
{
foreach (var environmentVariableName in GetBrowserBinaryEnvironmentVariableNames())
{
if (environment.TryGetValue(environmentVariableName, out var configuredPath) &&
!string.IsNullOrWhiteSpace(configuredPath) &&
File.Exists(configuredPath))
{
HarnessLog.Write($"Using browser binary from environment variable '{environmentVariableName}'.");
return configuredPath;
}
}
foreach (var candidatePath in browserBinaryCandidates)
{
if (File.Exists(candidatePath))
{
HarnessLog.Write($"Using browser binary candidate '{candidatePath}'.");
return candidatePath;
}
}
var searchedLocations = browserBinaryCandidates.Count == 0
? " none"
: $"{Environment.NewLine}- {string.Join($"{Environment.NewLine}- ", browserBinaryCandidates)}";
throw new InvalidOperationException($"{BrowserBinaryNotFoundMessage}{Environment.NewLine}{SearchedLocationsLabel}{searchedLocations}");
}
private static Dictionary<string, string?> CreateEnvironmentSnapshot()
{
return new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase)
{
[BrowserDriverEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserDriverEnvironmentVariableName),
[BrowserBinaryEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserBinaryEnvironmentVariableName),
[BrowserPathEnvironmentVariableName] = Environment.GetEnvironmentVariable(BrowserPathEnvironmentVariableName),
};
}
private static void SetEnvironmentVariableIfMissing(
string environmentVariableName,
string value,
IReadOnlyDictionary<string, string?> environment)
{
if (environment.TryGetValue(environmentVariableName, out var configuredValue) &&
!string.IsNullOrWhiteSpace(configuredValue))
{
return;
}
Environment.SetEnvironmentVariable(environmentVariableName, value);
}
private static ReadOnlyCollection<string> CreateDefaultBrowserBinaryCandidates()
{
var candidates = new List<string>();
if (OperatingSystem.IsMacOS())
{
candidates.Add(MacChromeBinaryPath);
candidates.Add(MacChromeForTestingBinaryPath);
return candidates.AsReadOnly();
}
if (OperatingSystem.IsLinux())
{
candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromeExecutableNameLinux));
candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromeStableExecutableNameLinux));
candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromiumExecutableNameLinux));
candidates.Add(Path.Combine(Path.DirectorySeparatorChar.ToString(), "usr", "bin", ChromiumBrowserExecutableNameLinux));
return candidates.AsReadOnly();
}
if (OperatingSystem.IsWindows())
{
AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.ProgramFiles, WindowsChromeRelativePath);
AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.ProgramFilesX86, WindowsChromeRelativePath);
AddWindowsBrowserCandidate(candidates, Environment.SpecialFolder.LocalApplicationData, WindowsChromeLocalAppDataRelativePath);
}
return candidates.AsReadOnly();
}
private static void AddWindowsBrowserCandidate(
List<string> candidates,
Environment.SpecialFolder specialFolder,
string relativePath)
{
var rootPath = Environment.GetFolderPath(specialFolder);
if (string.IsNullOrWhiteSpace(rootPath))
{
return;
}
candidates.Add(Path.Combine(rootPath, relativePath));
}
private static string GetChromeDriverExecutableFileName()
{
return OperatingSystem.IsWindows()
? ChromeDriverExecutableNameWindows
: ChromeDriverExecutableName;
}
private static string GetDriverCacheRootPath()
{
return Path.Combine(Path.GetTempPath(), DriverCacheDirectoryName);
}
private static string GetDriverVersionMappingPath(string cacheRootPath, string browserBuild, string driverPlatform)
{
return Path.Combine(cacheRootPath, $"{browserBuild}-{driverPlatform}{DriverVersionCacheFileNameSuffix}");
}
private static IEnumerable<string> GetBrowserBinaryEnvironmentVariableNames()
{
yield return BrowserBinaryEnvironmentVariableName;
yield return BrowserPathEnvironmentVariableName;
}
[GeneratedRegex(BrowserVersionPattern, RegexOptions.CultureInvariant)]
private static partial Regex BrowserVersionRegex();
}
internal sealed record BrowserAutomationSettings(string DriverPath, string BrowserBinaryPath);