Skip to content

Commit 0315076

Browse files
authored
Check firefox version automatically (#1776)
1 parent 3e9577a commit 0315076

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

lib/PuppeteerSharp.Tests/FixturesTests/FixturesTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public FixturesTests(ITestOutputHelper output) : base(output) { }
1616

1717
[PuppeteerTest("fixtures.spec.ts", "Fixtures", "should dump browser process stderr")]
1818
[SkipBrowserFact(skipFirefox: true)]
19-
public void ShouldDumpBrowserProcessStderr()
19+
public async Task ShouldDumpBrowserProcessStderr()
2020
{
2121
var success = false;
2222
var process = GetTestAppProcess(
2323
"PuppeteerSharp.Tests.DumpIO",
24-
$"\"{new BrowserFetcher(Product.Chrome).RevisionInfo().ExecutablePath}\"");
24+
$"\"{(await new BrowserFetcher(Product.Chrome).GetRevisionInfoAsync()).ExecutablePath}\"");
2525

2626
process.ErrorDataReceived += (_, e) =>
2727
{
@@ -39,7 +39,7 @@ public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses()
3939
{
4040
var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
4141
var ChromiumLauncher = new ChromiumLauncher(
42-
new BrowserFetcher(Product.Chrome).RevisionInfo().ExecutablePath,
42+
(await new BrowserFetcher(Product.Chrome).GetRevisionInfoAsync()).ExecutablePath,
4343
new LaunchOptions { Headless = true });
4444

4545
await ChromiumLauncher.StartAsync().ConfigureAwait(false);

lib/PuppeteerSharp/BrowserFetcher.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using ICSharpCode.SharpZipLib.BZip2;
1414
using ICSharpCode.SharpZipLib.Tar;
15+
using Newtonsoft.Json;
1516
using PuppeteerSharp.Helpers;
1617
using PuppeteerSharp.Helpers.Linux;
1718

@@ -65,7 +66,7 @@ public class BrowserFetcher
6566
/// <summary>
6667
/// Default Chromium revision.
6768
/// </summary>
68-
public const string DefaultFirefoxRevision = "90.0a1";
69+
public string DefaultFirefoxRevision { get; private set; } = "latest";
6970

7071
/// <summary>
7172
/// Gets the downloads folder.
@@ -215,7 +216,8 @@ public void Remove(string revision)
215216
/// Gets the revision info.
216217
/// </summary>
217218
/// <returns>Revision info.</returns>
218-
public RevisionInfo RevisionInfo() => RevisionInfo(Product == Product.Chrome ? DefaultChromiumRevision : DefaultFirefoxRevision);
219+
public async Task<RevisionInfo> GetRevisionInfoAsync()
220+
=> RevisionInfo(Product == Product.Chrome ? DefaultChromiumRevision : await GetDefaultFirefoxRevisionAsync().ConfigureAwait(false));
219221

220222
/// <summary>
221223
/// Gets the revision info.
@@ -249,7 +251,11 @@ public RevisionInfo RevisionInfo(string revision)
249251
/// Downloads the revision.
250252
/// </summary>
251253
/// <returns>Task which resolves to the completed download.</returns>
252-
public Task<RevisionInfo> DownloadAsync() => DownloadAsync(Product == Product.Chrome ? DefaultChromiumRevision : DefaultFirefoxRevision);
254+
public async Task<RevisionInfo> DownloadAsync()
255+
=> await DownloadAsync(
256+
Product == Product.Chrome
257+
? DefaultChromiumRevision
258+
: await GetDefaultFirefoxRevisionAsync().ConfigureAwait(false)).ConfigureAwait(false);
253259

254260
/// <summary>
255261
/// Downloads the revision.
@@ -564,6 +570,19 @@ private string GetRevisionFromPath(string folderName)
564570
return splits[1];
565571
}
566572

573+
private async Task<string> GetDefaultFirefoxRevisionAsync()
574+
{
575+
if (DefaultFirefoxRevision == "latest")
576+
{
577+
using var client = new HttpClient();
578+
var response = await client.GetStringAsync("https://product-details.mozilla.org/1.0/firefox_versions.json").ConfigureAwait(false);
579+
var version = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
580+
DefaultFirefoxRevision = version["FIREFOX_NIGHTLY"];
581+
}
582+
583+
return DefaultFirefoxRevision;
584+
}
585+
567586
private static string GetArchiveName(Product product, Platform platform, string revision)
568587
{
569588
if (product == Product.Chrome)

lib/PuppeteerSharp/Launcher.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<Browser> LaunchAsync(LaunchOptions options)
4949
{
5050
EnsureSingleLaunchOrConnect();
5151
_product = options.Product;
52-
var executable = GetOrFetchBrowserExecutable(options);
52+
var executable = await GetOrFetchBrowserExecutableAsync(options).ConfigureAwait(false);
5353

5454
Process = options.Product switch
5555
{
@@ -146,7 +146,7 @@ private async Task<string> GetWSEndpointAsync(string browserURL)
146146
/// Gets the executable path.
147147
/// </summary>
148148
/// <returns>The executable path.</returns>
149-
public string GetExecutablePath() => ResolveExecutablePath();
149+
public Task<string> GetExecutablePathAsync() => ResolveExecutablePathAsync();
150150

151151
#endregion
152152

@@ -162,13 +162,13 @@ private void EnsureSingleLaunchOrConnect()
162162
_processLaunched = true;
163163
}
164164

165-
private string GetOrFetchBrowserExecutable(LaunchOptions options)
165+
private async Task<string> GetOrFetchBrowserExecutableAsync(LaunchOptions options)
166166
{
167167
var browserExecutable = options.ExecutablePath;
168168

169169
if (string.IsNullOrEmpty(browserExecutable))
170170
{
171-
browserExecutable = ResolveExecutablePath();
171+
browserExecutable = await ResolveExecutablePathAsync().ConfigureAwait(false);
172172
}
173173

174174
if (!File.Exists(browserExecutable))
@@ -178,7 +178,7 @@ private string GetOrFetchBrowserExecutable(LaunchOptions options)
178178
return browserExecutable;
179179
}
180180

181-
private string ResolveExecutablePath()
181+
private async Task<string> ResolveExecutablePathAsync()
182182
{
183183
var executablePath = Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH");
184184

@@ -204,7 +204,7 @@ private string ResolveExecutablePath()
204204
}
205205
return revisionInfo.ExecutablePath;
206206
}
207-
revisionInfo = browserFetcher.RevisionInfo();
207+
revisionInfo = await browserFetcher.GetRevisionInfoAsync().ConfigureAwait(false);
208208
if (!revisionInfo.Local)
209209
{
210210
throw new FileNotFoundException("Process revision is not downloaded. Run BrowserFetcher.DownloadAsync or download the process manually", revisionInfo.ExecutablePath);

lib/PuppeteerSharp/PuppeteerSharp.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
<Description>Headless Browser .NET API</Description>
1414
<PackageId>PuppeteerSharp</PackageId>
1515
<PackageReleaseNotes></PackageReleaseNotes>
16-
<PackageVersion>4.0.0</PackageVersion>
17-
<ReleaseVersion>4.0.0</ReleaseVersion>
18-
<AssemblyVersion>4.0.0.0</AssemblyVersion>
19-
<FileVersion>4.0.0.0</FileVersion>
16+
<PackageVersion>5.0.0</PackageVersion>
17+
<ReleaseVersion>5.0.0</ReleaseVersion>
18+
<AssemblyVersion>5.0.0.0</AssemblyVersion>
19+
<FileVersion>5.0.0.0</FileVersion>
2020
<SynchReleaseVersion>false</SynchReleaseVersion>
2121
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
2222
<CodeAnalysisRuleSet>../PuppeteerSharp.ruleset</CodeAnalysisRuleSet>

0 commit comments

Comments
 (0)