示例#1
0
        virtual public List <string> GetIdentifiers()
        {
            var options = new List <Tuple <By, Func <IWebElement, string>, List <string> > >()
            {
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//label"), (e) => e.Text, new List <string>()),
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//*[self::button or self::a or (self::input and @type='button')]"), (e) => e.Text, new List <string>()),
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//*[@alt]"), (e) => e.GetAttribute("alt"), new List <string>()),
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//*[@name]"), (e) => e.GetAttribute("name"), new List <string>()),
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//*[@aria-label]"), (e) => e.GetAttribute("aria-label"), new List <string>()),
                new Tuple <By, Func <IWebElement, string>, List <string> >(
                    By.XPath("//*[@title]"), (e) => e.GetAttribute("title"), new List <string>()),
            };

            Parallel.ForEach(options, (option, loopState) =>
            {
                var elements = SeleniumDriver.FindElements(option.Item1);
                foreach (var e in elements)
                {
                    try
                    {
                        option.Item3.Add(option.Item2(e));
                    }
                    catch
                    { }
                }
            });

            return(options.SelectMany(o => o.Item3).Distinct().OrderBy(s => s.ToLower()).ToList());
        }
示例#2
0
        public WebDriver Prefix(SelectorPrefix prefix)
        {
            var p = new ValidatedPrefix();
            var l = Prefixes.Concat(prefix);

            var possibles = l.CrossMultiply();

            RetryExecutor.RetryFor(() =>
            {
                var iframes = SeleniumDriver.FindElements(By.XPath("//iframe"));
                var valid   = possibles.AsParallel().AsOrdered().Where(xpath => SeleniumDriver.FindElements(By.XPath(xpath)).Any()).ToList();
                if (valid.Any())
                {
                    p.Init("filtered", valid);
                }
                else if (iframes.Any())
                {
                    foreach (var iframe in iframes)
                    {
                        try
                        {
                            SeleniumDriver.SwitchTo().Frame(iframe);
                            valid = possibles.AsParallel().AsOrdered().Where(xpath => SeleniumDriver.FindElements(By.XPath(xpath)).Any()).ToList();
                            if (valid.Any())
                            {
                                p.Init("filtered", valid);
                            }
                        }
                        catch
                        { }
                    }
                    SeleniumDriver.SwitchTo().DefaultContent();
                }
                if (!p.IsInitialized)
                {
                    throw new Exception($"Was unable to find any that matched prefix, tried:{possibles.LogFormat()}");
                }
            }, TimeSpan.FromMilliseconds(SeleniumGridConfiguration.RetryMs));

            var wdm = new WebDriver(
                SeleniumDriver,
                RootUrl,
                SeleniumGridConfiguration,
                RetryExecutor,
                SelectorFactory,
                ElementFactory,
                XpathProvider,
                MovieLogger,
                WebElementSourceLog,
                new List <SelectorPrefix> {
                p
            }
                );

            Children.Add(wdm);
            return(wdm);
        }
示例#3
0
        public IEnumerable <TableElement> GetTables(int minimumWidth)
        {
            var xpath = $"//table[" +
                        $"tr[th[{minimumWidth}] or td[{minimumWidth}]] or " +
                        $"tbody/tr[th[{minimumWidth}] or td[{minimumWidth}]] or " +
                        $"thead/tr[th[{minimumWidth}] or td[{minimumWidth}]]" +
                        $"]";
            //var xpath = $"//tr[*[self::td or self::th][{minimumWidth}] and (.|parent::tbody)[1]/parent::table]/ancestor::table[1]";
            var tables = SeleniumDriver.
                         FindElements(By.XPath(xpath))
                         .Select(t => new TableElement(t, SeleniumDriver, ElementFactory, XpathProvider)).ToList();
            var Ordinal = 1;

            foreach (var table in tables)
            {
                table.Ordinal = Ordinal++;
                table.Xpath   = xpath;
            }
            tables.AsParallel().ForAll(table => table.Setup());
            return(tables.Where(t => t.IsValid).ToList());
        }
示例#4
0
 public IEnumerable <Element> SelectMany(Selector selector)
 => RetryExecutor.RetryFor(() =>
 {
     var loggingWebdriver = new LoggingWebDriver(SeleniumDriver, MovieLogger, WebElementSourceLog);
     try
     {
         var elements = FindElements(selector, loggingWebdriver);
         if (elements != null)
         {
             return(elements);
         }
         //iframes ?
         var iframes = SeleniumDriver.FindElements(By.XPath("//iframe"));
         foreach (var iframe in iframes)
         {
             try
             {
                 loggingWebdriver.Log($"Trying iframe:{iframe}");
                 SeleniumDriver.SwitchTo().Frame(iframe);
                 elements = FindElements(selector, loggingWebdriver);
                 if (elements != null)
                 {
                     return(elements);
                 }
             }
             catch
             { }
         }
         SeleniumDriver.SwitchTo().DefaultContent();
         return(new List <Element>());
     }
     finally
     {
         if (loggingWebdriver.Screenshots.Any())
         {
             Screenshots = loggingWebdriver.Screenshots.Select(x => x.AsByteArray).ToList();
         }
     }
 }, TimeSpan.FromMilliseconds(SeleniumGridConfiguration.RetryMs));
示例#5
0
 public Element Select(Selector selector, TimeSpan?retryDuration = null, int?index = null)
 => RetryExecutor.RetryFor(() =>
 {
     var loggingWebdriver = new LoggingWebDriver(SeleniumDriver, MovieLogger, WebElementSourceLog);
     try
     {
         var element = FindElement(selector, loggingWebdriver, index);
         if (element != null)
         {
             return(element);
         }
         //iframes ?
         var iframes = SeleniumDriver.FindElements(By.XPath("//iframe"));
         foreach (var iframe in iframes)
         {
             try
             {
                 loggingWebdriver.Log($"Trying iframe:{iframe}");
                 SeleniumDriver.SwitchTo().Frame(iframe);
                 element = FindElement(selector, loggingWebdriver, index);
                 if (element != null)
                 {
                     return(element);
                 }
             }
             catch
             { }
         }
         SeleniumDriver.SwitchTo().DefaultContent();
         throw new Exception($"element was not found; tried:\n{loggingWebdriver.GetLogs()}, maybe try one of these identifiers {GetIdentifiers().Take(10).LogFormat()}");
     }
     finally
     {
         if (loggingWebdriver.Screenshots.Any())
         {
             Screenshots = loggingWebdriver.Screenshots.Select(x => x.AsByteArray).ToList();
         }
     }
 }, retryDuration ?? TimeSpan.FromMilliseconds(SeleniumGridConfiguration.RetryMs));
示例#6
0
        public TableElement GetTables(IEnumerable <string> headers, StringComparison comparison = StringComparison.CurrentCulture, int?index = null)
        => RetryExecutor.RetryFor(() =>
        {
            var loggingWebdriver = new LoggingWebDriver(SeleniumDriver, MovieLogger, WebElementSourceLog);
            try
            {
                var possilbeTables = GetTables(headers.Count()).ToList();
                Func <string, string, bool> comparer = (s1, s2) => s1.Equals(s2, comparison);

                var tableElements = possilbeTables.Where(t => headers.Where(h => !string.IsNullOrEmpty(h)).Except(t.Header.Keys,
                                                                                                                  new Core.EqualityComparer <string>(comparer)).None());

                if (tableElements.One())
                {
                    return(tableElements.First());
                }
                //Exact match overrides in case of multiples.
                if (tableElements.Where(x => headers.Count() == x.MaxColumnIndex).One())
                {
                    return(tableElements.Where(x => headers.Count() == x.MaxColumnIndex).First());
                }
                if (tableElements.Many())
                {
                    if (index == null)
                    {
                        throw new Exception($"multiple talbes matched the definition of {headers.LogFormat()}, table headers were {tableElements.LogFormat(t => $"Table: {t.Header.Keys.LogFormat()}")};");
                    }
                    if (index >= tableElements.Count())
                    {
                        throw new Exception($"only found {tableElements.Count()} tables, index of {index} is out of range. (zero based)");
                    }
                    return(tableElements.ToArray()[index.Value]);
                }
                //iframes ?
                var iframes = SeleniumDriver.FindElements(By.XPath("//iframe"));
                foreach (var iframe in iframes)
                {
                    try
                    {
                        loggingWebdriver.Log($"Trying iframe:{iframe}");
                        SeleniumDriver.SwitchTo().Frame(iframe);
                        possilbeTables = GetTables(headers.Count() - 1).ToList();

                        tableElements = possilbeTables.Where(t => headers.Where(h => !string.IsNullOrEmpty(h)).Except(t.Header.Keys,
                                                                                                                      new Core.EqualityComparer <string>(comparer)).None());

                        if (tableElements.One())
                        {
                            return(tableElements.First());
                        }
                        if (tableElements.Many())
                        {
                            if (index == null)
                            {
                                throw new Exception($"multiple talbes matched the definition of {headers.LogFormat()}, table headers were {tableElements.LogFormat(t => $"Table: {t.Header.Keys.LogFormat()}")};");
                            }
                            if (index >= tableElements.Count())
                            {
                                throw new Exception($"only found {tableElements.Count()} tables, index of {index} is out of range. (zero based)");
                            }
                            return(tableElements.ToArray()[index.Value]);
                        }
                    }
                    catch
                    { }
                }
                SeleniumDriver.SwitchTo().DefaultContent();
                throw new Exception($"table was not found");
            }
            finally
            {
                if (loggingWebdriver.Screenshots.Any())
                {
                    Screenshots = loggingWebdriver.Screenshots.Select(x => x.AsByteArray).ToList();
                }
            }
        }, TimeSpan.FromMilliseconds(SeleniumGridConfiguration.RetryMs));