Exemple #1
0
        public BrowserFixture()
        {
            var opts = new ChromeOptions();

            // Comment this out if you want to watch or interact with the browser (e.g., for debugging)
            opts.AddArgument("--headless");

            // Log errors
            opts.SetLoggingPreference(LogType.Browser, LogLevel.All);

            // On Windows/Linux, we don't need to set opts.BinaryLocation
            // But for Travis Mac builds we do
            var binaryLocation = Environment.GetEnvironmentVariable("TEST_CHROME_BINARY");

            if (!string.IsNullOrEmpty(binaryLocation))
            {
                opts.BinaryLocation = binaryLocation;
                Console.WriteLine($"Set {nameof(ChromeOptions)}.{nameof(opts.BinaryLocation)} to {binaryLocation}");
            }

            try
            {
                var driver = new RemoteWebDriver(opts);
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
                Browser = driver;
                Logs    = new RemoteLogs(driver);
            }
            catch (WebDriverException ex)
            {
                var message =
                    "Failed to connect to the web driver. Please see the readme and follow the instructions to install selenium." +
                    "Remember to start the web driver with `selenium-standalone start` before running the end-to-end tests.";
                throw new InvalidOperationException(message, ex);
            }
        }
        public BrowserFixture(IMessageSink diagnosticsMessageSink)
        {
            DiagnosticsMessageSink = diagnosticsMessageSink;

            if (!IsHostAutomationSupported())
            {
                DiagnosticsMessageSink.OnMessage(new DiagnosticMessage("Host does not support browser automation."));
                return;
            }

            var opts = new ChromeOptions();

            // Comment this out if you want to watch or interact with the browser (e.g., for debugging)
            opts.AddArgument("--headless");

            // Log errors
            opts.SetLoggingPreference(LogType.Browser, LogLevel.All);

            // On Windows/Linux, we don't need to set opts.BinaryLocation
            // But for Travis Mac builds we do
            var binaryLocation = Environment.GetEnvironmentVariable("TEST_CHROME_BINARY");

            if (!string.IsNullOrEmpty(binaryLocation))
            {
                opts.BinaryLocation = binaryLocation;
                DiagnosticsMessageSink.OnMessage(new DiagnosticMessage($"Set {nameof(ChromeOptions)}.{nameof(opts.BinaryLocation)} to {binaryLocation}"));
            }

            var driver = new RemoteWebDriver(SeleniumStandaloneServer.Instance.Uri, opts);

            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
            Browser = driver;
            Logs    = new RemoteLogs(driver);
        }
        public ChromeDriver CreateBrowser(CancellationToken cancellationToken, bool captureBrowserMemory = false)
        {
            var options = new ChromeOptions();

            if (_runHeadlessBrowser)
            {
                options.AddArgument("--headless");
            }

            if (captureBrowserMemory)
            {
                options.AddArgument("--enable-precise-memory-info");
            }

            options.SetLoggingPreference(LogType.Browser, LogLevel.All);

            var       attempt     = 0;
            const int maxAttempts = 3;

            do
            {
                try
                {
                    var driver = new ChromeDriver(options);

                    driver
                    .Manage()
                    .Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);

                    driver.Navigate().GoToUrl(SutUri);

                    // Run in background.
                    var logs = new RemoteLogs(driver);
                    _ = Task.Run(async() =>
                    {
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken);

                            var consoleLogs = logs.GetLog(LogType.Browser);
                            foreach (var entry in consoleLogs)
                            {
                                _output?.WriteLine($"[Browser Log]: {entry.Timestamp}: {entry.Message}");
                            }
                        }
                    });

                    return(driver);
                }
                catch (Exception ex)
                {
                    _output?.WriteLine($"Error initializing WebDriver: {ex.Message}");
                }

                attempt++;
            } while (attempt < maxAttempts);

            throw new InvalidOperationException("Couldn't create a Selenium Chrome driver client. The server is irresponsive");
        }
Exemple #4
0
        public static async Task <RemoteWebDriver> CreateBrowser(CancellationToken cancellationToken, bool captureBrowserMemory = false)
        {
            var uri = await WaitForServerAsync(SeleniumPort, cancellationToken);

            var options = new ChromeOptions();

            if (RunHeadlessBrowser)
            {
                options.AddArgument("--headless");
            }

            if (captureBrowserMemory)
            {
                options.AddArgument("--enable-precise-memory-info");
            }

            options.SetLoggingPreference(LogType.Browser, LogLevel.All);

            var       attempt     = 0;
            const int MaxAttempts = 3;

            do
            {
                try
                {
                    // The driver opens the browser window and tries to connect to it on the constructor.
                    // Under heavy load, this can cause issues
                    // To prevent this we let the client attempt several times to connect to the server, increasing
                    // the max allowed timeout for a command on each attempt linearly.
                    var driver = new RemoteWebDriver(
                        uri,
                        options.ToCapabilities(),
                        TimeSpan.FromSeconds(60).Add(TimeSpan.FromSeconds(attempt * 60)));

                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);

                    if (PoolForBrowserLogs)
                    {
                        // Run in background.
                        var logs = new RemoteLogs(driver);
                        _ = Task.Run(async() =>
                        {
                            while (!cancellationToken.IsCancellationRequested)
                            {
                                await Task.Delay(TimeSpan.FromSeconds(3));

                                var consoleLogs = logs.GetLog(LogType.Browser);
                                foreach (var entry in consoleLogs)
                                {
                                    Console.WriteLine($"[Browser Log]: {entry.Timestamp}: {entry.Message}");
                                }
                            }
                        });
                    }

                    return(driver);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error initializing RemoteWebDriver: {ex.Message}");
                }

                attempt++;
            } while (attempt < MaxAttempts);

            throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is irresponsive");
        }