public TComponent Create <TComponent, TBy>(TBy by, bool shouldCacheElement) where TBy : FindStrategy where TComponent : Component { var elementRepository = new ComponentRepository(); return(elementRepository.CreateComponentThatIsFound <TComponent>(by, null, shouldCacheElement)); }
private IEnumerable <TComponent> GetAndWaitWebDriverElements() { if (_shouldCacheFoundElements && _cachedElements == null) { _cachedElements = GetAndWaitNativeElements().ToList(); } if (_shouldCacheFoundElements && _cachedElements != null) { foreach (var element in _cachedElements) { yield return(element); } } else { foreach (var element in GetAndWaitNativeElements()) { yield return(element); } } IEnumerable <TComponent> GetAndWaitNativeElements() { foreach (var foundElement in _foundElements) { yield return(foundElement); } if (_by != null) { var nativeElements = WaitWebDriverElements(); int index = 0; foreach (var nativeElement in nativeElements) { var elementRepository = new ComponentRepository(); if (_parenTComponent != null) { var element = elementRepository.CreateComponentWithParent <TComponent>(_by, _parenTComponent, nativeElement, index++, _shouldCacheFoundElements); yield return(element); } else { var element = elementRepository.CreateComponentThatIsFound <TComponent>(_by, nativeElement, _shouldCacheFoundElements); yield return(element); } } } } }
private dynamic CastCell(ComponentRepository repo, ControlColumnData controlData, TableCell tableCell) { var element = repo.CreateComponentWithParent(controlData.By, tableCell.WrappedElement, controlData.ComponentType, false); // Resolve the appropriate Readonly Control Data Handler dynamic controlDataHandler = ControlDataHandlerResolver.ResolveReadonlyDataHandler(element.GetType()); if (controlDataHandler == null) { throw new Exception($"Cannot find proper IReadonlyControlDataHandler for type: {element.GetType().Name}. Make sure it is registered in the ServiceContainer"); } dynamic elementValue = controlDataHandler.GetData(element); return(elementValue); }
private IEnumerable <IWebElement> WaitWebDriverElements() { var elementFinder = _parenTComponent == null ? new NativeElementFinderService(WrappedDriver) : new NativeElementFinderService(_parenTComponent); var elementWaiter = new ComponentWaitService(); if (_parenTComponent == null) { return(ConditionalWait(elementFinder)); } else { var elementRepository = new ComponentRepository(); var parenTComponent = elementRepository.CreateComponentThatIsFound <Component>(_by, _parenTComponent, true); return(ConditionalWait(elementFinder)); } }
public IEnumerable <TComponent> GetCells <TComponent>() where TComponent : Component, new() { var listOfElements = new List <TComponent>(); var cells = GetCells().ToList(); for (int columnIndex = 0; columnIndex < cells.Count; columnIndex++) { var cell = cells[columnIndex]; TComponent element = new TComponent(); if (cell.CellControlComponentType == null) { listOfElements.Add(cell.As <TComponent>()); } else { var repo = new ComponentRepository(); element = repo.CreateComponentWithParent(cell.CellControlBy, cell.WrappedElement, typeof(TComponent), false); listOfElements.Add(element); } } return(listOfElements); }
/// <summary> /// Cast row values to Table Row Model. /// </summary> /// <typeparam name="TRowObject">Table Row Model.</typeparam> /// <param name="rowIndex">Row index.</param> /// <param name="propertiesToSkip">Header name.</param> /// <returns>Model.</returns> public TRowObject CastRow <TRowObject>(int rowIndex, params string[] propertiesToSkip) where TRowObject : new() { var cells = TableService.GetRowCells(rowIndex); if (cells.Count != ControlColumnDataCollection.Count) { // Compare headers to determine why the cells count is different var actual = TableService.Headers.Select(c => c.InnerText.Trim(" 0".ToCharArray())).ToList(); var expected = ControlColumnDataCollection.Select(c => c.HeaderName).ToList(); CollectionAssert.AreEqual(expected, actual, $"Expected: {expected.Stringify()}\r\nActual: {actual.Stringify()}"); } var dto = new TRowObject(); var properties = dto.GetType().GetProperties().ToList(); foreach (var propertyInfo in properties) { string headerName = HeaderNamesService.GetHeaderNameByProperty(propertyInfo); if (propertiesToSkip.Contains(headerName)) { continue; } int?headerPosition = HeaderNamesService.GetHeaderPosition(headerName, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList()); if (headerPosition == null) { continue; } var controlData = GetControlDataByProperty(propertyInfo); if (controlData != null && controlData.ComponentType != null && controlData.ComponentType.IsSubclassOf(typeof(Component))) { var repo = new ComponentRepository(); var xpath = $".{cells[(int)headerPosition].GetXPath()}"; var tableCell = this.CreateByXpath <TableCell>(xpath); dynamic elementValue; if (controlData.By == null) { controlData.By = new FindXpathStrategy(xpath); elementValue = CastCell(repo, controlData, tableCell); controlData.By = null; } else { elementValue = CastCell(repo, controlData, tableCell); } var elementType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType; var newValue = elementValue == null ? default : Convert.ChangeType(elementValue, elementType); elementValue = Convert.ChangeType(newValue, propertyInfo.PropertyType); propertyInfo.SetValue(dto, elementValue); } else { string htmlNodeValue = HttpUtility.HtmlDecode(TableService.GetRowCells(rowIndex)[(int)headerPosition].InnerText).Trim(); var type = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType; object elementValue; if (type == typeof(DateTime) || type == typeof(DateTime?)) { DateTime dateTime; DateTime.TryParse(htmlNodeValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime); elementValue = (DateTime?)dateTime; } else { elementValue = string.IsNullOrEmpty(htmlNodeValue) ? default : Convert.ChangeType(htmlNodeValue, type, CultureInfo.InvariantCulture); } propertyInfo.SetValue(dto, elementValue); } } return(dto); }