Beispiel #1
0
        private IWebElement AssertExactlyOneElementExists(IDecoratedWebDriver webDriver, By locator)
        {
            var elements = webDriver.FindElements(locator);

            if (elements.Count() == 0)
            {
                throw new NotFoundException($"The element {locator} could not be found. ");
            }
            if (elements.Count() > 1)
            {
                throw new NotFoundException($"The element {locator} was found {elements.Count()} times instead of exactly once. ");
            }

            return(elements.Single());
        }
Beispiel #2
0
        /// <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);
        }