Skip to content

Commit 7a34cdf

Browse files
authored
[wasm] test-browser: Add support for Edge (#425)
The code for Edge, and Chrome is pretty much the same, except for the concrete types, so sharing all that.
1 parent ee50e1b commit 7a34cdf

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

src/Microsoft.DotNet.XHarness.CLI/CommandArguments/WASM/WasmTestBrowserCommandArguments.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ internal enum Browser
2828
/// <summary>
2929
/// Firefox
3030
/// </summary>
31-
Firefox
31+
Firefox,
32+
33+
/// <summary>
34+
/// Edge
35+
/// </summary>
36+
Edge
3237
}
3338

3439
internal class WasmTestBrowserCommandArguments : TestCommandArguments

src/Microsoft.DotNet.XHarness.CLI/Commands/WASM/Browser/WasmTestBrowserCommand.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
using SeleniumLogLevel = OpenQA.Selenium.LogLevel;
2121
using OpenQA.Selenium;
2222
using System.Linq;
23+
using OpenQA.Selenium.Edge;
24+
using System.Runtime.InteropServices;
25+
using OpenQA.Selenium.Chromium;
2326

2427
namespace Microsoft.DotNet.XHarness.CLI.Commands.Wasm
2528
{
@@ -59,6 +62,7 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
5962
Browser.Chrome => GetChromeDriver(logger),
6063
Browser.Safari => GetSafariDriver(logger),
6164
Browser.Firefox => GetFirefoxDriver(logger),
65+
Browser.Edge => GetEdgeDriver(logger),
6266

6367
// shouldn't reach here
6468
_ => throw new ArgumentException($"Unknown browser : {_arguments.Browser}")
@@ -105,8 +109,27 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
105109
}
106110

107111
private (DriverService, IWebDriver) GetChromeDriver(ILogger logger)
112+
=> GetChromiumDriver<ChromeOptions, ChromeDriver, ChromeDriverService>(
113+
"chromedriver",
114+
options => ChromeDriverService.CreateDefaultService(),
115+
logger);
116+
117+
private (DriverService, IWebDriver) GetEdgeDriver(ILogger logger)
118+
=> GetChromiumDriver<EdgeOptions, EdgeDriver, EdgeDriverService>(
119+
"edgedriver",
120+
options =>
121+
{
122+
options.UseChromium = true;
123+
return EdgeDriverService.CreateDefaultServiceFromOptions(options);
124+
}, logger);
125+
126+
private (DriverService, IWebDriver) GetChromiumDriver<TDriverOptions, TDriver, TDriverService>(
127+
string driverName, Func<TDriverOptions, TDriverService> getDriverService, ILogger logger)
128+
where TDriver: ChromiumDriver
129+
where TDriverOptions: ChromiumOptions
130+
where TDriverService: ChromiumDriverService
108131
{
109-
var options = new ChromeOptions();
132+
var options = Activator.CreateInstance<TDriverOptions>();
110133
options.SetLoggingPreference(LogType.Browser, SeleniumLogLevel.All);
111134

112135
options.AddArguments(new List<string>(_arguments.BrowserArgs)
@@ -129,7 +152,7 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
129152
"--metrics-recording-only"
130153
});
131154

132-
logger.LogInformation($"Starting chromedriver with args: {string.Join(' ', options.Arguments)}");
155+
logger.LogInformation($"Starting {driverName} with args: {string.Join(' ', options.Arguments)}");
133156

134157
// We want to explicitly specify a timeout here. This is for for the
135158
// driver commands, like getLog. The default is 60s, which ends up
@@ -147,22 +170,26 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
147170
"Cannot start the driver service"
148171
};
149172

150-
foreach (var file in Directory.EnumerateFiles(_arguments.OutputDirectory, "chromedriver-*.log"))
173+
foreach (var file in Directory.EnumerateFiles(_arguments.OutputDirectory, $"{driverName}-*.log"))
151174
File.Delete(file);
152175

153176
int max_retries = 3;
154177
int retry_num = 0;
155178
while(true)
156179
{
157-
ChromeDriverService? driverService = null;
180+
TDriverService? driverService = null;
158181
try
159182
{
160-
driverService = ChromeDriverService.CreateDefaultService();
183+
driverService = getDriverService(options);
184+
161185
driverService.EnableAppendLog = false;
162186
driverService.EnableVerboseLogging = true;
163-
driverService.LogPath = Path.Combine(_arguments.OutputDirectory, $"chromedriver-{retry_num}.log");
187+
driverService.LogPath = Path.Combine(_arguments.OutputDirectory, $"{driverName}-{retry_num}.log");
188+
189+
if (!(Activator.CreateInstance(typeof(TDriver), driverService, options, _arguments.Timeout) is TDriver driver))
190+
throw new ArgumentException($"Failed to create instance of {typeof(TDriver)}");
164191

165-
return (driverService, new ChromeDriver(driverService, options, _arguments.Timeout));
192+
return (driverService, driver);
166193
}
167194
catch (WebDriverException wde) when (err_snippets.Any(s => wde.Message.Contains(s)) && retry_num < max_retries - 1)
168195
{
@@ -173,7 +200,7 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
173200
// (chrome not reachable)
174201

175202
// Log on max-1 tries, and rethrow on the last one
176-
logger.LogWarning($"Failed to start chrome, attempt #{retry_num}: {wde}");
203+
logger.LogWarning($"Failed to start the browser, attempt #{retry_num}: {wde}");
177204

178205
driverService?.Dispose();
179206
}

0 commit comments

Comments
 (0)