Esempio n. 1
0
        public void StartBrowser(BrowserConfiguration browserConfiguration, ServicesCollection childContainer)
        {
            try
            {
                var wrappedWebDriver = WrappedWebDriverCreateService.Create(browserConfiguration);

                childContainer.RegisterInstance <IWebDriver>(wrappedWebDriver);
                childContainer.RegisterInstance(((RemoteWebDriver)wrappedWebDriver).SessionId.ToString(), "SessionId");
                childContainer.RegisterInstance(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.Url, "GridUri");

                ////childContainer.RegisterInstance(new BrowserService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new CookiesService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new DialogService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new JavaScriptService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new InteractionsService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new NavigationService(wrappedWebDriver));
                ////childContainer.RegisterInstance(new ComponentCreateService());
                childContainer.RegisterInstance <IWebDriverElementFinderService>(new NativeElementFinderService(wrappedWebDriver));
                childContainer.RegisterNull <int?>();
                childContainer.RegisterNull <IWebElement>();
                IsBrowserStartedCorrectly = true;
            }
            catch
            {
                IsBrowserStartedCorrectly = false;
                throw;
            }
        }
        public static IWebDriver Create(BrowserConfiguration executionConfiguration)
        {
            ProcessCleanupService.KillAllDriversAndChildProcessesWindows();

            DisposeDriverService.TestRunStartTime = DateTime.Now;

            BrowserConfiguration = executionConfiguration;
            var wrappedWebDriver = default(IWebDriver);

            _proxyService = ServicesCollection.Current.Resolve <ProxyService>();
            var webDriverProxy = new OpenQA.Selenium.Proxy
            {
                HttpProxy = $"http://127.0.0.1:{_proxyService.Port}",
                SslProxy  = $"http://127.0.0.1:{_proxyService.Port}",
            };

            switch (executionConfiguration.ExecutionType)
            {
            case ExecutionType.Regular:
                wrappedWebDriver = InitializeDriverRegularMode(executionConfiguration, webDriverProxy);
                break;

            case ExecutionType.Grid:
                var gridUrl = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.Url;
                if (gridUrl == null || !Uri.IsWellFormedUriString(gridUrl.ToString(), UriKind.Absolute))
                {
                    throw new ArgumentException("To execute your tests in WebDriver Grid mode you need to set the gridUri in the browserSettings file.");
                }

                DebuggerPort = GetFreeTcpPort();

                if (executionConfiguration.IsLighthouseEnabled && (executionConfiguration.BrowserType.Equals(BrowserType.Chrome) || executionConfiguration.BrowserType.Equals(BrowserType.ChromeHeadless)))
                {
                    executionConfiguration.DriverOptions.AddArgument("--remote-debugging-address=0.0.0.0");
                    executionConfiguration.DriverOptions.AddArgument($"--remote-debugging-port={DebuggerPort}");
                }

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                wrappedWebDriver = new RemoteWebDriver(new Uri(gridUrl), executionConfiguration.DriverOptions);
                break;
            }

            var gridPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.PageLoadTimeout;

            wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(gridPageLoadTimeout);
            var gridScriptTimeout = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.ScriptTimeout;

            wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(gridScriptTimeout);

            if (executionConfiguration.BrowserType != BrowserType.Edge)
            {
                FixDriverCommandExecutionDelay((WebDriver)wrappedWebDriver);
            }

            ChangeWindowSize(executionConfiguration.Size, wrappedWebDriver);

            return(wrappedWebDriver);
        }
Esempio n. 3
0
        public void StartBrowser(BrowserConfiguration browserConfiguration, ServicesCollection childContainer)
        {
            try
            {
                var wrappedWebDriver = WrappedWebDriverCreateService.Create(browserConfiguration);

                childContainer.RegisterInstance <IWebDriver>(wrappedWebDriver);
                childContainer.RegisterInstance(((WebDriver)wrappedWebDriver).SessionId.ToString(), "SessionId");
                childContainer.RegisterInstance(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.Url, "GridUri");
                childContainer.RegisterInstance <IWebDriverElementFinderService>(new NativeElementFinderService(wrappedWebDriver));
                childContainer.RegisterNull <int?>();
                childContainer.RegisterNull <IWebElement>();
                IsBrowserStartedCorrectly = true;
            }
            catch (Exception ex)
            {
                DebugInformation.PrintStackTrace(ex);
                IsBrowserStartedCorrectly = false;
                throw;
            }
        }
        private static IWebDriver InitializeDriverRegularMode(BrowserConfiguration executionConfiguration, OpenQA.Selenium.Proxy webDriverProxy)
        {
            IWebDriver wrappedWebDriver;

            switch (executionConfiguration.BrowserType)
            {
            case BrowserType.Chrome:
                new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser);
                var chromeDriverService = ChromeDriverService.CreateDefaultService();
                chromeDriverService.SuppressInitialDiagnosticInformation = true;
                chromeDriverService.EnableVerboseLogging = false;
                var chromeOptions = executionConfiguration.DriverOptions;
                chromeOptions.AddArguments("--log-level=3");
                Port = GetFreeTcpPort();
                chromeDriverService.Port = Port;
                DebuggerPort             = GetFreeTcpPort();

                if (executionConfiguration.IsLighthouseEnabled)
                {
                    chromeOptions.AddArgument("--remote-debugging-address=0.0.0.0");
                    chromeOptions.AddArgument($"--remote-debugging-port={DebuggerPort}");
                    ////ProcessProvider.StartCLIProcess($"chrome-debug --port={Port}");
                    ////chromeOptions.DebuggerAddress = $"127.0.0.1:{Port}";
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    string packedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    chromeOptions.AddExtension(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    string unpackedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load unpacked extension from path: {unpackedExtensionPath}");
                    chromeOptions.AddArguments($"load-extension={unpackedExtensionPath}");
                }

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled && !executionConfiguration.IsLighthouseEnabled)
                {
                    chromeOptions.Proxy = webDriverProxy;
                }

                chromeOptions.AddArgument("hide-scrollbars");
                chromeOptions.SetLoggingPreference(LogType.Browser, LogLevel.All);
                chromeOptions.SetLoggingPreference("performance", LogLevel.All);

                wrappedWebDriver = new ChromeDriver(chromeDriverService, chromeOptions);
                break;

            case BrowserType.ChromeHeadless:
                new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser);
                var chromeHeadlessDriverService = ChromeDriverService.CreateDefaultService();
                chromeHeadlessDriverService.SuppressInitialDiagnosticInformation = true;
                Port = GetFreeTcpPort();
                chromeHeadlessDriverService.Port = Port;
                var chromeHeadlessOptions = executionConfiguration.DriverOptions;
                chromeHeadlessOptions.AddArguments("--headless");
                chromeHeadlessOptions.AddArguments("--log-level=3");

                chromeHeadlessOptions.AddArguments("--test-type");
                chromeHeadlessOptions.AddArguments("--disable-infobars");
                chromeHeadlessOptions.AddArguments("--allow-no-sandbox-job");
                chromeHeadlessOptions.AddArguments("--ignore-certificate-errors");
                chromeHeadlessOptions.AddArguments("--disable-gpu");
                chromeHeadlessOptions.AddArguments("--no-sandbox");
                chromeHeadlessOptions.AddUserProfilePreference("credentials_enable_service", false);
                chromeHeadlessOptions.AddUserProfilePreference("profile.password_manager_enabled", false);
                chromeHeadlessOptions.AddArgument("hide-scrollbars");
                chromeHeadlessOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss;

                Port = GetFreeTcpPort();
                chromeHeadlessDriverService.Port = Port;
                DebuggerPort = GetFreeTcpPort();

                if (executionConfiguration.IsLighthouseEnabled)
                {
                    chromeHeadlessOptions.AddArgument("--remote-debugging-address=0.0.0.0");
                    chromeHeadlessOptions.AddArgument($"--remote-debugging-port={DebuggerPort}");
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    string packedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    chromeHeadlessOptions.AddExtension(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    string unpackedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load unpacked extension from path: {unpackedExtensionPath}");
                    chromeHeadlessOptions.AddArguments($"load-extension={unpackedExtensionPath}");
                }

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    chromeHeadlessOptions.Proxy = webDriverProxy;
                }

                chromeHeadlessOptions.SetLoggingPreference(LogType.Browser, LogLevel.All);
                chromeHeadlessOptions.SetLoggingPreference("performance", LogLevel.All);

                wrappedWebDriver = new ChromeDriver(chromeHeadlessDriverService, chromeHeadlessOptions);
                break;

            case BrowserType.Firefox:
                new DriverManager().SetUpDriver(new FirefoxConfig(), VersionResolveStrategy.Latest);
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var firefoxOptions = executionConfiguration.DriverOptions;
                firefoxOptions.AddAdditionalOption("acceptInsecureCerts", true);
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    firefoxOptions.Proxy = webDriverProxy;
                }

                var firefoxService = FirefoxDriverService.CreateDefaultService();
                firefoxService.SuppressInitialDiagnosticInformation = true;
                firefoxService.Port = GetFreeTcpPort();
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
                    {
                        firefoxService.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                    }
                    else
                    {
                        firefoxService.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
                    }

                    // TODO: Anton(15.12.2019): Add option to set the path via environment variable.
                }

                var firefoxProfile = ServicesCollection.Current.Resolve <FirefoxProfile>(executionConfiguration.ClassFullName);
                if (firefoxProfile == null)
                {
                    firefoxOptions.Profile = new FirefoxProfile(_driverExecutablePath);
                    ServicesCollection.Current.RegisterInstance(firefoxOptions.Profile, executionConfiguration.ClassFullName);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    Logger.LogError($"Packed Extension loading not supported in Firefox!");

                    // 05-Nov-2020 navramov: Extension loading does not work
                    ////string packedExtensionPath = ConfigurationService.GetSection<WebSettings>().Firefox.PackedExtensionPath.NormalizeAppPath();
                    ////Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    ////firefoxOptions.Profile.AddExtension(ConfigurationService.GetSection<WebSettings>().Firefox.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    Logger.LogError($"Unpacked Extension loading not supported in Firefox!");
                }

                var firefoxTimeout = TimeSpan.FromSeconds(180);
                wrappedWebDriver = new FirefoxDriver(firefoxService, firefoxOptions, firefoxTimeout);
                break;

            case BrowserType.FirefoxHeadless:
                new DriverManager().SetUpDriver(new FirefoxConfig());
                var firefoxHeadlessOptions = executionConfiguration.DriverOptions;
                firefoxHeadlessOptions.AddArguments("--headless");
                firefoxHeadlessOptions.AddAdditionalOption("acceptInsecureCerts", true);
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    firefoxHeadlessOptions.Proxy = webDriverProxy;
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    string packedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    firefoxHeadlessOptions.Profile.AddExtension(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    Logger.LogError($"Unpacked Extension loading not supported in Firefox!");
                }

                var service = FirefoxDriverService.CreateDefaultService();
                service.SuppressInitialDiagnosticInformation = true;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
                    {
                        service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                    }
                    else
                    {
                        service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
                    }

                    // TODO: Anton(15.12.2019): Add option to set the path via environment variable.
                }

                service.Port     = GetFreeTcpPort();
                wrappedWebDriver = new FirefoxDriver(service, firefoxHeadlessOptions);
                break;

            case BrowserType.Edge:
                new DriverManager().SetUpDriver(new EdgeConfig());
                var edgeDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService();
                edgeDriverService.SuppressInitialDiagnosticInformation = true;
                var edgeOptions = executionConfiguration.DriverOptions;
                edgeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
                edgeOptions.UseChromium      = true;
                edgeOptions.AddArguments("--log-level=3");
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    edgeOptions.Proxy = webDriverProxy;
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    string packedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    edgeOptions.AddExtension(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    string unpackedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load unpacked extension from path: {unpackedExtensionPath}");
                    edgeOptions.AddArguments($"load-extension={unpackedExtensionPath}");
                }

                edgeOptions.SetLoggingPreference(LogType.Browser, LogLevel.Severe);
                edgeOptions.SetLoggingPreference("performance", LogLevel.All);


                wrappedWebDriver = new Microsoft.Edge.SeleniumTools.EdgeDriver(edgeDriverService, edgeOptions);
                break;

            case BrowserType.EdgeHeadless:
                new DriverManager().SetUpDriver(new EdgeConfig());
                var edgeHeadlessDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService();
                edgeHeadlessDriverService.SuppressInitialDiagnosticInformation = true;
                var edgeHeadlessOptions = executionConfiguration.DriverOptions;
                edgeHeadlessOptions.AddArguments("--headless");
                edgeHeadlessOptions.AddArguments("--log-level=3");

                edgeHeadlessOptions.AddArguments("--test-type");
                edgeHeadlessOptions.AddArguments("--disable-infobars");
                edgeHeadlessOptions.AddArguments("--allow-no-sandbox-job");
                edgeHeadlessOptions.AddArguments("--ignore-certificate-errors");
                edgeHeadlessOptions.AddArguments("--disable-gpu");
                edgeHeadlessOptions.AddArguments("--no-sandbox");
                edgeHeadlessOptions.AddUserProfilePreference("credentials_enable_service", false);
                edgeHeadlessOptions.AddUserProfilePreference("profile.password_manager_enabled", false);
                edgeHeadlessOptions.AddArgument("hide-scrollbars");
                edgeHeadlessOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss;

                edgeHeadlessOptions.PageLoadStrategy = PageLoadStrategy.Normal;
                edgeHeadlessOptions.UseChromium      = true;
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    edgeHeadlessOptions.Proxy = webDriverProxy;
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath != null)
                {
                    string packedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load packed extension from path: {packedExtensionPath}");
                    edgeHeadlessOptions.AddExtension(ConfigurationService.GetSection <WebSettings>().ExecutionSettings.PackedExtensionPath);
                }

                if (ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath != null)
                {
                    string unpackedExtensionPath = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.UnpackedExtensionPath.NormalizeAppPath();
                    Logger.LogInformation($"Trying to load unpacked extension from path: {unpackedExtensionPath}");
                    edgeHeadlessOptions.AddExtensionPath(unpackedExtensionPath);
                }

                edgeHeadlessOptions.SetLoggingPreference(LogType.Browser, LogLevel.Severe);
                edgeHeadlessOptions.SetLoggingPreference("performance", LogLevel.All);

                wrappedWebDriver = new Microsoft.Edge.SeleniumTools.EdgeDriver(edgeHeadlessDriverService, edgeHeadlessOptions);
                break;

            case BrowserType.Opera:
                new DriverManager().SetUpDriver(new OperaConfig());

                // the driver will be different for different OS.
                // Check for different releases- https://github.com/operasoftware/operachromiumdriver/releases
                var operaOptions = executionConfiguration.DriverOptions;

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    operaOptions.Proxy = webDriverProxy;
                }

                var operaService = OperaDriverService.CreateDefaultService();
                operaService.SuppressInitialDiagnosticInformation = true;
                operaService.Port = GetFreeTcpPort();

                try
                {
                    wrappedWebDriver = new OperaDriver(operaService, operaOptions);
                }
                catch (WebDriverException ex) when(ex.Message.Contains("DevToolsActivePort file doesn't exist"))
                {
                    throw new Exception("This is a known issue in the latest versions of Opera driver. It is reported to the Opera team. As soon it is fixed we will update BELLATRIX.", ex);
                }

                break;

            case BrowserType.InternetExplorer:
                new DriverManager().SetUpDriver(new InternetExplorerConfig());

                // Steps to configure IE to always allow blocked content:
                // From Internet Explorer, select the Tools menu, then the Options...
                // In the Internet Options dialog, select the Advanced tab...
                // Scroll down until you see the Security options. Enable the checkbox "Allow active content to run in files on My Computer"
                // Also, check https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
                // in case of OpenQA.Selenium.NoSuchWindowException: Unable to get browser --> Uncheck IE Options --> Security Tab -> Uncheck "Enable Protected Mode"
                var ieOptions = executionConfiguration.DriverOptions;
                ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
                ieOptions.IgnoreZoomLevel      = true;
                ieOptions.EnableNativeEvents   = false;
                ieOptions.EnsureCleanSession   = true;
                ieOptions.PageLoadStrategy     = PageLoadStrategy.Eager;
                ieOptions.ForceShellWindowsApi = true;
                ieOptions.AddAdditionalCapability("disable-popup-blocking", true);

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    ieOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new InternetExplorerDriver(_driverExecutablePath, ieOptions);
                break;

            case BrowserType.Safari:
                var safariOptions = executionConfiguration.DriverOptions;

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    safariOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new SafariDriver(safariOptions);
                break;

            default:
                throw new NotSupportedException($"Not supported browser {executionConfiguration.BrowserType}");
            }

            return(wrappedWebDriver);
        }
        public static IWebDriver Create(BrowserConfiguration executionConfiguration)
        {
            ProcessCleanupService.KillAllDriversAndChildProcessesWindows();

            DisposeDriverService.TestRunStartTime = DateTime.Now;

            BrowserConfiguration = executionConfiguration;
            var wrappedWebDriver = default(IWebDriver);

            _proxyService = ServicesCollection.Current.Resolve <ProxyService>();
            var webDriverProxy = new OpenQA.Selenium.Proxy
            {
                HttpProxy = $"http://127.0.0.1:{_proxyService.Port}",
                SslProxy  = $"http://127.0.0.1:{_proxyService.Port}",
            };

            switch (executionConfiguration.ExecutionType)
            {
            case ExecutionType.Regular:
                wrappedWebDriver = InitializeDriverRegularMode(executionConfiguration, webDriverProxy);
                break;

            case ExecutionType.Grid:
                var gridUri = ConfigurationService.GetSection <WebSettings>().Remote.GridUri;
                if (gridUri == null || !Uri.IsWellFormedUriString(gridUri.ToString(), UriKind.Absolute))
                {
                    throw new ArgumentException("To execute your tests in WebDriver Grid mode you need to set the gridUri in the browserSettings file.");
                }

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                wrappedWebDriver = new RemoteWebDriver(gridUri, executionConfiguration.DriverOptions);
                var gridPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Remote.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(gridPageLoadTimeout);
                var gridScriptTimeout = ConfigurationService.GetSection <WebSettings>().Remote.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(gridScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Remote;
                ChangeWindowSize(executionConfiguration.BrowserType, executionConfiguration.Size, wrappedWebDriver);
                break;

            case ExecutionType.BrowserStack:
                var browserStackUri = ConfigurationService.GetSection <WebSettings>().BrowserStack.GridUri;
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().BrowserStack;
                if (browserStackUri == null || !Uri.IsWellFormedUriString(browserStackUri.ToString(), UriKind.Absolute))
                {
                    throw new ArgumentException("To execute your tests in BrowserStack you need to set the gridUri in the browserSettings file.");
                }

                wrappedWebDriver = new RemoteWebDriver(browserStackUri, executionConfiguration.DriverOptions);
                var browserStackPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().BrowserStack.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(browserStackPageLoadTimeout);
                var browserStackScriptTimeout = ConfigurationService.GetSection <WebSettings>().BrowserStack.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(browserStackScriptTimeout);
                break;

            case ExecutionType.CrossBrowserTesting:
                var crossBrowserTestingUri = ConfigurationService.GetSection <WebSettings>().CrossBrowserTesting.GridUri;
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().CrossBrowserTesting;
                if (crossBrowserTestingUri == null || !Uri.IsWellFormedUriString(crossBrowserTestingUri.ToString(), UriKind.Absolute))
                {
                    throw new ArgumentException("To execute your tests in CrossBrowserTesting you need to set the gridUri in the browserSettings file.");
                }

                wrappedWebDriver = new RemoteWebDriver(crossBrowserTestingUri, executionConfiguration.DriverOptions);
                var crossBrowserTestingPageLoadTimeout        = ConfigurationService.GetSection <WebSettings>().CrossBrowserTesting.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(crossBrowserTestingPageLoadTimeout);
                var crossBrowserTestingScriptTimeout          = ConfigurationService.GetSection <WebSettings>().CrossBrowserTesting.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(crossBrowserTestingScriptTimeout);
                break;

            case ExecutionType.SauceLabs:
                var sauceLabsSettings = ConfigurationService.GetSection <WebSettings>().SauceLabs;
                var sauceLabsUri      = sauceLabsSettings.GridUri;
                if (sauceLabsUri == null || !Uri.IsWellFormedUriString(sauceLabsUri.ToString(), UriKind.Absolute))
                {
                    throw new ArgumentException("To execute your tests in SauceLabs you need to set the gridUri in the browserSettings file.");
                }

                wrappedWebDriver = new RemoteWebDriver(sauceLabsUri, executionConfiguration.DriverOptions);
                var sauceLabsPageLoadTimeout = sauceLabsSettings.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(sauceLabsPageLoadTimeout);
                var sauceLabsScriptTimeout = sauceLabsSettings.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(sauceLabsScriptTimeout);
                BrowserSettings = sauceLabsSettings;
                break;
            }

            if (executionConfiguration.BrowserType != BrowserType.Edge)
            {
                FixDriverCommandExecutionDelay((RemoteWebDriver)wrappedWebDriver);
            }

            ChangeWindowSize(executionConfiguration.BrowserType, executionConfiguration.Size, wrappedWebDriver);

            return(wrappedWebDriver);
        }
        private static IWebDriver InitializeDriverRegularMode(BrowserConfiguration executionConfiguration, OpenQA.Selenium.Proxy webDriverProxy)
        {
            IWebDriver wrappedWebDriver;

            switch (executionConfiguration.BrowserType)
            {
            case BrowserType.Chrome:
                new DriverManager().SetUpDriver(new ChromeConfig());
                var chromeDriverService = ChromeDriverService.CreateDefaultService(_driverExecutablePath);
                chromeDriverService.SuppressInitialDiagnosticInformation = true;
                chromeDriverService.EnableVerboseLogging = false;
                chromeDriverService.Port = GetFreeTcpPort();
                var chromeOptions = GetChromeOptions(executionConfiguration.ClassFullName);
                chromeOptions.AddArguments("--log-level=3");
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    chromeOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new ChromeDriver(chromeDriverService, chromeOptions);
                var chromePageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Chrome.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(chromePageLoadTimeout);
                var chromeScriptTimeout = ConfigurationService.GetSection <WebSettings>().Chrome.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(chromeScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Chrome;
                break;

            case BrowserType.ChromeHeadless:
                new DriverManager().SetUpDriver(new ChromeConfig());
                var chromeHeadlessDriverService = ChromeDriverService.CreateDefaultService(_driverExecutablePath);
                chromeHeadlessDriverService.SuppressInitialDiagnosticInformation = true;
                chromeHeadlessDriverService.Port = GetFreeTcpPort();
                var chromeHeadlessOptions = GetChromeOptions(executionConfiguration.ClassFullName);
                chromeHeadlessOptions.AddArguments("--headless");
                chromeHeadlessOptions.AddArguments("--log-level=3");
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    chromeHeadlessOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new ChromeDriver(chromeHeadlessDriverService, chromeHeadlessOptions);
                var chromeHeadlessPageLoadTimeout             = ConfigurationService.GetSection <WebSettings>().ChromeHeadless.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(chromeHeadlessPageLoadTimeout);
                var chromeHeadlessScriptTimeout = ConfigurationService.GetSection <WebSettings>().ChromeHeadless.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(chromeHeadlessScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().ChromeHeadless;
                break;

            case BrowserType.Firefox:
                new DriverManager().SetUpDriver(new FirefoxConfig());
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var firefoxOptions = GetFirefoxOptions(executionConfiguration.ClassFullName);
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    firefoxOptions.Proxy = webDriverProxy;
                }

                var firefoxService = FirefoxDriverService.CreateDefaultService(_driverExecutablePath);
                firefoxService.SuppressInitialDiagnosticInformation = true;
                firefoxService.Port = GetFreeTcpPort();
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
                    {
                        firefoxService.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                    }
                    else
                    {
                        firefoxService.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
                    }

                    // TODO: Anton(15.12.2019): Add option to set the path via environment variable.
                }

                var firefoxProfile = ServicesCollection.Current.Resolve <FirefoxProfile>(executionConfiguration.ClassFullName);
                if (firefoxProfile != null)
                {
                    firefoxOptions.Profile = firefoxProfile;
                }

                var firefoxTimeout = TimeSpan.FromSeconds(180);
                wrappedWebDriver = new FirefoxDriver(firefoxService, firefoxOptions, firefoxTimeout);
                var firefoxPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Firefox.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(firefoxPageLoadTimeout);
                var firefoxScriptTimeout = ConfigurationService.GetSection <WebSettings>().Firefox.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(firefoxScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Firefox;
                break;

            case BrowserType.FirefoxHeadless:
                new DriverManager().SetUpDriver(new FirefoxConfig());
                var firefoxHeadlessOptions = GetFirefoxOptions(executionConfiguration.ClassFullName);
                firefoxHeadlessOptions.AddArguments("--headless");
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    firefoxHeadlessOptions.Proxy = webDriverProxy;
                }

                var service = FirefoxDriverService.CreateDefaultService(_driverExecutablePath);
                service.SuppressInitialDiagnosticInformation = true;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
                    {
                        service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                    }
                    else
                    {
                        service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
                    }

                    // TODO: Anton(15.12.2019): Add option to set the path via environment variable.
                }

                service.Port     = GetFreeTcpPort();
                wrappedWebDriver = new FirefoxDriver(service, firefoxHeadlessOptions);
                var firefoxHeadlessPageLoadTimeout            = ConfigurationService.GetSection <WebSettings>().FirefoxHeadless.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(firefoxHeadlessPageLoadTimeout);
                var firefoxHeadlessScriptTimeout = ConfigurationService.GetSection <WebSettings>().FirefoxHeadless.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(firefoxHeadlessScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().FirefoxHeadless;
                break;

            case BrowserType.Edge:
                new DriverManager().SetUpDriver(new EdgeConfig());
                var edgeDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService(_driverExecutablePath);
                edgeDriverService.SuppressInitialDiagnosticInformation = true;
                var edgeOptions = GetEdgeOptions(executionConfiguration.ClassFullName);
                edgeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
                edgeOptions.UseChromium      = true;
                edgeOptions.AddArguments("--log-level=3");
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    edgeOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new Microsoft.Edge.SeleniumTools.EdgeDriver(edgeDriverService, edgeOptions);
                var edgePageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Edge.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(edgePageLoadTimeout);
                var edgeScriptTimeout = ConfigurationService.GetSection <WebSettings>().Edge.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(edgeScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Edge;
                break;

            case BrowserType.EdgeHeadless:
                new DriverManager().SetUpDriver(new EdgeConfig());
                var edgeHeadlessDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService(_driverExecutablePath);
                edgeHeadlessDriverService.SuppressInitialDiagnosticInformation = true;
                var edgeHeadlessOptions = GetEdgeOptions(executionConfiguration.ClassFullName);
                edgeHeadlessOptions.AddArguments("--headless");
                edgeHeadlessOptions.AddArguments("--log-level=3");
                edgeHeadlessOptions.PageLoadStrategy = PageLoadStrategy.Normal;
                edgeHeadlessOptions.UseChromium      = true;
                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    edgeHeadlessOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new Microsoft.Edge.SeleniumTools.EdgeDriver(edgeHeadlessDriverService, edgeHeadlessOptions);
                var edgeHeadlessPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Edge.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(edgeHeadlessPageLoadTimeout);
                var edgeHeadlessScriptTimeout = ConfigurationService.GetSection <WebSettings>().Edge.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(edgeHeadlessScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Edge;
                break;

            case BrowserType.Opera:
                new DriverManager().SetUpDriver(new OperaConfig());

                // the driver will be different for different OS.
                // Check for different releases- https://github.com/operasoftware/operachromiumdriver/releases
                var operaOptions = GetOperaOptions(executionConfiguration.ClassFullName);

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    operaOptions.Proxy = webDriverProxy;
                }

                var operaService = OperaDriverService.CreateDefaultService(_driverExecutablePath);
                operaService.SuppressInitialDiagnosticInformation = true;
                operaService.Port = GetFreeTcpPort();

                try
                {
                    wrappedWebDriver = new OperaDriver(operaService, operaOptions);
                }
                catch (WebDriverException ex) when(ex.Message.Contains("DevToolsActivePort file doesn't exist"))
                {
                    throw new Exception("This is a known issue in the latest versions of Opera driver. It is reported to the Opera team. As soon it is fixed we will update BELLATRIX.", ex);
                }

                var operaPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Opera.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(operaPageLoadTimeout);
                var operaScriptTimeout = ConfigurationService.GetSection <WebSettings>().Opera.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(operaScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Opera;
                break;

            case BrowserType.InternetExplorer:
                new DriverManager().SetUpDriver(new InternetExplorerConfig());

                // Steps to configure IE to always allow blocked content:
                // From Internet Explorer, select the Tools menu, then the Options...
                // In the Internet Options dialog, select the Advanced tab...
                // Scroll down until you see the Security options. Enable the checkbox "Allow active content to run in files on My Computer"
                // Also, check https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
                // in case of OpenQA.Selenium.NoSuchWindowException: Unable to get browser --> Uncheck IE Options --> Security Tab -> Uncheck "Enable Protected Mode"
                var ieOptions = GetInternetExplorerOptions(executionConfiguration.ClassFullName);
                ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
                ieOptions.IgnoreZoomLevel      = true;
                ieOptions.EnableNativeEvents   = false;
                ieOptions.EnsureCleanSession   = true;
                ieOptions.PageLoadStrategy     = PageLoadStrategy.Eager;
                ieOptions.ForceShellWindowsApi = true;
                ieOptions.AddAdditionalCapability("disable-popup-blocking", true);

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    ieOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new InternetExplorerDriver(_driverExecutablePath, ieOptions);

                var iePageLoadTimeout = ConfigurationService.GetSection <WebSettings>().InternetExplorer.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(iePageLoadTimeout);
                var ieScriptTimeout = ConfigurationService.GetSection <WebSettings>().InternetExplorer.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(ieScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().InternetExplorer;
                break;

            case BrowserType.Safari:
                var safariOptions = GetSafariOptions(executionConfiguration.ClassFullName);

                if (executionConfiguration.ShouldCaptureHttpTraffic && _proxyService.IsEnabled)
                {
                    safariOptions.Proxy = webDriverProxy;
                }

                wrappedWebDriver = new SafariDriver(safariOptions);

                var safariPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().Safari.PageLoadTimeout;
                wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(safariPageLoadTimeout);
                var safariScriptTimeout = ConfigurationService.GetSection <WebSettings>().Safari.ScriptTimeout;
                wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(safariScriptTimeout);
                BrowserSettings = ConfigurationService.GetSection <WebSettings>().Safari;
                break;

            default:
                throw new NotSupportedException($"Not supported browser {executionConfiguration.BrowserType}");
            }

            return(wrappedWebDriver);
        }