/// <summary> /// Immediately matches the current condition by trying to locate the element. /// </summary> /// <param name="condition">Condition to match</param> /// <returns>The element if it exists; will throw an exception if the element cannot be matched. </returns> protected IWebElement Match(ElementStateCondition condition) { // TODO: Use custom exceptions here var webElement = default(IWebElement); if (condition.State.HasFlag(ElementState.Exists)) { var webElements = _webDriver.FindElements(condition.Locator); if (webElements.Count() == 0) { throw new NoSuchElementException($"{condition.Locator}"); } if (webElements.Count() != 1) { throw new InvalidOperationException($"ERROR: There is more than one instance of {condition.State.ToString()}. There must be exactly one instance on the page. "); } webElement = webElements[0]; } if (condition.State.HasFlag(ElementState.IsDisplayed)) { if (!webElement.Displayed) { throw new NoSuchElementException($"The Element is not visible", new ElementNotVisibleException($"The Element is not visible: {webElement}")); } } if (condition.State.HasFlag(ElementState.IsEnabled)) { if (!webElement.Enabled) { throw new NoSuchElementException($"The Element is not enabled", new ElementNotInteractableException($"The Element is not enabled: {webElement}")); } } return(webElement); }
/// <summary> /// Handler for the specific and most common use case - one match. /// </summary> /// <param name="condition">The condition to match</param> /// <returns>The WebElement matching the condition; or any number of Selenium and generic exceptions. </returns> public IWebElement State(ElementStateCondition condition) { State(new ElementStateCondition[] { condition }); return(Match(condition)); }