protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
        {
            string[] conditionalXPathTerms = builder.Options.Terms.
                Where(x => (x.StartsWith("[") && x.EndsWith("]")) || x.StartsWith("@")).
                ToArray();

            string[] conditionalXPathSelectors = conditionalXPathTerms.
                Select(x => x.StartsWith("@") ? x : x.Substring(1, x.Length - 2)).
                ToArray();

            if (conditionalXPathSelectors.Length > 1)
            {
                conditionalXPathSelectors = conditionalXPathSelectors.
                    Select(x => $"({x})").
                    ToArray();
            }

            string conditionalXPath = conditionalXPathSelectors.Any()
                ? builder.WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.JoinOr(conditionalXPathSelectors)])
                : null;

            string[] outerXPathSelectors = builder.Options.Terms.
                Except(conditionalXPathTerms).
                Select(x => acceptableXPathPrefixValues.Any(prefix => x.StartsWith(prefix)) ? x : ".//" + x).
                ToArray();

            string outerXPath = outerXPathSelectors.Any()
                ? builder.WrapWithIndex(x => x._($"({string.Join(" | ", outerXPathSelectors)})")).DescendantOrSelf.ComponentXPath
                : null;

            if (conditionalXPath != null && outerXPath != null)
                return $"(({outerXPath}) | ({conditionalXPath}))";
            else
                return outerXPath ?? conditionalXPath;
        }
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            string labelXPath = new ComponentScopeXPathBuilder(options).
                WrapWithIndex(x => x.Descendant._("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");
            if (string.IsNullOrEmpty(elementId))
            {
                return new SequalComponentScopeLocateResult(label, new FindFirstDescendantStrategy());
            }
            else
            {
                ComponentScopeLocateOptions idOptions = options.Clone();
                idOptions.Terms = new[] { elementId };
                idOptions.Index = null;
                idOptions.Match = TermMatch.Equals;

                return new SequalComponentScopeLocateResult(scope, new FindByIdStrategy(), idOptions);
            }
        }
        protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
        {
            string classCondition = GetClassCondition(options);

            return builder.
                WrapWithIndex(x => x.Descendant.Any[classCondition]).
                DescendantOrSelf.ComponentXPath;
        }
        protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
        {
            string idCondition = string.IsNullOrWhiteSpace(options.IdXPathFormat)
                ? builder.TermsConditionOf("id")
                : builder.JoinOr(options.Terms.Select(term => options.IdXPathFormat.FormatWith(term)));

            return builder.WrapWithIndex(x => x.Descendant.Any[idCondition]).DescendantOrSelf.ComponentXPath;
        }
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            ComponentScopeXPathBuilder builder = new ComponentScopeXPathBuilder(options);

            string xPath = Build(builder, options);

            return new XPathComponentScopeLocateResult(xPath, scope, searchOptions);
        }
        protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
        {
            string[] conditions = options.Terms.Length > 1
                ? options.Terms.Select(x => $"({x})").ToArray()
                : options.Terms;

            return builder.WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.JoinOr(conditions)]);
        }
        private string GetClassCondition(ComponentScopeLocateOptions options)
        {
            string conditionFormat = "contains(concat(' ', normalize-space(@class), ' '), ' {0} ')";

            var conditionOrParts = options.Terms.
                Select(t => t.Split(' ').Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray()).
                Where(qps => qps.Any()).
                Select(qps => string.Join(" and ", qps.Select(qp => conditionFormat.FormatWith(qp)))).
                ToArray();

            if (conditionOrParts.Count() == 1)
                return conditionOrParts.First();
            else
                return string.Join(" or ", conditionOrParts.Select(x => "({0})".FormatWith(x)));
        }
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            By by = By.CssSelector(string.Join(",", options.Terms));

            if (options.Index.HasValue)
            {
                var elements = scope.GetAll(by.With(searchOptions));
                if (elements.Count <= options.Index.Value)
                    throw ExceptionFactory.CreateForNoSuchElement(by: by, searchContext: scope);
                else
                    return new SequalComponentScopeLocateResult(elements[options.Index.Value], sequalStrategy);
            }
            else
            {
                return new SequalComponentScopeLocateResult(by, sequalStrategy);
            }
        }
        public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            var headers = scope.GetAll(By.XPath(headerXPath).OfAnyVisibility());
            var headerNamePredicate = options.Match.GetPredicate();

            int? columnIndex = headers.
                Select((x, i) => new { Text = 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);
        }
Example #10
0
        private XPathComponentScopeLocateResult[] ExecuteStrategyAndResolveResults(IComponentScopeLocateStrategy strategy, IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
        {
            ComponentScopeLocateResult result = strategy.Find(scope, options, searchOptions);

            return(ResolveScopeLocateResult(result, scope, searchOptions));
        }
Example #11
0
        private static ComponentScopeLocateResult ExecuteToGetResult(object strategy, ISearchContext scope, ComponentScopeLocateOptions scopeLocateOptions, SearchOptions searchOptions)
        {
            // TODO: Remove first "if" and last "else" in Atata v2.0.0, as strategy argument should be of IComponentScopeFindStrategy type.
#pragma warning disable CS0618 // Type or member is obsolete
            if (strategy is IComponentScopeLocateStrategy componentScopeLocateStrategy)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                IWebElement scopeElement = (scope as IWebElement)
                                           ?? scope.GetWithLogging(By.TagName("body").With(new SearchOptions()));

                return(componentScopeLocateStrategy.Find(scopeElement, scopeLocateOptions, searchOptions));
            }
            else if (strategy is IComponentScopeFindStrategy componentScopeFindStrategy)
            {
                return(componentScopeFindStrategy.Find(scope, scopeLocateOptions, searchOptions));
            }
            else
            {
                throw new ArgumentException(
                          $"Unsupported {strategy.GetType().FullName} type of {nameof(strategy)}. Strategy should be either {nameof(IComponentScopeFindStrategy)} or IComponentScopeLocateStrategy.",
                          nameof(strategy));
            }
        }
Example #12
0
        private static XPathComponentScopeFindResult[] Execute(object strategy, ISearchContext scope, ComponentScopeLocateOptions scopeLocateOptions, SearchOptions searchOptions)
        {
            ComponentScopeLocateResult result = ExecuteToGetResult(strategy, scope, scopeLocateOptions, searchOptions);

            return(ResolveResult(result, scope, scopeLocateOptions, searchOptions));
        }
Example #13
0
 protected abstract string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options);
Example #14
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));
            }
        }
Example #15
0
 public StrategyScopeLocator(UIComponent component, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions)
 {
     this.component          = component;
     this.strategy           = strategy;
     this.scopeLocateOptions = scopeLocateOptions;
 }
Example #16
0
        private XPathComponentScopeLocateResult[] ResolveScopeLocateResults(ComponentScopeLocateResult result, IWebElement scopeSource, SearchOptions searchOptions)
        {
            result.CheckNotNull("result");

            MissingComponentScopeLocateResult missingResult = result as MissingComponentScopeLocateResult;

            if (missingResult != null)
            {
                return(null);
            }

            XPathComponentScopeLocateResult xPathResult = result as XPathComponentScopeLocateResult;

            if (xPathResult != null)
            {
                return new[] { xPathResult }
            }
            ;

            SequalComponentScopeLocateResult sequalResult = result as SequalComponentScopeLocateResult;

            if (sequalResult != null)
            {
                ComponentScopeLocateOptions nextScopeLocateOptions = sequalResult.ScopeLocateOptions ?? scopeLocateOptions;

                if (sequalResult.ScopeSource != null)
                {
                    ComponentScopeLocateResult nextResult = sequalResult.Strategy.Find(sequalResult.ScopeSource, nextScopeLocateOptions, searchOptions);
                    return(ResolveScopeLocateResults(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 => ResolveScopeLocateResults(nextResult, nextScopeSources[0], nextSearchOptions)).
                                  Where(xPathResults => xPathResults != null).
                                  SelectMany(xPathResults => xPathResults).
                                  ToArray();

                    if (results.Any())
                    {
                        return(results);
                    }
                    else if (searchOptions.IsSafely)
                    {
                        return(null);
                    }
                    else
                    {
                        throw ExceptionFactory.CreateForNoSuchElement(by: sequalResult.ScopeSourceBy);
                    }
                }
            }

            throw new ArgumentException("Unsupported ComponentScopeLocateResult type: {0}".FormatWith(result.GetType().FullName), "result");
        }
    }
 public SequalComponentScopeLocateResult(IWebElement scopeSource, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions = null)
 {
     ScopeSource        = scopeSource;
     Strategy           = strategy;
     ScopeLocateOptions = scopeLocateOptions;
 }
 public SequalComponentScopeLocateResult(IWebElement scopeSource, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions = null)
 {
     ScopeSource = scopeSource;
     Strategy = strategy;
     ScopeLocateOptions = scopeLocateOptions;
 }
 public StrategyScopeLocator(UIComponent component, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions)
 {
     this.component = component;
     this.strategy = strategy;
     this.scopeLocateOptions = scopeLocateOptions;
 }
Example #20
0
 public SequalComponentScopeLocateResult(IWebElement scopeSource, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions = null)
     : this(new[] { scopeSource ?? throw new ArgumentNullException(nameof(scopeSource)) }, strategy, scopeLocateOptions)
 public ComponentScopeXPathBuilder(ComponentScopeLocateOptions options)
 {
     Options = options;
 }
 protected abstract string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options);
Example #23
0
        private static XPathComponentScopeFindResult[] ResolveResult(
            ComponentScopeLocateResult result,
            ISearchContext scopeSource,
            ComponentScopeLocateOptions scopeLocateOptions,
            SearchOptions searchOptions)
        {
            result.CheckNotNull(nameof(result));

            if (result is MissingComponentScopeFindResult missingResult)
            {
                return(new XPathComponentScopeFindResult[0]);
            }

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

            if (result is SubsequentComponentScopeFindResult subsequentResult)
            {
                ComponentScopeLocateOptions nextScopeLocateOptions = subsequentResult.ScopeLocateOptions ?? scopeLocateOptions;

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

                    SearchOptions nextSearchOptions = SearchOptions.SafelyAtOnce();

                    // TODO: When there are no results, do retry.
                    var results = nextScopeSources.
                                  Select(nextScopeSource => Execute(subsequentResult.Strategy, nextScopeSource, nextScopeLocateOptions, nextSearchOptions)).
                                  Where(xPathResults => xPathResults != null).
                                  SelectMany(xPathResults => xPathResults).
                                  ToArray();

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

            throw new ArgumentException($"Unsupported {nameof(ComponentScopeLocateResult)} type: {result.GetType().FullName}", nameof(result));
        }
    }
 public SequalComponentScopeLocateResult(By scopeSourceBy, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions = null)
 {
     ScopeSourceBy      = scopeSourceBy;
     Strategy           = strategy;
     ScopeLocateOptions = scopeLocateOptions;
 }
 public SequalComponentScopeLocateResult(By scopeSourceBy, IComponentScopeLocateStrategy strategy, ComponentScopeLocateOptions scopeLocateOptions = null)
 {
     ScopeSourceBy = scopeSourceBy;
     Strategy = strategy;
     ScopeLocateOptions = scopeLocateOptions;
 }