Skip to content

Commit

Permalink
Implement BrowserFetcher (hardkoded#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Jun 26, 2018
1 parent 73cce69 commit c65c6d8
Show file tree
Hide file tree
Showing 25 changed files with 438 additions and 280 deletions.
2 changes: 1 addition & 1 deletion ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ E.g.
```csharp
// Arrange
var options = new LaunchOptions { /* */ };
var chromiumRevision = Downloader.DefaultRevision;
var chromiumRevision = BrowserFetcher.DefaultRevision;
var browser = await Puppeteer.LaunchAsync(options, chromiumRevision);
var page = browser.NewPageAsync();

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g
## Take screenshots

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await Downloader.CreateDefault().DownloadAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
Expand All @@ -50,11 +50,11 @@ await page.SetViewport(new ViewPortOptions
## Generate PDF files

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.PdfAsync(outputFile);
Expand Down
4 changes: 2 additions & 2 deletions demos/PuppeteerSharpPdfDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static async Task Main(string[] args)
};

Console.WriteLine("Downloading chromium");
await Downloader.CreateDefault().DownloadRevisionAsync(Downloader.DefaultRevision);
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

Console.WriteLine("Navigating google");
using (var browser = await Puppeteer.LaunchAsync(options, Downloader.DefaultRevision))
using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
await page.GoToAsync("http://www.google.com");
Expand Down
8 changes: 4 additions & 4 deletions docfx_project/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g
## Take screenshots

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await Downloader.CreateDefault().DownloadAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
Expand All @@ -32,11 +32,11 @@ await page.SetViewport(new ViewPortOptions
## Generate PDF files

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.PdfAsync(outputFile);
Expand Down
8 changes: 4 additions & 4 deletions docfx_project/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g
## Take screenshots

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await Downloader.CreateDefault().DownloadAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
Expand All @@ -32,11 +32,11 @@ await page.SetViewport(new ViewPortOptions
## Generate PDF files

```cs
await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
}, chromiumRevision);
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.PdfAsync(outputFile);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public DisconnectedTests(ITestOutputHelper output) : base(output)
[Fact]
public async Task ShouldEmittedWhenBrowserGetsClosedDisconnectedOrUnderlyingWebsocketGetsClosed()
{
var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.ChromiumRevision, TestConstants.LoggerFactory);
var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.LoggerFactory);
var connectOptions = new ConnectOptions { BrowserWSEndpoint = originalBrowser.WebSocketEndpoint };
var remoteBrowser1 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);
var remoteBrowser2 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);
Expand All @@ -37,4 +37,4 @@ public async Task ShouldEmittedWhenBrowserGetsClosedDisconnectedOrUnderlyingWebs
Assert.Equal(1, disconnectedRemote2);
}
}
}
}
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/Issues/Issue0128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ await Assert.ThrowsAsync<ChromeProcessException>(async () =>
{
var options = TestConstants.DefaultBrowserOptions();
options.Args = new[] { "-remote-debugging-port=-2" };
await Puppeteer.LaunchAsync(options, TestConstants.ChromiumRevision, TestConstants.LoggerFactory);
await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory);
});
}
}
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/PuppeteerBrowserBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PuppeteerBrowserBaseTest(ITestOutputHelper output) : base(output)

protected virtual async Task InitializeAsync()
{
Browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.ChromiumRevision, TestConstants.LoggerFactory);
Browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.LoggerFactory);
}

protected virtual async Task DisposeAsync() => await Browser.CloseAsync();
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/PuppeteerLoaderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Dispose()

private async Task SetupAsync()
{
var downloaderTask = Downloader.CreateDefault().DownloadRevisionAsync(TestConstants.ChromiumRevision);
var downloaderTask = new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

Server = SimpleServer.Create(TestConstants.Port, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer"));
HttpsServer = SimpleServer.CreateHttps(TestConstants.HttpsPort, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer"));
Expand All @@ -32,4 +32,4 @@ private async Task SetupAsync()
await Task.WhenAll(downloaderTask, serverStart, httpsServerStart);
}
}
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/PuppeteerTests/AppModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task ShouldWork()
var options = TestConstants.DefaultBrowserOptions();
options.AppMode = true;

using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.ChromiumRevision, TestConstants.LoggerFactory))
using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
using (var page = await browser.NewPageAsync())
{
Assert.Equal(121, await page.EvaluateExpressionAsync<int>("11 * 11"));
Expand Down
42 changes: 42 additions & 0 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/BrowserFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.PuppeteerTests
{
[Collection("PuppeteerLoaderFixture collection")]
public class BrowserFetcherTests : PuppeteerBaseTest
{
public BrowserFetcherTests(ITestOutputHelper output) : base(output) { }

[Fact]
public async Task ShouldDownloadAndExtractLinuxBinary()
{
var downloadsFolder = Path.Combine(Directory.GetCurrentDirectory(), ".test-chromium");
var browserFetcher = Puppeteer.CreateBrowserFetcher(new BrowserFetcherOptions
{
Platform = Platform.Linux,
Path = downloadsFolder,
Host = TestConstants.ServerUrl
});
var revisionInfo = browserFetcher.RevisionInfo(123456);

Server.SetRedirect(revisionInfo.Url.Substring(TestConstants.ServerUrl.Length), "/chromium-linux.zip");
Assert.False(revisionInfo.Local);
Assert.Equal(Platform.Linux, revisionInfo.Platform);
Assert.False(await browserFetcher.CanDownloadAsync(100000));
Assert.True(await browserFetcher.CanDownloadAsync(123456));

revisionInfo = await browserFetcher.DownloadAsync(123456);
Assert.True(revisionInfo.Local);
Assert.Equal("LINUX BINARY\n", File.ReadAllText(revisionInfo.ExecutablePath));
Assert.Equal(new[] { 123456 }, browserFetcher.LocalRevisions());
browserFetcher.Remove(123456);
Assert.Empty(browserFetcher.LocalRevisions());

new DirectoryInfo(downloadsFolder).Delete(true);
}
}
}
26 changes: 0 additions & 26 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/DownloaderTests.cs

This file was deleted.

Loading

0 comments on commit c65c6d8

Please sign in to comment.