Example #1
0
        public IWebDriver OpenBrowser(string PathToDrivers)
        {
            if (GetBrowserType() == BrowserType.Chrome)
            {
                OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();

                options.AddArgument("--start-maximized");
                options.AddArgument("--ignore-certificate-errors");
                options.AddArgument("--disable-popup-blocking");
                options.AddArgument("--safebrowsing-disable-download-protection");
                options.AddArgument("--incognito");
                if (headless.ToLower() == "yes")
                {
                    options.AddArgument("--headless");
                }
                options.AddUserProfilePreference("download.prompt_for_download", false);
                options.AddUserProfilePreference("disable-popup-blocking", true);
                //options.AddUserProfilePreference("safebrowsing.enabled", true);
                options.AddUserProfilePreference("safebrowsing", "enabled");

                driv = new OpenQA.Selenium.Chrome.ChromeDriver(PathToDrivers, options);
            }

            return(driv);
        }
Example #2
0
        /// <summary>
        /// Open the new Chrome Browser and return the chrome driver as IwebDriver
        /// Note: this fucntion will close the chrome/chromedriver if its opened already, otherwise new chrome driver will not work perfectly
        /// Note: Ensure the chromedriver is downloaded and placed into your exe folder
        /// </summary>
        /// <returns></returns>
        public IWebDriver openChrome()
        {
            OpenQA.Selenium.Chrome.ChromeOptions choptions = new OpenQA.Selenium.Chrome.ChromeOptions();
            //choptions.BinaryLocation = "C:\\Program Files\\Google\\\\Application\\Chrom.exe";
            choptions.AddArgument("--test-type");
            choptions.AddArgument("--disable-plugins");
            choptions.AddArgument("--disable-extensions");
            //choptions.AddArgument("--enable-automation");
            choptions.AddArgument("--no-sandbox");
            choptions.AddUserProfilePreference("credentials_enable_service", false);
            choptions.AddUserProfilePreference("profile.password_manager_enabled", false);
            //choptions.AddArgument("--start-maximized");

            //choptions.AddArgument("--allow-external-pages");
            //choptions.AddArgument("--allow-running-insecure-content");
            //choptions.AddArgument("--new-window");
            //choptions.AddArguments("--enable-strict-powerful-feature-restrictions");
            //choptions.AddUserProfilePreference("profile.default_content_setting_values.images", 0);

            //Close the previous  driver is it have opened
            foreach (System.Diagnostics.Process proTem in System.Diagnostics.Process.GetProcessesByName("chromedriver"))
            {
                proTem.CloseMainWindow();
            }
            //Close the chrome browser if its opened already
            foreach (System.Diagnostics.Process proTem in System.Diagnostics.Process.GetProcessesByName("chrome"))
            {
                proTem.CloseMainWindow();
            }

            //Open the new
            return(new OpenQA.Selenium.Chrome.ChromeDriver(strchromedriver, choptions, new TimeSpan(0, 5, 0)));
        }
Example #3
0
        private IWebDriver OpenBrowser(string BrowserType, string Headless = "")
        {
            if (BrowserType.ToLower() == "chrome")
            {
                OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();

                options.AddArgument("--start-maximized");
                options.AddArgument("--ignore-certificate-errors");
                options.AddArgument("--disable-popup-blocking");
                options.AddArgument("--safebrowsing-disable-download-protection");
                options.AddArgument("--incognito");

                if (Headless.ToLower() == "yes" || Headless == "")
                {
                    options.AddArgument("--headless");
                }
                options.AddUserProfilePreference("download.prompt_for_download", false);
                options.AddUserProfilePreference("disable-popup-blocking", true);
                options.AddUserProfilePreference("safebrowsing", "enabled");

                IWebDriver driv = new OpenQA.Selenium.Chrome.ChromeDriver(options);
                return(driv);
            }

            else
            {
                return(new OpenQA.Selenium.Firefox.FirefoxDriver());
            }
        }
Example #4
0
        public void DebugLocalChrome()
        {
            try
            {
                //set configuration to be picked up and set Locator
                Config  = false;
                Browser = "Chrome";
                Env     = "Desktop";

                string path = TestUtility.GetAssemblyDirectory() + "//chromedriver.exe";
                Environment.SetEnvironmentVariable("webdriver.chrome.driver", path);

                ChromeOptions chrOpts = new ChromeOptions();
                chrOpts.AddArguments("test-type");
                chrOpts.AddArguments("--disable-extensions");
                // chrOpts.AddArgument("incognito");
                chrOpts.AddUserProfilePreference("download.prompt_for_download", ConfigurationManager.AppSettings["ShowBrowserDownloadPrompt"]);
                driver = new ChromeDriver(chrOpts);
                driver.Manage().Window.Maximize();
                driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(Convert.ToInt32(ConfigurationManager.AppSettings.Get("ElementPageLoad"))));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #5
0
        public bool RecycleDriver(int recycleNavigateCount = 100)
        {
            if (this.Driver != null && this.NavigateCount < recycleNavigateCount)
            {
                return(false);
            }
            if (this.Driver != null)
            {
                this.Driver.Quit();
            }
            var options = new OpenQA.Selenium.Chrome.ChromeOptions();

            options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
            options.AddArguments("--start-maximized");
            this.Driver = new OpenQA.Selenium.Chrome.ChromeDriver(options);
            //this.RemoteWebDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();
            NavigateCount = 0;
            return(true);
        }
Example #6
0
        /// <summary>
        /// Método sem recaptcha
        /// </summary>
        /// <param name="linhaDigitavel">Linha digitável do boleto</param>
        public static void Consultar(string cnpj, string id, string linhaDigitavel, string cpfCnpjPagador)
        {
            IWebDriver driver = null;

            try
            {
                //Trata linha digitável
                linhaDigitavel = linhaDigitavel.Replace(".", string.Empty).Replace(" ", string.Empty);

                #region Diretório / Arquivos

                DirectoryInfo directoryInfo = new System.IO.DirectoryInfo("Boletos");
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }

                directoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfo.FullName, cnpj));
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }

                directoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfo.FullName, string.Format("{0:yyyy-MM-dd}", DateTime.Now)));
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }

                FileInfo fileInfo = new System.IO.FileInfo(Path.Combine(directoryInfo.FullName, "Boletos.pdf"));
                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }

                #endregion Diretório / Arquivo

                #region Chrome - Options

                if (ConfigurationManager.AppSettings["selenium_webdriver"] == "chrome")
                {
                    OpenQA.Selenium.Chrome.ChromeDriverService chromeService = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService();
                    chromeService.HideCommandPromptWindow = true;
                    chromeService.SuppressInitialDiagnosticInformation = true;

                    OpenQA.Selenium.Chrome.ChromeOptions chromeOptions = new OpenQA.Selenium.Chrome.ChromeOptions();
                    chromeOptions.AddUserProfilePreference("download.default_directory", directoryInfo.FullName);
                    chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
                    chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

                    //Disable
                    chromeOptions.AddArgument("disable-infobars");
                    //chromeOptions.AddArgument("headless");Utilizado para suprimir a exibição da janela do chrome

                    driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeService, chromeOptions);
                }

                #endregion Chrome - Options

                #region Firefox - Options

                if (ConfigurationManager.AppSettings["selenium_webdriver"] == "firefox")
                {
                    /*
                     * Firefoz config options
                     *
                     * http://kb.mozillazine.org/About:config_entries#Browser.
                     * */

                    //Create FireFox Service
                    OpenQA.Selenium.Firefox.FirefoxDriverService firefoxService = OpenQA.Selenium.Firefox.FirefoxDriverService.CreateDefaultService();
                    firefoxService.HideCommandPromptWindow = true;
                    firefoxService.SuppressInitialDiagnosticInformation = true;

                    //Create FireFox Profile object
                    OpenQA.Selenium.Firefox.FirefoxOptions firefoxOptions = new OpenQA.Selenium.Firefox.FirefoxOptions();

                    //Set location to store files after downloading.
                    firefoxOptions.SetPreference("browser.download.folderList", 2);
                    firefoxOptions.SetPreference("browser.helperApps.alwaysAsk.force", false);
                    firefoxOptions.SetPreference("browser.download.manager.focusWhenStarting", false);
                    firefoxOptions.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
                    firefoxOptions.SetPreference("pdfjs.disabled", true);
                    firefoxOptions.SetPreference("browser.download.dir", directoryInfo.FullName);

                    //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
                    firefoxOptions.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

                    // Use this to disable Acrobat plugin for previewing PDFs in Firefox (if you have Adobe reader installed on your computer)
                    firefoxOptions.SetPreference("plugin.scan.Acrobat", "99.0");
                    firefoxOptions.SetPreference("plugin.scan.plid.all", false);

                    //Pass profile parameter In webdriver to use preferences to download file.
                    driver = new OpenQA.Selenium.Firefox.FirefoxDriver(firefoxService, firefoxOptions);
                }

                #endregion Firefox - Options

                if (driver == null)
                {
                    throw new Exception("WebDriver do Selenium não definido nas configurações");
                }

                driver.Navigate().GoToUrl("https://www.itau.com.br/servicos/boletos/atualizar/");

                //Aguarda processamento da página
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));

                //Textbox
                var elem = wait.Until(d =>
                {
                    try
                    {
                        var ele = d.FindElement(By.Id("representacaoNumerica"));
                        return(ele.Displayed ? ele : null);
                    }
                    catch (UnhandledAlertException)
                    {
                        return(null);
                    }
                    catch (StaleElementReferenceException)
                    {
                        return(null);
                    }
                    catch (NoSuchElementException)
                    {
                        return(null);
                    }
                });


                //Preenche os dados da pesquisa
                driver.FindElement(By.Id("representacaoNumerica")).SendKeys(linhaDigitavel);
                driver.FindElement(By.Id("txtDocumentoSacado")).SendKeys(cpfCnpjPagador);
                driver.FindElement(By.Id("btnProximo")).Click();

                //Aguarda
                System.Threading.Thread.Sleep(2500);

                //Acessa aba aerta
                driver.SwitchTo().Window(driver.WindowHandles.Last());

                //Botão de cownload
                elem = wait.Until(d =>
                {
                    try
                    {
                        var ele = d.FindElement(By.Name("frmPDF"));
                        return(ele.Displayed ? ele : null);
                    }
                    catch (UnhandledAlertException)
                    {
                        return(null);
                    }
                    catch (StaleElementReferenceException)
                    {
                        return(null);
                    }
                    catch (NoSuchElementException)
                    {
                        return(null);
                    }
                });

                //Download
                ((IJavaScriptExecutor)driver).ExecuteScript("javascript:document.frmPDF.submit();");

                //Aguarda
                System.Threading.Thread.Sleep(3000);

                //Renomear
                fileInfo.Refresh();
                if (fileInfo.Exists)
                {
                    fileInfo.MoveTo(Path.Combine(directoryInfo.FullName, string.Format("{0}.pdf", id)));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (driver != null)
                {
                    driver.Dispose();
                }
            }
        }