/// <summary> /// Creates the web driver from the specified browser factory configuration. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The configured web driver.</returns> protected override IWebDriver CreateLocalDriver(BrowserFactoryConfiguration browserFactoryConfiguration) { var chromeOptions = new ChromeOptions { LeaveBrowserRunning = false }; if (browserFactoryConfiguration.Settings.ContainsKey(ChromeArgumentSetting)) { var cmdLineSetting = browserFactoryConfiguration.Settings[ChromeArgumentSetting]; if (!string.IsNullOrWhiteSpace(cmdLineSetting)) { foreach (var arg in cmdLineSetting.Split(';')) { chromeOptions.AddArgument(arg); } } } foreach (string additionArgument in this.AdditionalArguments) { chromeOptions.AddArgument(additionArgument); } var chromeDriverService = ChromeDriverService.CreateDefaultService("."); chromeDriverService.HideCommandPromptWindow = true; foreach (var preference in browserFactoryConfiguration.UserProfilePreferences) { chromeOptions.AddUserProfilePreference(preference.Key, preference.Value); } return(new ChromeDriver(chromeDriverService, chromeOptions)); }
/// <summary> /// Creates the web driver. /// </summary> /// <param name="seleniumDriver">The selenium driver.</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 IWebDriver CreateWebDriver(ISeleniumDriver seleniumDriver, BrowserFactoryConfiguration browserFactoryConfiguration) { IWebDriver driver = seleniumDriver.Create(browserFactoryConfiguration); // Set Driver Settings var managementSettings = driver.Manage(); // Set timeouts var applicationConfiguration = SettingHelper.GetConfigurationSection().Application; var timeouts = managementSettings.Timeouts(); timeouts.ImplicitWait = browserFactoryConfiguration.ElementLocateTimeout; timeouts.PageLoad = browserFactoryConfiguration.PageLoadTimeout; ActionBase.DefaultTimeout = browserFactoryConfiguration.ElementLocateTimeout; WaitForPageAction.DefaultTimeout = browserFactoryConfiguration.PageLoadTimeout; ActionBase.RetryValidationUntilTimeout = applicationConfiguration.RetryValidationUntilTimeout; if (seleniumDriver.MaximizeWindow) { // Maximize window managementSettings.Window.Maximize(); } return(driver); }
/// <summary> /// Creates the remote driver. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The created remote web driver.</returns> public IWebDriver Create(BrowserFactoryConfiguration browserFactoryConfiguration) { var remoteUri = this.GetRemoteDriverUri(browserFactoryConfiguration); if (remoteUri == null) { return(this.CreateLocalDriver(browserFactoryConfiguration)); } DriverOptions driverOptions = this.CreateRemoteDriverOptions(browserFactoryConfiguration); // Add any additional settings that are not reserved var envRegex = new System.Text.RegularExpressions.Regex("\\$\\{(.+)\\}"); var reservedSettings = new[] { RemoteUrlSetting }; foreach (var setting in browserFactoryConfiguration.Settings .OfType <NameValueConfigurationElement>() .Where(s => reservedSettings .All(r => !string.Equals(r, s.Name, StringComparison.OrdinalIgnoreCase)))) { // Support environment variables var value = setting.Value; var match = envRegex.Match(value); if (match.Success) { value = SettingHelper.GetEnvironmentVariable(match.Groups[1].Value); } driverOptions.AddAdditionalCapability(setting.Name, value); } return(new RemoteScreenshotWebDriver(remoteUri, driverOptions.ToCapabilities())); }
/// <summary> /// Validates the specified browser factory configuration. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <param name="seleniumDriverPath">The selenium driver path.</param> public void Validate(BrowserFactoryConfiguration browserFactoryConfiguration, string seleniumDriverPath) { // If we're using a remote driver, don't check paths if (this.GetRemoteDriverUri(browserFactoryConfiguration) != null) { return; } IWebDriver driver = null; try { driver = this.CreateLocalDriver(browserFactoryConfiguration); driver.Quit(); } catch (DriverServiceNotFoundException ex) { if (seleniumDriverPath == null) { // Error if we weren't able to construct a path earlier. throw; } try { this.Download(seleniumDriverPath); } catch (Exception) { throw ex; } } }
/// <summary> /// Creates the driver options. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The driver options.</returns> protected override DriverOptions CreateRemoteDriverOptions(BrowserFactoryConfiguration browserFactoryConfiguration) { ChromeOptions chromeOptions = new ChromeOptions(); foreach (var preference in browserFactoryConfiguration.UserProfilePreferences) { chromeOptions.AddUserProfilePreference(preference.Key, preference.Value); } return(chromeOptions); }
public void TestCheckForScreenshotWithNoErrorButSettingEnabledTakesSuccessfulScreenshot() { var listener = new Mock <ITraceListener>(MockBehavior.Strict); listener.Setup(l => l.WriteTestOutput(It.Is <string>(s => s.Contains("TestFileName.jpg")))); var scenarioContext = new Mock <IScenarioContextHelper>(MockBehavior.Strict); scenarioContext.Setup(s => s.GetError()).Returns((Exception)null); scenarioContext.Setup(s => s.GetStepFileName(false)).Returns("TestFileName"); var browser = new Mock <IBrowser>(MockBehavior.Strict); browser.Setup(b => b.TakeScreenshot(It.IsAny <string>(), "TestFileName")).Returns("TestFileName.jpg"); browser.Setup(b => b.SaveHtml(It.IsAny <string>(), "TestFileName")).Returns((string)null); browser.Setup(b => b.Close(true)); browser.Setup(b => b.IsCreated).Returns(true); var container = new Mock <IObjectContainer>(MockBehavior.Strict); container.Setup(c => c.Resolve <IScenarioContextHelper>()).Returns(scenarioContext.Object); container.Setup(c => c.Resolve <ITraceListener>()).Returns(listener.Object); // Setup Configuration BrowserFactoryConfiguration config = new BrowserFactoryConfiguration { CreateScreenshotOnExit = true }; var browserFactory = new Mock <BrowserFactory>(config); WebDriverSupport.SetBrowserFactory(browserFactory.Object); WebDriverSupport.SetCurrentBrowser(browser.Object); var driverSupport = new WebDriverSupport(container.Object); driverSupport.CheckForScreenshot(); config.CreateScreenshotOnExit = false; container.VerifyAll(); browser.VerifyAll(); scenarioContext.VerifyAll(); listener.VerifyAll(); }
public void TestTearDownAfterScenarioWhenReuseBrowserIsTrue() { // arrange BrowserFactoryConfiguration config = new BrowserFactoryConfiguration { ReuseBrowser = true }; var browserFactory = new Mock <BrowserFactory>(config); WebDriverSupport.SetBrowserFactory(browserFactory.Object); var browser = new Mock <IBrowser>(MockBehavior.Loose); WebDriverSupport.SetCurrentBrowser(browser.Object); // act WebDriverSupport.TearDownAfterScenario(); // assert browser.Verify(b => b.Close(true), Times.Never()); }
/// <summary> /// Gets the remote driver URI. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The URI if the setting is valid, otherwise <c>null</c>.</returns> /// <exception cref="System.Configuration.ConfigurationErrorsException">Thrown if the URI is not valid.</exception> private Uri GetRemoteDriverUri(BrowserFactoryConfiguration browserFactoryConfiguration) { if (!browserFactoryConfiguration.Settings.ContainsKey(RemoteUrlSetting)) { return(null); } var remoteSetting = browserFactoryConfiguration.Settings[RemoteUrlSetting]; if (string.IsNullOrWhiteSpace(remoteSetting)) { return(null); } Uri remoteUri; if (!Uri.TryCreate(remoteSetting, UriKind.Absolute, out remoteUri)) { throw new ConfigurationErrorsException( $"The {RemoteUrlSetting} setting is not a valid URI: {remoteSetting}"); } return(remoteUri); }
/// <summary> /// Initializes a new instance of the <see cref="BrowserFactory" /> class. /// </summary> /// <param name="configuration">The configuration.</param> protected BrowserFactory(BrowserFactoryConfiguration configuration) { this.Configuration = configuration; }
/// <summary> /// Creates the driver options. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The driver options.</returns> protected abstract DriverOptions CreateRemoteDriverOptions(BrowserFactoryConfiguration browserFactoryConfiguration);
/// <summary> /// Creates the local web driver from the specified browser factory configuration. /// </summary> /// <param name="browserFactoryConfiguration">The browser factory configuration.</param> /// <returns>The configured web driver.</returns> protected abstract IWebDriver CreateLocalDriver(BrowserFactoryConfiguration browserFactoryConfiguration);