-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathWasmBrowserTestRunner.cs
276 lines (239 loc) · 10.8 KB
/
WasmBrowserTestRunner.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.WebSockets;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.DotNet.XHarness.CLI.CommandArguments.Wasm;
using Microsoft.DotNet.XHarness.Common.CLI;
using Microsoft.Extensions.Logging;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumLogLevel = OpenQA.Selenium.LogLevel;
namespace Microsoft.DotNet.XHarness.CLI.Commands.Wasm
{
internal class WasmBrowserTestRunner
{
private readonly WasmTestBrowserCommandArguments _arguments;
private readonly ILogger _logger;
private readonly IEnumerable<string> _passThroughArguments;
private readonly WasmTestMessagesProcessor _messagesProcessor;
// Messages from selenium prepend the url, and location where the message originated
// Eg. `foo` becomes `http://localhost:8000/xyz.js 0:12 "foo"
static readonly Regex s_consoleLogRegex = new(@"^\s*[a-z]*://[^\s]+\s+\d+:\d+\s+""(.*)""\s*$", RegexOptions.Compiled);
public WasmBrowserTestRunner(WasmTestBrowserCommandArguments arguments, IEnumerable<string> passThroughArguments,
WasmTestMessagesProcessor messagesProcessor, ILogger logger)
{
_arguments = arguments;
_logger = logger;
_passThroughArguments = passThroughArguments;
_messagesProcessor = messagesProcessor;
}
public async Task<ExitCode> RunTestsWithWebDriver(DriverService driverService, IWebDriver driver)
{
var htmlFilePath = Path.Combine(_arguments.AppPackagePath, _arguments.HTMLFile.Value);
if (!File.Exists(htmlFilePath))
{
_logger.LogError($"Could not find html file {htmlFilePath}");
return ExitCode.GENERAL_FAILURE;
}
var cts = new CancellationTokenSource();
try
{
var consolePumpTcs = new TaskCompletionSource<bool>();
ServerURLs serverURLs = await WebServer.Start(
_arguments,
_arguments.AppPackagePath,
_logger,
socket => RunConsoleMessagesPump(socket, consolePumpTcs, cts.Token),
cts.Token);
string testUrl = BuildUrl(serverURLs);
var seleniumLogMessageTask = Task.Run(() => RunSeleniumLogMessagePump(driver, cts.Token), cts.Token);
cts.CancelAfter(_arguments.Timeout);
_logger.LogTrace($"Opening in browser: {testUrl}");
driver.Navigate().GoToUrl(testUrl);
TaskCompletionSource<bool> wasmExitReceivedTcs = _messagesProcessor.WasmExitReceivedTcs;
var tasks = new Task[]
{
wasmExitReceivedTcs.Task,
consolePumpTcs.Task,
seleniumLogMessageTask,
Task.Delay(_arguments.Timeout)
};
if (_arguments.BackgroundThrottling)
{
// throttling only happens when the page is not visible
driver.Manage().Window.Minimize();
}
var task = await Task.WhenAny(tasks).ConfigureAwait(false);
if (task == tasks[^1] || cts.IsCancellationRequested)
{
if (driverService.IsRunning)
{
// Selenium isn't able to kill chrome in this case :/
int pid = driverService.ProcessId;
var p = Process.GetProcessById(pid);
if (p != null)
{
_logger.LogDebug($"Tests timed out. Killing driver service pid {pid}");
p.Kill(true);
}
}
// timed out
if (!cts.IsCancellationRequested)
cts.Cancel();
return ExitCode.TIMED_OUT;
}
if (task == wasmExitReceivedTcs.Task && wasmExitReceivedTcs.Task.IsCompletedSuccessfully)
{
_logger.LogTrace($"Looking for `tests_done` element, to get the exit code");
var testsDoneElement = new WebDriverWait(driver, TimeSpan.FromSeconds(30))
.Until (e => e.FindElement(By.Id("tests_done")));
if (int.TryParse(testsDoneElement.Text, out var code))
{
return (ExitCode) Enum.ToObject(typeof(ExitCode), code);
}
return ExitCode.RETURN_CODE_NOT_SET;
}
if (task.IsFaulted)
{
_logger.LogDebug($"task faulted {task.Exception}");
throw task.Exception!;
}
return ExitCode.TIMED_OUT;
}
finally
{
if (!cts.IsCancellationRequested)
{
cts.Cancel();
}
}
}
private async Task RunConsoleMessagesPump(WebSocket socket, TaskCompletionSource<bool> tcs, CancellationToken token)
{
byte[] buff = new byte[4000];
var mem = new MemoryStream();
try {
while (!token.IsCancellationRequested)
{
if (socket.State != WebSocketState.Open)
{
_logger.LogError($"DevToolsProxy: Socket is no longer open.");
tcs.SetResult(false);
return;
}
WebSocketReceiveResult result = await socket.ReceiveAsync(new ArraySegment<byte>(buff), token).ConfigureAwait(false);
if (result.MessageType == WebSocketMessageType.Close)
{
tcs.SetResult(false);
return;
}
mem.Write(buff, 0, result.Count);
if (result.EndOfMessage)
{
var line = Encoding.UTF8.GetString(mem.GetBuffer(), 0, (int)mem.Length);
line += Environment.NewLine;
_messagesProcessor.Invoke(line);
mem.SetLength(0);
mem.Seek(0, SeekOrigin.Begin);
}
}
// the result is not used
tcs.SetResult(false);
}
catch (OperationCanceledException oce)
{
if (!token.IsCancellationRequested)
_logger.LogDebug($"RunConsoleMessagesPump cancelled: {oce}");
tcs.SetResult(false);
}
catch (Exception ex)
{
tcs.SetException(ex);
throw;
}
}
// This listens for any `console.log` messages.
// Since we pipe messages from managed code, and console.* to the websocket,
// this wouldn't normally get much. But listening on this to catch any messages
// that we miss piping to the websocket.
private void RunSeleniumLogMessagePump(IWebDriver driver, CancellationToken token)
{
try
{
ILogs logs = driver.Manage().Logs;
while (!token.IsCancellationRequested)
{
foreach (var logType in logs.AvailableLogTypes)
{
foreach (var logEntry in logs.GetLog(logType))
{
if (logEntry.Level == SeleniumLogLevel.Severe)
{
// These are errors from the browser, some of which might be
// thrown as part of tests. So, we can't differentiate when
// it is an error that we can ignore, vs one that should stop
// the execution completely.
//
// Note: these could be received out-of-order as compared to
// console messages via the websocket.
//
// (see commit message for more info)
_logger.LogError($"[out of order message from the {logType}]: {logEntry.Message}");
continue;
}
var match = s_consoleLogRegex.Match(Regex.Unescape(logEntry.Message));
string msg = match.Success ? match.Groups[1].Value : logEntry.Message;
_messagesProcessor.Invoke(msg);
}
}
}
}
catch (WebDriverException wde) when (wde.Message.Contains("timed out after"))
{ }
catch (Exception ex)
{
_logger.LogDebug($"Failed trying to read log messages via selenium: {ex}");
throw;
}
}
private string BuildUrl(ServerURLs serverURLs)
{
var uriBuilder = new UriBuilder($"{serverURLs.Http}/{_arguments.HTMLFile}");
var sb = new StringBuilder();
if (_arguments.DebuggerPort.Value != null)
sb.Append($"arg=--debug");
foreach (var envVariable in _arguments.WebServerHttpEnvironmentVariables.Value)
{
if (sb.Length > 0)
sb.Append('&');
sb.Append($"arg={HttpUtility.UrlEncode($"--setenv={envVariable}={serverURLs!.Http}")}");
}
if (_arguments.WebServerUseHttps)
{
foreach (var envVariable in _arguments.WebServerHttpsEnvironmentVariables.Value)
{
if (sb.Length > 0)
sb.Append('&');
sb.Append($"arg={HttpUtility.UrlEncode($"--setenv={envVariable}={serverURLs!.Https}")}");
}
}
foreach (var arg in _passThroughArguments)
{
if (sb.Length > 0)
sb.Append('&');
sb.Append($"arg={HttpUtility.UrlEncode(arg)}");
}
uriBuilder.Query = sb.ToString();
return uriBuilder.ToString();
}
}
}