예제 #1
0
        public ChromeOptions ToChromeOptions(ChromeBrowserSettings settings)
        {
            if (settings == null)
            {
                throw new System.ArgumentNullException(nameof(settings));
            }

            var result = new ChromeOptions();

            settings.Options.Arguments.ToList().ForEach(argument => result.AddArgument(argument));
            settings.Options.LoggingPreferences.ToList().ForEach(loggingPreference => result.SetLoggingPreference(loggingPreference.Key, loggingPreference.Value));
            settings.Options.UserProfilePreferences.ToList().ForEach(userProfilePreference => result.AddUserProfilePreference(userProfilePreference.Key, userProfilePreference.Value));

            if (!settings.Options.PerformanceLoggingPreferences.IsEmpty)
            {
                // It would appear that setting Options.PerformanceLoggingPreferences to non-null has side effects; hence, we only set it if we actually have tracing categories to deal with.
                result.PerformanceLoggingPreferences = new ChromePerformanceLoggingPreferences();
                settings.Options.PerformanceLoggingPreferences.TracingCategories.ForEach(tracingCategory => result.PerformanceLoggingPreferences.AddTracingCategory(tracingCategory));
            }

            return(result);
        }
예제 #2
0
        private static void RegisterBrowserSettings(ILogger logger, string prefix, IServiceCollection services)
        {
            var runtimeSettingsUtilities = new RuntimeSettings(logger);
            var paths             = runtimeSettingsUtilities.CalculatePathsOfSettingsFiles(prefix, Path.Combine(Directory.GetCurrentDirectory(), "Runtime"), "BrowserSettings", "common-chrome.json");
            var configurationRoot = runtimeSettingsUtilities.BuildConfiguration(prefix, paths);

            var browserName     = configurationRoot.GetSection("browserName")?.Value?.ToUpper();
            var browserSettings = configurationRoot.GetSection("browserSettings");

            switch (browserName)
            {
            case "CHROME":
                var instance = new ChromeBrowserSettings();

                browserSettings.Bind(instance);

                instance = SubstituteEnvironmentVariables(instance);

                instance.BrowserName = browserName;
                instance.Cleanse();

                services.AddSingleton(instance);
                services.AddSingleton <IBrowserProperties>(instance);
                break;

            case "EDGE":
                var edgeInstance = new EdgeBrowserSettings();

                browserSettings.Bind(edgeInstance);

                edgeInstance = SubstituteEnvironmentVariables(edgeInstance);

                edgeInstance.BrowserName = browserName;
                edgeInstance.Cleanse();

                services.AddSingleton(edgeInstance);
                services.AddSingleton <IBrowserProperties>(edgeInstance);
                break;

            case "FIREFOX":
                var ffInstance = new FireFoxBrowserSettings();

                browserSettings.Bind(ffInstance);

                ffInstance = SubstituteEnvironmentVariables(ffInstance);

                ffInstance.BrowserName = browserName;
                ffInstance.Cleanse();

                services.AddSingleton(ffInstance);
                services.AddSingleton <IBrowserProperties>(ffInstance);
                break;

            case "INTERNETEXPLORER":
                var ieInstance = new InternetExplorerBrowserSettings();

                browserSettings.Bind(ieInstance);

                ieInstance = SubstituteEnvironmentVariables(ieInstance);

                ieInstance.BrowserName = browserName;
                ieInstance.Cleanse();

                services.AddSingleton(ieInstance);
                services.AddSingleton <IBrowserProperties>(ieInstance);
                break;

            default:
                throw new ArgumentOutOfRangeException($"The browser called {browserName} is currently not supported. ");
            }
        }
예제 #3
0
        private IWebDriver StartBrowser(RemoteWebDriverSettings remoteWebDriverSettings, ChromeBrowserSettings settings, IControlSettings controlSettings)
        {
            var adapter       = new ChromeBrowserSettingsAdapter();
            var chromeOptions = adapter.ToChromeOptions(settings);

            Environment.SetEnvironmentVariable("CHROME_LOG_FILE", null);
            if (settings.Options.CaptureChromeLogFile)
            {
                Environment.SetEnvironmentVariable("CHROME_LOG_FILE", settings.Options.ChromeLogPath);
            }
            if (!remoteWebDriverSettings.UseRemoteDriver)
            {
                var chromeDriver = controlSettings.AttachToExistingSessionIfItExists ? new AttachableChromeDriver(chromeOptions) : new ChromeDriver(chromeOptions);
                return(chromeDriver);
            }

            return(StartRemoteBrowser(remoteWebDriverSettings, chromeOptions, controlSettings));
        }