protected virtual void HandleTimeoutException(WebDriverTimeoutException ex, DesiredState desiredState, By locator, List <IWebElement> foundElements, string name = null)
        {
            var message = string.IsNullOrEmpty(name)
                ? $"No elements with locator '{locator}' were found in {desiredState.StateName} state"
                : $"Element [{name}] was not found by locator '{locator}' in {desiredState.StateName} state";

            if (desiredState.IsCatchingTimeoutException)
            {
                if (!foundElements.Any())
                {
                    if (desiredState.IsThrowingNoSuchElementException)
                    {
                        throw new NoSuchElementException(message);
                    }
                    Logger.Debug("loc.no.elements.found.in.state", null, locator.ToString(), desiredState.StateName);
                }
                else
                {
                    Logger.Debug("loc.elements.were.found.but.not.in.state", null, locator.ToString(), desiredState.StateName);
                }
            }
            else
            {
                if (desiredState.IsThrowingNoSuchElementException && !foundElements.Any())
                {
                    throw new NoSuchElementException($"{message}: {ex.Message}");
                }
                throw new WebDriverTimeoutException($"{ex.Message}: {message}");
            }
        }
        public virtual ReadOnlyCollection <IWebElement> FindElements(By locator, Func <IWebElement, bool> elementStateCondition, TimeSpan?timeout = null, string name = null)
        {
            var desiredState = new DesiredState(elementStateCondition, "desired")
            {
                IsCatchingTimeoutException = true,
            };

            return(FindElements(locator, desiredState, timeout, name));
        }
        private bool IsElementClickable(TimeSpan?timeout, bool catchTimeoutException)
        {
            var desiredState = new DesiredState(element => element.Displayed && element.Enabled, "CLICKABLE")
            {
                IsCatchingTimeoutException = catchTimeoutException
            };

            return(IsElementInDesiredCondition(timeout, desiredState));
        }
        public virtual IWebElement FindElement(By locator, Func <IWebElement, bool> elementStateCondition, string stateName, TimeSpan?timeout = null, string name = null)
        {
            var desiredState = new DesiredState(elementStateCondition, stateName)
            {
                IsCatchingTimeoutException       = false,
                IsThrowingNoSuchElementException = true
            };

            return(FindElements(locator, desiredState, timeout, name).First());
        }
        private bool IsElementInDesiredState(Func <IWebElement, bool> elementStateCondition, string state, TimeSpan?timeout)
        {
            var desiredState = new DesiredState(elementStateCondition, state)
            {
                IsCatchingTimeoutException       = true,
                IsThrowingNoSuchElementException = true
            };

            return(IsElementInDesiredCondition(timeout, desiredState));
        }
        public virtual ReadOnlyCollection <IWebElement> FindElements(By locator, DesiredState desiredState, TimeSpan?timeout = null, string name = null)
        {
            var foundElements  = new List <IWebElement>();
            var resultElements = new List <IWebElement>();

            try
            {
                ConditionalWait.WaitFor(driver =>
                {
                    foundElements  = driver.FindElements(locator).ToList();
                    resultElements = foundElements.Where(desiredState.ElementStateCondition).ToList();
                    return(resultElements.Any());
                }, timeout);
            }
            catch (WebDriverTimeoutException ex)
            {
                HandleTimeoutException(ex, desiredState, locator, foundElements, name);
            }
            return(resultElements.AsReadOnly());
        }
        public override ReadOnlyCollection <IWebElement> FindElements(By locator, DesiredState desiredState, TimeSpan?timeout = null, string name = null)
        {
            var foundElements  = new List <IWebElement>();
            var resultElements = new List <IWebElement>();

            try
            {
                ConditionalWait.WaitForTrue(() =>
                {
                    foundElements  = SearchContextSupplier().FindElements(locator).ToList();
                    resultElements = foundElements.Where(desiredState.ElementStateCondition).ToList();
                    return(resultElements.Any());
                }, timeout);
            }
            catch (TimeoutException ex)
            {
                HandleTimeoutException(new WebDriverTimeoutException(ex.Message, ex), desiredState, locator, foundElements, name);
            }
            return(resultElements.AsReadOnly());
        }
 private bool IsElementInDesiredCondition(TimeSpan?timeout, DesiredState elementStateCondition)
 {
     return(ElementFinder.FindElements(elementLocator, elementStateCondition, timeout).Any());
 }