Exemple #1
0
        private IEnumerable <Element> FindElements(Selector selector, LoggingWebDriver loggingWebdriver)
        {
            var wrappers = selector.PrioritizedSearchers.Select(s => new Wrapper {
                Searcher = s
            }).ToList();
            var loopResults = Parallel.ForEach(wrappers, (wrapper, loopState) =>
            {
                var searcher = wrapper.Searcher;
                var results  = searcher.SearchIn(loggingWebdriver, Prefixes);

                SuccessfulSearchers.Add(searcher);
                loopState.Break();
                wrapper.Elements = results;
                return;
            });
            var r     = loopResults.IsCompleted;
            var index = 0;

            foreach (var w in wrappers)
            {
                if (w.Elements != null)
                {
                    return(w.Elements);
                }
                if (w.Exception != null)
                {
                    throw new AggregateException($"Error throw on xpath {index}", w.Exception);
                }
                index++;
            }
            return(null);
        }
Exemple #2
0
        private Element FindElement(Selector selector, LoggingWebDriver loggingWebdriver, int?index = null)
        {
            var wrappers = selector.PrioritizedSearchers.Select(s => new Wrapper {
                Searcher = s
            }).ToList();
            var loopResults = Parallel.ForEach(wrappers,
                                               //new ParallelOptions { MaxDegreeOfParallelism = 4 },
                                               (wrapper, loopState) =>
            {
                var searcher = wrapper.Searcher;

                var results = searcher.SearchIn(loggingWebdriver, Prefixes);
                if (loopState.ShouldExitCurrentIteration)
                {
                    return;
                }
                else if (results.One())
                {
                    SuccessfulSearchers.Add(searcher);
                    loopState.Break();
                    wrapper.Element = results.First();
                    return;
                }
                else if (results.Many() && index.HasValue)
                {
                    SuccessfulSearchers.Add(searcher);
                    loopState.Break();
                    var a = results.ToArray();
                    if (a.Count() <= index.Value)
                    {
                        wrapper.Exception = new Exception($"Not enough items found, found {a.Count()} and desired index {index}");
                    }
                    else
                    {
                        wrapper.Element = a[index.Value];
                    }
                    return;
                }
                else if (results.Many())
                {
                    var by = WebElementSourceLog.Get(results.First().WebElement).By;
                    if (by.IsXpath())
                    {
                        //lets find the only clickable one,
                        //if two are precisely on top of each other it will not filter that
                        //but that is unlikely to happen in real applications
                        var el = FindOnlyClickable(by.Xpath());
                        if (el != null)
                        {
                            var pos  = el.Location;
                            var rpos = results.Select(x => new { x.Location, x }).ToList();

                            var filterHidden = rpos
                                               .Where(e => e.Location == pos)
                                               .Select(x => x.x);

                            if (filterHidden.One())
                            {
                                SuccessfulSearchers.Add(searcher);
                                loopState.Break();
                                wrapper.Element = filterHidden.First();
                                return;
                            }
                        }
                    }

                    var items = results.Select(e => $"{e.Tag}@{e.Location.X},{e.Location.Y}").LogFormat();
                    loopState.Break();
                    wrapper.Exception = new Exception($"Multiple results were found using {searcher.LogFormat()}");
                    return;
                }
            });
            var r            = loopResults.IsCompleted;
            var wrapperIndex = 0;

            foreach (var w in wrappers)
            {
                if (w.Element != null)
                {
                    AugmentWebElementSources(selector, w.Element.WebElement);
                    return(w.Element);
                }
                if (w.Exception != null)
                {
                    throw new AggregateException($"Error throw on xpath {wrapperIndex}", w.Exception);
                }
                wrapperIndex++;
            }
            return(null);
        }
Exemple #3
0
        private Element FindElement(Selector selector, LoggingWebDriver loggingWebdriver, int?index = null)
        {
            var wrappers = selector.PrioritizedSearchers.Select(s => new Wrapper {
                Searcher = s
            }).ToList();
            var loopResults = Parallel.ForEach(wrappers,
                                               //new ParallelOptions { MaxDegreeOfParallelism = 4 },
                                               (wrapper, loopState) =>
            {
                var searcher = wrapper.Searcher;

                var results = searcher.SearchIn(loggingWebdriver, Prefixes);
                if (loopState.ShouldExitCurrentIteration)
                {
                    return;
                }
                else if (results.One())
                {
                    SuccessfulSearchers.Add(searcher);
                    loopState.Break();
                    wrapper.Element = results.First();
                    return;
                }
                else if (results.Many() && index.HasValue)
                {
                    SuccessfulSearchers.Add(searcher);
                    loopState.Break();
                    var a = results.ToArray();
                    if (a.Count() <= index.Value)
                    {
                        wrapper.Exception = new Exception($"Not enough items found, found {a.Count()} and desired index {index}");
                    }
                    else
                    {
                        wrapper.Element = a[index.Value];
                    }
                    return;
                }
                else if (results.Many())
                {
                    //lets make sure none are hidden
                    var filterHidden = results
                                       .Select(e => new { e, o = loggingWebdriver.GetElementFromPoint(e.Location.X + 1, e.Location.Y + 1) })
                                       .Where(p => p.e.Tag == p.o?.TagName && p.e.Location == p.o?.Location);
                    if (filterHidden.One())
                    {
                        SuccessfulSearchers.Add(searcher);
                        loopState.Break();
                        wrapper.Element = results.First();
                        return;
                    }
                    //check if they are logical duplicates.
                    if (results.GroupBy(e => e.Id).One())
                    {
                        SuccessfulSearchers.Add(searcher);
                        loopState.Break();
                        wrapper.Element = results.First();
                        return;
                    }
                    //scroll up ?
                    //WebDriver.ExecuteScript("window.scrollTo(0,1)");
                    var items = results.Select(e => $"{e.Tag}@{e.Location.X},{e.Location.Y}").LogFormat();
                    loopState.Break();
                    wrapper.Exception = new Exception($"Multiple results were found using {searcher.LogFormat()}");
                    return;
                }
            });
            var r            = loopResults.IsCompleted;
            var wrapperIndex = 0;

            foreach (var w in wrappers)
            {
                if (w.Element != null)
                {
                    return(w.Element);
                }
                if (w.Exception != null)
                {
                    throw new AggregateException($"Error throw on xpath {wrapperIndex}", w.Exception);
                }
                wrapperIndex++;
            }
            return(null);
        }