public SelectElement(IWebElement element, IWebDriver driver) : base(element, driver) { if (element.TagName == "select") { OldStyleSelect = new OpenQA.Selenium.Support.UI.SelectElement(element); LazyAvailableOptions = new Lazy <IList <IWebElement> >(() => OldStyleSelect.Options); LazySelectedOptions = new Lazy <IList <IWebElement> >(() => OldStyleSelect.AllSelectedOptions); } else { var listId = element.GetAttribute("list"); if (string.IsNullOrWhiteSpace(listId)) { throw new Exception("The {element.TagName} is neither a select element nor an element with a list attribute"); } LazyAvailableOptions = new Lazy <IList <IWebElement> >(() => driver.FindElements(By.XPath($"//datalist[@id='{listId}']/option"))); LazySelectedOptions = new Lazy <IList <IWebElement> >(() => new List <IWebElement>()); var value = element.GetAttribute("value"); if (!string.IsNullOrWhiteSpace(value)) { var selected = AvailableOptions.Where(o => o.GetAttribute("value") == value); if (selected.None()) { throw new Exception($"element with list:{listId} and value:{value} did not find any mathching options in the {AvailableOptions.Count()} options"); } if (selected.Many()) { throw new Exception($"element with list:{listId} and value:{value} found {selected.Count()} mathching options in the {AvailableOptions.Count()} options"); } SelectedOptions.Add(selected.First()); } } }
public override void Enter(string text) { if (OldStyleSelect != null) { if (text == null) { return; } if (OldStyleSelect != null) { var id = OldStyleSelect.WrappedElement.GetAttribute("id"); if (string.IsNullOrWhiteSpace(id)) { try { OldStyleSelect.SelectByText(text); return; } catch { } try { OldStyleSelect.SelectByText(text.ToUpper()); return; } catch { } try { OldStyleSelect.SelectByText(ToCammelCase(text)); return; } catch { } try { OldStyleSelect.SelectByText(text.ToLower()); return; } catch { } try { OldStyleSelect.SelectByValue(text); return; } catch { } //Partial match ? var l = AvailableOptions.ToList(); var realText = l.Where(x => x.Text.ToLower().Contains(text.ToLower())); if (realText.Count() == 1) { try { OldStyleSelect.SelectByIndex(l.IndexOf(realText.First())); return; } catch { } } } var key = text.ToUpper(); var options = FindByExactMatch(id, key); if (options.One()) { OldStyleSelect.SelectByValue(options.First().GetAttribute("value")); return; } if (options.Many()) { if (options.One(x => x.Text.ToUpper() == text.ToUpper())) { OldStyleSelect.SelectByValue(options.First(x => x.Text.ToUpper() == text.ToUpper()).GetAttribute("value")); } else { OldStyleSelect.SelectByValue(options.First().GetAttribute("value")); } return; } options = FindByContains(id, key); if (options.One()) { OldStyleSelect.SelectByValue(options.First().GetAttribute("value")); return; } if (options.Many()) { if (options.One(x => x.Text.ToUpper().Contains(text.ToUpper()))) { OldStyleSelect.SelectByValue(options.First(x => x.Text.ToUpper().Contains(text.ToUpper())).GetAttribute("value")); } else { OldStyleSelect.SelectByValue(options.First().GetAttribute("value")); } return; } } throw new GherkinException($"Unable to find {text} in the selection, only found {OldStyleSelect.Options.LogFormat(x => x.Text)}"); } else { var options = AvailableOptions.Where(o => string.Equals(o.GetAttribute("value"), text, ComparisonDefaults.StringComparison)); if (options.One()) { WebElement.SendKeys(options.First().GetAttribute("value")); } else if (options.Many()) { throw new GherkinException("too many matches"); //TODO: cleanup } else { throw new GherkinException("no matches"); } } }