public static IWebDriver CreateRemoteDriver(this WebDriverConfig config)
        {
            config.ValidateNotNull(nameof(config));
            config.ValidateDriverName("Remote");

            Uri driverUri;

            if (config.DriverLocation == null)
            {
                driverUri = new Uri("http://127.0.0.1:4444/wd/hub/");
            }
            else
            {
                driverUri = new Uri(config.DriverLocation);
            }
            var capabilities = new RemoteSessionSettings();

            if (config.Capabilities != null)
            {
                foreach (var capability in config.Capabilities)
                {
                    capabilities.AddMetadataSetting(capability.Key, capability.Value);
                }
            }
            var commandTimeout = GetCommandTimeout(config);

            return(new RemoteWebDriver(driverUri, capabilities, commandTimeout)
                   .ApplyCommon(config));
        }
        public static IWebDriver CreateDriver(this string driverName)
        {
            var config = new WebDriverConfig {
                Name = driverName
            };

            return(config.CreateDriver());
        }
 public static void ValidateDriverName(this WebDriverConfig config, string driverName)
 {
     if (!string.Equals(config.Name, driverName, StringComparison.OrdinalIgnoreCase))
     {
         throw new InvalidOperationException(
                   $"Invalid driver name '{config.Name}'! " +
                   $"Can't create {driverName}Driver from a configuration that specifies a diffrent driver than {driverName}.");
     }
 }
        private static IWebDriver ApplyCommon(this IWebDriver driver, WebDriverConfig config)
        {
            driver.ApplyElementWaitTimeout(config)
            .ApplyPageLoadTimeout(config)
            .ApplyImplicitWaitTimeout(config)
            .SetScreenshotsFolder(config.ScreenshotsFolder);

            return(driver);
        }
 private static void AddAdditionalCapabilies(this DriverOptions options, WebDriverConfig config)
 {
     if (config.AdditionalCapabilities != null)
     {
         foreach (var capability in config.AdditionalCapabilities)
         {
             options.AddAdditionalCapability(capability.Key, capability.Value);
         }
     }
 }
        private static TimeSpan GetCommandTimeout(this WebDriverConfig config)
        {
            var isValid = int.TryParse(config.CommandTimeoutMin, out var commandTimeoutMin);

            if (!isValid)
            {
                commandTimeoutMin = 1; // default
            }

            return(TimeSpan.FromMinutes(commandTimeoutMin));
        }
 private static Uri GetRemoteHubUri(this WebDriverConfig config)
 {
     if (config.DriverLocation == null)
     {
         return(new Uri("http://127.0.0.1:4444/wd/hub/"));
     }
     else
     {
         return(new Uri(config.DriverLocation));
     }
 }
        private static string GetDriverPath(this WebDriverConfig config)
        {
            var folder = config.DriverLocation;

            if (folder == null)
            {
                return(Utils.GetExecutigAssemblyPath());
            }
            if (!Path.IsPathRooted(folder))
            {
                folder = Path.Combine(Utils.GetExecutigAssemblyPath(), folder);
            }
            return(folder);
        }
        public static IWebDriver CreateDriver(this WebDriverConfig config)
        {
            var name = config?.Name ?? "null";

            switch (name.ToLower())
            {
            case "chrome": return(config.CreateChromeDriver());

            case "firefox": return(config.CreateFirefoxDriver());
            }

            throw new ArgumentException(
                      $"Invalid driver name '{config.Name}'! " +
                      $"Supported drivers are: Chrome and FireFox.");
        }
        private static IWebDriver ApplyImplicitWaitTimeout(this IWebDriver driver, WebDriverConfig config)
        {
            if (config.ImplicitElementWaitTimeoutMs != null)
            {
                var isValid = int.TryParse(config.ImplicitElementWaitTimeoutMs, out int implicitElementWaitTimeoutMs);
                if (!isValid)
                {
                    throw new ArgumentException($"Invalid {nameof(config.ImplicitElementWaitTimeoutMs)}!");
                }
                driver.SetImplicitElementWaitTimeoutMs(implicitElementWaitTimeoutMs);
            }
            else
            {
                driver.SetImplicitElementWaitTimeoutMs(0); // no wait
            }

            return(driver);
        }
        private static IWebDriver ApplyPageLoadTimeout(this IWebDriver driver, WebDriverConfig config)
        {
            if (config.PageLoadTimeoutMs != null)
            {
                var isValid = int.TryParse(config.PageLoadTimeoutMs, out int pageLoadTimeoutMs);
                if (!isValid)
                {
                    throw new ArgumentException($"Invalid {nameof(config.PageLoadTimeoutMs)}!");
                }
                driver.SetPageLoadTimeoutMs(pageLoadTimeoutMs);
            }
            else
            {
                driver.SetPageLoadTimeoutMs(60000); // 60 seconds
            }

            return(driver);
        }
        private static IWebDriver ApplyElementWaitTimeout(this IWebDriver driver, WebDriverConfig config)
        {
            if (config.ElementWaitTimeoutMs != null)
            {
                var isValid = int.TryParse(config.ElementWaitTimeoutMs, out int elementWaitTimeoutMs);
                if (!isValid)
                {
                    throw new ArgumentException($"Invalid {nameof(config.ElementWaitTimeoutMs)}!");
                }
                driver.SetElementWaitTimeoutMs(elementWaitTimeoutMs);
            }
            else
            {
                driver.SetElementWaitTimeoutMs(5000); // 5 seconds
            }

            return(driver);
        }
        public static IWebDriver CreateFirefoxDriver(this WebDriverConfig config)
        {
            config.ValidateNotNull(nameof(config));
            config.ValidateDriverName("Firefox");

            var options = new FirefoxOptions();

            options.PlatformName   = config.PlatformName;
            options.BrowserVersion = config.BrowserVersion;
            options.AddAdditionalCapabilies(config);
            if (config.Arguments.IsNotNullOrEmpty())
            {
                options.AddArguments(config.Arguments);
            }
            var commandTimeout = GetCommandTimeout(config);

            return((config.IsLocal()
                    ? new FirefoxDriver(GetDriverPath(config), options, commandTimeout)
                    : new RemoteWebDriver(GetRemoteHubUri(config), options.ToCapabilities(), commandTimeout))
                   .ApplyCommon(config));
        }
        public static IWebDriver CreateFirefoxDriver(this WebDriverConfig config)
        {
            config.ValidateNotNull(nameof(config));
            config.ValidateDriverName("Firefox");

            var path    = GetDriverPath(config);
            var options = new FirefoxOptions();

            if (config.Arguments.IsNotNullOrEmpty())
            {
                options.AddArguments(config.Arguments);
            }
            if (config.Capabilities != null)
            {
                foreach (var capability in config.Capabilities)
                {
                    options.AddAdditionalCapability(capability.Key, capability.Value);
                }
            }
            var commandTimeout = GetCommandTimeout(config);

            return(new FirefoxDriver(path, options, commandTimeout)
                   .ApplyCommon(config));
        }
 private static bool IsLocal(this WebDriverConfig config) =>
 !string.IsNullOrWhiteSpace(config.DriverLocation) && !config.DriverLocation.StartsWith("http", StringComparison.OrdinalIgnoreCase);