Powered By Blogger

Search Here!

Sunday, October 12, 2014

Download File In Firefox and Chrome Driver !

Firefox Driver -

private static IWebDriver FireFoxWebDriver()
        {
            // create profile object
            var profile = new FirefoxProfile();
            // get Log Execution Path
            String getExecutingPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
            // set profile preferences
            profile.SetPreference("FireFox" + DateTime.Now.Ticks + ".log", getExecutingPath);
            profile.SetPreference("browser.download.folderList", 2);
            profile.SetPreference("browser.download.dir", DownloadFilePath);
            profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc docx xls xlsx pdf txt zip");
            IWebDriver webDriver = new FirefoxDriver(new FirefoxBinary(), profile, TimeSpan.FromMinutes(3));
            // set page load duration
            webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(TimeOut));
            // set cursor position center of the screen
            Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
            return webDriver;
        }

Chrome Driver -

private static IWebDriver ChromeWebDriver()
        {
            // chrome preferences
            var chromeOptions = new ChromeOptionsWithPrefs
            {
                Prefs = new Dictionary
                {
                    {"download.default_directory", DownloadFilePath},
                    {"download.prompt_for_download", false},
                    {"profile.default_content_settings.popups", 0},
                    {"intl.accept_languages", "nl"}
                }
            };

            // chrome capabilities
            var desiredCapabilities = DesiredCapabilities.Chrome();
            desiredCapabilities.SetCapability(ChromeOptions.Capability, chromeOptions);
            // chrome driver path
            string chromeDriverPath = (Path.GetDirectoryName
                (Assembly.GetExecutingAssembly().GetName().CodeBase)
                + "\\..\\..\\..\\..\\ExternalAssemblies").Replace("file:\\", "");
            // create chrome browser instance
            IWebDriver webDriver = new ChromeDriver(chromeDriverPath, chromeOptions, TimeSpan.FromMinutes(3));
            return webDriver;
        }

Jenkins Selenium Grid Configuration !

Integrate Selenium WebDriver (or RC) with Jenkins, in order to run full end-to-end / UI automation testing as part of your build process.

{
  "capabilities":
      [
        {
          "browserName": "*firefox",
          "maxInstances": 1,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "*googlechrome",
          "maxInstances": 1,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "*iexplore",
          "maxInstances": 1,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "firefox",
          "maxInstances": 1,
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "chrome",
          "maxInstances": 1,
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 1,
          "seleniumProtocol": "WebDriver"
        }
      ],
    "configuration":
        {
         "host": null,
         "port": 4444,
         "newSessionWaitTimeout": -1,
         "servlets" : [],
         "prioritizer": null,
         "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
         "throwOnCapabilityNotPresent": true,
         "nodePolling": 5000,
         "cleanUpCycle": 5000,
         "timeout": 300000,
         "browserTimeout": 0,
         "maxSession": 5,
         "jettyMaxThreads":-1,
         "url":"http://10.52.132.38:4444/wd/hub"
        }
}

Example: How To Handle Page Synchronization and Loading In Angular JS Site using Webdriver !

This method helps you to wait till Progress bar still on page.

public void HoldTillPageSynchronizationCompletes()
        {
            bool isPageInProgress = HoldTillElementIsVisible(By.CssSelector("div[class=Progress]"), 5);
            if (isPageInProgress)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                while (stopWatch.Elapsed.TotalMinutes < _waitTimeOut)
                {
                    string isPageStillInProgress = WebDriver.FindElement(By.CssSelector("table[id=mydivimg]")).GetAttribute("style");
                    if (isPageStillInProgress.Contains("display: none;"))
                    {
                        stopWatch.Stop(); break;
                    }
                }
            }
        }


This method helps you to wait till search records visible on page.

public void HoldTillRecordLoadInPageCompletes()
        {
            bool isLoadRecordInProgress = HoldTillElementIsVisible(By.CssSelector("div[style='display: block;']"), 5);
            if (isLoadRecordInProgress)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                while (stopWatch.Elapsed.TotalMinutes < _waitTimeOut)
                {
                    string isLoadRecordStillInProgress = WebDriver.FindElement(By.CssSelector("div[id=load_Summary]")).GetAttribute("style");
                    if (isLoadRecordStillInProgress.Contains("display: none;"))
                    {
                        stopWatch.Stop(); break;
                    }
                }
            }
        }

This is a common method helps you to hold for finding the element. In this element surely visible on the page but after sometime.

protected bool HoldTillElementIsVisible(By by, int timeOut = -1)
        {
            //Wait For Element
            if (timeOut == -1)
            {
                timeOut = this._waitTimeOut;
            }
            try
            {
                WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(timeOut));
                wait.Until(ExpectedConditions.ElementIsVisible(@by));
                return true;
            }
            //Exception Handling
            catch (Exception)
            {
                return false;
            }
        }