private static string ResolveExecutablePath() { var executablePath = Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH"); if (!string.IsNullOrEmpty(executablePath)) { if (!File.Exists(executablePath)) { throw new FileNotFoundException("Tried to use PUPPETEER_EXECUTABLE_PATH env variable to launch browser but did not find any executable", executablePath); } return(executablePath); } var browserFetcher = new BrowserFetcher(); var revision = Environment.GetEnvironmentVariable("PUPPETEER_CHROMIUM_REVISION"); RevisionInfo revisionInfo; if (!string.IsNullOrEmpty(revision) && int.TryParse(revision, out var revisionNumber)) { revisionInfo = browserFetcher.RevisionInfo(revisionNumber); if (!revisionInfo.Local) { throw new FileNotFoundException("Tried to use PUPPETEER_CHROMIUM_REVISION env variable to launch browser but did not find executable", revisionInfo.ExecutablePath); } return(revisionInfo.ExecutablePath); } revisionInfo = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision); if (!revisionInfo.Local) { throw new FileNotFoundException("Chromium revision is not downloaded. Run BrowserFetcher.DownloadAsync or download Chromium manually", revisionInfo.ExecutablePath); } return(revisionInfo.ExecutablePath); }
private async Task <string> ResolveExecutablePathAsync() { var executablePath = Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH"); if (!string.IsNullOrEmpty(executablePath)) { if (!File.Exists(executablePath)) { throw new FileNotFoundException("Tried to use PUPPETEER_EXECUTABLE_PATH env variable to launch browser but did not find any executable", executablePath); } return(executablePath); } var revision = Environment.GetEnvironmentVariable("PUPPETEER_CHROMIUM_REVISION"); using var browserFetcher = new BrowserFetcher(_product); RevisionInfo revisionInfo; if (!string.IsNullOrEmpty(revision)) { revisionInfo = browserFetcher.RevisionInfo(revision); if (!revisionInfo.Local) { throw new FileNotFoundException("Tried to use PUPPETEER_CHROMIUM_REVISION env variable to launch browser but did not find executable", revisionInfo.ExecutablePath); } return(revisionInfo.ExecutablePath); } revisionInfo = await browserFetcher.GetRevisionInfoAsync().ConfigureAwait(false); if (!revisionInfo.Local) { throw new FileNotFoundException("Process revision is not downloaded. Run BrowserFetcher.DownloadAsync or download the process manually", revisionInfo.ExecutablePath); } return(revisionInfo.ExecutablePath); }
/// <summary> /// The method launches a browser instance with given arguments. The browser will be closed when the Browser is disposed. /// </summary> /// <param name="options">Options for launching Chrome</param> /// <returns>A connected browser.</returns> /// <remarks> /// See <a href="https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/">this article</a> /// for a description of the differences between Chromium and Chrome. /// <a href="https://chromium.googlesource.com/chromium/src/+/lkcr/docs/chromium_browser_vs_google_chrome.md">This article</a> describes some differences for Linux users. /// </remarks> public async Task <Browser> LaunchAsync(LaunchOptions options) { if (_chromiumLaunched) { throw new InvalidOperationException("Unable to create or connect to another chromium process"); } _chromiumLaunched = true; var chromeArguments = InitChromeArgument(options); var chromeExecutable = options.ExecutablePath; if (string.IsNullOrEmpty(chromeExecutable)) { var browserFetcher = new BrowserFetcher(); chromeExecutable = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath; } if (!File.Exists(chromeExecutable)) { throw new FileNotFoundException("Failed to launch chrome! path to executable does not exist", chromeExecutable); } _chromeProcess = CreateChromeProcess(options, chromeArguments, chromeExecutable); try { var connectionDelay = options.SlowMo; var browserWSEndpoint = await WaitForEndpoint(_chromeProcess, options.Timeout).ConfigureAwait(false); try { var keepAliveInterval = 0; _connection = await Connection .Create(browserWSEndpoint, connectionDelay, keepAliveInterval, _loggerFactory) .ConfigureAwait(false); var browser = await Browser.CreateAsync( _connection, Array.Empty <string>(), options.IgnoreHTTPSErrors, !options.AppMode, _chromeProcess, GracefullyCloseChrome ) .ConfigureAwait(false); await EnsureInitialPageAsync(browser).ConfigureAwait(false); return(browser); } catch (Exception ex) { throw new ChromeProcessException("Failed to create connection", ex); } } catch { KillChrome(); await _chromeCloseTcs.Task.ConfigureAwait(false); throw; } }
static async System.Threading.Tasks.Task StartWebBrowser() { var browserRevision = PuppeteerSharp.BrowserFetcher.DefaultChromiumRevision; var browserFetcher = new PuppeteerSharp.BrowserFetcher(new PuppeteerSharp.BrowserFetcherOptions { Path = BrowserDownloadDirPath() }); await browserFetcher.DownloadAsync(browserRevision); browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions { Headless = false, UserDataDir = UserDataDirPath("default"), DefaultViewport = null, ExecutablePath = browserFetcher.RevisionInfo(browserRevision).ExecutablePath, }); browserPage = (await browser.PagesAsync()).FirstOrDefault() ?? await browser.NewPageAsync(); await browserPage.ExposeFunctionAsync("____callback____", (string returnValue) => { callbackFromBrowserDelegate?.Invoke(returnValue); return(0); }); }
/// <summary> /// The method launches a browser instance with given arguments. The browser will be closed when the Browser is disposed. /// </summary> /// <param name="options">Options for launching Chrome</param> /// <returns>A connected browser.</returns> /// <remarks> /// See <a href="https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/">this article</a> /// for a description of the differences between Chromium and Chrome. /// <a href="https://chromium.googlesource.com/chromium/src/+/lkcr/docs/chromium_browser_vs_google_chrome.md">This article</a> describes some differences for Linux users. /// </remarks> public async Task <Browser> LaunchAsync(LaunchOptions options) { if (_chromiumLaunched) { throw new InvalidOperationException("Unable to create or connect to another chromium process"); } _chromiumLaunched = true; var chromeArguments = InitChromeArgument(options); var chromeExecutable = options.ExecutablePath; if (string.IsNullOrEmpty(chromeExecutable)) { var browserFetcher = new BrowserFetcher(); chromeExecutable = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath; } if (!File.Exists(chromeExecutable)) { throw new FileNotFoundException("Failed to launch chrome! path to executable does not exist", chromeExecutable); } CreateChromeProcess(options, chromeArguments, chromeExecutable); try { var connectionDelay = options.SlowMo; var browserWSEndpoint = await WaitForEndpoint(_chromeProcess, options.Timeout, options.DumpIO); var keepAliveInterval = options.KeepAliveInterval; _connection = await Connection.Create(browserWSEndpoint, connectionDelay, keepAliveInterval, _loggerFactory); _processLoaded = true; if (options.LogProcess) { _logger.LogInformation("Process Count: {ProcessCount}", Interlocked.Increment(ref _processCount)); } return(await Browser.CreateAsync(_connection, options, _chromeProcess, KillChrome)); } catch (Exception ex) { ForceKillChrome(); throw new ChromeProcessException("Failed to create connection", ex); } }
private static string GetOrFetchChromeExecutable(LaunchOptions options) { var chromeExecutable = options.ExecutablePath; if (string.IsNullOrEmpty(chromeExecutable)) { var browserFetcher = new BrowserFetcher(); chromeExecutable = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath; } if (!File.Exists(chromeExecutable)) { throw new FileNotFoundException("Failed to launch chrome! path to executable does not exist", chromeExecutable); } return(chromeExecutable); }