public TElement Create <TElement, TBy>(TBy by, bool shouldCacheElement) where TBy : FindStrategy where TElement : Element { var elementRepository = new ElementRepository(); return(elementRepository.CreateElementThatIsFound <TElement>(by, null, shouldCacheElement)); }
private IEnumerable <TElement> 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 <TElement> 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 ElementRepository(); if (_parentElement != null) { var element = elementRepository.CreateElementWithParent <TElement>(_by, _parentElement, nativeElement, index++, _shouldCacheFoundElements); yield return(element); } else { var element = elementRepository.CreateElementThatIsFound <TElement>(_by, nativeElement, _shouldCacheFoundElements); yield return(element); } } } } }
private dynamic CastCell(ElementRepository repo, ControlColumnData controlData, TableCell tableCell) { var element = repo.CreateElementWithParent(controlData.By, tableCell.WrappedElement, controlData.ElementType, 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 = _parentElement == null ? new NativeElementFinderService(WrappedDriver) : new NativeElementFinderService(_parentElement); var elementWaiter = new ElementWaitService(); if (_parentElement == null) { return(ConditionalWait(elementFinder)); } else { var elementRepository = new ElementRepository(); var parentElement = elementRepository.CreateElementThatIsFound <Element>(_by, _parentElement, true); return(ConditionalWait(elementFinder)); } }
public IEnumerable <TElement> GetCells <TElement>() where TElement : Element, new() { var listOfElements = new List <TElement>(); var cells = GetCells().ToList(); for (int columnIndex = 0; columnIndex < cells.Count; columnIndex++) { var cell = cells[columnIndex]; TElement element = new TElement(); if (cell.CellControlElementType == null) { listOfElements.Add(cell.As <TElement>()); } else { var repo = new ElementRepository(); element = repo.CreateElementWithParent(cell.CellControlBy, cell.WrappedElement, typeof(TElement), false); listOfElements.Add(element); } } return(listOfElements); }
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.ElementType != null && controlData.ElementType.IsSubclassOf(typeof(Element))) { var repo = new ElementRepository(); 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); }