public static Func <string, string, bool> GetPredicate(this TermMatch match)
        {
            switch (match)
            {
            case TermMatch.Contains:
                return((text, term) => text.Contains(term));

            case TermMatch.Equals:
                return((text, term) => text.Trim() == term);

            case TermMatch.StartsWith:
                return((text, term) => text.Trim().StartsWith(term));

            case TermMatch.EndsWith:
                return((text, term) => text.Trim().EndsWith(term));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(match, nameof(match));
            }
        }
示例#2
0
        public static IWebElement GetScopeElement(this ScopeSource scopeSource, UIComponent component)
        {
            switch (scopeSource)
            {
            case ScopeSource.Parent:
                return(component.Parent.Scope);

            case ScopeSource.Grandparent:
                return(component.Parent.Parent.Scope);

            case ScopeSource.PageObject:
                return(component.Owner.Scope);

            case ScopeSource.Page:
                return(component.Driver.Get(By.TagName("body")));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(scopeSource, nameof(scopeSource));
            }
        }
示例#3
0
        internal sealed override IWebElement OnGetScopeElement(SearchOptions searchOptions)
        {
            ExecuteTriggers(TriggerEvents.BeforeAccess);

            IWebElement element = ScopeLocator.GetElement(searchOptions);

            if (!searchOptions.IsSafely && element == null)
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          new SearchFailureData
                {
                    ElementName   = ComponentFullName,
                    SearchOptions = searchOptions
                });
            }

            ExecuteTriggers(TriggerEvents.AfterAccess);

            return(element);
        }
示例#4
0
        private static string ResolveResultPrefix(AssertionStatus status)
        {
            switch (status)
            {
            case AssertionStatus.Passed:
                return(null);

            case AssertionStatus.Warning:
                return(WarningResultPrefix);

            case AssertionStatus.Failed:
                return(FailedResultPrefix);

            case AssertionStatus.Exception:
                return(ExceptionResultPrefix);

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(status, nameof(status));
            }
        }
        private Func <IWebElement, string> CreateGetContentDelegate(ContentSource source)
        {
            switch (source)
            {
            case ContentSource.Text:
                return(element => element.Text);

            case ContentSource.TextContent:
                return(element => element.GetAttribute("textContent").Trim());

            case ContentSource.InnerHtml:
                return(element => element.GetAttribute("innerHTML").Trim());

            case ContentSource.Value:
                return(element => element.GetAttribute("value"));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(source, nameof(source));
            }
        }
        internal static string GetShouldText(this TermMatch match)
        {
            switch (match)
            {
            case TermMatch.Contains:
                return("contain");

            case TermMatch.Equals:
                return("equal");

            case TermMatch.StartsWith:
                return("start with");

            case TermMatch.EndsWith:
                return("end with");

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(match, nameof(match));
            }
        }
        public static string GetXPathOperationFormat(this TermMatch match)
        {
            switch (match)
            {
            case TermMatch.Contains:
                return("contains({0}, '{1}')");

            case TermMatch.Equals:
                return("normalize-space({0}) = '{1}'");

            case TermMatch.StartsWith:
                return("starts-with(normalize-space({0}), '{1}')");

            case TermMatch.EndsWith:
                return("substring(normalize-space({0}), string-length(normalize-space({0})) - string-length('{1}') + 1) = '{1}'");

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(match, nameof(match));
            }
        }
示例#8
0
        public bool IsMissing(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = searchOptions ?? new SearchOptions();

            SearchOptions quickSearchOptions = searchOptions.Clone();

            quickSearchOptions.IsSafely = true;
            quickSearchOptions.Timeout  = TimeSpan.Zero;

            var driver = AtataContext.Current.Driver;
            StrategyScopeLocatorExecutionData executionData = _executionDataCollector.Get(quickSearchOptions);

            bool isMissing = driver.Try(searchOptions.Timeout, searchOptions.RetryInterval).Until(_ =>
            {
                XPathComponentScopeFindResult[] xPathResults = _executor.Execute(executionData);

                if (xPathResults.Any())
                {
                    Dictionary <By, ISearchContext> byScopePairs = xPathResults.ToDictionary(x => x.CreateBy(xPathCondition), x => x.ScopeSource);
                    return(driver.Try(TimeSpan.Zero).MissingAll(byScopePairs));
                }
                else
                {
                    return(true);
                }
            });

            if (!searchOptions.IsSafely && !isMissing)
            {
                throw ExceptionFactory.CreateForNotMissingElement(
                          new SearchFailureData
                {
                    ElementName   = executionData.Component.ComponentFullName,
                    SearchOptions = searchOptions
                });
            }
            else
            {
                return(isMissing);
            }
        }
        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));
            }
        }
        public static ISearchContext GetScopeContextUsingParent <TOwner>(this ScopeSource scopeSource, IUIComponent <TOwner> parentComponent)
            where TOwner : PageObject <TOwner>
        {
            switch (scopeSource)
            {
            case ScopeSource.Parent:
                return(parentComponent.ScopeContext);

            case ScopeSource.Grandparent:
                return((parentComponent.Parent ?? throw UIComponentNotFoundException.ForParentOf(parentComponent.ComponentFullName)).ScopeContext);

            case ScopeSource.PageObject:
                return(parentComponent.Owner.ScopeContext);

            case ScopeSource.Page:
                return((parentComponent?.Context ?? AtataContext.Current).Driver);

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(scopeSource, nameof(scopeSource));
            }
        }
示例#11
0
        public static IWebElement GetScopeElementUsingParent <TOwner>(this ScopeSource scopeSource, IUIComponent <TOwner> parentComponent)
            where TOwner : PageObject <TOwner>
        {
            switch (scopeSource)
            {
            case ScopeSource.Parent:
                return(parentComponent.Scope);

            case ScopeSource.Grandparent:
                return(parentComponent.Parent.Scope);

            case ScopeSource.PageObject:
                return(parentComponent.Owner.Scope);

            case ScopeSource.Page:
                return(AtataContext.Current.Driver.Get(By.TagName("body")));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(scopeSource, nameof(scopeSource));
            }
        }
示例#12
0
        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 IWebElement GetElement(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = searchOptions ?? new SearchOptions();

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

            if (element == null && !searchOptions.IsSafely)
            {
                throw ExceptionFactory.CreateForNoSuchElement(
                          new SearchFailureData
                {
                    ElementName   = ElementName,
                    SearchOptions = searchOptions
                });
            }
            else
            {
                return(element);
            }
        }
示例#14
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(by: By.XPath(xPathResults.First().XPath + xPathCondition));
                }
                else
                {
                    return(element);
                }
            }
            else
            {
                return(null);
            }
        }
        public bool IsMissing(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = searchOptions ?? new SearchOptions();

            bool isMissing = AtataContext.Current.Driver
                             .Try(searchOptions.Timeout, searchOptions.RetryInterval)
                             .Until(_ => !predicate(searchOptions).Any());

            if (!isMissing && !searchOptions.IsSafely)
            {
                throw ExceptionFactory.CreateForNotMissingElement(
                          new SearchFailureData
                {
                    ElementName   = ElementName,
                    SearchOptions = searchOptions
                });
            }
            else
            {
                return(isMissing);
            }
        }
示例#16
0
        [Obsolete("Use GetScopeContext(...) instead.")] // Obsolete since v1.5.0.
        public static IWebElement GetScopeElement(this ScopeSource scopeSource, UIComponent component, SearchOptions options = null)
        {
            options = options ?? new SearchOptions();

            switch (scopeSource)
            {
            case ScopeSource.Parent:
                return(component.Parent.GetScope(options));

            case ScopeSource.Grandparent:
                return(component.Parent.Parent.GetScope(options));

            case ScopeSource.PageObject:
                return(component.Owner.GetScope(options));

            case ScopeSource.Page:
                return(component.Driver.GetWithLogging(By.TagName("body").With(options)));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(scopeSource, nameof(scopeSource));
            }
        }
示例#17
0
        public static ISearchContext GetScopeContext(this ScopeSource scopeSource, UIComponent component, SearchOptions options = null)
        {
            options = options ?? new SearchOptions();

            switch (scopeSource)
            {
            case ScopeSource.Parent:
                return(component.Parent.GetScopeContext(options));

            case ScopeSource.Grandparent:
                return(component.Parent.Parent.GetScopeContext(options));

            case ScopeSource.PageObject:
                return(component.Owner.GetScopeContext(options));

            case ScopeSource.Page:
                return(component.Driver);

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(scopeSource, nameof(scopeSource));
            }
        }
        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);
            }
        }
示例#19
0
        public static By GetBy(this WaitBy waitBy, string selector)
        {
            switch (waitBy)
            {
            case WaitBy.Id:
                return(By.Id(selector));

            case WaitBy.Name:
                return(By.Name(selector));

            case WaitBy.Class:
                return(By.ClassName(selector));

            case WaitBy.Css:
                return(By.CssSelector(selector));

            case WaitBy.XPath:
                return(By.XPath(selector));

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(waitBy, nameof(waitBy));
            }
        }
示例#20
0
        public bool IsMissing(SearchOptions searchOptions = null, string xPathCondition = null)
        {
            searchOptions = ResolveSearchOptions(searchOptions);

            SearchOptions quickSearchOptions = SearchOptions.SafelyAtOnce();

            quickSearchOptions.Visibility = searchOptions.Visibility;

            bool isMissing = component.Driver.Try(searchOptions.Timeout, searchOptions.RetryInterval).Until(_ =>
            {
                XPathComponentScopeLocateResult[] xPathResults = GetScopeLocateResults(quickSearchOptions);
                if (xPathResults.Any())
                {
                    Dictionary <By, ISearchContext> byScopePairs = xPathResults.ToDictionary(x => x.CreateBy(xPathCondition), x => (ISearchContext)x.ScopeSource);
                    return(component.Driver.Try(TimeSpan.Zero).MissingAll(byScopePairs));
                }
                else
                {
                    return(true);
                }
            });

            if (!searchOptions.IsSafely && !isMissing)
            {
                throw ExceptionFactory.CreateForNotMissingElement(
                          new SearchFailureData
                {
                    ElementName   = component.ComponentFullName,
                    SearchOptions = searchOptions
                });
            }
            else
            {
                return(isMissing);
            }
        }
示例#21
0
        protected WaitUnit[] GetWaitUnits(WaitUntil until)
        {
            switch (until)
            {
            case WaitUntil.Missing:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Any)
                });

            case WaitUntil.Hidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Hidden)
                });

            case WaitUntil.MissingOrHidden:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Visible)
                });

            case WaitUntil.Visible:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible)
                });

            case WaitUntil.VisibleOrHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Any)
                });

            case WaitUntil.VisibleThenHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible),
                    CreatePresenceUnit(Visibility.Hidden)
                });

            case WaitUntil.VisibleThenMissing:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible),
                    CreateAbsenceUnit(Visibility.Any)
                });

            case WaitUntil.VisibleThenMissingOrHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible),
                    CreateAbsenceUnit(Visibility.Visible)
                });

            case WaitUntil.MissingThenVisible:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Any),
                    CreatePresenceUnit(Visibility.Visible)
                });

            case WaitUntil.HiddenThenVisible:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Hidden),
                    CreatePresenceUnit(Visibility.Visible)
                });

            case WaitUntil.MissingOrHiddenThenVisible:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Visible),
                    CreatePresenceUnit(Visibility.Visible)
                });

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(until, nameof(until));
            }
        }
示例#22
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");
        }
    }
示例#23
0
        public static WaitUnit[] GetWaitUnits(this Until until, WaitOptions options = null)
        {
            if (options == null)
            {
                options = new WaitOptions();
            }

            switch (until)
            {
            case Until.Missing:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Any, until, options)
                });

            case Until.Hidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Hidden, until, options)
                });

            case Until.MissingOrHidden:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Visible, until, options)
                });

            case Until.Visible:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible, until, options)
                });

            case Until.VisibleOrHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Any, until, options)
                });

            case Until.VisibleThenHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible, until, options),
                    CreatePresenceUnit(Visibility.Hidden, until, options)
                });

            case Until.VisibleThenMissing:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible, until, options),
                    CreateAbsenceUnit(Visibility.Any, until, options)
                });

            case Until.VisibleThenMissingOrHidden:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Visible, until, options),
                    CreateAbsenceUnit(Visibility.Visible, until, options)
                });

            case Until.MissingThenVisible:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Any, until, options),
                    CreatePresenceUnit(Visibility.Visible, until, options)
                });

            case Until.HiddenThenVisible:
                return(new[]
                {
                    CreatePresenceUnit(Visibility.Hidden, until, options),
                    CreatePresenceUnit(Visibility.Visible, until, options)
                });

            case Until.MissingOrHiddenThenVisible:
                return(new[]
                {
                    CreateAbsenceUnit(Visibility.Visible, until, options),
                    CreatePresenceUnit(Visibility.Visible, until, options)
                });

            default:
                throw ExceptionFactory.CreateForUnsupportedEnumValue(until, nameof(until));
            }
        }
示例#24
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));
        }