Exemplo n.º 1
0
        public virtual IList <T> FindElements <T>(By locator, string name = null, ElementSupplier <T> supplier = null, ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed) where T : IElement
        {
            var elementSupplier = ResolveSupplier(supplier);

            switch (expectedCount)
            {
            case ElementsCount.Zero:
                ConditionalWait.WaitForTrue(() => !ElementFinder.FindElements(locator, state, TimeSpan.Zero, name).Any(),
                                            message: LocalizationManager.GetLocalizedMessage("loc.elements.with.name.found.but.should.not", name, locator.ToString(), state.ToString()));
                break;

            case ElementsCount.MoreThenZero:
                ConditionalWait.WaitForTrue(() => ElementFinder.FindElements(locator, state, TimeSpan.Zero, name).Any(),
                                            message: LocalizationManager.GetLocalizedMessage("loc.no.elements.with.name.found.by.locator", name, locator.ToString()));
                break;

            case ElementsCount.Any:
                ConditionalWait.WaitFor(() => ElementFinder.FindElements(locator, state, TimeSpan.Zero, name) != null);
                break;

            default:
                throw new ArgumentOutOfRangeException($"No such expected value: {expectedCount}");
            }

            var             webElements = ElementFinder.FindElements(locator, state, TimeSpan.Zero, name);
            IEnumerable <T> elements    = webElements.Select((webElement, index) =>
            {
                var elementIndex = index + 1;
                var elementName  = $"{name ?? "element"} {elementIndex}";
                return(elementSupplier(GenerateXpathLocator(locator, webElement, elementIndex), elementName, state));
            });

            return(elements.ToList());
        }
        public void WaitForSliding()
        {
            var style = string.Empty;

            ConditionalWait.WaitForTrue(() =>
            {
                if (style == Style)
                {
                    return(true);
                }
                style = Style;
                return(false);
            }, message: "Sliding should stop after a while");
        }
        public static void OpenAutomationPracticeSite(string customUrl = null)
        {
            var resourceLimitLabel = Get <IElementFactory>()
                                     .GetLabel(By.XPath("//h1[.='Resource Limit Is Reached']"), "Resource Limit Is Reached");

            Browser.GoTo(customUrl ?? Constants.UrlAutomationPractice);
            Browser.WaitForPageToLoad();
            ConditionalWait.WaitForTrue(() =>
            {
                if (resourceLimitLabel.State.IsDisplayed)
                {
                    Browser.Refresh();
                    Browser.WaitForPageToLoad();
                    return(false);
                }
                return(true);
            }, timeout: TimeSpan.FromMinutes(3), pollingInterval: TimeSpan.FromSeconds(15),
                                        message: $"Failed to load [{customUrl ?? Constants.UrlAutomationPractice}] website. {resourceLimitLabel.Name} message is displayed.");
        }
        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());
        }