/// <summary> /// The initialize web driver chrome. /// </summary> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool InitializeWebDriverChrome() { var retVal = true; var driverOptions = GetChromeOptionsOptions(); try { if (Configuration.KillAllSeleniumBeforeInit) { if (!KillProcesses("chromedriver.exe")) { return(false); } } var driverService = ChromeDriverService.CreateDefaultService(Configuration.DriverServerPath); driverService.LogPath = DriverLogFile; driverService.EnableVerboseLogging = true; //TODO get this right WebDriver = new ChromeDriver( driverService, driverOptions, TimeSpan.FromSeconds(Configuration.BrowserTimeout)); WebDriver.Manage().Window.Maximize(); ResetImplicitlyWait(); } catch (Exception ex) { StfLogger.LogInternal($"Couldn't Initialize WebAdapter - got error [{ex.Message}]"); retVal = false; } return(retVal); }
/// <summary> /// The click. /// </summary> /// <param name="by"> /// The by. /// </param> /// <param name="depth"> /// Only used internally to stir out of endless recursion /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool Click(By by, int depth = 0) { var button = FindElement(by); bool retVal; if (button == null || depth >= 5) { StfLogger.LogInternal($"Can't find button - by=[{by}]"); return(false); } try { if (!button.Enabled) { WaitForComplete(1); } button.Click(); retVal = true; } catch (Exception ex) { StfLogger.LogInternal("Click failed - Have to reclick - got exception [{0}]", ex.Message); // if we cant get to click the button, then the UI has changed (like we did press search, but the page wasn't rendered fully yet) retVal = Click(by, depth + 1); } return(retVal); }
/// <summary> /// The initialize web driver internet explorer. /// </summary> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool InitializeWebDriverInternetExplorer() { var retVal = true; var driverOptions = GetInternetExplorerOptions(); try { var driverService = InternetExplorerDriverService.CreateDefaultService(Configuration.DriverServerPath); driverService.LogFile = DriverLogFile; driverService.LoggingLevel = GetDriverLoggingLevel(); WebDriver = new InternetExplorerDriver( driverService, driverOptions, TimeSpan.FromSeconds(Configuration.BrowserTimeout)); WebDriver.Manage().Window.Maximize(); ResetImplicitlyWait(); } catch (Exception ex) { StfLogger.LogInternal($"Couldn't Initialize WebAdapter - got error [{ex.Message}]"); retVal = false; } StfLogger.LogDebug("Done initializing {0}. Successful: {1}", GetType().Name, retVal.ToString()); return(retVal); }
/// <summary> /// Make sure LogLevel is set to internal and set it back again /// </summary> /// <param name="message"> /// The message. /// </param> private void LogBaseClassMessage(string message) { var oldLoglevel = StfLogger.LogLevel; StfLogger.LogLevel = StfLogLevel.Internal; StfLogger.LogInternal(message); StfLogger.LogLevel = oldLoglevel; }
/// <summary> /// The init test data. /// </summary> /// <param name="testdataObject"> /// The testdata object. /// </param> /// <typeparam name="T"> /// The testdata object that should be filled with the values from the current row in the TestContext /// </typeparam> /// <returns> /// The <see cref="T"/>. /// </returns> protected T InitTestData <T>(T testdataObject = default(T)) where T : IStfTestData, new() { if (!TestDataDriven()) { if (testdataObject != null) { testdataObject.StfIteration = -1; } return(testdataObject); } var retVal = new T(); var properties = typeof(T).GetProperties(); var attributedProperties = properties.Where(prop => prop.IsDefined(typeof(StfTestDataAttribute), false)).ToList(); var dataRow = TestContext.DataRow; retVal.StfIteration = DataRowIndex(); foreach (var columnName in dataRow.Table.Columns) { var propertyName = columnName.ToString(); var property = properties.FirstOrDefault(pp => pp.Name == propertyName); // did we find the correspondig property in the testdata class? if (property == null && attributedProperties.Any()) { // hmm, lets see if there is a map for this column... property = attributedProperties.FirstOrDefault(pp => pp.GetCustomAttribute <StfTestDataAttribute>().ColumnName == propertyName); } if (property == null) { continue; } var propertyType = property.PropertyType; try { var val = TypeDescriptor.GetConverter(propertyType).ConvertFromString(dataRow[propertyName].ToString()); property.SetValue(retVal, val); } catch (Exception ex) { StfLogger.LogInternal( "Caught error setting value for property [{0}] in testdata object [{1}]. Error: [{2}]", property.Name, typeof(T).Name, ex.Message); } } // we might later alter stuff, so StfIgnoreRow also is triggered by StfIgnore retVal.StfIgnoreRow = StfIgnoreRow; return(retVal); }
/// <summary> /// Get the web adapter configuration /// </summary> /// <returns> /// Instance of web adapter configuration /// </returns> private WebAdapterConfiguration GetConfiguration() { WebAdapterConfiguration retVal; try { var stfConfiguration = StfContainer.Get <StfConfiguration>(); retVal = new WebAdapterConfiguration(); stfConfiguration.LoadUserConfiguration(retVal); } catch (Exception ex) { StfLogger.LogInternal($"Couldn't GetConfiguration - got error [{ex.Message}]"); retVal = null; } return(retVal); }
/// <summary> /// The open url. /// </summary> /// <param name="url"> /// The url. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool OpenUrl(string url) { var retVal = true; try { WebDriver.Navigate().GoToUrl(url); } catch (Exception ex) { var msg = $"Couldn't OpenUrl [{url}] - got error [{ex.Message}]"; // this might not be an error - if we are looking for something to become rendered in a loop.... StfLogger.LogInternal(msg); StfLogger.LogScreenshot(StfLogLevel.Internal, msg); retVal = false; } return(retVal); }
/// <summary> /// The find element using an explicit wait timeout /// </summary> /// <param name="by"> /// The by. /// </param> /// <param name="secondsToWait"> /// The max number of seconds to wait. /// </param> /// <returns> /// The <see cref="IWebElement"/>. /// </returns> public IWebElement FindElement(By by, int secondsToWait) { IWebElement retVal; SetImplicitlyWait(1); try { var webDriverWaiter = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(secondsToWait)); retVal = webDriverWaiter.Until(ExpectedConditions.ElementIsVisible(by)); } catch (Exception ex) { // this might not be an error - if we are looking for something to become rendered in a loop.... StfLogger.LogInternal("Couldn't find element [{0}]. Waited for [{1}] seconds - got error [{2}]", by, secondsToWait, ex.Message); retVal = null; } ResetImplicitlyWait(); return(retVal); }
/// <summary> /// The check path. /// </summary> /// <param name="path"> /// The path. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool CheckPath(string path) { if (Directory.Exists(path)) { return(true); } var retVal = false; try { Directory.CreateDirectory(path); retVal = true; } catch (Exception ex) { StfLogger.LogInternal("Unable to create directory [{0}]. Got error: [{1}]", path, ex.Message); } return(retVal); }
/// <summary> /// The get text by by. Returns the text - if element not found string.empty is returned /// </summary> /// <param name="by"> /// The by. /// </param> /// <param name="secondsToWait"> /// The seconds To Wait. /// </param> /// <param name="depth"> /// Only used internally to stir out of endless recursion /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string GetText(By by, int secondsToWait = 1, int depth = 0) { var element = FindElement(by, secondsToWait); var retVal = string.Empty; if (element == null || depth >= 5) { return(retVal); } try { retVal = element.Text; } catch (Exception ex) { StfLogger.LogInternal("Reading text failed - Have to reRead - got exception [{0}]", ex.Message); // if we cant get to the text, then the UI has changed (like we did press search, but the page wasn't rendered fully yet) retVal = GetText(by, secondsToWait, depth + 1); } return(retVal); }