Exemplo n.º 1
0
        protected void ClickItems(T value, ClickItemPredicate predicate)
        {
            List <T> individualValues = GetIndividualValues(value).ToList();

            IWebElement[] elements = GetItemElements();
            foreach (IWebElement element in elements)
            {
                T    elementValue = GetElementValue(element);
                bool isInValue    = individualValues.Contains(elementValue);

                if (isInValue)
                {
                    individualValues.Remove(elementValue);
                }

                if (predicate(isInValue, element.Selected))
                {
                    element.Click();
                }
            }

            if (individualValues.Any())
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          "Unable to locate element{0}: {1}.".FormatWith(
                              individualValues.Count > 1 ? "s" : null,
                              ConvertIndividualValuesToString(individualValues, true)));
            }
        }
Exemplo n.º 2
0
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            By by = By.CssSelector(string.Join(",", options.Terms));

            if (options.OuterXPath != null)
            {
                by = By.XPath(options.OuterXPath + "*").Then(by);
            }

            if (options.Index.HasValue)
            {
                var elements = scope.GetAll(by.With(searchOptions));
                if (elements.Count <= options.Index.Value)
                {
                    if (searchOptions.IsSafely)
                    {
                        return(new MissingComponentScopeLocateResult());
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(by: by, searchContext: scope);
                    }
                }
                else
                {
                    return(new SequalComponentScopeLocateResult(elements[options.Index.Value], sequalStrategy));
                }
            }
            else
            {
                return(new SequalComponentScopeLocateResult(by, sequalStrategy));
            }
        }
Exemplo n.º 3
0
        public override IWebElement FindElement(ISearchContext context)
        {
            ReadOnlyCollection <IWebElement> elements = FindElements(context);

            return(elements.FirstOrDefault()
                   ?? throw ExceptionFactory.CreateForNoSuchElement(by: this, searchContext: context));
        }
Exemplo n.º 4
0
        protected void ClickItems(T value, ClickItemPredicate predicate)
        {
            List <T> individualValues = GetIndividualValues(value).ToList();

            IWebElement[] elements = GetItemElements();
            foreach (IWebElement element in elements)
            {
                T    elementValue = GetElementValue(element);
                bool isInValue    = individualValues.Contains(elementValue);

                if (isInValue)
                {
                    individualValues.Remove(elementValue);
                }

                if (predicate(isInValue, element.Selected))
                {
                    element.Click();
                }
            }

            if (individualValues.Any())
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          new SearchFailureData
                {
                    ElementName = $"{ConvertIndividualValuesToString(individualValues, true)} checkbox element{(individualValues.Count > 1 ? "s" : null)} of {ComponentFullName}"
                });
            }
        }
Exemplo n.º 5
0
        public IWebElement GetElement(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = searchOptions ?? new SearchOptions();

            var executionData = _executionDataCollector.Get(searchOptions);

            XPathComponentScopeFindResult[] xPathResults = _executor.Execute(executionData);

            if (xPathResults.Any())
            {
                IWebElement element = xPathResults.Select(x => x.Get(xPathCondition)).FirstOrDefault(x => x != null);

                if (element == null && !searchOptions.IsSafely)
                {
                    throw ExceptionFactory.CreateForNoSuchElement(
                              new SearchFailureData
                    {
                        ElementName   = executionData.Component.ComponentFullName,
                        By            = xPathResults.First().CreateBy(xPathCondition),
                        SearchOptions = searchOptions
                    });
                }
                else
                {
                    return(element);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        private ComponentScopeLocateResult ProcessCollectionOfElements(ReadOnlyCollection <IWebElement> elements, ISearchContext scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            if (options.Index.HasValue)
            {
                if (elements.Count <= options.Index.Value)
                {
                    if (searchOptions.IsSafely)
                    {
                        return(new MissingComponentScopeFindResult());
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(
                                  new SearchFailureData
                        {
                            ElementName   = $"{(options.Index.Value + 1).Ordinalize()} by script: {Script}",
                            SearchOptions = searchOptions,
                            SearchContext = scope
                        });
                    }
                }
                else
                {
                    ComponentScopeLocateOptions sequalOptions = options.Clone();
                    sequalOptions.Index = null;

                    return(new SubsequentComponentScopeFindResult(elements[options.Index.Value], s_sequalStrategy, sequalOptions));
                }
            }
            else
            {
                return(new SubsequentComponentScopeFindResult(elements, s_sequalStrategy));
            }
        }
Exemplo n.º 7
0
        public ComponentScopeLocateResult Find(ISearchContext scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            object scriptResult = ExecuteScript(scope);

            if (scriptResult is ReadOnlyCollection <IWebElement> elements)
            {
                return(ProcessCollectionOfElements(elements, scope, options, searchOptions));
            }
            else if (scriptResult is IWebElement element)
            {
                return(new SubsequentComponentScopeFindResult(element, s_sequalStrategy));
            }
            else if (scriptResult != null)
            {
                throw new InvalidOperationException($"Invalid script result. The script should return an element or collection of elements. But was returned: {scriptResult}");
            }
            else if (searchOptions.IsSafely)
            {
                return(new MissingComponentScopeFindResult());
            }
            else
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          new SearchFailureData
                {
                    ElementName   = $"by script: {Script}",
                    SearchOptions = searchOptions,
                    SearchContext = scope
                });
            }
        }
Exemplo n.º 8
0
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            var headers             = scope.GetAll(By.XPath(headerXPath).With(searchOptions).OfAnyVisibility());
            var headerNamePredicate = options.Match.GetPredicate();

            int?columnIndex = headers.
                              Select((x, i) => new { x.Text, Index = i }).
                              Where(x => options.Terms.Any(term => headerNamePredicate(x.Text, term))).
                              Select(x => (int?)x.Index).
                              FirstOrDefault();

            if (columnIndex == null)
            {
                if (searchOptions.IsSafely)
                {
                    return(new MissingComponentScopeLocateResult());
                }
                else
                {
                    throw ExceptionFactory.CreateForNoSuchElement(options.GetTermsAsString(), searchContext: scope);
                }
            }

            return(new FindByColumnIndexStrategy(columnIndex.Value).Find(scope, options, searchOptions));
        }
Exemplo n.º 9
0
        public IWebElement GetElement(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = ResolveSearchOptions(searchOptions);

            XPathComponentScopeLocateResult[] xPathResults = GetScopeLocateResults(searchOptions);
            if (xPathResults != null && xPathResults.Any())
            {
                IWebElement element = xPathResults.Select(x => x.Get(xPathCondition)).FirstOrDefault(x => x != null);

                if (element == null && !searchOptions.IsSafely)
                {
                    throw ExceptionFactory.CreateForNoSuchElement(
                              new SearchFailureData
                    {
                        ElementName   = component.ComponentFullName,
                        By            = By.XPath(xPathResults.First().XPath + xPathCondition),
                        SearchOptions = searchOptions
                    });
                }
                else
                {
                    return(element);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 10
0
        public ComponentScopeLocateResult Find(ISearchContext scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            int?columnIndex = GetColumnIndex(scope, options, searchOptions);

            if (columnIndex == null)
            {
                if (searchOptions.IsSafely)
                {
                    return(new MissingComponentScopeFindResult());
                }
                else
                {
                    throw ExceptionFactory.CreateForNoSuchElement(
                              new SearchFailureData
                    {
                        ElementName   = $"\"{options.GetTermsAsString()}\" column header",
                        SearchOptions = searchOptions,
                        SearchContext = scope
                    });
                }
            }

            IComponentScopeFindStrategy nextStrategy = CreateColumnIndexStrategy(columnIndex.Value);

            return(new SubsequentComponentScopeFindResult(scope, nextStrategy));
        }
Exemplo n.º 11
0
        private XPathComponentScopeLocateResult[] ResolveScopeLocateResult(ComponentScopeLocateResult result, IWebElement scopeSource, SearchOptions searchOptions)
        {
            result.CheckNotNull(nameof(result));

            if (result is MissingComponentScopeLocateResult missingResult)
            {
                return(new XPathComponentScopeLocateResult[0]);
            }

            if (result is XPathComponentScopeLocateResult xPathResult)
            {
                return new[] { xPathResult }
            }
            ;

            if (result is SequalComponentScopeLocateResult sequalResult)
            {
                ComponentScopeLocateOptions nextScopeLocateOptions = sequalResult.ScopeLocateOptions ?? scopeLocateOptions;

                if (sequalResult.ScopeSource != null)
                {
                    ComponentScopeLocateResult nextResult = sequalResult.Strategy.Find(sequalResult.ScopeSource, nextScopeLocateOptions, searchOptions);
                    return(ResolveScopeLocateResult(nextResult, sequalResult.ScopeSource, searchOptions));
                }
                else
                {
                    IWebElement[] nextScopeSources = scopeSource.GetAll(sequalResult.ScopeSourceBy.With(searchOptions)).ToArray();

                    SearchOptions nextSearchOptions = SearchOptions.SafelyAtOnce();

                    var results = nextScopeSources.
                                  Select(nextScopeSource => sequalResult.Strategy.Find(nextScopeSource, nextScopeLocateOptions, nextSearchOptions)).
                                  Select(nextResult => ResolveScopeLocateResult(nextResult, nextScopeSources[0], nextSearchOptions)).
                                  Where(xPathResults => xPathResults != null).
                                  SelectMany(xPathResults => xPathResults).
                                  ToArray();

                    if (results.Any())
                    {
                        return(results);
                    }
                    else if (searchOptions.IsSafely)
                    {
                        return(new XPathComponentScopeLocateResult[0]);
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(by: sequalResult.ScopeSourceBy);
                    }
                }
            }

            throw new ArgumentException($"Unsupported {nameof(ComponentScopeLocateResult)} type: {result.GetType().FullName}", nameof(result));
        }
    }
Exemplo n.º 12
0
        protected IWebElement[] GetItemElements()
        {
            IWebElement[] elements = ScopeLocator.GetElements();

            // TODO: Review to throw more detailed exception.
            if (elements.Length == 0)
            {
                throw ExceptionFactory.CreateForNoSuchElement(ComponentFullName);
            }

            return(elements);
        }
Exemplo n.º 13
0
        internal sealed override IWebElement OnGetScopeElement(SearchOptions searchOptions)
        {
            ExecuteTriggers(TriggerEvents.BeforeAccess);

            IWebElement element = ScopeLocator.GetElement(searchOptions);

            if (!searchOptions.IsSafely && element == null)
            {
                throw ExceptionFactory.CreateForNoSuchElement(ComponentFullName);
            }

            ExecuteTriggers(TriggerEvents.AfterAccess);

            return(element);
        }
Exemplo n.º 14
0
        public XPathComponentScopeFindResult[] Execute(StrategyScopeLocatorExecutionData executionData)
        {
            ISearchContext scopeContext = executionData.ScopeSource.GetScopeContext(
                executionData.Component,
                SearchOptions.Safely(executionData.IsSafely));

            if (scopeContext is null)
            {
                return(new XPathComponentScopeFindResult[0]);
            }

            foreach (var unit in executionData.LayerUnits)
            {
                XPathComponentScopeFindResult[] xPathResults = Execute(unit.Strategy, scopeContext, unit.ScopeLocateOptions, unit.SearchOptions);

                if (!xPathResults.Any())
                {
                    return(xPathResults);
                }

                IWebElement element = xPathResults.Select(x => x.Get()).FirstOrDefault(x => x != null);

                if (element is null)
                {
                    if (!executionData.IsSafely)
                    {
                        XPathComponentScopeFindResult firstResult = xPathResults.First();

                        throw ExceptionFactory.CreateForNoSuchElement(
                                  new SearchFailureData
                        {
                            ElementName   = $"layer of {executionData.Component.ComponentFullName}",
                            By            = By.XPath(firstResult.XPath),
                            SearchOptions = firstResult.SearchOptions
                        });
                    }
                    else
                    {
                        return(new XPathComponentScopeFindResult[0]);
                    }
                }

                scopeContext = unit.ScopeContextResolver.Resolve(element);
            }

            return(Execute(executionData.FinalUnit.Strategy, scopeContext, executionData.FinalUnit.ScopeLocateOptions, executionData.FinalUnit.SearchOptions));
        }
Exemplo n.º 15
0
        public IWebElement GetElement(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = searchOptions ?? new SearchOptions();

            IWebElement element = AtataContext.Current.Driver.Try(searchOptions.Timeout, searchOptions.RetryInterval).Until(_ =>
            {
                return(predicate(searchOptions).FirstOrDefault());
            });

            if (element == null && !searchOptions.IsSafely)
            {
                throw ExceptionFactory.CreateForNoSuchElement();
            }
            else
            {
                return(element);
            }
        }
Exemplo n.º 16
0
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            string labelXPath = new ComponentScopeXPathBuilder(options).
                                WrapWithIndex(x => x.OuterXPath._("label")[y => y.TermsConditionOfContent]);

            IWebElement label = scope.Get(By.XPath(labelXPath).With(searchOptions).Label(options.GetTermsAsString()));

            if (label == null)
            {
                if (searchOptions.IsSafely)
                {
                    return(new MissingComponentScopeLocateResult());
                }
                else
                {
                    throw ExceptionFactory.CreateForNoSuchElement(options.GetTermsAsString(), searchContext: scope);
                }
            }

            string elementId = label.GetAttribute("for");
            IdXPathForLabelAttribute idXPathForLabelAttribute;

            if (string.IsNullOrEmpty(elementId))
            {
                return(new SequalComponentScopeLocateResult(label, new FindFirstDescendantStrategy()));
            }
            else if ((idXPathForLabelAttribute = options.Metadata.Get <IdXPathForLabelAttribute>(x => x.At(AttributeLevels.Component))) != null)
            {
                ComponentScopeLocateOptions idOptions = options.Clone();
                idOptions.Terms = new[] { idXPathForLabelAttribute.XPathFormat.FormatWith(elementId) };
                idOptions.Index = null;

                return(new SequalComponentScopeLocateResult(scope, new FindByXPathStrategy(), idOptions));
            }
            else
            {
                ComponentScopeLocateOptions idOptions = options.Clone();
                idOptions.Terms = new[] { elementId };
                idOptions.Index = null;
                idOptions.Match = TermMatch.Equals;

                return(new SequalComponentScopeLocateResult(scope, new FindByIdStrategy(), idOptions));
            }
        }
Exemplo n.º 17
0
        public ComponentScopeLocateResult Find(ISearchContext scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            By by = By.CssSelector(string.Join(",", options.Terms));

            if (options.OuterXPath != null)
            {
                by = By.XPath(options.OuterXPath + "*").Then(by);
            }

            if (options.Index.HasValue)
            {
                var elements = scope.GetAllWithLogging(by.With(searchOptions));

                if (elements.Count <= options.Index.Value)
                {
                    if (searchOptions.IsSafely)
                    {
                        return(new MissingComponentScopeFindResult());
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(
                                  new SearchFailureData
                        {
                            ElementName   = $"{(options.Index.Value + 1).Ordinalize()} matching selector",
                            By            = by,
                            SearchOptions = searchOptions,
                            SearchContext = scope
                        });
                    }
                }
                else
                {
                    return(new SubsequentComponentScopeFindResult(elements[options.Index.Value], _sequalStrategy));
                }
            }
            else
            {
                return(new SubsequentComponentScopeFindResult(by, _sequalStrategy));
            }
        }
        private IWebElement Find(By by)
        {
            SearchOptions options = by.GetSearchOptionsOrDefault();

            ReadOnlyCollection <IWebElement> lastFoundElements = null;

            IWebElement FindElement(T context)
            {
                lastFoundElements = context.FindElements(by);

                return(options.Visibility == Visibility.Any
                    ? lastFoundElements.FirstOrDefault()
                    : lastFoundElements.FirstOrDefault(CreateVisibilityPredicate(options.Visibility)));
            }

            Stopwatch searchWatch = Stopwatch.StartNew();

            IWebElement element = Until(FindElement, options.ToRetryOptions());

            searchWatch.Stop();

            if (!options.IsSafely && element == null)
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          new SearchFailureData
                {
                    By            = by,
                    SearchTime    = searchWatch.Elapsed,
                    SearchOptions = options,
                    AlikeElementsWithInverseVisibility = lastFoundElements,
                    SearchContext = Context
                });
            }
            else
            {
                return(element);
            }
        }
Exemplo n.º 19
0
        private XPathComponentScopeLocateResult[] ResolveScopeLocateResult(ComponentScopeLocateResult result, IWebElement scopeSource, SearchOptions searchOptions)
        {
            result.CheckNotNull(nameof(result));

            if (result is MissingComponentScopeLocateResult missingResult)
            {
                return(new XPathComponentScopeLocateResult[0]);
            }

            if (result is XPathComponentScopeLocateResult xPathResult)
            {
                return new[] { xPathResult }
            }
            ;

            if (result is SequalComponentScopeLocateResult sequalResult)
            {
                ComponentScopeLocateOptions nextScopeLocateOptions = sequalResult.ScopeLocateOptions ?? scopeLocateOptions;

                if (sequalResult.ScopeSources?.Count() == 1)
                {
                    return(ExecuteStrategyAndResolveResults(sequalResult.Strategy, sequalResult.ScopeSources.First(), nextScopeLocateOptions, searchOptions));
                }
                else
                {
                    IEnumerable <IWebElement> nextScopeSources = sequalResult.ScopeSourceBy != null
                        ? scopeSource.GetAll(sequalResult.ScopeSourceBy.With(searchOptions))
                        : sequalResult.ScopeSources;

                    SearchOptions nextSearchOptions = SearchOptions.SafelyAtOnce();

                    var results = nextScopeSources.
                                  Select(nextScopeSource => ExecuteStrategyAndResolveResults(sequalResult.Strategy, nextScopeSource, nextScopeLocateOptions, nextSearchOptions)).
                                  Where(xPathResults => xPathResults != null).
                                  SelectMany(xPathResults => xPathResults).
                                  ToArray();

                    if (results.Any())
                    {
                        return(results);
                    }
                    else if (searchOptions.IsSafely)
                    {
                        return(new XPathComponentScopeLocateResult[0]);
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(
                                  new SearchFailureData
                        {
                            ElementName   = component.ComponentFullName,
                            By            = sequalResult.ScopeSourceBy,
                            SearchOptions = searchOptions,
                            SearchContext = scopeSource
                        });
                    }
                }
            }

            throw new ArgumentException($"Unsupported {nameof(ComponentScopeLocateResult)} type: {result.GetType().FullName}", nameof(result));
        }