/// <summary> /// Creates the web driver. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The created web driver.</returns> /// <exception cref="System.InvalidOperationException">Thrown if the browser is not supported.</exception> internal static IWebDriver CreateWebDriver(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration) { IWebDriver driver; if (!RemoteDriverExists(browserFactoryConfiguration.Settings, browserType, out driver)) { switch (browserType) { case BrowserType.IE: var explorerOptions = new InternetExplorerOptions { EnsureCleanSession = browserFactoryConfiguration.EnsureCleanSession }; var internetExplorerDriverService = InternetExplorerDriverService.CreateDefaultService(); internetExplorerDriverService.HideCommandPromptWindow = true; driver = new InternetExplorerDriver(internetExplorerDriverService, explorerOptions); break; case BrowserType.FireFox: driver = GetFireFoxDriver(browserFactoryConfiguration); break; case BrowserType.Chrome: var chromeOptions = new ChromeOptions { LeaveBrowserRunning = false }; var chromeDriverService = ChromeDriverService.CreateDefaultService(); chromeDriverService.HideCommandPromptWindow = true; driver = new ChromeDriver(chromeDriverService, chromeOptions); break; case BrowserType.PhantomJS: var phantomJsDriverService = PhantomJSDriverService.CreateDefaultService(); phantomJsDriverService.HideCommandPromptWindow = true; driver = new PhantomJSDriver(phantomJsDriverService); break; case BrowserType.Safari: driver = new SafariDriver(); break; default: throw new InvalidOperationException(string.Format("Browser type '{0}' is not supported in Selenium local mode. Did you mean to configure a remote driver?", browserType)); } } // Set Driver Settings var managementSettings = driver.Manage(); // Set timeouts var applicationConfiguration = SpecBind.Helpers.SettingHelper.GetConfigurationSection().Application; managementSettings.Timeouts() .ImplicitlyWait(browserFactoryConfiguration.ElementLocateTimeout) .SetPageLoadTimeout(browserFactoryConfiguration.PageLoadTimeout); ActionBase.DefaultTimeout = browserFactoryConfiguration.ElementLocateTimeout; WaitForPageAction.DefaultTimeout = browserFactoryConfiguration.PageLoadTimeout; ActionBase.RetryValidationUntilTimeout = applicationConfiguration.RetryValidationUntilTimeout; // Maximize window managementSettings.Window.Maximize(); return driver; }
/// <summary> /// Creates the web driver. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The created web driver.</returns> /// <exception cref="System.InvalidOperationException">Thrown if the browser is not supported.</exception> internal static IWebDriver CreateWebDriver(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration) { IWebDriver driver; if (!RemoteDriverExists(browserFactoryConfiguration.Settings, browserType, out driver)) { switch (browserType) { case BrowserType.IE: var explorerOptions = new InternetExplorerOptions { EnsureCleanSession = browserFactoryConfiguration.EnsureCleanSession }; driver = new InternetExplorerDriver(explorerOptions); break; case BrowserType.FireFox: driver = GetFireFoxDriver(browserFactoryConfiguration); break; case BrowserType.Chrome: var chromeOptions = new ChromeOptions { LeaveBrowserRunning = false }; if (browserFactoryConfiguration.EnsureCleanSession) { chromeOptions.AddArgument("--incognito"); } driver = new ChromeDriver(); break; case BrowserType.PhantomJS: driver = new PhantomJSDriver(); break; case BrowserType.Safari: driver = new SafariDriver(); break; default: throw new InvalidOperationException(string.Format("Browser type '{0}' is not supported in Selenium local mode. Did you mean to configure a remote driver?", browserType)); } } // Set Driver Settings var managementSettings = driver.Manage(); // Set timeouts managementSettings.Timeouts() .ImplicitlyWait(browserFactoryConfiguration.ElementLocateTimeout) .SetPageLoadTimeout(browserFactoryConfiguration.PageLoadTimeout); // Maximize window managementSettings.Window.Maximize(); return driver; }
/// <summary> /// Creates the browser. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <param name="logger">The logger.</param> /// <returns>A browser object.</returns> /// <exception cref="System.InvalidOperationException">Thrown if the browser type is not supported.</exception> protected override IBrowser CreateBrowser(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration, ILogger logger) { string browserKey = null; switch (browserType) { case BrowserType.FireFox: browserKey = "firefox"; break; case BrowserType.Chrome: browserKey = "chrome"; break; case BrowserType.IE: break; default: throw new InvalidOperationException(string.Format("Browser type '{0}' is not supported in Coded UI.", browserType)); } Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.Control; Playback.PlaybackSettings.SearchTimeout = (int)browserFactoryConfiguration.ElementLocateTimeout.TotalMilliseconds; Playback.PlaybackSettings.WaitForReadyTimeout = (int)browserFactoryConfiguration.PageLoadTimeout.TotalMilliseconds; var launchAction = new Func<BrowserWindow>(() => { //Switch key if needed. if (browserKey != null) { BrowserWindow.CurrentBrowser = browserKey; } var window = BrowserWindow.Launch(); if (browserFactoryConfiguration.EnsureCleanSession) { BrowserWindow.ClearCache(); BrowserWindow.ClearCookies(); } return window; }); var browser = new Lazy<BrowserWindow>(launchAction, LazyThreadSafetyMode.None); return new CodedUIBrowser(browser, logger); }
/// <summary> /// Loads the configuration and creates the browser object. /// </summary> /// <param name="createMethod">The create method.</param> /// <returns>The <see cref="IBrowser"/> object.</returns> private IBrowser LoadConfigurationAndCreateBrowser(Func<BrowserType, BrowserFactoryConfigurationElement, ILogger, IBrowser> createMethod) { var configSection = SettingHelper.GetConfigurationSection(); // Default configuration settings var browserFactoryConfiguration = new BrowserFactoryConfigurationElement { BrowserType = Enum.GetName(typeof(BrowserType), BrowserType.IE), ElementLocateTimeout = TimeSpan.FromSeconds(30.0), PageLoadTimeout = TimeSpan.FromSeconds(30.0) }; if (configSection != null && configSection.BrowserFactory != null) { browserFactoryConfiguration = configSection.BrowserFactory; } var browserType = this.GetBrowserType(browserFactoryConfiguration); return createMethod(browserType, browserFactoryConfiguration, this.Logger); }
/// <summary> /// Validates the driver setup. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> protected virtual void ValidateDriverSetup(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration) { }
/// <summary> /// Gets the type of the browser to leverage. /// </summary> /// <param name="section">The configuration section.</param> /// <returns>The browser type.</returns> protected virtual BrowserType GetBrowserType(BrowserFactoryConfigurationElement section) { BrowserType browserType; if (!string.IsNullOrWhiteSpace(section.BrowserType) && Enum.TryParse(section.BrowserType, true, out browserType)) { return browserType; } return BrowserType.IE; }
/// <summary> /// Creates the browser. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <param name="logger">The logger.</param> /// <returns>A browser object.</returns> protected abstract IBrowser CreateBrowser(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration, ILogger logger);
/// <summary> /// Gets the FireFox driver. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The configured web driver.</returns> private static IWebDriver GetFireFoxDriver(BrowserFactoryConfigurationElement browserFactoryConfiguration) { IWebDriver driver; if (browserFactoryConfiguration.Settings != null && browserFactoryConfiguration.Settings.Count > 0) { var ffprofile = new FirefoxProfile(); foreach (NameValueConfigurationElement configurationElement in browserFactoryConfiguration.Settings) { // Removed debug lines but left in comments for future logger support // Debug.WriteLine("SpecBind.Selenium.SeleniumBrowserFactory.GetFireFoxDriver: Setting firefox profile setting:{0} with value: {1}", configurationElement.Name, configurationElement.Value); bool boolValue; int intValue; if (int.TryParse(configurationElement.Value, out intValue)) { // Debug.WriteLine("SpecBind.Selenium.SeleniumBrowserFactory.GetFireFoxDriver: Setting firefox profile setting with int value: '{0}'", configurationElement.Name); ffprofile.SetPreference(configurationElement.Name, intValue); } else if (bool.TryParse(configurationElement.Value, out boolValue)) { // Debug.WriteLine("SpecBind.Selenium.SeleniumBrowserFactory.GetFireFoxDriver: Setting firefox profile setting with bool value: '{0}'", configurationElement.Name); ffprofile.SetPreference(configurationElement.Name, boolValue); } else { // Debug.WriteLine("SpecBind.Selenium.SeleniumBrowserFactory.GetFireFoxDriver: Setting firefox profile setting with string value: '{0}'", configurationElement.Name); ffprofile.SetPreference(configurationElement.Name, configurationElement.Value); } } driver = new FirefoxDriver(ffprofile); } else { driver = new FirefoxDriver(); } if (browserFactoryConfiguration.EnsureCleanSession) { driver.Manage().Cookies.DeleteAllCookies(); } return driver; }
/// <summary> /// Validates the driver setup. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> protected override void ValidateDriverSetup(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration) { // If we're using a remote driver, don't check paths if (GetRemoteDriverUri(browserFactoryConfiguration.Settings) != null) { return; } try { var driver = CreateWebDriver(browserType, browserFactoryConfiguration); driver.Quit(); } catch (DriverServiceNotFoundException ex) { if (SeleniumDriverPath == null) { // Error if we weren't able to construct a path earlier. throw; } try { switch (browserType) { case BrowserType.IE: DownloadIeDriver(); break; case BrowserType.Chrome: DownloadChromeDriver(); break; case BrowserType.PhantomJS: DownloadPhantomJsDriver(); break; default: throw; } } catch (Exception) { throw ex; } } }
/// <summary> /// Creates the browser. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <param name="logger">The logger.</param> /// <returns>A browser object.</returns> /// <exception cref="System.InvalidOperationException">Thrown if the browser type is not supported.</exception> protected override IBrowser CreateBrowser(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration, ILogger logger) { var launchAction = new Func<IWebDriver>(() => CreateWebDriver(browserType, browserFactoryConfiguration)); var browser = new Lazy<IWebDriver>(launchAction, LazyThreadSafetyMode.None); return new SeleniumBrowser(browser, logger); }
/// <summary> /// Creates the browser. /// </summary> /// <param name="browserType">Type of the browser.</param> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <param name="logger">The logger.</param> /// <returns>A browser object.</returns> protected override IBrowser CreateBrowser(BrowserType browserType, BrowserFactoryConfigurationElement browserFactoryConfiguration, ILogger logger) { BrowserMock = new Mock<IBrowser>(); return BrowserMock.Object; }