-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserAutomationBootstrapTests.cs
More file actions
215 lines (184 loc) · 8.14 KB
/
BrowserAutomationBootstrapTests.cs
File metadata and controls
215 lines (184 loc) · 8.14 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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DotPilot.UITests;
[TestFixture]
public sealed class BrowserAutomationBootstrapTests
{
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 BrowserBinaryExecutableName = "chrome-under-test";
private string? _originalBrowserDriverPath;
private string? _originalBrowserBinaryPath;
private string? _originalBrowserPath;
[SetUp]
public void CaptureOriginalEnvironment()
{
_originalBrowserDriverPath = Environment.GetEnvironmentVariable(BrowserDriverEnvironmentVariableName);
_originalBrowserBinaryPath = Environment.GetEnvironmentVariable(BrowserBinaryEnvironmentVariableName);
_originalBrowserPath = Environment.GetEnvironmentVariable(BrowserPathEnvironmentVariableName);
}
[TearDown]
public void RestoreOriginalEnvironment()
{
Environment.SetEnvironmentVariable(BrowserDriverEnvironmentVariableName, _originalBrowserDriverPath);
Environment.SetEnvironmentVariable(BrowserBinaryEnvironmentVariableName, _originalBrowserBinaryPath);
Environment.SetEnvironmentVariable(BrowserPathEnvironmentVariableName, _originalBrowserPath);
}
[Test]
public void WhenDriverPathPointsToBinaryThenResolverNormalizesToContainingDirectory()
{
using var sandbox = new BrowserAutomationSandbox();
var driverFilePath = sandbox.CreateFile(GetChromeDriverExecutableFileName());
var browserBinaryPath = sandbox.CreateFile(BrowserBinaryExecutableName);
var environment = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase)
{
[BrowserDriverEnvironmentVariableName] = driverFilePath,
[BrowserBinaryEnvironmentVariableName] = browserBinaryPath,
};
var settings = BrowserAutomationBootstrap.Resolve(environment, []);
Assert.That(settings.DriverPath, Is.EqualTo(Path.GetDirectoryName(driverFilePath)));
Assert.That(settings.BrowserBinaryPath, Is.EqualTo(browserBinaryPath));
}
[Test]
public void WhenBrowserBinaryEnvironmentVariableIsMissingThenResolverFallsBackToCandidatePaths()
{
using var sandbox = new BrowserAutomationSandbox();
var driverFilePath = sandbox.CreateFile(GetChromeDriverExecutableFileName());
var browserBinaryPath = sandbox.CreateFile(BrowserBinaryExecutableName);
var environment = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase)
{
[BrowserDriverEnvironmentVariableName] = driverFilePath,
[BrowserBinaryEnvironmentVariableName] = null,
[BrowserPathEnvironmentVariableName] = null,
};
var settings = BrowserAutomationBootstrap.Resolve(environment, [browserBinaryPath]);
Assert.That(settings.DriverPath, Is.EqualTo(Path.GetDirectoryName(driverFilePath)));
Assert.That(settings.BrowserBinaryPath, Is.EqualTo(browserBinaryPath));
}
[Test]
public void WhenCachedDriverVersionMappingExistsThenResolverUsesCachedDriverDirectory()
{
using var sandbox = new BrowserAutomationSandbox();
var browserBuild = "145.0.7632";
var driverPlatform = GetExpectedDriverPlatform();
var driverVersion = "145.0.7632.117";
var cacheRootPath = sandbox.CreateDirectory("driver-cache");
var driverDirectory = Path.Combine(cacheRootPath, driverVersion, $"chromedriver-{driverPlatform}");
Directory.CreateDirectory(driverDirectory);
sandbox.CreateFile(Path.Combine(driverDirectory, GetChromeDriverExecutableFileName()));
BrowserAutomationBootstrap.PersistDriverVersionMapping(cacheRootPath, browserBuild, driverPlatform, driverVersion);
var resolvedDirectory = BrowserAutomationBootstrap.ResolveCachedChromeDriverDirectory(
cacheRootPath,
browserBuild,
driverPlatform);
Assert.That(resolvedDirectory, Is.EqualTo(driverDirectory));
}
[Test]
public void WhenCachedDriverExistsWithoutVersionMappingThenResolverIgnoresIt()
{
using var sandbox = new BrowserAutomationSandbox();
var cacheRootPath = sandbox.CreateDirectory("driver-cache");
var driverDirectory = Path.Combine(cacheRootPath, "145.0.7632.117", $"chromedriver-{GetExpectedDriverPlatform()}");
Directory.CreateDirectory(driverDirectory);
sandbox.CreateFile(Path.Combine(driverDirectory, GetChromeDriverExecutableFileName()));
var resolvedDirectory = BrowserAutomationBootstrap.ResolveCachedChromeDriverDirectory(
cacheRootPath,
"145.0.7632.117");
Assert.That(resolvedDirectory, Is.Null);
}
[Test]
public void WhenVersionProbeProcessTimesOutThenItFailsFast()
{
var startInfo = CreateSleepStartInfo();
var exception = Assert.Throws<TimeoutException>(
() => BrowserAutomationBootstrap.RunProcessAndCaptureOutput(
startInfo,
TimeSpan.FromMilliseconds(50),
"version probe timed out"));
Assert.That(exception, Is.Not.Null);
Assert.That(exception!.Message, Does.Contain("version probe timed out"));
}
private static string GetChromeDriverExecutableFileName()
{
return OperatingSystem.IsWindows()
? ChromeDriverExecutableNameWindows
: ChromeDriverExecutableName;
}
private static string GetExpectedDriverPlatform()
{
if (OperatingSystem.IsMacOS())
{
return RuntimeInformation.ProcessArchitecture == Architecture.Arm64
? "mac-arm64"
: "mac-x64";
}
if (OperatingSystem.IsLinux() && RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
return "linux64";
}
return RuntimeInformation.ProcessArchitecture == Architecture.X86
? "win32"
: "win64";
}
private sealed class BrowserAutomationSandbox : IDisposable
{
private readonly string _rootPath = Path.Combine(
Path.GetTempPath(),
$"{nameof(BrowserAutomationBootstrapTests)}_{Guid.NewGuid():N}");
public BrowserAutomationSandbox()
{
Directory.CreateDirectory(_rootPath);
}
public string CreateFile(string fileName)
{
var filePath = Path.Combine(_rootPath, fileName);
var directoryPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrWhiteSpace(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(filePath, fileName);
return filePath;
}
public string CreateDirectory(string relativePath)
{
var directoryPath = Path.Combine(_rootPath, relativePath);
Directory.CreateDirectory(directoryPath);
return directoryPath;
}
public void Dispose()
{
if (Directory.Exists(_rootPath))
{
Directory.Delete(_rootPath, recursive: true);
}
}
}
private static ProcessStartInfo CreateSleepStartInfo()
{
if (OperatingSystem.IsWindows())
{
return new ProcessStartInfo
{
FileName = "powershell",
Arguments = "-NoProfile -Command Start-Sleep -Seconds 5",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
}
return new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = "-c \"sleep 5\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
}
}