Пример #1
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine(AppConfig.help);
                    Console.WriteLine(">> press enter to exit");
                    Console.ReadLine();
                    return(0);
                }
                var valid = AppConfig.ParseArgs(args);
                AppConfig.semaphore = new System.Threading.SemaphoreSlim(AppConfig.maxPara);
                if (valid == false)
                {
                    Console.WriteLine(">> press enter to exit");
                    Console.ReadLine();
                    return(0);
                }
                chromeOpts = await Browser.StartPuppeteer();

                await using var browser = await Puppeteer.LaunchAsync(chromeOpts);

                if (!await Browser.LoginToTiqa(browser))
                {
                    AppConfig.ErrHand(null, "XX couldn't log in");
                    return(0);
                }
                //await Task.WhenAll(AppConfig.allTasks);
                //AppConfig.WriteOut($">> null page responses: {AppConfig.NullResponse}\tpage response handler errors: {AppConfig.HandledErrs}");
                AppConfig.WriteOut(">> press enter to exit", true);
                if (AppConfig.silent)
                {
                }
                return(1);
            }
            catch (Exception ex)
            {
                AppConfig.ErrHand(ex);
                AppConfig.WriteOut(">> press enter to exit", true);
                return(0);
            }
            finally
            {
                if (browser != null)
                {
                    await browser.DisposeAsync();
                }
                if (AppConfig.ol != null)
                {
                    AppConfig.ol.Close();
                    AppConfig.ol.Dispose();
                }
            }
        }
Пример #2
0
        public Browser(Connection connection, LaunchOptions options, Func <Task> closeCallBack)
        {
            Connection          = connection;
            IgnoreHTTPSErrors   = options.IgnoreHTTPSErrors;
            AppMode             = options.AppMode;
            _targets            = new Dictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();

            Connection.Closed          += (object sender, EventArgs e) => Disconnected?.Invoke(this, new EventArgs());
            Connection.MessageReceived += Connect_MessageReceived;

            _closeCallBack = closeCallBack;
        }
Пример #3
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).ConfigureAwait(false);

                var keepAliveInterval = 0;

                _connection = await Connection.Create(browserWSEndpoint, connectionDelay, keepAliveInterval, _loggerFactory).ConfigureAwait(false);

                _processLoaded = true;

                if (options.LogProcess)
                {
                    _logger.LogInformation("Process Count: {ProcessCount}", Interlocked.Increment(ref _processCount));
                }

                var browser = await Browser.CreateAsync(_connection, options.IgnoreHTTPSErrors, !options.AppMode, _chromeProcess, GracefullyCloseChrome).ConfigureAwait(false);
                await EnsureInitialPageAsync(browser).ConfigureAwait(false);

                return(browser);
            }
            catch (Exception ex)
            {
                KillChrome();
                throw new ChromeProcessException("Failed to create connection", ex);
            }
        }
Пример #4
0
        private async Task <string> GetOrFetchBrowserExecutableAsync(LaunchOptions options)
        {
            var browserExecutable = options.ExecutablePath;

            if (string.IsNullOrEmpty(browserExecutable))
            {
                browserExecutable = await ResolveExecutablePathAsync().ConfigureAwait(false);
            }

            if (!File.Exists(browserExecutable))
            {
                throw new FileNotFoundException("Failed to launch browser! path to executable does not exist", browserExecutable);
            }
            return(browserExecutable);
        }
Пример #5
0
        private static string GetOrFetchChromeExecutable(LaunchOptions options)
        {
            var chromeExecutable = options.ExecutablePath;

            if (string.IsNullOrEmpty(chromeExecutable))
            {
                chromeExecutable = ResolveExecutablePath();
            }

            if (!File.Exists(chromeExecutable))
            {
                throw new FileNotFoundException("Failed to launch chrome! path to executable does not exist", chromeExecutable);
            }
            return(chromeExecutable);
        }
Пример #6
0
        private static string GetOrFetchBrowserExecutable(LaunchOptions options)
        {
            string browserExecutable = options.ExecutablePath;

            //if (string.IsNullOrEmpty(browserExecutable))
            //{
            //    browserExecutable = ResolveExecutablePath();
            //}

            if (!File.Exists(browserExecutable))
            {
                throw new FileNotFoundException("Failed to launch browser! path to executable does not exist", browserExecutable);
            }
            return(browserExecutable);
        }
Пример #7
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 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;
            }
        }
Пример #8
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>
        /// <param name="chromiumRevision">The revision of Chrome to launch.</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, int chromiumRevision)
        {
            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 downloader   = Downloader.CreateDefault();
                var revisionInfo = downloader.RevisionInfo(Downloader.CurrentPlatform, chromiumRevision);
                chromeExecutable = revisionInfo.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);
            }
        }
Пример #9
0
        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);
        }
Пример #10
0
        /// <summary>
        /// Creates a new <see cref="LauncherBase"/> instance.
        /// </summary>
        /// <param name="executable">Full path of executable.</param>
        /// <param name="options">Options for launching Base.</param>
        /// <param name="loggerFactory">Logger factory</param>
        public LauncherBase(string executable, LaunchOptions options)
        {
            _options = options;

            Process = new Process
            {
                EnableRaisingEvents = true
            };
            Process.StartInfo.UseShellExecute       = false;
            Process.StartInfo.FileName              = executable;
            Process.StartInfo.RedirectStandardError = true;

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

            if (options.DumpIO)
            {
                Process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
            }
        }
Пример #11
0
        private void CreateChromeProcess(LaunchOptions options, List <string> chromeArguments, string chromeExecutable)
        {
            _chromeProcess = new Process();
            _chromeProcess.EnableRaisingEvents       = true;
            _chromeProcess.StartInfo.UseShellExecute = false;
            _chromeProcess.StartInfo.FileName        = chromeExecutable;
            _chromeProcess.StartInfo.Arguments       = string.Join(" ", chromeArguments);

            SetEnvVariables(_chromeProcess.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());

            if (!options.DumpIO)
            {
                _chromeProcess.StartInfo.RedirectStandardOutput = false;
                _chromeProcess.StartInfo.RedirectStandardError  = false;
            }

            _chromeProcess.Exited += async(sender, e) =>
            {
                await AfterProcessExit();
            };
        }
Пример #12
0
        private Process CreateChromeProcess(LaunchOptions options, List <string> chromeArguments, string chromeExecutable)
        {
            var chromeProcess = new Process
            {
                EnableRaisingEvents = true
            };

            chromeProcess.StartInfo.UseShellExecute       = false;
            chromeProcess.StartInfo.FileName              = chromeExecutable;
            chromeProcess.StartInfo.Arguments             = string.Join(" ", chromeArguments);
            chromeProcess.StartInfo.RedirectStandardError = true;

            SetEnvVariables(chromeProcess.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());

            chromeProcess.Exited += OnChromeProcessExited;
            if (options.DumpIO)
            {
                chromeProcess.ErrorDataReceived += OnChromeProcessErrorDataReceived;
            }

            return(chromeProcess);
        }
Пример #13
0
        /// <summary>
        /// Creates a new <see cref="LauncherBase"/> instance.
        /// </summary>
        /// <param name="executable">Full path of executable.</param>
        /// <param name="options">Options for launching Base.</param>
        public LauncherBase(string executable, LaunchOptions options)
        {
            _stateManager          = new StateManager();
            _stateManager.Starting = new ChromiumStartingState(_stateManager);

            Options = options ?? throw new ArgumentNullException(nameof(options));

            Process = new Process
            {
                EnableRaisingEvents = true
            };
            Process.StartInfo.UseShellExecute       = false;
            Process.StartInfo.FileName              = executable;
            Process.StartInfo.RedirectStandardError = true;

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

            if (options.DumpIO)
            {
                Process.ErrorDataReceived += (_, e) => Console.Error.WriteLine(e.Data);
            }
        }
Пример #14
0
        /// <summary>
        /// Creates a new <see cref="LauncherBase"/> instance.
        /// </summary>
        /// <param name="executable">Full path of executable.</param>
        /// <param name="options">Options for launching Base.</param>
        /// <param name="loggerFactory">Logger factory</param>
        public LauncherBase(string executable, LaunchOptions options, ILoggerFactory loggerFactory)
        {
            _options = options;
            _logger  = options.LogProcess
                ? loggerFactory.CreateLogger <LauncherBase>()
                : null;

            Process = new Process
            {
                EnableRaisingEvents = true
            };
            Process.StartInfo.UseShellExecute       = false;
            Process.StartInfo.FileName              = executable;
            Process.StartInfo.RedirectStandardError = true;

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

            if (options.DumpIO)
            {
                Process.ErrorDataReceived += (_, e) => Console.Error.WriteLine(e.Data);
            }
        }
Пример #15
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 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;
            }
        }
Пример #16
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)
        {
            EnsureSingleLaunchOrConnect();

            var chromiumExecutable = GetOrFetchChromeExecutable(options);

            Process = new ChromiumProcess(chromiumExecutable, options, _loggerFactory);
            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)
                                  .ConfigureAwait(false);

                    await EnsureInitialPageAsync(browser).ConfigureAwait(false);

                    return(browser);
                }
                catch (Exception ex)
                {
                    throw new ChromiumProcessException("Failed to create connection", ex);
                }
            }
            catch
            {
                await Process.KillAsync().ConfigureAwait(false);

                throw;
            }
        }
Пример #17
0
        /// <summary>
        /// Creates a new <see cref="ChromiumProcess"/> instance.
        /// </summary>
        /// <param name="chromiumExecutable">Full path of Chromium executable.</param>
        /// <param name="options">Options for launching Chromium.</param>
        /// <param name="loggerFactory">Logger factory</param>
        public ChromiumProcess(string chromiumExecutable, LaunchOptions options, ILoggerFactory loggerFactory)
        {
            _options = options;

            List <string> chromiumArgs;

            (chromiumArgs, _tempUserDataDir) = PrepareChromiumArgs(options);

            Process = new Process
            {
                EnableRaisingEvents = true
            };
            Process.StartInfo.UseShellExecute       = false;
            Process.StartInfo.FileName              = chromiumExecutable;
            Process.StartInfo.Arguments             = string.Join(" ", chromiumArgs);
            Process.StartInfo.RedirectStandardError = true;

            SetEnvVariables(Process.StartInfo.EnvironmentVariables, options.Env, Environment.GetEnvironmentVariables());

            if (options.DumpIO)
            {
                Process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
            }
        }
Пример #18
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>
 /// <param name="loggerFactory">The logger factory</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 static Task <Browser> LaunchAsync(LaunchOptions options, ILoggerFactory loggerFactory = null)
 => new Launcher(loggerFactory).LaunchAsync(options);
Пример #19
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>
 /// <param name="chromiumRevision">The revision of Chrome to launch.</param>
 /// <param name="loggerFactory">The logger factory</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 static Task <Browser> LaunchAsync(LaunchOptions options, int chromiumRevision, ILoggerFactory loggerFactory = null)
 => new Launcher(loggerFactory).LaunchAsync(options, chromiumRevision);
Пример #20
0
        private List <string> InitChromeArgument(LaunchOptions options)
        {
            var chromeArguments = new List <string>(DefaultArgs);

            _options = options;

            if (options.AppMode)
            {
                options.Headless = false;
            }
            else
            {
                chromeArguments.AddRange(AutomationArgs);
            }

            if (!options.IgnoreDefaultArgs ||
                !chromeArguments.Any(argument => argument.StartsWith("--remote-debugging-", StringComparison.Ordinal)))
            {
                chromeArguments.Add("--remote-debugging-port=0");
            }

            var userDataDirOption = options.Args.FirstOrDefault(i => i.StartsWith(UserDataDirArgument, StringComparison.Ordinal));

            if (string.IsNullOrEmpty(userDataDirOption))
            {
                if (string.IsNullOrEmpty(options.UserDataDir))
                {
                    _temporaryUserDataDir = GetTemporaryDirectory();
                    chromeArguments.Add($"{UserDataDirArgument}={_temporaryUserDataDir.Quote()}");
                }
                else
                {
                    chromeArguments.Add($"{UserDataDirArgument}={options.UserDataDir.Quote()}");
                }
            }
            else
            {
                _options.UserDataDir = userDataDirOption.Replace($"{UserDataDirArgument}=", string.Empty).UnQuote();
            }

            if (options.Devtools)
            {
                chromeArguments.Add("--auto-open-devtools-for-tabs");
                options.Headless = false;
            }

            if (options.Headless)
            {
                chromeArguments.AddRange(new[] {
                    "--headless",
                    "--disable-gpu",
                    "--hide-scrollbars",
                    "--mute-audio"
                });
            }

            if (!options.IgnoreDefaultArgs && options.Args.Any() && options.Args.All(arg => arg.StartsWith("-")))
            {
                chromeArguments.Add("about:blank");
            }

            if (options.Args.Any())
            {
                chromeArguments.AddRange(options.Args);
            }

            return(chromeArguments);
        }
Пример #21
0
        private static (List <string> firefoxArgs, TempDirectory tempUserDataDirectory) PrepareFirefoxArgs(LaunchOptions options)
        {
            var firefoxArgs = new List <string>();

            if (!options.IgnoreDefaultArgs)
            {
                firefoxArgs.AddRange(GetDefaultArgs(options));
            }
            else if (options.IgnoredDefaultArgs?.Length > 0)
            {
                firefoxArgs.AddRange(GetDefaultArgs(options).Except(options.IgnoredDefaultArgs));
            }
            else
            {
                firefoxArgs.AddRange(options.Args);
            }

            if (!firefoxArgs.Any(a => a.StartsWith("-remote-debugging", StringComparison.OrdinalIgnoreCase)))
            {
                firefoxArgs.Add("--remote-debugging-port=0");
            }

            TempDirectory tempUserDataDirectory = null;

            if (!firefoxArgs.Contains("-profile") && !firefoxArgs.Contains("--profile"))
            {
                tempUserDataDirectory = new TempDirectory();
                CreateProfile(tempUserDataDirectory);
                firefoxArgs.Add("--profile");
                firefoxArgs.Add($"{tempUserDataDirectory.Path.Quote()}");
            }

            return(firefoxArgs, tempUserDataDirectory);
        }
Пример #22
0
        private static (List <string> ChromiumArgs, TempDirectory TempUserDataDirectory) PrepareChromiumArgs(LaunchOptions options)
        {
            var chromiumArgs = new List <string>();

            if (!options.IgnoreDefaultArgs)
            {
                chromiumArgs.AddRange(GetDefaultArgs(options));
            }
            else if (options.IgnoredDefaultArgs?.Length > 0)
            {
                chromiumArgs.AddRange(GetDefaultArgs(options).Except(options.IgnoredDefaultArgs));
            }
            else
            {
                chromiumArgs.AddRange(options.Args);
            }

            TempDirectory tempUserDataDirectory = null;

            if (!chromiumArgs.Any(argument => argument.StartsWith("--remote-debugging-", StringComparison.Ordinal)))
            {
                chromiumArgs.Add("--remote-debugging-port=0");
            }

            var userDataDirOption = chromiumArgs.FirstOrDefault(i => i.StartsWith(UserDataDirArgument, StringComparison.Ordinal));

            if (string.IsNullOrEmpty(userDataDirOption))
            {
                tempUserDataDirectory = new TempDirectory();
                chromiumArgs.Add($"{UserDataDirArgument}={tempUserDataDirectory.Path.Quote()}");
            }

            return(chromiumArgs, tempUserDataDirectory);
        }
Пример #23
0
 public static async Task <Browser> LaunchAsync(LaunchOptions options, int chromiumRevision)
 {
     return(await new Launcher().LaunchAsync(options, chromiumRevision));
 }
Пример #24
0
        private static (List <string> firefoxArgs, TempDirectory tempUserDataDirectory) PrepareFirefoxArgs(LaunchOptions options)
        {
            var firefoxArgs = new List <string>();

            if (!options.IgnoreDefaultArgs)
            {
                firefoxArgs.AddRange(GetDefaultArgs(options));
            }
            else if (options.IgnoredDefaultArgs?.Length > 0)
            {
                firefoxArgs.AddRange(GetDefaultArgs(options).Except(options.IgnoredDefaultArgs));
            }
            else
            {
                firefoxArgs.AddRange(options.Args);
            }

            TempDirectory tempUserDataDirectory = null;

            if (!firefoxArgs.Contains("-profile") && !firefoxArgs.Contains("--profile"))
            {
                tempUserDataDirectory = new TempDirectory();
                firefoxArgs.Add("--profile");
                firefoxArgs.Add($"{tempUserDataDirectory.Path.Quote()}");
            }

            return(firefoxArgs, tempUserDataDirectory);
        }
Пример #25
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>
 /// <param name="chromiumRevision">The revision of Chrome to launch.</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 static Task <Browser> LaunchAsync(LaunchOptions options, int chromiumRevision)
 => new Launcher().LaunchAsync(options, chromiumRevision);
Пример #26
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>
        /// <param name="chromiumRevision">The revision of Chrome to launch.</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, int chromiumRevision)
        {
            var chromeArguments = new List <string>(DefaultArgs);

            _options = options;

            if (options.AppMode)
            {
                options.Headless = false;
            }
            else
            {
                chromeArguments.AddRange(AutomationArgs);
            }

            var userDataDirOption = options.Args.FirstOrDefault(i => i.StartsWith(UserDataDirArgument, StringComparison.Ordinal));

            if (string.IsNullOrEmpty(userDataDirOption))
            {
                if (string.IsNullOrEmpty(options.UserDataDir))
                {
                    _temporaryUserDataDir = GetTemporaryDirectory();
                    chromeArguments.Add($"{UserDataDirArgument}={_temporaryUserDataDir}");
                }
                else
                {
                    chromeArguments.Add($"{UserDataDirArgument}={options.UserDataDir}");
                }
            }
            else
            {
                _options.UserDataDir = userDataDirOption.Replace($"{UserDataDirArgument}=", string.Empty);
            }

            if (options.Devtools)
            {
                chromeArguments.Add("--auto-open-devtools-for-tabs");
                options.Headless = false;
            }

            if (options.Headless)
            {
                chromeArguments.AddRange(new[] {
                    "--headless",
                    "--disable-gpu",
                    "--hide-scrollbars",
                    "--mute-audio"
                });
            }

            var chromeExecutable = options.ExecutablePath;

            if (string.IsNullOrEmpty(chromeExecutable))
            {
                var downloader   = Downloader.CreateDefault();
                var revisionInfo = downloader.RevisionInfo(Downloader.CurrentPlatform, chromiumRevision);
                chromeExecutable = revisionInfo.ExecutablePath;
            }
            if (!File.Exists(chromeExecutable))
            {
                throw new FileNotFoundException("Failed to launch chrome! path to executable does not exist", chromeExecutable);
            }

            if (options.Args.Any())
            {
                chromeArguments.AddRange(options.Args);
            }

            _chromeProcess = new Process();
            _chromeProcess.EnableRaisingEvents       = true;
            _chromeProcess.StartInfo.UseShellExecute = false;
            _chromeProcess.StartInfo.FileName        = chromeExecutable;
            _chromeProcess.StartInfo.Arguments       = string.Join(" ", chromeArguments);

            SetEnvVariables(_chromeProcess.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());

            if (!options.DumpIO)
            {
                _chromeProcess.StartInfo.RedirectStandardOutput = false;
                _chromeProcess.StartInfo.RedirectStandardError  = false;
            }

            _chromeProcess.Exited += async(sender, e) =>
            {
                await AfterProcessExit();
            };

            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);

                _processLoaded = true;

                if (options.LogProcess)
                {
                    Console.WriteLine($"PROCESS COUNT: {Interlocked.Increment(ref _processCount)}");
                }

                return(await Browser.CreateAsync(_connection, options, _chromeProcess, KillChrome));
            }
            catch (Exception ex)
            {
                ForceKillChrome();
                throw new ChromeProcessException("Failed to create connection", ex);
            }
        }
Пример #27
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>
 /// <param name="loggerFactory">The logger factory</param>
 /// <param name="product">The browser to be used (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.
 ///
 /// Environment Variables
 /// Puppeteer looks for certain <see href="https://en.wikipedia.org/wiki/Environment_variable">environment variables</see>() to aid its operations.
 /// - <c>PUPPETEER_CHROMIUM_REVISION</c> - specify a certain version of Chromium you'd like Puppeteer to use. See <see cref="Puppeteer.LaunchAsync(LaunchOptions, ILoggerFactory, Product)"/> on how executable path is inferred.
 ///   **BEWARE**: Puppeteer is only <see href="https://github.com/GoogleChrome/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy">guaranteed to work</see> with the bundled Chromium, use at your own risk.
 /// - <c>PUPPETEER_EXECUTABLE_PATH</c> - specify an executable path to be used in <see cref="Puppeteer.LaunchAsync(LaunchOptions, ILoggerFactory, Product)"/>.
 ///   **BEWARE**: Puppeteer is only <see href="https://github.com/GoogleChrome/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy">guaranteed to work</see> with the bundled Chromium, use at your own risk.
 /// </remarks>
 public static Task <Browser> LaunchAsync(LaunchOptions options, ILoggerFactory loggerFactory = null, Product product = Product.Chrome)
 => new Launcher(loggerFactory).LaunchAsync(options, product);
Пример #28
0
 /// <summary>
 /// Returns an array of argument based on the options provided and the platform where the library is running
 /// </summary>
 /// <returns>Chromium arguments.</returns>
 /// <param name="options">Options.</param>
 public static string[] GetDefaultArgs(LaunchOptions options = null)
 => ChromiumLauncher.GetDefaultArgs(options ?? new LaunchOptions());
Пример #29
0
        public static async Task Main(string[] args)
        {
            var currentDirectory = Directory.GetCurrentDirectory();
            var downloadPath     = Path.Combine(currentDirectory, "CustomChromium");

            Console.WriteLine($"Attemping to set up puppeteer to use Chromium found under directory {downloadPath} ");

            if (!Directory.Exists(downloadPath))
            {
                Console.WriteLine("Custom directory not found. Creating directory");
                Directory.CreateDirectory(downloadPath);
            }

            Console.WriteLine("Downloading Chromium...");

            var browserFetcherOptions = new BrowserFetcherOptions {
                Path = downloadPath
            };
            var browserFetcher = new BrowserFetcher(browserFetcherOptions);
            await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);

            var executablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision);

            if (string.IsNullOrEmpty(executablePath))
            {
                Console.WriteLine("Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue");
                Console.ReadLine();
                return;
            }

            Console.WriteLine($"Attemping to start Chromium using executable path: {executablePath}");

            var options = new LaunchOptions {
                Headless = true, ExecutablePath = executablePath
            };

            using (var browser = await Puppeteer.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {
                    await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
                    await page.SetViewportAsync(new ViewPortOptions { Width = 1920, Height = 1080 });

                    var waitUntil = new NavigationOptions {
                        Timeout = 0, WaitUntil = new[] { WaitUntilNavigation.Networkidle0 }
                    };
                    string url = "https://github.com/puppeteer/puppeteer/issues/1345";
                    await page.GoToAsync(url, waitUntil);

                    #region Screenshot Dashboard:
                    var optionsScreenShot = new ScreenshotOptions {
                        FullPage = true
                    };
                    //Đường dẫn lưu file
                    var savePath = Path.Combine(currentDirectory, "Capture");
                    if (!Directory.Exists(savePath))
                    {
                        Console.WriteLine("SavePath directory not found. Creating directory");
                        Directory.CreateDirectory(savePath);
                    }
                    string date       = DateTime.Now.ToString("yyyyMMddHHmmss");
                    var    outputfile = savePath + "/capture_" + date + ".png";
                    await page.ScreenshotAsync(outputfile, optionsScreenShot);

                    Console.WriteLine("Capture completed! Path: " + outputfile, ConsoleColor.Green);
                    #endregion

                    await page.CloseAsync();
                }
            return;
        }
Пример #30
0
 /// <summary>
 /// Returns an array of argument based on the options provided and the platform where the library is running
 /// </summary>
 /// <returns>Chromium arguments.</returns>
 /// <param name="options">Options.</param>
 public static string[] GetDefaultArgs(LaunchOptions options = null)
 => (options?.Product ?? Product.Chrome) == Product.Chrome
         ? ChromiumLauncher.GetDefaultArgs(options ?? new LaunchOptions())
         : FirefoxLauncher.GetDefaultArgs(options ?? new LaunchOptions());