예제 #1
0
        /// <summary>
        /// Instead of creating elegant and small method I have to develop this because AddAdditionalCapability does not set isGlobalCapability == true
        /// implicitly for some browsers. So for Chrome, IE, Firefox you should do it manually, for Safari, Edge - not (AddAdditionalCapability does not
        /// even have such parameter, same as base DriverOptions).
        /// </summary>
        /// <param name="options">Driver options.</param>
        /// <returns>DriverOptions.</returns>
        private static DriverOptions GetRemoteDesktopDriverOptions(DriverOptions options)
        {
            var capabilities = GetRemoteDesktopCapabilitiesSet();

            if (options.GetType() == typeof(ChromeOptions))
            {
                var chromeOptions = (ChromeOptions)options;
                chromeOptions.AddArgument(BrowserArguments.DisableWebSecurity);
                chromeOptions.AddArgument(BrowserArguments.DisableSiteIsolationTrials);
                capabilities.ToList().ForEach(pair => chromeOptions.AddAdditionalCapability(pair.Key, pair.Value, true));

                return(options);
            }

            if (options.GetType() == typeof(FirefoxOptions))
            {
                var firefoxOptions = (FirefoxOptions)options;
                capabilities.ToList().ForEach(pair => firefoxOptions.AddAdditionalCapability(pair.Key, pair.Value, true));

                return(options);
            }

            if (options.GetType() == typeof(InternetExplorerOptions))
            {
                var internetExplorerOptions = (InternetExplorerOptions)options;
                capabilities.ToList().ForEach(pair => internetExplorerOptions.AddAdditionalCapability(pair.Key, pair.Value, true));

                return(options);
            }

            capabilities.ToList().ForEach(pair => options.AddAdditionalCapability(pair.Key, pair.Value));

            return(options);
        }
예제 #2
0
        private void SetOptionByPropertyName(DriverOptions options, KeyValuePair <string, object> option, Exception exception)
        {
            var optionProperty = options
                                 .GetType()
                                 .GetProperties()
                                 .FirstOrDefault(property => IsPropertyNameMatchOption(property.Name, option.Key) && property.CanWrite)
                                 ?? throw exception;

            optionProperty.SetValue(options, option.Value);
        }
        private void SetOptionByPropertyName(DriverOptions options, KeyValuePair <string, object> option, Exception exception)
        {
            var optionProperty = options
                                 .GetType()
                                 .GetProperties()
                                 .FirstOrDefault(property => IsPropertyNameMatchOption(property.Name, option.Key) && property.CanWrite)
                                 ?? throw exception;
            var propertyType = optionProperty.PropertyType;
            var valueToSet   = IsEnumValue(propertyType, option.Value)
                ? ParseEnumValue(propertyType, option.Value)
                : option.Value;

            optionProperty.SetValue(options, valueToSet);
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionRequest"/> class.
        /// </summary>
        /// <param name="reportSettings">Report settings with the project and job names to report.</param>
        /// <param name="capabilities">Capabilities that should be sent to the Agent for driver initialization.</param>
        public SessionRequest(ReportSettings reportSettings, DriverOptions capabilities)
        {
            if (reportSettings != null)
            {
                this.ProjectName = reportSettings.ProjectName;
                this.JobName     = reportSettings.JobName;
            }

            // Convert DriverOptions to a format that preserves arguments and extensions when serializing it.
            if (capabilities.GetType().Equals(typeof(GenericOptions)))
            {
                this.Capabilities = ((GenericOptions)capabilities).ToDictionary();
            }
            else
            {
                this.Capabilities = capabilities.ToString().FromJson <Dictionary <string, object> >();
            }
        }
예제 #5
0
        public IWebDriver GetBrowser(BrowserType browser, DriverOptions options = null, LogLevel logLevel = LogLevel.Severe)
        {
            if (!string.IsNullOrEmpty(WebDriverSettings.SeleniumGridServer))
            {
                Log.Debug("Selenium Grid Server is specified in app.config, will attempt to retrieve Browser from Grid...");
                return(GetRemoteBrowser(browser, WebDriverSettings.BrowserVersion, options, logLevel));
            }

            Log.Debug($"EXECUTING: GetBrowser(): {browser},{(options != null ? options.GetType().Name : string.Empty)},{logLevel}");
            var driverPath = Path.GetDirectoryName(new Uri(typeof(WebDriverFactory).Assembly.CodeBase).LocalPath);

            Log.Debug($"Path to Driver binaries: {driverPath}");

            SetupDriverOptions(browser, options, logLevel);

            switch (browser)
            {
            case BrowserType.Chrome:
                // Prevents "Chrome being controlled by automated test software" message from showing
                _chromeOpts.AddArgument("disable-infobars");
                return(new ChromeDriver(driverPath, _chromeOpts));

            case BrowserType.Edge:
                return(new EdgeDriver(driverPath, _edgeOpts));

            case BrowserType.Firefox:
                // 60 Seconds is the Default Max Command Timeout for RemoteWebDriver - https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Remote/RemoteWebDriver.cs#L69
                return(new FirefoxDriver(FirefoxDriverService.CreateDefaultService(driverPath), _firefoxOpts, TimeSpan.FromSeconds(60)));

            case BrowserType.IE:
                return(new InternetExplorerDriver(driverPath, _ieOpts));

            case BrowserType.Phantomjs:
                return(new PhantomJSDriver(driverPath, _phantomJsOpts));

            case BrowserType.Safari:
                return(new SafariDriver(driverPath, _safariOpts));

            default:
                throw new Exception($"Unsupported BrowserType={browser}");
            }
        }
예제 #6
0
 public HeadlessDriver(DriverOptions options = null, bool hideCommandPromptWindow = true)
 {
     if (typeof(T) == typeof(ChromeDriver))
     {
         DriverService = ChromeDriverService.CreateDefaultService();
         DriverOptions = options;
         if (options == null)
         {
             var opt = new ChromeOptions();
             opt.AddArgument("--headless");
             DriverOptions = opt;
         }
     }
     else if (typeof(T) == typeof(FirefoxDriver))
     {
         DriverService = FirefoxDriverService.CreateDefaultService();
         DriverOptions = options;
         if (options == null)
         {
             var opt = new FirefoxOptions();
             opt.AddArgument("--headless");
             DriverOptions = opt;
         }
     }
     else
     {
         throw new NotImplementedException($"{typeof(T).Name} doesn't support headless mode.");
     }
     DriverService.HideCommandPromptWindow = hideCommandPromptWindow;
     try
     {
         Driver = (T)Activator.CreateInstance(typeof(T), DriverService, DriverOptions,
                                              TimeSpan.FromSeconds(60.0));
     }
     catch (MissingMethodException)
     {
         throw new MissingMethodException(
                   $"Couldn't initialize {typeof(T).Name} with {options?.GetType().Name}.");
     }
 }
예제 #7
0
        public static DriverOptions AddCapabilities(this DriverOptions options, Dictionary <string, string> capabilities)
        {
            var _options = options;

            if ((capabilities is null) || (!capabilities.Any()))
            {
                Log.Logger().LogInformation($"Dictionary with capabilities is null or empty. Return {options.GetType().Name.ToLower()} without DesireCapability");
                return(_options);
            }

            foreach (var(key, value) in capabilities)
            {
                _options.AddAdditionalCapability(key, value);
            }

            return(_options);
        }
예제 #8
0
        private void StartSdkSession(IRestResponse startSessionResponse, DriverOptions capabilities)
        {
            if (capabilities is AppiumOptions)
            {
                // Appium capabilities cannot be deserialized directly into an AppiumOptions instance,
                // so we need to do this ourselves
                Logger.Info($"Creating Appium options from capabilities returned by Agent...");

                AppiumSessionResponse sessionResponse = CustomJsonSerializer.FromJson <AppiumSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                AppiumOptions appiumOptions = this.CreateAppiumOptions(sessionResponse.Capabilities);

                this.CreateAgentSession(sessionResponse, appiumOptions);

                this.OpenSocketConnectionUsing(sessionResponse);
            }
            else
            {
                // Capabilities for web browsers and the generic driver can be serialized directly.
                // Since there's a separate Options class for each supported browser, we'll have to
                // differentiate between them as well.
                if (capabilities is ChromeOptions)
                {
                    ChromeSessionResponse sessionResponse = CustomJsonSerializer.FromJson <ChromeSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else if (capabilities is FirefoxOptions)
                {
                    FirefoxSessionResponse sessionResponse = CustomJsonSerializer.FromJson <FirefoxSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else if (capabilities is EdgeOptions)
                {
                    EdgeSessionResponse sessionResponse = CustomJsonSerializer.FromJson <EdgeSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else if (capabilities is SafariOptions)
                {
                    SafariSessionResponse sessionResponse = CustomJsonSerializer.FromJson <SafariSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else if (capabilities is InternetExplorerOptions)
                {
                    IESessionResponse sessionResponse = CustomJsonSerializer.FromJson <IESessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else if (capabilities is GenericOptions)
                {
                    GenericSessionResponse sessionResponse = CustomJsonSerializer.FromJson <GenericSessionResponse>(startSessionResponse.Content, this.serializerSettings);

                    this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities);

                    this.OpenSocketConnectionUsing(sessionResponse);
                }
                else
                {
                    throw new SdkException($"The options type '{capabilities.GetType().Name}' is not supported by the OpenSDK.");
                }
            }
        }
예제 #9
0
        private IWebDriver GetRemoteBrowser(BrowserType browser, string version = null, DriverOptions options = null, LogLevel logLevel = LogLevel.Severe)
        {
            var gridAddress = new Uri($"{WebDriverSettings.SeleniumGridServer}/wd/hub");

            Log.Debug($"EXECUTING: GetRemoteBrowser(): {gridAddress.AbsoluteUri},{browser},{(!string.IsNullOrEmpty(version) ? version : string.Empty)},{(options != null ? options.GetType().Name : string.Empty)},{logLevel}");
            ICapabilities capabillities;

            SetupDriverOptions(browser, options, logLevel);

            switch (browser)
            {
            case BrowserType.Chrome:
                capabillities = _chromeOpts.ToCapabilities();
                break;

            case BrowserType.Edge:
                capabillities = _edgeOpts.ToCapabilities();
                break;

            case BrowserType.Firefox:
                capabillities = _firefoxOpts.ToCapabilities();
                break;

            case BrowserType.IE:
                _ieOpts.AddAdditionalCapability("version", !string.IsNullOrEmpty(version) ? version : "11");
                capabillities = _ieOpts.ToCapabilities();
                break;

            case BrowserType.Phantomjs:
                capabillities = _phantomJsOpts.ToCapabilities();
                break;

            case BrowserType.Safari:
                capabillities = _safariOpts.ToCapabilities();
                break;

            default:
                throw new Exception($"Unsupported BrowserType={browser}");
            }

            try
            {
                return(new RemoteWebDriver(gridAddress, capabillities));
            }

            catch (Exception e)
            {
                if (e is WebDriverTimeoutException || e is WebDriverException)
                {
                    var msg = $"Unable to connect to Selenium Grid: {gridAddress}, {e.Message}";
                    throw new Exception(msg, e);
                }

                throw new Exception($"An exception has occurred: {e.Message}", e);
            }
        }