Example #1
0
        /// <inheritdoc cref="IBrowserType"/>
        public override async Task <IBrowserApp> LaunchBrowserAppAsync(LaunchOptions options = null)
        {
            options ??= new LaunchOptions();

            var(chromiumArgs, tempUserDataDir) = PrepareChromiumArgs(options);
            string     chromiumExecutable = GetBrowserExecutablePath(options);
            BrowserApp browserApp         = null;

            var process = new ChromiumProcessManager(
                chromiumExecutable,
                chromiumArgs,
                tempUserDataDir,
                options.Timeout,
                async() =>
            {
                if (browserApp == null)
                {
                    return;
                }

                var transport = await BrowserHelper.CreateTransportAsync(browserApp.ConnectOptions).ConfigureAwait(false);
                await transport.SendAsync(new ConnectionRequest
                {
                    Id     = ChromiumConnection.BrowserCloseMessageId,
                    Method = new BrowserCloseRequest().Command,
                }.ToJson()).ConfigureAwait(false);
            },
                exitCode =>
            {
                browserApp?.ProcessKilled(exitCode);
            });

            try
            {
                SetEnvVariables(process.Process.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());

                if (options.DumpIO)
                {
                    process.Process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
                }

                await process.StartAsync().ConfigureAwait(false);

                var connectOptions = new ConnectOptions()
                {
                    BrowserWSEndpoint = process.Endpoint,
                    SlowMo            = options.SlowMo,
                };

                browserApp = new BrowserApp(process, () => process.GracefullyClose(), connectOptions);
                return(browserApp);
            }
            catch
            {
                await process.KillAsync().ConfigureAwait(false);

                throw;
            }
        }
Example #2
0
        /// <inheritdoc cref="IBrowserType.LaunchBrowserAppAsync(LaunchOptions)"/>
        public override async Task <IBrowserApp> LaunchBrowserAppAsync(LaunchOptions options = null)
        {
            options ??= new LaunchOptions();

            var(firefoxArguments, tempProfileDir) = PrepareFirefoxArgs(options);
            string     firefoxExecutable = GetBrowserExecutablePath(options);
            BrowserApp browserApp        = null;

            var process = new FirefoxProcessManager(
                firefoxExecutable,
                firefoxArguments,
                tempProfileDir,
                options.Timeout,
                async() =>
            {
                if (browserApp == null)
                {
                    return;
                }

                var transport = await BrowserHelper.CreateTransportAsync(browserApp.ConnectOptions).ConfigureAwait(false);
                await transport.SendAsync(new BrowserCloseRequest().Command).ConfigureAwait(false);
            },
                (exitCode) =>
            {
                browserApp?.ProcessKilled(exitCode);
            });

            try
            {
                SetEnvVariables(process.Process.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());

                if (options.DumpIO)
                {
                    process.Process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
                }

                await process.StartAsync().ConfigureAwait(false);

                var connectOptions = new ConnectOptions()
                {
                    BrowserWSEndpoint = process.Endpoint,
                    SlowMo            = options.SlowMo,
                };

                return(new BrowserApp(process, () => Task.CompletedTask, connectOptions));
            }
            catch
            {
                await process.KillAsync().ConfigureAwait(false);

                throw;
            }
        }
        internal static async Task <IBrowser> ConnectAsync(IBrowserApp app, ConnectOptions options)
        {
            var transport = await BrowserHelper.CreateTransportAsync(options).ConfigureAwait(false);

            var connection = new FirefoxConnection(transport);
            var response   = await connection.SendAsync(new TargetGetBrowserContextsRequest()).ConfigureAwait(false);

            var browser = new FirefoxBrowser(app, connection, response.BrowserContextIds);
            await connection.SendAsync(new TargetEnableRequest()).ConfigureAwait(false);

            await browser.WaitForTargetAsync(t => t.Type == TargetType.Page).ConfigureAwait(false);

            return(browser);
        }
        internal static async Task <IBrowser> ConnectAsync(IBrowserApp app, ConnectOptions options)
        {
            var transport = await BrowserHelper.CreateTransportAsync(options).ConfigureAwait(false);

            var connection = new ChromiumConnection(transport);
            var response   = await connection.RootSession.SendAsync(new TargetGetBrowserContextsRequest()).ConfigureAwait(false);

            var browser = new ChromiumBrowser(connection, response.BrowserContextIds);
            await connection.RootSession.SendAsync(new TargetSetDiscoverTargetsRequest { Discover = true }).ConfigureAwait(false);

            await browser.WaitForTargetAsync(t => t.Type == TargetType.Page).ConfigureAwait(false);

            return(browser);
        }