public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses() { var browserClosedTaskWrapper = new TaskCompletionSource <bool>(); var chromiumProcess = new ChromiumProcess( new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath, new LaunchOptions { Headless = true }, TestConstants.LoggerFactory); await chromiumProcess.StartAsync().ConfigureAwait(false); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = chromiumProcess.EndPoint }); browser.Disconnected += (sender, e) => { browserClosedTaskWrapper.SetResult(true); }; KillProcess(chromiumProcess.Process.Id); await browserClosedTaskWrapper.Task; Assert.True(browser.IsClosed); }
public async Task ShouldBeAbleToReconnectToADisconnectedBrowser() { var options = new ConnectOptions() { BrowserWSEndpoint = Browser.WebSocketEndpoint }; var url = TestConstants.ServerUrl + "/frames/nested-frames.html"; var page = await Browser.NewPageAsync(); await page.GoToAsync(url); Browser.Disconnect(); await using (var browser = await Puppeteer.ConnectAsync(options, TestConstants.LoggerFactory)) { var pages = (await browser.PagesAsync()).ToList(); var restoredPage = pages.FirstOrDefault(x => x.Url == url); Assert.NotNull(restoredPage); var frameDump = FrameUtils.DumpFrames(restoredPage.MainFrame); Assert.Equal(TestConstants.NestedFramesDumpResult, frameDump); var response = await restoredPage.EvaluateExpressionAsync <int>("7 * 8"); Assert.Equal(56, response); } }
public static async Task <string> ProcessVideo( [ActivityTrigger] DurableActivityContext ctx, TraceWriter log ) { var url = ctx.GetInput <string>(); var asm = Assembly.Load("Microsoft.Extensions.Options, Culture=neutral, PublicKeyToken=adb9793829ddae60"); PuppeteerSharp.Browser browser = null; try { browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = Constants.BrowserWSEndpoint }); var page = await browser.NewPageAsync(); await page.GoToAsync(url, new NavigationOptions { Timeout = 4000 }); } catch (Exception ex) { return(ex.Message); } finally { if (browser != null) { browser.Disconnect(); } } return($"Processed {url} shit all."); }
public async Task ShouldTerminateNetworkWaiters() { using (var browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions())) using (var remote = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browser.WebSocketEndpoint })) { var newPage = await remote.NewPageAsync(); var requestTask = newPage.WaitForRequestAsync(TestConstants.EmptyPage); var responseTask = newPage.WaitForResponseAsync(TestConstants.EmptyPage); await browser.CloseAsync(); var exception = await Assert.ThrowsAsync <TargetClosedException>(() => requestTask); Assert.Contains("Target closed", exception.Message); Assert.DoesNotContain("Timeout", exception.Message); exception = await Assert.ThrowsAsync <TargetClosedException>(() => responseTask); Assert.Contains("Target closed", exception.Message); Assert.DoesNotContain("Timeout", exception.Message); } }
public async Task ShouldEmittedWhenBrowserGetsClosedDisconnectedOrUnderlyingWebsocketGetsClosed() { var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.ChromiumRevision, 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); var disconnectedOriginal = 0; var disconnectedRemote1 = 0; var disconnectedRemote2 = 0; originalBrowser.Disconnected += (sender, e) => ++ disconnectedOriginal; remoteBrowser1.Disconnected += (sender, e) => ++ disconnectedRemote1; remoteBrowser2.Disconnected += (sender, e) => ++ disconnectedRemote2; remoteBrowser2.Disconnect(); Assert.Equal(0, disconnectedOriginal); Assert.Equal(0, disconnectedRemote1); Assert.Equal(1, disconnectedRemote2); await originalBrowser.CloseAsync(); Assert.Equal(1, disconnectedOriginal); Assert.Equal(1, disconnectedRemote1); Assert.Equal(1, disconnectedRemote2); }
private async Task <string> FillHtmlCache(string address) { Browser browser = null; try { browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = "wss://chrome.browserless.io?token=" + _configuration["Browserless:Token"] }); var page = await browser.NewPageAsync(); await page.GoToAsync(address); await page.WaitForNavigationAsync(); await page.EvaluateExpressionAsync <string>( $"const base = document.createElement('base'); base.href = '{address}'; document.head.prepend(base);" + "const elements = document.querySelectorAll('script, link[rel=\"import\"]'); elements.forEach(e => e.remove());" + "const cssText = Array.from(document.querySelector('#react-native-stylesheet').sheet.cssRules).reduce((prev, cssRule) => prev + cssRule.cssText, '');" + "const style = document.createElement('style'); style.innerText = cssText; document.head.prepend(style);"); var content = await page.GetContentAsync(); return(content); } finally { if (browser != null) { await browser.CloseAsync(); } } }
public async Task ShouldBeAbleToConnectToTheSamePageSimultaneously() { var browserOne = await Puppeteer.LaunchAsync(new LaunchOptions()); var browserTwo = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browserOne.WebSocketEndpoint }); var tcs = new TaskCompletionSource <Page>(); async void TargetCreated(object sender, TargetChangedArgs e) { tcs.TrySetResult(await e.Target.PageAsync()); browserOne.TargetCreated -= TargetCreated; } browserOne.TargetCreated += TargetCreated; var page2Task = browserTwo.NewPageAsync(); await Task.WhenAll(tcs.Task, page2Task); var page1 = tcs.Task.Result; var page2 = page2Task.Result; Assert.Equal(56, await page1.EvaluateExpressionAsync <int>("7 * 8")); Assert.Equal(42, await page2.EvaluateExpressionAsync <int>("7 * 6")); await browserOne.CloseAsync(); }
public async Task ShouldSupportIgnoreHTTPSErrorsOption() { await using (var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions())) await using (var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = originalBrowser.WebSocketEndpoint, IgnoreHTTPSErrors = true })) await using (var page = await browser.NewPageAsync()) { var requestTask = HttpsServer.WaitForRequest( "/empty.html", request => request.HttpContext.Features.Get <ITlsHandshakeFeature>().Protocol); var responseTask = page.GoToAsync(TestConstants.HttpsPrefix + "/empty.html"); await Task.WhenAll( requestTask, responseTask); var response = responseTask.Result; Assert.True(response.Ok); Assert.NotNull(response.SecurityDetails); Assert.Equal( TestUtils.CurateProtocol(requestTask.Result.ToString()), TestUtils.CurateProtocol(response.SecurityDetails.Protocol)); } }
public async Task ShouldSupportTargetFilter() { await using (var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions())) { var page1 = await originalBrowser.NewPageAsync(); await page1.GoToAsync(TestConstants.EmptyPage); var page2 = await originalBrowser.NewPageAsync(); await page2.GoToAsync(TestConstants.EmptyPage + "?should-be-ignored"); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = originalBrowser.WebSocketEndpoint, TargetFilter = (TargetInfo targetInfo) => !targetInfo.Url.Contains("should-be-ignored"), }); var pages = await browser.PagesAsync(); await page2.CloseAsync(); await page1.CloseAsync(); await browser.CloseAsync(); Assert.Equal( new string[] { "about:blank", TestConstants.EmptyPage }, pages.Select((Page p) => p.Url).OrderBy(t => t)); } }
public async Task ShouldCloseTheBrowserWhenTheProcessCloses() { var process = GetTestAppProcess( "PuppeteerSharp.Tests.CloseMe", $"\"{new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath}\""); var webSocketTaskWrapper = new TaskCompletionSource <string>(); var browserClosedTaskWrapper = new TaskCompletionSource <bool>(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.OutputDataReceived += (sender, e) => webSocketTaskWrapper.TrySetResult(e.Data); process.Start(); process.BeginOutputReadLine(); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = await webSocketTaskWrapper.Task }); browser.Disconnected += (sender, e) => { browserClosedTaskWrapper.SetResult(true); }; KillProcess(process.Id); await browserClosedTaskWrapper.Task; Assert.True(process.HasExited); }
public async Task ShouldBeAbleToReconnect() { var browserOne = await Puppeteer.LaunchAsync(new LaunchOptions()); var browserWSEndpoint = browserOne.WebSocketEndpoint; var page1 = await browserOne.NewPageAsync(); await page1.GoToAsync(TestConstants.EmptyPage); browserOne.Disconnect(); var browserTwo = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint }); var pages = await browserTwo.PagesAsync(); var pageTwo = pages.First(page => page.Url == TestConstants.EmptyPage); await pageTwo.ReloadAsync(); var bodyHandle = await pageTwo.WaitForSelectorAsync("body", new WaitForSelectorOptions { Timeout = 10000 }); await bodyHandle.DisposeAsync(); await browserTwo.CloseAsync(); }
public static async Task <string> Authenticate( [ActivityTrigger] DurableActivityContext ctx, TraceWriter log) { var credentials = ctx.GetInput <string>(); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = Constants.BrowserWSEndpoint }); var page = await browser.NewPageAsync(); var login = new UIAction.Login(); CookieParam[] cookies = null; cookies = await login.RunAsync(page, log); // page is closed implicitly try { browser.Disconnect(); } catch { } if (cookies != null) { return(CookieConverter.EncodeCookie(cookies)); } throw new Exception("Failed to authenticate."); }
public static async Task <int> QueryGuru( [ActivityTrigger] DurableActivityContext ctx, TraceWriter log) { var arguments = ctx.GetInput <Tuple <string, string> >(); var searchargs = arguments.Item1.Split(':'); if (searchargs.Length != 2) { throw new ArgumentException("Activity.QueryGuru: Expected search query to be in format <query>:<type>"); } var query = searchargs[0]; var type = searchargs[1]; var cookies = CookieConverter.DecodeCookie(arguments.Item2); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = Constants.BrowserWSEndpoint }); var page = await browser.NewPageAsync(); await page.SetCookieAsync(cookies); var search = new UIAction.NavigateSearch(query, type); var pages = await search.RunAsync(page); browser.Disconnect(); return(pages); }
public async Task ShouldRejectNavigationWhenBrowserCloses() { Server.SetRoute("/one-style.css", context => Task.Delay(10000)); using (var browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions())) { var remote = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browser.WebSocketEndpoint }); var page = await remote.NewPageAsync(); var navigationTask = page.GoToAsync(TestConstants.ServerUrl + "/one-style.html", new NavigationOptions { Timeout = 60000 }); await Server.WaitForRequest("/one-style.css"); remote.Disconnect(); var exception = await Assert.ThrowsAsync <NavigationException>(() => navigationTask); Assert.Contains("Navigation failed because browser has disconnected!", exception.Message); } }
public async Task ShouldRejectWaitForSelectorWhenBrowserCloses() { Server.SetRoute("/empty.html", context => Task.Delay(10000)); using (var browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions())) { var remote = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browser.WebSocketEndpoint }); var page = await remote.NewPageAsync(); var watchdog = page.WaitForSelectorAsync("div", new WaitForSelectorOptions { Timeout = 60000 }); remote.Disconnect(); var exception = await Assert.ThrowsAsync <EvaluationFailedException>(() => watchdog); //Using the type instead of the message because the exception could come //Whether from the Connection rejecting a message from the CDPSession //or from the CDPSession trying to send a message to a closed connection Assert.IsType <TargetClosedException>(exception.InnerException); Assert.Equal("Connection disposed", ((TargetClosedException)exception.InnerException).CloseReason); } }
public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses() { var browserClosedTaskWrapper = new TaskCompletionSource <bool>(); var ChromiumLauncher = new ChromiumLauncher( new BrowserFetcher(Product.Chrome).RevisionInfo().ExecutablePath, new LaunchOptions { Headless = true }); await ChromiumLauncher.StartAsync().ConfigureAwait(false); var browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = ChromiumLauncher.EndPoint }); browser.Disconnected += (_, _) => { browserClosedTaskWrapper.SetResult(true); }; KillProcess(ChromiumLauncher.Process.Id); await browserClosedTaskWrapper.Task; Assert.True(browser.IsClosed); }
private static async Task <string> TestPuppeteerSharp() { RunnerBase runnerInfo = new 汽车之家(); var chrom = @".local-chromium\Win64-735830\chrome.exe"; if (File.Exists(chrom)) { Console.WriteLine("test"); } var launchOptions = new LaunchOptions { Headless = false, IgnoreHTTPSErrors = true, ExecutablePath = chrom, // Args = new[] { //"--proxy-server=127.0.0.1:50376", //"--no-sandbox", //"--disable-infobars",不行啊, //"--disable-setuid-sandbox", //"--ignore-certificate-errors", //}, }; Browser browser = null; Page page = null; if (!runnerInfo.IsConnect) { browser = await Puppeteer.LaunchAsync(launchOptions); //New tab page var browserWSEndpoint = browser.WebSocketEndpoint; File.WriteAllText("WebSocketEndpoint.txt", browserWSEndpoint); page = await browser.NewPageAsync(); } else { var browserWSEndpoint = File.ReadAllText("WebSocketEndpoint.txt"); browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint }); page = await browser.NewPageAsync(); } await runnerInfo.GetDatas(page); #region Dispose resources //Close tab page await page.CloseAsync(); //Close headless browser, all pages will be closed here. await browser.CloseAsync(); #endregion Dispose resources return(string.Empty); }
public async Task ShouldNotReturnChildProcessForRemoteBrowser() { var browserWSEndpoint = Browser.WebSocketEndpoint; var remoteBrowser = await Puppeteer.ConnectAsync( new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint }, TestConstants.LoggerFactory); Assert.Null(remoteBrowser.Process); remoteBrowser.Disconnect(); }
public async Task <Stream> CreatePdfFromUrlAsync(string url, Configuration.PdfOptions pdfOptions) { using (var browser = await Puppeteer.ConnectAsync(GetConnectionOptions())) using (var page = await browser.NewPageAsync()) { await page.GoToAsync(url); return(await page.PdfStreamAsync(_converter.Convert(pdfOptions))); } }
public override async Task <Browser> NewBrowser() { Logger.Debug("Launch browser"); var options = new ConnectOptions() { BrowserWSEndpoint = BrowserWSEndpoint }; return(await Puppeteer.ConnectAsync(options)); }
public async Task ShouldSetTheBrowserConnectedState() { var newBrowser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = Browser.WebSocketEndpoint }); Assert.True(newBrowser.IsConnected); newBrowser.Disconnect(); Assert.False(newBrowser.IsConnected); }
private async Task PreparePage() { Browser browser = await Puppeteer.ConnectAsync(new ConnectOptions() { BrowserWSEndpoint = WebSocket, IgnoreHTTPSErrors = true }); Page = await browser.NewPageAsync(); await Page.SetViewportAsync(new ViewPortOptions() { Width = 1920, Height = 1080 }); await Page.SetRequestInterceptionAsync(true); // Intercepting the page seems to finish it prematurely Page.Request += async(sender, e) => { //try //{ switch (e.Request.ResourceType) { case ResourceType.Font: case ResourceType.EventSource: case ResourceType.Image: case ResourceType.Manifest: case ResourceType.Media: case ResourceType.Other: case ResourceType.Ping: case ResourceType.TextTrack: case ResourceType.Unknown: await e.Request.AbortAsync(); break; case ResourceType.StyleSheet: case ResourceType.Document: case ResourceType.Fetch: case ResourceType.Script: case ResourceType.WebSocket: case ResourceType.Xhr: default: await e.Request.ContinueAsync(); break; } //} //catch (Exception ex) //{ // Console.WriteLine($"Error => {ex.Message}"); // await e.Request.ContinueAsync(); //} }; }
private Task <Browser> ConnectBrowserAsync() { var options = new ConnectOptions { BrowserWSEndpoint = _settings.Token != null ? $"{_settings.BrowserWsEndpoint}?token={_settings.Token}" : _settings.BrowserWsEndpoint, }; return(Puppeteer.ConnectAsync(options, _loggerFactory)); }
public async Task <Stream> CreatePdfAsync(string html, Configuration.PdfOptions pdfOptions) { using (var browser = await Puppeteer.ConnectAsync(GetConnectionOptions())) using (var page = await browser.NewPageAsync()) { await Task.WhenAll(page.SetContentAsync(html), page.SetCacheEnabledAsync(false)); return(await page.PdfStreamAsync(_converter.Convert(pdfOptions))); } }
public async Task ShouldReturnProcessInstance() { var process = Browser.Process; Assert.True(process.Id > 0); var browserWSEndpoint = Browser.WebSocketEndpoint; var remoteBrowser = await Puppeteer.ConnectAsync( new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint }, TestConstants.LoggerFactory); Assert.Null(remoteBrowser.Process); remoteBrowser.Disconnect(); }
public async Task <Browser> ConnectAsync(ConnectOptions options) { _plugins.ForEach(e => e.BeforeConnect(options)); var browser = await Puppeteer.ConnectAsync(options); _plugins.ForEach(e => e.AfterConnect(browser)); await OnStart(new BrowserStartContext() { StartType = StartType.Connect }, browser); return(browser); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log, Microsoft.Azure.WebJobs.ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); string url = req.Query["url"]; if (url == null) { return(new BadRequestObjectResult("Please pass a name on the query string")); } else { string apikey = config["browserlessApiKey"]; var options = new ConnectOptions() { BrowserWSEndpoint = $"wss://chrome.browserless.io?token={apikey}" }; var browser = await Puppeteer.ConnectAsync(options); var page = await browser.NewPageAsync(); await page.GoToAsync(url); var stream = await page.ScreenshotStreamAsync(new ScreenshotOptions { FullPage = true }); byte[] bytesInStream = new byte[stream.Length]; stream.Read(bytesInStream, 0, bytesInStream.Length); stream.Dispose(); await page.CloseAsync(); browser.Disconnect(); return(new FileContentResult(bytesInStream, "image/png")); } }
public async Task ShouldThrowWhenTryingToConnectToNonExistingBrowser() { var options = TestConstants.DefaultBrowserOptions(); options.Args = new string[] { "--remote-debugging-port=21222" }; var originalBrowser = await Puppeteer.LaunchAsync(options); var browserURL = "http://127.0.0.1:2122"; await Assert.ThrowsAsync <ChromiumProcessException>(() => Puppeteer.ConnectAsync(new ConnectOptions { BrowserURL = browserURL })); await originalBrowser.CloseAsync(); }
public async Task ShouldBeAbleToCloseRemoteBrowser() { var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions()); var remoteBrowser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = originalBrowser.WebSocketEndpoint }); var tcsDisconnected = new TaskCompletionSource <bool>(); originalBrowser.Disconnected += (_, _) => tcsDisconnected.TrySetResult(true); await Task.WhenAll( tcsDisconnected.Task, remoteBrowser.CloseAsync()); }
public async Task ShouldBeAbleToSetBrowserPropertiesUsingConnectOptions() { var initActionExecuted = false; var options = new ConnectOptions { BrowserWSEndpoint = Browser.WebSocketEndpoint, InitAction = brw => { initActionExecuted = true; } }; var browser = await Puppeteer.ConnectAsync(options, TestConstants.LoggerFactory); Assert.True(initActionExecuted); await browser.CloseAsync(); }