Пример #1
0
 public void Add(IPremissaWeb premissa)
 {
     premissas.Add(premissa);
 }
Пример #2
0
        /// <summary>
        /// Tenta recuperar o elemento web enquanto as condições (timeout, howManyTimes) forem satisfeitas
        /// </summary>
        /// <param name="driver">IWebDriver</param>
        /// <param name="howManyTimes">Quantidade limite de tentativas (-1 para infinitas)</param>
        /// <param name="searchMechanism">Mecanismo de busca para buscar elemento (IWebElement) vide classe OpenQA.Selenium.By</param>
        /// <param name="timeBetweenTriesSeconds">Intervalo de tempo, em segundos, entre tentativas consecutivas</param>
        /// Set this value to false if you don't want to thrown known exceptions from this framework. In this case null value will be returned if the element was not found.</param>
        /// <param name="premissaWeb">Premissas para serem verificadas antes de realizar as tentativas</param>
        public static IWebElement TryGetElement(this IWebDriver driver, int howManyTimes, int timeBetweenTriesSeconds, By searchMechanism, bool throwExceptions = true, IPremissaWeb premissaWeb = null)
        {
            premissaWeb?.Verificar();

            var countTentativas = 0;
            var horarioInicio   = DateTime.Now;

            while (true)
            {
                try
                {
                    CheckTimeOutAndTry(howManyTimes, horarioInicio, ref countTentativas);

                    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeBetweenTriesSeconds));
                    //var myDynamicElement = wait.Until(d => d.FindElement(searchMechanism));
                    var myDynamicElement = driver.FindElement(searchMechanism);
                    return(myDynamicElement);
                }
                catch (TryEnoughTimesToGetException)
                {
                    if (throwExceptions)
                    {
                        throw;
                    }

                    return(null);
                }
                catch (Exception e)
                {
                    Thread.Sleep(timeBetweenTriesSeconds * 1000);
                    Console.WriteLine(e.Message);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Tenta clicar no elemento web enquanto as condições (timeout, howManyTimes) forem satisfeitas
        /// </summary>
        /// <param name="driver">IWebDriver</param>
        /// <param name="howManyTimes">Quantidade limite de tentativas (-1 para infinitas)</param>
        /// <param name="searchMecanism">Mecanismo de busca para buscar elemento (IWebElement) vide classe OpenQA.Selenium.By</param>
        /// <param name="timeBetweenTriesSeconds">Intervalo de tempo, em segundos, entre tentativas consecutivas</param>
        /// <param name="premissaWeb">Premissas para serem verificadas antes de realizar as tentativas</param>
        public static bool TryClick(this IWebDriver driver, int howManyTimes, int timeBetweenTriesSeconds, By searchMecanism, bool throwExceptions = true, IPremissaWeb premissaWeb = null)
        {
            try
            {
                var element = TryGetElement(driver, howManyTimes, timeBetweenTriesSeconds, searchMecanism, throwExceptions, premissaWeb);
                element.Click();
            }
            catch (TryEnoughTimesToGetException)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(false);
            }
            catch (Exception e)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(false);
            }
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Tenta algumas vezes pegar o valor do elemento HTML
        /// </summary>
        /// <param name="driver">IWebDriver</param>
        /// <param name="howManyTimes">Quantidade limite de tentativas (-1 para infinitas)</param>
        /// <param name="searchMecanism">Mecanismo de busca para buscar elemento (IWebElement) vide classe OpenQA.Selenium.By</param>
        /// <param name="timeBetweenTriesSeconds">Intervalo de tempo, em segundos, entre tentativas consecutivas</param>
        /// <param name="premissaWeb">Premissas para serem verificadas antes de realizar as tentativas</param>
        /// <returns>text do campo</returns>
        public static string TryGetTextFromElement(this IWebDriver driver, int howManyTimes, int timeBetweenTriesSeconds, By searchMecanism, bool throwExceptions = true, IPremissaWeb premissaWeb = null)
        {
            try
            {
                var element = TryGetElement(driver, howManyTimes, timeBetweenTriesSeconds, searchMecanism, throwExceptions, premissaWeb);
                var text    = element.Text == "" ? element.GetAttribute("value") : element.Text;
                return(text);
            }
            catch (TryEnoughTimesToGetException)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(null);
            }
            catch (Exception e)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(null);
            }
        }
Пример #5
0
        /// <summary>
        /// Tenta enviar caracteres para o elemento web enquanto as condições (timeout, howManyTimes) forem satisfeitas
        /// </summary>
        /// <param name="driver">IWebDriver</param>
        /// <param name="howManyTimes">Quantidade limite de tentativas (-1 para infinitas)</param>
        /// <param name="searchMecanism">Mecanismo de busca para buscar elemento (IWebElement) vide classe OpenQA.Selenium.By</param>
        /// <param name="timeBetweenTriesSeconds">Intervalo de tempo, em segundos, entre tentativas consecutivas</param>
        /// <param name="premissaWeb">Premissas para serem verificadas antes de realizar as tentativas</param>
        public static bool TrySendKeys(this IWebDriver driver, int howManyTimes, int timeBetweenTriesSeconds, By searchMecanism, string text, bool throwExceptions = true, IPremissaWeb premissaWeb = null)
        {
            try
            {
                var element = TryGetElement(driver, howManyTimes, timeBetweenTriesSeconds, searchMecanism, throwExceptions, premissaWeb);
                element.Click();
                element.Clear();
                element.SendKeys(text);
                if (element.GetAttribute("value") == null || element.GetAttribute("value") != text)
                {
                    Thread.Sleep(timeBetweenTriesSeconds * 1000);
                    //throw new UnableToSendTextToElementException($"Campo '{element.GetAttribute("id")}' não estava disponivel para digitação do text '{text}' ");
                    throw new UnableToSendTextToElementException($"Unable to set value '{text}' to HTML element '{element.GetAttribute("id")}'");
                }
                return(true);
            }
            catch (TryEnoughTimesToGetException)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(false);
            }
            catch (Exception e)
            {
                if (throwExceptions)
                {
                    throw;
                }

                return(false);
            }
            return(true);
        }
Пример #6
0
        /// <summary>
        /// Tenta recuperar o elemento web enquanto as condições (timeout, howManyTimes) forem satisfeitas
        /// </summary>
        /// <param name="driver">IWebDriver</param>
        /// <param name="howManyTimes">Quantidade limite de tentativas (-1 para infinitas)</param>
        /// <param name="searchMechanism">Mecanismo de busca para buscar elemento (IWebElement) vide classe OpenQA.Selenium.By</param>
        /// <param name="throwExceptions">Indica se exceções conhecidas neste namespace devem ser disparadas. Se false retornará nulo caso não consiga encontrar o elemento. -</param>
        /// <param name="timeBetweenTriesSeconds">Intervalo de tempo, em segundos, entre tentativas consecutivas</param>
        /// <param name="premissaWeb">Premissas para serem verificadas antes de realizar as tentativas</param>
        public static ICollection <IWebElement> TryGetElements(this IWebDriver driver, int howManyTimes, int timeBetweenTriesSeconds, By searchMechanism, bool throwExceptions = true, IPremissaWeb premissaWeb = null)
        {
            premissaWeb?.Verificar();

            var countTentativas = 0;
            var horarioInicio   = DateTime.Now;

            while (true)
            {
                try
                {
                    CheckTimeOutAndTry(howManyTimes, horarioInicio, ref countTentativas);

                    var wait             = new WebDriverWait(driver, TimeSpan.FromSeconds(timeBetweenTriesSeconds));
                    var myDynamicElement = wait.Until(d => d.FindElements(searchMechanism));
                    return(myDynamicElement);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }