-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEdgeWebDriverUpdater.cs
105 lines (74 loc) · 4.15 KB
/
EdgeWebDriverUpdater.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
using Gsemac.Collections;
using Gsemac.IO.Logging;
using Gsemac.Net.Http;
using Gsemac.Net.Http.Extensions;
using Gsemac.Net.WebBrowsers;
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
namespace Gsemac.Net.WebDrivers {
public class EdgeWebDriverUpdater :
WebDriverUpdaterBase {
// Public members
public EdgeWebDriverUpdater() :
this(Logger.Null) {
}
public EdgeWebDriverUpdater(ILogger logger) :
this(WebDriverUpdaterOptions.Default, logger) {
}
public EdgeWebDriverUpdater(IWebDriverUpdaterOptions webDriverUpdaterOptions) :
this(webDriverUpdaterOptions, Logger.Null) {
}
public EdgeWebDriverUpdater(IWebDriverUpdaterOptions webDriverUpdaterOptions, ILogger logger) :
this(HttpWebRequestFactory.Default, webDriverUpdaterOptions, logger) {
}
public EdgeWebDriverUpdater(IHttpWebRequestFactory webRequestFactory, IWebDriverUpdaterOptions webDriverUpdaterOptions) :
this(webRequestFactory, webDriverUpdaterOptions, Logger.Null) {
}
public EdgeWebDriverUpdater(IHttpWebRequestFactory webRequestFactory, IWebDriverUpdaterOptions webDriverUpdaterOptions, ILogger logger) :
base(webRequestFactory, new WebDriverUpdaterOptions(webDriverUpdaterOptions) { WebBrowserId = WebBrowserId.MicrosoftEdge }, logger) {
this.webRequestFactory = webRequestFactory;
}
// Protected members
protected override Uri GetWebDriverUri(IWebBrowserInfo webBrowserInfo) {
if (webBrowserInfo is null)
throw new ArgumentNullException(nameof(webBrowserInfo));
// Note that Edge and EdgeHTML use different web drivers-- MicrosoftEdgeDriver.exe and msedgedriver.exe, respectively.
// The latest version of EdgeHTML is 18.19041, so that can be used to determine which web driver to download.
// Currently, this method only gets web driver URIs for Edge, and not EdgeHTML.
string edgeMajorVersionStr = webBrowserInfo.Version.Major.ToString(CultureInfo.InvariantCulture);
// We'll get an XML document listing all web driver versions available for this version of Edge.
// There might not be one for this specific version, so we'll pick the closest.
using (IWebClient client = webRequestFactory.CreateWebClient()) {
int maxResults = 999;
string responseXml = client.DownloadString($"https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver?prefix={edgeMajorVersionStr}&delimiter=%2F&maxresults={maxResults}&restype=container&comp=list");
string webDriverVersionStr = Regex.Matches(responseXml, @"<Name>([\d.]+)", RegexOptions.IgnoreCase).Cast<Match>()
.Select(m => m.Groups[1].Value) // version strings
.OrderByDescending(versionString => CountMatchingRevisions(versionString, edgeMajorVersionStr))
.ThenByDescending(versionString => versionString, new NaturalSortComparer())
.FirstOrDefault();
if (string.IsNullOrWhiteSpace(webDriverVersionStr))
webDriverVersionStr = edgeMajorVersionStr;
return new Uri($"https://msedgedriver.azureedge.net/{webDriverVersionStr}/edgedriver_{GetPlatformOS()}.zip");
}
}
protected override string GetWebDriverExecutablePath() {
return WebDriverUtilities.EdgeDriverExecutablePath;
}
// Private members
private readonly IHttpWebRequestFactory webRequestFactory;
private int CountMatchingRevisions(string versionString1, string versionString2) {
return versionString1.Split('.')
.Zip(versionString2.Split('.'), (n, m) => Tuple.Create(n, m))
.Where(pair => pair.Item1.Equals(pair.Item2))
.Count();
}
private string GetPlatformOS() {
// For now, we'll focus on Windows.
return Environment.Is64BitOperatingSystem ?
"win64" :
"win32";
}
}
}