Exemplo n.º 1
0
        private IWebElement FindElement(string search, string by, TimeSpan timeout)
        {
            IWebElement element  = null;
            var         searchBy = Enum.GetValues(typeof(ElementSearchBy))
                                   .Cast <ElementSearchBy?>()
                                   .FirstOrDefault(e => e.ToString().Equals(by, StringComparison.OrdinalIgnoreCase));

            try
            {
                switch (searchBy)
                {
                case null:
                    throw new ArgumentException("Value for argument 'By' was not recognized");

                case ElementSearchBy.Id:
                    search  = search.StartsWith("#") ? search.TrimStart(new char[] { '#' }) : search;
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Id(search)));
                    break;

                case ElementSearchBy.Class:
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.ClassName(search)));
                    break;

                case ElementSearchBy.CssSelector:
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(search)));
                    break;

                case ElementSearchBy.Tag:
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.TagName(search)));
                    break;

                case ElementSearchBy.Xpath:
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath(search)));
                    break;

                case ElementSearchBy.Name:
                    element = new WebDriverWait(webDriver, timeout).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Name(search)));
                    break;

                case ElementSearchBy.Query:
                    element = new WebDriverWait(webDriver, timeout).Until(CustomExpectedConditions.ElementExistsByJavaScript(search));
                    break;

                case ElementSearchBy.Jquery:
                    element = new WebDriverWait(webDriver, timeout).Until(CustomExpectedConditions.ElementExistsByJquery(search));
                    break;
                }
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch
            {
                string errorMessage = $"Timeout occured while waiting for element. Search phrase: '{search}', by: '{by}' {(webDriver is InternetExplorerDriver ? ". It might be necessary to disable protection mode and lower security level in Internet Explorer." : string.Empty)}";
                throw new TimeoutException(errorMessage);
            }

            return(element);
        }
        private IWebElement FindElement(string search, string by, int timeoutSeconds)
        {
            IWebElement     element = null;
            ElementSearchBy searchBy;

            if (Enum.TryParse <ElementSearchBy>(by.CapitalizeFirstLetter(), out searchBy) == false)
            {
                throw new ArgumentException("Argument 'By' was not recognized");
            }
            try
            {
                switch (searchBy)
                {
                case ElementSearchBy.Id:
                    search  = search.StartsWith("#") ? search.TrimStart(new char[] { '#' }) : search;
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Id(search)));
                    break;

                case ElementSearchBy.Class:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.ClassName(search)));
                    break;

                case ElementSearchBy.CssSelector:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(search)));
                    break;

                case ElementSearchBy.Tag:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.TagName(search)));
                    break;

                case ElementSearchBy.Xpath:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath(search)));
                    break;

                case ElementSearchBy.Name:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Name(search)));
                    break;

                case ElementSearchBy.Query:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(CustomExpectedConditions.ElementExistsByJavaScript(search));
                    break;

                case ElementSearchBy.Jquery:
                    element = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutSeconds)).Until(CustomExpectedConditions.ElementExistsByJquery(search));
                    break;
                }
            }
            catch
            {
                string errorMessage = $"Timeout occured while waiting for element. Search phrase: '{search}', by: '{by}' {(webDriver is InternetExplorerDriver ? ". It might be necessary to disable protection mode and lower security level in Internet Explorer." : string.Empty)}";
                throw new TimeoutException(errorMessage);
            }
            return(element);
        }