Beispiel #1
0
        public override IWebDriver CreateDriver(DriverProperty driverProperty)
        {
            ParameterValidator.ValidateNotNull(driverProperty, "Driver Property");

            FirefoxOptions options = new FirefoxOptions();

            if (driverProperty.Headless)
            {
                options.AddArgument("--headless");
                options.SetPreference("intl.accept_languages", "en,en_US");
            }

            if (driverProperty.DownloadLocation != null)
            {
                options.SetPreference("browser.download.folderList", 2);
                options.SetPreference("browser.download.dir", driverProperty.DownloadLocation);
            }

            if (driverProperty.Arguments != null)
            {
                options.AddArguments(driverProperty.GetArgumentsAsArray());
            }

            if (DriverVersion == null)
            {
                lock (Instancelock)
                {
                    if (DriverVersion == null)
                    {
                        if (string.IsNullOrEmpty(driverProperty.DriverVersion))
                        {
                            DriverVersion = GetStableVersion();
                        }
                        else
                        {
                            DriverVersion = driverProperty.DriverVersion;
                        }
                    }
                }
            }

            // run in private mode
            options.AddArgument("--private");

            // Setup driver binary
            try
            {
                new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig(), DriverVersion);
            }
            catch (Exception)
            {
                throw new Exception($"Cannot get Firefox driver version {DriverVersion}");
            }

            return(new FirefoxDriver(options));
        }
        public override IWebDriver CreateDriver(DriverProperty driverProperty)
        {
            ParameterValidator.ValidateNotNull(driverProperty, "Driver Property");

            ChromeOptions options = new ChromeOptions();

            if (driverProperty.Headless)
            {
                options.AddArgument("--headless");
                options.AddArgument("--disable-gpu");
                options.AddUserProfilePreference("disable-popup-blocking", "true");
                options.AddUserProfilePreference("intl.accept_languages", "en,en_US");
            }

            if (driverProperty.DownloadLocation != null)
            {
                options.AddUserProfilePreference("download.default_directory", driverProperty.DownloadLocation);
            }

            if (driverProperty.Arguments != null)
            {
                options.AddArguments(driverProperty.GetArgumentsAsArray());
            }

            if (DriverVersion == null)
            {
                lock (Instancelock)
                {
                    if (DriverVersion == null)
                    {
                        if (string.IsNullOrEmpty(driverProperty.DriverVersion))
                        {
                            DriverVersion = GetStableVersion();
                        }
                        else
                        {
                            DriverVersion = driverProperty.DriverVersion;
                        }
                    }
                }
            }

            // Use custom Chrome binary (81)
            // https://chromium.cypress.io/win64/stable/81.0.4044.92
            // options.BinaryLocation = "C:\\Users\\sbaltuonis\\Downloads\\chrome-win\\chrome.exe";

            // Run in private mode
            options.AddArgument("--incognito");

            // Enable mobile emulation
            if (!string.IsNullOrEmpty(driverProperty.DeviceName))
            {
                options.EnableMobileEmulation(driverProperty.DeviceName);
            }
            else if (!string.IsNullOrEmpty(driverProperty.CustomUserAgent))
            {
                if (driverProperty.CustomWidth <= 0 ||
                    driverProperty.CustomHeight <= 0 ||
                    driverProperty.CustomPixelRatio <= 0)
                {
                    throw new Exception("Custom device width, height and pixel ratio must be greater than 0");
                }

                ChromeMobileEmulationDeviceSettings emulationSettings = new ChromeMobileEmulationDeviceSettings
                {
                    UserAgent  = driverProperty.CustomUserAgent,
                    Width      = driverProperty.CustomWidth,
                    Height     = driverProperty.CustomHeight,
                    PixelRatio = driverProperty.CustomPixelRatio
                };

                options.EnableMobileEmulation(emulationSettings);
            }

            // Setup driver binary
            try
            {
                new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig(), DriverVersion);
            }
            catch (Exception)
            {
                throw new Exception($"Cannot get Chrome driver version {DriverVersion}");
            }

            #pragma warning disable CA2000 // Dispose objects before losing scope
            return(new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromSeconds(180)));

            #pragma warning restore CA2000 // Dispose objects before losing scope
        }