internal Browser( Connection connection, string[] contextIds, bool ignoreHTTPSErrors, ViewPortOptions defaultViewport, LauncherBase launcher, Func <TargetInfo, bool> targetFilter) { Connection = connection; IgnoreHTTPSErrors = ignoreHTTPSErrors; DefaultViewport = defaultViewport; TargetsMap = new ConcurrentDictionary <string, Target>(); ScreenshotTaskQueue = new TaskQueue(); DefaultContext = new BrowserContext(Connection, this, null); _contexts = contextIds.ToDictionary( contextId => contextId, contextId => new BrowserContext(Connection, this, contextId)); Connection.Disconnected += Connection_Disconnected; Connection.MessageReceived += Connect_MessageReceived; Launcher = launcher; _logger = Connection.LoggerFactory.CreateLogger <Browser>(); _targetFilterCallback = targetFilter ?? ((TargetInfo _) => true); }
internal static async Task <Browser> CreateAsync( Connection connection, string[] contextIds, bool ignoreHTTPSErrors, ViewPortOptions defaultViewPort, LauncherBase launcher) { var browser = new Browser(connection, contextIds, ignoreHTTPSErrors, defaultViewPort, launcher); await connection.SendAsync("Target.setDiscoverTargets", new TargetSetDiscoverTargetsRequest { Discover = true }).ConfigureAwait(false); return(browser); }
/// <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 the browser</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 (options == null) { throw new ArgumentNullException(nameof(options)); } EnsureSingleLaunchOrConnect(); _product = options.Product; var executable = await GetOrFetchBrowserExecutableAsync(options).ConfigureAwait(false); Process = options.Product switch { Product.Chrome => new ChromiumLauncher(executable, options), Product.Firefox => new FirefoxLauncher(executable, options), _ => throw new ArgumentException("Invalid product"), }; try { await Process.StartAsync().ConfigureAwait(false); try { var connection = await Connection .Create(Process.EndPoint, options, _loggerFactory) .ConfigureAwait(false); var browser = await Browser .CreateAsync(connection, Array.Empty <string>(), options.IgnoreHTTPSErrors, options.DefaultViewport, Process, options.TargetFilter) .ConfigureAwait(false); await browser.WaitForTargetAsync(t => t.Type == TargetType.Page).ConfigureAwait(false); return(browser); } catch (Exception ex) { throw new ProcessException("Failed to create connection", ex); } } catch { await Process.KillAsync().ConfigureAwait(false); throw; } }
/// <summary> /// Initializes a new instance of the <see cref="Browser"/> class. /// </summary> /// <param name="connection">The connection</param> /// <param name="contextIds">The context ids></param> /// <param name="ignoreHTTPSErrors">The option to ignoreHTTPSErrors</param> /// <param name="defaultViewport">Default viewport</param> /// <param name="launcher">The launcher</param> public Browser( Connection connection, string[] contextIds, bool ignoreHTTPSErrors, ViewPortOptions defaultViewport, LauncherBase launcher) { Connection = connection; IgnoreHTTPSErrors = ignoreHTTPSErrors; DefaultViewport = defaultViewport; TargetsMap = new ConcurrentDictionary <string, Target>(); ScreenshotTaskQueue = new TaskQueue(); DefaultContext = new BrowserContext(Connection, this, null); _contexts = contextIds.ToDictionary( contextId => contextId, contextId => new BrowserContext(Connection, this, contextId)); Connection.Disconnected += Connection_Disconnected; Connection.MessageReceived += Connect_MessageReceived; Launcher = launcher; }
/// <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 the browser</param> /// <param name="product">The browser to launch (Chrome, Firefox)</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) { EnsureSingleLaunchOrConnect(); string executable = GetOrFetchBrowserExecutable(options); Process = new ChromiumLauncher(executable, options); try { await Process.StartAsync().ConfigureAwait(false); try { var connection = await Connection .Create(Process.EndPoint, options) .ConfigureAwait(false); var browser = await Browser .CreateAsync(connection, Array.Empty <string>(), options.IgnoreHTTPSErrors, options.DefaultViewport, Process) .ConfigureAwait(false); await browser.WaitForTargetAsync(t => t.Type == TargetType.Page).ConfigureAwait(false); return(browser); } catch (Exception ex) { throw new ProcessException("Failed to create connection", ex); } } catch { await Process.KillAsync().ConfigureAwait(false); throw; } }