示例#1
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));
示例#2
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));
示例#3
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));